2018-06-29 12:44:16 +03:00
|
|
|
package metadata
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"reflect"
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestMD(t *testing.T) {
|
|
|
|
|
tests := []struct {
|
|
|
|
|
addValues map[string]interface{}
|
|
|
|
|
expectedValues map[string]interface{}
|
|
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
// Add initial metadata key/vals
|
|
|
|
|
map[string]interface{}{"key1": "val1", "key2": 2},
|
|
|
|
|
map[string]interface{}{"key1": "val1", "key2": 2},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
// Add additional key/vals.
|
|
|
|
|
map[string]interface{}{"key3": 3, "key4": 4.5},
|
|
|
|
|
map[string]interface{}{"key1": "val1", "key2": 2, "key3": 3, "key4": 4.5},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Using one same md and ctx for all test cases
|
|
|
|
|
ctx := context.TODO()
|
2018-06-29 15:03:25 +01:00
|
|
|
ctx = context.WithValue(ctx, metadataKey{}, M{})
|
|
|
|
|
m, _ := FromContext(ctx)
|
2018-06-29 12:44:16 +03:00
|
|
|
|
|
|
|
|
for i, tc := range tests {
|
|
|
|
|
for k, v := range tc.addValues {
|
2018-06-29 15:03:25 +01:00
|
|
|
m.SetValue(k, v)
|
2018-06-29 12:44:16 +03:00
|
|
|
}
|
2018-06-29 15:03:25 +01:00
|
|
|
if !reflect.DeepEqual(tc.expectedValues, map[string]interface{}(m)) {
|
|
|
|
|
t.Errorf("Test %d: Expected %v but got %v", i, tc.expectedValues, m)
|
2018-06-29 12:44:16 +03:00
|
|
|
}
|
|
|
|
|
|
2018-06-29 15:03:25 +01:00
|
|
|
// Make sure that md is recieved from context successfullly
|
|
|
|
|
mFromContext, ok := FromContext(ctx)
|
2018-06-29 12:44:16 +03:00
|
|
|
if !ok {
|
2018-06-29 15:03:25 +01:00
|
|
|
t.Errorf("Test %d: md is not recieved from the context", i)
|
2018-06-29 12:44:16 +03:00
|
|
|
}
|
2018-06-29 15:03:25 +01:00
|
|
|
if !reflect.DeepEqual(m, mFromContext) {
|
|
|
|
|
t.Errorf("Test %d: md recieved from context differs from initial. Initial: %v, from context: %v", i, m, mFromContext)
|
2018-06-29 12:44:16 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|