Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Checkpoint #157 #179

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions cmd/make-tests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package cmd

import (
"fmt"
"github.com/elasticpath/epcc-cli/external/resources"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"html/template"
"os"
)

const testTemplate = `#!/usr/bin/env bats
{{if .resource.GetCollectionInfo -}}
@test "{{ .resource.PluralName }} empty get collection" {
run epcc get {{ .resource.PluralName }}
[ $status -eq 0 ]
}
{{end}}
{{if and .resource.GetCollectionInfo .resource.DeleteEntityInfo -}}
@test "{{ .resource.PluralName }} delete-all support" {
run epcc delete-all {{ .resource.PluralName }}
[ $status -eq 0 ]
}
{{end}}

`

var MakeTests = &cobra.Command{
Use: "make-tests",
Short: "Make a bunch of BATS tests",
Hidden: true,

RunE: func(cmd *cobra.Command, args []string) error {

err := os.MkdirAll("tests/resources", 0755)
if err != nil {
return err
}

createResourceString := map[string]string{}

for resourceName, resource := range resources.GetPluralResources() {

createString := fmt.Sprintf("epcc create %s ", resource.PluralName)

if resource.CreateEntityInfo != nil {
for attrName, attrVal := range resource.Attributes {

createString += attrName + " "

switch attrVal.Type {
case "FILE":
case "BOOLEAN":
createString += "true"
case "INT":
createString += "%(date +%s)"
case "STRING":
createString += "string"
default:
createString += "unsupported"
}

}

createResourceString[resourceName] = createString
}

}

for _, resourceName := range resources.GetPluralResourceNames() {
testFilename := fmt.Sprintf("./tests/resources/%s.bats", resourceName)
_, err := os.Stat(testFilename)

if os.IsNotExist(err) {
log.Infof("Tests for %s do not exist", testFilename)
} else {
log.Infof("Tests for %s exist already", testFilename)

}

tmpl, err := template.New("test").Parse(testTemplate)

if err != nil {
return err
}

resource, ok := resources.GetResourceByName(resourceName)

if !ok {
panic("Could not find resource for " + resourceName)
}

f, err := os.OpenFile(testFilename, os.O_CREATE+os.O_TRUNC+os.O_WRONLY, 0755)

defer f.Close()
if err != nil {
return err
}

err = tmpl.Execute(f, map[string]interface{}{
"resource": resource,
"create_map": createResourceString,
})

if err != nil {
return err
}

}

return nil
},
}
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func init() {
login,
logout,
ResetStore,
MakeTests,
)
Logs.AddCommand(LogsList, LogsShow, LogsClear)

Expand Down
3 changes: 2 additions & 1 deletion external/httpclient/httpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ func doRequestInternal(ctx context.Context, method string, contentType string, p
log.Debugf("%s %s", method, reqURL.String())

// TODO maybe check if it's json and if not do something else.
json.PrintJson(string(body))
// Log to standard error because the body should always be jqable
json.PrintJsonToStderr(string(body))
log.Debugf("%s %s", resp.Proto, resp.Status)
} else {
log.Debugf("%s %s ==> %s %s", method, reqURL.String(), resp.Proto, resp.Status)
Expand Down
3 changes: 3 additions & 0 deletions external/resources/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
_ "embed"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
"sort"
)

//go:embed resources.yaml
Expand Down Expand Up @@ -93,6 +94,7 @@ func GetPluralResourceNames() []string {
keys = append(keys, key)
}

sort.Strings(keys)
return keys
}

Expand All @@ -107,6 +109,7 @@ func GetSingularResourceNames() []string {
keys = append(keys, key)
}

sort.Strings(keys)
return keys
}

Expand Down
60 changes: 60 additions & 0 deletions tests/resource_smoke_test.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/usr/bin/env bats

#setup_file() {
# #run epcc reset-store .+
# #[ $status -eq 0 ]
#}
setup() {
load 'test_helper/bats-support/load' # this is required by bats-assert!
load 'test_helper/bats-assert/load'

}

@test "account-addresses crud smoke test" {

# Fixture Setup
run epcc delete-all accounts
[ $status -eq 0 ]
run epcc delete-all account-addresses
[ $status -eq 0 ]
run epcc create account name account-addresses-test legal_name my_legal_name
[ $status -eq 0 ]

# Execute SUT
[ $status -eq 0 ]
run epcc get account-addresses name=account-addresses-test
[ $status -eq 0 ]
run epcc create account-addresses name=account-addresses-test name test legal_name my_legal_name
[ $status -eq 0 ]
run epcc update account-addresses name=account-addresses-test name=test name test2
[ $status -eq 0 ]
run epcc get account-addresses name=account-addresses-test
[ $status -eq 0 ]
assert_output --partial '"name": "test2",'
run epcc get account-addresses name=test2
assert_output --partial '"legal_name": "my_legal_name",'

# Verification



}


@test "accounts crud smoke test" {
run epcc delete-all accounts
[ $status -eq 0 ]
run epcc get accounts
[ $status -eq 0 ]
run epcc create account name test legal_name my_legal_name
[ $status -eq 0 ]
run epcc update account name=test name test2
[ $status -eq 0 ]
run epcc get accounts
[ $status -eq 0 ]
assert_output --partial '"name": "test2",'
run epcc get account name=test2
assert_output --partial '"legal_name": "my_legal_name",'
}


12 changes: 12 additions & 0 deletions tests/resources/account-addresses.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bats
@test "account-addresses empty get collection" {
run epcc get account-addresses
[ $status -eq 0 ]
}

@test "account-addresses delete-all support" {
run epcc delete-all account-addresses
[ $status -eq 0 ]
}


5 changes: 5 additions & 0 deletions tests/resources/account-authentication-settings.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bats
@test "account-authentication-settings empty get collection" {
run epcc get account-authentication-settings
[ $status -eq 0 ]
}
4 changes: 4 additions & 0 deletions tests/resources/account-management-authentication-tokens.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bats



8 changes: 8 additions & 0 deletions tests/resources/account-members.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bats
@test "account-members empty get collection" {
run epcc get account-members
[ $status -eq 0 ]
}



12 changes: 12 additions & 0 deletions tests/resources/account-memberships.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bats
@test "account-memberships empty get collection" {
run epcc get account-memberships
[ $status -eq 0 ]
}

@test "account-memberships delete-all support" {
run epcc delete-all account-memberships
[ $status -eq 0 ]
}


Empty file added tests/resources/accounts.bats
Empty file.
8 changes: 8 additions & 0 deletions tests/resources/authentication-realms.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bats
@test "authentication-realms empty get collection" {
run epcc get authentication-realms
[ $status -eq 0 ]
}



4 changes: 4 additions & 0 deletions tests/resources/cart-checkout.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bats



12 changes: 12 additions & 0 deletions tests/resources/cart-items.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bats
@test "cart-items empty get collection" {
run epcc get cart-items
[ $status -eq 0 ]
}

@test "cart-items delete-all support" {
run epcc delete-all cart-items
[ $status -eq 0 ]
}


4 changes: 4 additions & 0 deletions tests/resources/cart-product-items.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bats



12 changes: 12 additions & 0 deletions tests/resources/carts.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bats
@test "carts empty get collection" {
run epcc get carts
[ $status -eq 0 ]
}

@test "carts delete-all support" {
run epcc delete-all carts
[ $status -eq 0 ]
}


8 changes: 8 additions & 0 deletions tests/resources/catalog-products.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bats
@test "catalog-products empty get collection" {
run epcc get catalog-products
[ $status -eq 0 ]
}



12 changes: 12 additions & 0 deletions tests/resources/currencies.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bats
@test "currencies empty get collection" {
run epcc get currencies
[ $status -eq 0 ]
}

@test "currencies delete-all support" {
run epcc delete-all currencies
[ $status -eq 0 ]
}


12 changes: 12 additions & 0 deletions tests/resources/customer-addresses.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bats
@test "customer-addresses empty get collection" {
run epcc get customer-addresses
[ $status -eq 0 ]
}

@test "customer-addresses delete-all support" {
run epcc delete-all customer-addresses
[ $status -eq 0 ]
}


8 changes: 8 additions & 0 deletions tests/resources/customer-authentication-settings.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bats
@test "customer-authentication-settings empty get collection" {
run epcc get customer-authentication-settings
[ $status -eq 0 ]
}



12 changes: 12 additions & 0 deletions tests/resources/customers.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bats
@test "customers empty get collection" {
run epcc get customers
[ $status -eq 0 ]
}

@test "customers delete-all support" {
run epcc delete-all customers
[ $status -eq 0 ]
}


12 changes: 12 additions & 0 deletions tests/resources/fields.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bats
@test "fields empty get collection" {
run epcc get fields
[ $status -eq 0 ]
}

@test "fields delete-all support" {
run epcc delete-all fields
[ $status -eq 0 ]
}


12 changes: 12 additions & 0 deletions tests/resources/files.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bats
@test "files empty get collection" {
run epcc get files
[ $status -eq 0 ]
}

@test "files delete-all support" {
run epcc delete-all files
[ $status -eq 0 ]
}


Loading