Skip to content

Commit

Permalink
new unit test
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Wessendorf <mwessend@redhat.com>
  • Loading branch information
matzew committed Jan 15, 2024
1 parent b70bf47 commit e9776cc
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 0 deletions.
133 changes: 133 additions & 0 deletions cmd/subscribe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,67 @@ func TestSubscribeWithAll(t *testing.T) {
}
}

func TestSubscribeWithMultiple(t *testing.T) {
root := fromTempDirectory(t)

_, err := fn.New().Init(fn.Function{Runtime: "go", Root: root})
if err != nil {
t.Fatal(err)
}

cmd := NewSubscribeCmd()
cmd.SetArgs([]string{"--source", "my-broker", "--filter", "foo=go"})

if err := cmd.Execute(); err != nil {
t.Fatal(err)
}

// Now load the function and ensure that the subscription is set correctly.
f, err := fn.NewFunction(root)
if err != nil {
t.Fatal(err)
}

if f.Deploy.Subscriptions == nil {
t.Fatal("Expected subscription to be present ")
}
if f.Deploy.Subscriptions[0].Source != "my-broker" {
t.Fatalf("Expected subscription for broker to be 'my-broker', but got '%v'", f.Deploy.Subscriptions[0].Source)
}

if f.Deploy.Subscriptions[0].Filters["foo"] != "go" {
t.Fatalf("Expected subscription filter for 'foo' to be 'go', but got '%v'", f.Deploy.Subscriptions[0].Filters["foo"])
}

cmd = NewSubscribeCmd()
cmd.SetArgs([]string{"--source", "my-broker", "--filter", "bar=foo"})

if err := cmd.Execute(); err != nil {
t.Fatal(err)
}

// Now load the function and ensure that the subscription is set correctly.
f, err = fn.NewFunction(root)
if err != nil {
t.Fatal(err)
}

if f.Deploy.Subscriptions == nil {
t.Fatal("Expected subscription to be present ")
}
if f.Deploy.Subscriptions[0].Source != "my-broker" {
t.Fatalf("Expected subscription for broker to be 'my-broker', but got '%v'", f.Deploy.Subscriptions[0].Source)
}

if f.Deploy.Subscriptions[0].Filters["foo"] != "go" {
t.Fatalf("Expected subscription filter for 'foo' to be 'go', but got '%v'", f.Deploy.Subscriptions[0].Filters["foo"])
}
if f.Deploy.Subscriptions[0].Filters["bar"] != "foo" {
t.Fatalf("Expected subscription filter for 'bar' to be 'foo', but got '%v'", f.Deploy.Subscriptions[0].Filters["foo"])
}

}

func TestSubscribeWithNoExplicitSourceAll(t *testing.T) {
root := fromTempDirectory(t)

Expand Down Expand Up @@ -71,3 +132,75 @@ func TestSubscribeWithNoExplicitSourceAll(t *testing.T) {
t.Fatalf("Expected subscription filter for 'foo' to be 'go', but got '%v'", f.Deploy.Subscriptions[0].Filters["foo"])
}
}

func TestSubscribeWithDuplicated(t *testing.T) {
root := fromTempDirectory(t)

_, err := fn.New().Init(fn.Function{Runtime: "go", Root: root})
if err != nil {
t.Fatal(err)
}

cmd := NewSubscribeCmd()
cmd.SetArgs([]string{"--source", "my-broker", "--filter", "foo=go"})

if err := cmd.Execute(); err != nil {
t.Fatal(err)
}

// Now load the function and ensure that the subscription is set correctly.
f, err := fn.NewFunction(root)
if err != nil {
t.Fatal(err)
}

if f.Deploy.Subscriptions == nil {
t.Fatal("Expected subscription to be present ")
}
if f.Deploy.Subscriptions[0].Source != "my-broker" {
t.Fatalf("Expected subscription for broker to be 'my-broker', but got '%v'", f.Deploy.Subscriptions[0].Source)
}

if f.Deploy.Subscriptions[0].Filters["foo"] != "go" {
t.Fatalf("Expected subscription filter for 'foo' to be 'go', but got '%v'", f.Deploy.Subscriptions[0].Filters["foo"])
}

// call it again with same
cmd = NewSubscribeCmd()
cmd.SetArgs([]string{"--source", "my-broker", "--filter", "foo=go"})

if err := cmd.Execute(); err != nil {
t.Fatal(err)
}
// Now load the function and ensure that the subscription is set correctly.
f, err = fn.NewFunction(root)
if err != nil {
t.Fatal(err)
}

if len(f.Deploy.Subscriptions) > 1 {
t.Fatal("Expected only one subscription to be present ")
}

// call it again and override
cmd = NewSubscribeCmd()
cmd.SetArgs([]string{"--source", "my-broker", "--filter", "foo=gogo"})

if err := cmd.Execute(); err != nil {
t.Fatal(err)
}

// Now load the function and ensure that the subscription is set correctly.
f, err = fn.NewFunction(root)
if err != nil {
t.Fatal(err)
}

if len(f.Deploy.Subscriptions) > 1 {
t.Fatal("Expected only one subscription to be present ")
}
if f.Deploy.Subscriptions[0].Filters["foo"] != "gogo" {
t.Fatalf("Expected subscription filter for 'foo' to be 'gogo', but got '%v'", f.Deploy.Subscriptions[0].Filters["foo"])
}

}
2 changes: 2 additions & 0 deletions third_party/VENDOR-LICENSE/github.com/hashicorp/hcl/go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/hashicorp/hcl

Check failure on line 1 in third_party/VENDOR-LICENSE/github.com/hashicorp/hcl/go.mod

View workflow job for this annotation

GitHub Actions / verify / Verify Deps and Codegen

Please run ./hack/update-codegen.sh. diff --git a/third_party/VENDOR-LICENSE/github.com/hashicorp/hcl/go.mod b/third_party/VENDOR-LICENSE/github.com/hashicorp/hcl/go.mod index 205f229..4debbbe 100644 --- a/third_party/VENDOR-LICENSE/github.com/hashicorp/hcl/go.mod +++ b/third_party/VENDOR-LICENSE/github.com/hashicorp/hcl/go.mod @@ -1,5 +1,3 @@ module github.com/hashicorp/hcl -go 1.21.4 - require github.com/davecgh/go-spew v1.1.1

go 1.21.4

require github.com/davecgh/go-spew v1.1.1

0 comments on commit e9776cc

Please sign in to comment.