Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gshilin-sdb committed Jun 30, 2021
1 parent b429a4b commit 940a712
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
4 changes: 0 additions & 4 deletions v5/go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module github.com/yaacov/tree-search-language/v5

require (
cloud.google.com/go v0.37.4 // indirect
github.com/Masterminds/squirrel v1.4.0
github.com/antlr/antlr4 v0.0.0-20200712162734-eb1adaa8a7a6
github.com/fatih/color v1.9.0 // indirect
Expand All @@ -12,13 +11,10 @@ require (
github.com/mattn/go-sqlite3 v1.14.0
github.com/onsi/ginkgo v1.14.0
github.com/onsi/gomega v1.10.1
github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c // indirect
github.com/xdg/stringprep v1.0.0 // indirect
go.mongodb.org/mongo-driver v1.3.5
golang.org/x/net v0.0.0-20200707034311-ab3426394381 // indirect
golang.org/x/sys v0.0.0-20200724161237-0e2f3a69832c // indirect
golang.org/x/text v0.3.3 // indirect
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
gopkg.in/yaml.v2 v2.3.0
)

Expand Down
57 changes: 56 additions & 1 deletion v5/pkg/walkers/semantics/walk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ var _ = Describe("Walk", func() {
Entry("not ilike GOOD", "title not ilike '%GOOD%'", false),
Entry("not ilike BAD", "title not ilike '%BAD%'", true),

// Two identififers
// Two identifiers
Entry("more pages", "spec.pages <= spec.rating", false),
Entry("add pages to number", "spec.pages = (spec.rating + 9)", true),
Entry("multiply pages with number", "spec.pages < (spec.rating * 3)", true),
Expand All @@ -91,3 +91,58 @@ var _ = Describe("Walk", func() {
Entry("dates", "date between 2019-12-30 and 2020-01-02", true),
)
})

func evalFactory(record map[string]interface{}) EvalFunc {
return func(name string) (value interface{}, ok bool) {
value, ok = record[name]
return
}
}

var _ = Describe("Walk has-many relationship", func() {
// Subscriptions has many ReservedResource
// ReservedResource has string field ResourceName
text := "subscription.managed = 'true' and subscription.status not in ('Deprovisioned','Deleted') and reserved_resource.resource_name in ('resourceA', 'resourceB')\n"

// Parse the text:
tree, err := tsl.ParseTSL(text)
Expect(err).ToNot(HaveOccurred())

subscriptionWithResourceAB := map[string]interface{}{
"subscription.managed": true,
"subscription.status": "Active",
"reserved_resource.resource_name": []string{
"resourceA",
"resourceB",
},
}
subscriptionWithResourceB := map[string]interface{}{
"subscription.managed": true,
"subscription.status": "Active",
"reserved_resource.resource_name": []string{
"resourceB",
},
}
subscriptionWithoutResourceAB := map[string]interface{}{
"subscription.managed": true,
"subscription.status": "Active",
"reserved_resource.resource_name": []string{
"resourceC",
},
}

eval := evalFactory(subscriptionWithResourceAB)
actual, err := Walk(tree, eval)
Expect(err).ToNot(HaveOccurred())
Expect(actual).To(Equal(true))

eval = evalFactory(subscriptionWithResourceB)
actual, err = Walk(tree, eval)
Expect(err).ToNot(HaveOccurred())
Expect(actual).To(Equal(true))

eval = evalFactory(subscriptionWithoutResourceAB)
actual, err = Walk(tree, eval)
Expect(err).ToNot(HaveOccurred())
Expect(actual).To(Equal(false))
})

0 comments on commit 940a712

Please sign in to comment.