From 9a9dba0cd261382396d05ae647d00d89e87ee6ab Mon Sep 17 00:00:00 2001 From: Scott Date: Tue, 25 Feb 2020 16:53:34 +1030 Subject: [PATCH 1/2] add unit tests for NewFromMap and EncodeAsString methods --- container/mts/meta/meta_test.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/container/mts/meta/meta_test.go b/container/mts/meta/meta_test.go index 9316ce55..87c52461 100644 --- a/container/mts/meta/meta_test.go +++ b/container/mts/meta/meta_test.go @@ -219,3 +219,35 @@ func TestKeys(t *testing.T) { t.Errorf("Did not get expected out. \nGot : %v, \nWant: %v\n", got, want) } } + +// TestNewFromMap checks that we can successfully create a new data struct from a map. +func TestNewFromMap(t *testing.T) { + want := map[string]string{ + "loc": "a,b,c", + "ts": "12345", + } + + meta := NewFromMap(want) + + got := meta.All() + + if !reflect.DeepEqual(got, want) { + t.Errorf("Did not get expected out. \nGot : %v, \nWant: %v\n", got, want) + } +} + +// TestEncodeAsString checks that metadata is correctly encoded as a string. +func TestEncodeAsString(t *testing.T) { + meta := NewFromMap(map[string]string{ + "loc": "a,b,c", + "ts": "12345", + }) + + got := meta.EncodeAsString() + want1 := "loc=a,b,c\tts=12345" + want2 := "ts=12345\tloc=a,b,c" + + if got != want1 && got != want2 { + t.Errorf("Did not get expected out. \nGot : %v, \nWant: %v or %v\n", got, want1, want2) + } +} From 7ae9f265e02afe33f41ca5dd520344da13ca6e38 Mon Sep 17 00:00:00 2001 From: Scott Date: Wed, 26 Feb 2020 09:53:08 +1030 Subject: [PATCH 2/2] add TestDeleteOrder to check that the order of keys is correct after deletion --- container/mts/meta/meta_test.go | 42 +++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/container/mts/meta/meta_test.go b/container/mts/meta/meta_test.go index 87c52461..6002b55c 100644 --- a/container/mts/meta/meta_test.go +++ b/container/mts/meta/meta_test.go @@ -251,3 +251,45 @@ func TestEncodeAsString(t *testing.T) { t.Errorf("Did not get expected out. \nGot : %v, \nWant: %v or %v\n", got, want1, want2) } } + +// TestDeleteOrder checks that the order of keys is correct after a deletion. +func TestDeleteOrder(t *testing.T) { + tests := []struct { + delKey string + want []string + }{ + { + "key1", + []string{"key2", "key3", "key4"}, + }, + { + "key2", + []string{"key1", "key3", "key4"}, + }, + { + "key3", + []string{"key1", "key2", "key4"}, + }, + { + "key4", + []string{"key1", "key2", "key3"}, + }, + } + + for _, test := range tests { + t.Logf("deleting %s", test.delKey) + meta := NewWith([][2]string{ + {"key1", "val1"}, + {"key2", "val2"}, + {"key3", "val3"}, + {"key4", "val4"}, + }) + meta.Delete(test.delKey) + + got := meta.order + want := test.want + if !reflect.DeepEqual(got, want) { + t.Errorf("Did not get expected out. \nGot: %v, \nWant: %v\n", got, want) + } + } +}