Skip to content

Commit

Permalink
chore: improving tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ekristen committed Jan 15, 2024
1 parent 3472cb3 commit fb0f643
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/filter/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,9 @@ func TestMerge(t *testing.T) {
},
}

validateErr := expected.Validate()
assert.NoError(t, validateErr)

// Check if the result is as expected
if !reflect.DeepEqual(f1, expected) {
t.Errorf("Merge() = %v, want %v", f1, expected)
Expand Down
93 changes: 93 additions & 0 deletions pkg/queue/item_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package queue

import (
"context"
"github.com/ekristen/libnuke/pkg/resource"

Check failure on line 5 in pkg/queue/item_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed (goimports)
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -30,10 +32,17 @@ func (r TestItemResource2) Remove() error {
return nil
}

type TestItemResourceLister struct{}

func (l *TestItemResourceLister) List(_ context.Context, o interface{}) ([]resource.Resource, error) {
return []resource.Resource{&TestItemResource{id: "test"}}, nil
}

var testItem = Item{
Resource: &TestItemResource{id: "test"},
State: ItemStateNew,
Reason: "brand new",
Type: "TestResource",
}

var testItem2 = Item{
Expand All @@ -56,6 +65,19 @@ func Test_Item(t *testing.T) {
assert.False(t, i.Equals(testItem2.Resource))
}

func Test_ItemList(t *testing.T) {
resource.ClearRegistry()
resource.Register(resource.Registration{
Name: "TestResource",
Lister: &TestItemResourceLister{},
})

i := testItem
list, err := i.List(context.TODO(), nil)
assert.NoError(t, err)
assert.Equal(t, 1, len(list))
}

func Test_Item_LegacyStringer(t *testing.T) {
i := testItem
val, err := i.GetProperty("")
Expand Down Expand Up @@ -142,3 +164,74 @@ func Test_ItemPrint(t *testing.T) {
})
}
}

// ------------------------------------------------------------------------

type TestItemResourceProperties struct{}

func (r *TestItemResourceProperties) Remove() error {
return nil
}
func (r *TestItemResourceProperties) Properties() types.Properties {
return types.NewProperties().Set("test", "testing")
}

func Test_ItemEqualProperties(t *testing.T) {
i := &Item{
Resource: &TestItemResourceProperties{},
State: ItemStateNew,
Reason: "brand new",
Type: "TestResource",
}

assert.True(t, i.Equals(i.Resource))
}

// ------------------------------------------------------------------------

type TestItemResourceStringer struct{}

func (r *TestItemResourceStringer) Remove() error {
return nil
}
func (r *TestItemResourceStringer) String() string {
return "test"
}

func Test_ItemEqualStringer(t *testing.T) {
i := &Item{
Resource: &TestItemResourceStringer{},
State: ItemStateNew,
Reason: "brand new",
Type: "TestResource",
}

ni := &Item{
Resource: &TestItemResourceNothing{},
State: ItemStateNew,
Reason: "brand new",
Type: "TestResource",
}

assert.True(t, i.Equals(i.Resource))
assert.False(t, i.Equals(ni.Resource))
}

// ------------------------------------------------------------------------

type TestItemResourceNothing struct{}

func (r *TestItemResourceNothing) Remove() error {
return nil
}

func Test_ItemEqualNothing(t *testing.T) {
i := &Item{
Resource: &TestItemResourceNothing{},
State: ItemStateNew,
Reason: "brand new",
Type: "TestResource",
}

assert.False(t, i.Equals(i.Resource))
}

0 comments on commit fb0f643

Please sign in to comment.