4 Copyright 2018 Intel Corporation.
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
24 func TestCreateDBClient(t *testing.T) {
25 t.Run("Successfully create DB client", func(t *testing.T) {
26 expected := &MongoStore{}
28 err := CreateDBClient("mongo")
30 t.Fatalf("CreateDBClient returned an error (%s)", err)
32 if reflect.TypeOf(DBconn) != reflect.TypeOf(expected) {
33 t.Fatalf("CreateDBClient set DBconn as:\n result=%T\n expected=%T", DBconn, expected)
36 t.Run("Fail to create client for unsupported DB", func(t *testing.T) {
37 err := CreateDBClient("fakeDB")
39 t.Fatal("CreateDBClient didn't return an error")
41 if !strings.Contains(string(err.Error()), "DB not supported") {
42 t.Fatalf("CreateDBClient method returned an error (%s)", err)
47 func TestSerialize(t *testing.T) {
49 inp := map[string]interface{}{
50 "UUID": "123e4567-e89b-12d3-a456-426655440000",
51 "Data": "sdaijsdiodalkfjsdlagf",
54 "Map": map[string]interface{}{
61 got, err := Serialize(inp)
66 expected := "{\"Data\":\"sdaijsdiodalkfjsdlagf\"," +
67 "\"Float\":34.4,\"Map\":{\"m1\":\"m1\",\"m2\":2,\"m3\":3}," +
68 "\"Number\":23,\"UUID\":\"123e4567-e89b-12d3-a456-426655440000\"}"
71 t.Errorf("Serialize returned unexpected string: %s;"+
72 " expected %sv", got, expected)
76 func TestDeSerialize(t *testing.T) {
77 testCases := []struct {
80 expected map[string]interface{}
84 label: "Sucessful deserialize entry",
85 input: "{\"Data\":\"sdaijsdiodalkfjsdlagf\"," +
86 "\"Float\":34.4,\"Map\":{\"m1\":\"m1\",\"m3\":3}," +
87 "\"UUID\":\"123e4567-e89b-12d3-a456-426655440000\"}",
88 expected: map[string]interface{}{
89 "UUID": "123e4567-e89b-12d3-a456-426655440000",
90 "Data": "sdaijsdiodalkfjsdlagf",
92 "Map": map[string]interface{}{
99 label: "Fail to deserialize invalid entry",
101 errMsg: "Error deSerializing {invalid}: invalid character 'i' looking for beginning of object key string",
104 for _, testCase := range testCases {
105 t.Run(testCase.label, func(t *testing.T) {
106 got := make(map[string]interface{})
107 err := DeSerialize(testCase.input, &got)
109 if testCase.errMsg == "" {
110 t.Fatalf("DeSerialize method return an un-expected (%s)", err)
112 if !strings.Contains(string(err.Error()), testCase.errMsg) {
113 t.Fatalf("DeSerialize method returned an error (%s)", err)
116 if !reflect.DeepEqual(testCase.expected, got) {
117 t.Errorf("Serialize returned unexpected : %v;"+
118 " expected %v", got, testCase.expected)