Skip to content

Commit

Permalink
add parameter tests for datasetCleaner
Browse files Browse the repository at this point in the history
  • Loading branch information
consolethinks committed Jun 18, 2024
1 parent 864e223 commit bed67ac
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 3 deletions.
19 changes: 16 additions & 3 deletions cmd/datasetCleaner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ import (
"fmt"
"log"
"net/http"
"github.com/paulscherrerinstitute/scicat/datasetUtils"
"time"

"github.com/paulscherrerinstitute/scicat/datasetUtils"

"github.com/fatih/color"
)

Expand Down Expand Up @@ -75,12 +76,24 @@ func main() {
showVersion := flag.Bool("version", false, "Show version number and exit")

flag.Parse()


if datasetUtils.TestFlags != nil {
datasetUtils.TestFlags(map[string]interface{}{
"user": *userpass,
"token": *token,
"testenv": *testenvFlag,
"devenv": *devenvFlag,
"nonInteractive": *nonInteractiveFlag,
"removeFromCatalog": *removeFromCatalogFlag,
"version": *showVersion})
return
}

if *showVersion {
fmt.Printf("%s\n", VERSION)
return
}

// check for program version only if running interactively
datasetUtils.CheckForNewVersion(client, APP, VERSION)
datasetUtils.CheckForServiceAvailability(client, *testenvFlag, true)
Expand Down
78 changes: 78 additions & 0 deletions cmd/datasetCleaner/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package main

import (
"flag"
"os"
"testing"

"github.com/paulscherrerinstitute/scicat/datasetUtils"
)

func TestMainFlags(t *testing.T) {
// test cases
tests := []struct {
name string
flags map[string]interface{}
args []string
}{
{
name: "Test without flags",
flags: map[string]interface{}{
"testenv": false,
"devenv": false,
"nonInteractive": false,
"removeFromCatalog": false,
"version": false,
"user": "",
"token": "",
},
args: []string{"test"},
},
{
name: "Set all flags",
flags: map[string]interface{}{
"testenv": true,
"devenv": true,
"nonInteractive": true,
"removeFromCatalog": true,
"version": true,
"user": "usertest:passtest",
"token": "token",
},
args: []string{
"test",
"--testenv",
"--devenv",
"--nonInteractive",
"--removeFromCatalog",
"--user",
"usertest:passtest",
"--token",
"token",
"--version",
},
},
}

// running test cases
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
flag.CommandLine = flag.NewFlagSet(test.name, flag.ExitOnError)
datasetUtils.TestFlags = func(flags map[string]interface{}) {
passing := true
for flag := range test.flags {
if flags[flag] != test.flags[flag] {
t.Logf("%s's value should be \"%v\" but it's \"%v\", or non-matching type", flag, test.flags[flag], flags[flag])
passing = false
}
}
if !passing {
t.Fail()
}
}

os.Args = test.args
main()
})
}
}

0 comments on commit bed67ac

Please sign in to comment.