-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dcf2653
commit 8ec8c46
Showing
6 changed files
with
198 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,107 @@ | ||
package resource | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/go-faker/faker/v4" | ||
"github.com/gofrs/uuid" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMeta_GetSet(t *testing.T) { | ||
meta := &Meta{ | ||
ID: uuid.Must(uuid.NewV7()), | ||
Namespace: "default", | ||
Name: faker.UUIDHyphenated(), | ||
Annotations: map[string]string{"key": "value"}, | ||
func TestMatch(t *testing.T) { | ||
id := uuid.Must(uuid.NewV7()) | ||
|
||
tests := []struct { | ||
source *Meta | ||
examples []*Meta | ||
matches []int | ||
}{ | ||
{ | ||
source: &Meta{ | ||
ID: id, | ||
Namespace: DefaultNamespace, | ||
Name: "node1", | ||
}, | ||
examples: []*Meta{ | ||
{ | ||
ID: id, | ||
}, | ||
{ | ||
ID: uuid.Must(uuid.NewV7()), | ||
}, | ||
}, | ||
matches: []int{0}, | ||
}, | ||
{ | ||
source: &Meta{ | ||
ID: id, | ||
Namespace: DefaultNamespace, | ||
Name: "node1", | ||
}, | ||
examples: []*Meta{ | ||
{ | ||
Namespace: DefaultNamespace, | ||
}, | ||
{ | ||
Namespace: "other", | ||
}, | ||
}, | ||
matches: []int{0}, | ||
}, | ||
{ | ||
source: &Meta{ | ||
ID: id, | ||
Namespace: DefaultNamespace, | ||
Name: "node1", | ||
}, | ||
examples: []*Meta{ | ||
{ | ||
Name: "node1", | ||
}, | ||
{ | ||
Name: "node2", | ||
}, | ||
}, | ||
matches: []int{0}, | ||
}, | ||
} | ||
|
||
assert.Equal(t, meta.ID, meta.GetID()) | ||
assert.Equal(t, meta.Namespace, meta.GetNamespace()) | ||
assert.Equal(t, meta.Name, meta.GetName()) | ||
assert.Equal(t, meta.Annotations, meta.GetAnnotations()) | ||
for _, tt := range tests { | ||
t.Run(fmt.Sprintf("%v, %v", tt.source, tt.examples), func(t *testing.T) { | ||
expected := make([]*Meta, 0, len(tt.matches)) | ||
for _, i := range tt.matches { | ||
expected = append(expected, tt.examples[i]) | ||
} | ||
assert.Equal(t, expected, Match(tt.source, tt.examples...)) | ||
}) | ||
} | ||
} | ||
|
||
func TestMatch(t *testing.T) { | ||
id1 := uuid.Must(uuid.NewV7()) | ||
id2 := uuid.Must(uuid.NewV7()) | ||
|
||
sp := &Meta{ID: id1, Namespace: "default", Name: "node1"} | ||
examples := []*Meta{ | ||
{ID: id1, Namespace: "default", Name: "node1"}, | ||
{ID: id1}, | ||
{Namespace: "default", Name: "node1"}, | ||
{ID: id2, Namespace: "default", Name: "node2"}, | ||
{ID: id2}, | ||
{Namespace: "default", Name: "node2"}, | ||
} | ||
func TestMeta_ID(t *testing.T) { | ||
meta := &Meta{} | ||
id := uuid.Must(uuid.NewV4()) | ||
meta.SetID(id) | ||
assert.Equal(t, id, meta.GetID()) | ||
} | ||
|
||
func TestMeta_Namespace(t *testing.T) { | ||
meta := &Meta{} | ||
namespace := "default" | ||
meta.SetNamespace(namespace) | ||
assert.Equal(t, namespace, meta.GetNamespace()) | ||
} | ||
|
||
expeced := []*Meta{examples[0], examples[1], examples[2]} | ||
func TestMeta_Name(t *testing.T) { | ||
meta := &Meta{} | ||
name := faker.UUIDHyphenated() | ||
meta.SetName(name) | ||
assert.Equal(t, name, meta.GetName()) | ||
} | ||
|
||
assert.Equal(t, expeced, Match(sp, examples...)) | ||
func TestMeta_Annotations(t *testing.T) { | ||
meta := &Meta{} | ||
annotations := map[string]string{"key": "value"} | ||
meta.SetAnnotations(annotations) | ||
assert.Equal(t, annotations, meta.GetAnnotations()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.