Use a standard Go project layout
[multicloud/k8s.git] / src / k8splugin / internal / db / store_test.go
1 // +build unit
2
3 /*
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.
14 */
15
16 package db
17
18 import (
19         "reflect"
20         "strings"
21         "testing"
22 )
23
24 func TestCreateDBClient(t *testing.T) {
25         t.Run("Successfully create DB client", func(t *testing.T) {
26                 expected := &MongoStore{}
27
28                 err := CreateDBClient("mongo")
29                 if err != nil {
30                         t.Fatalf("CreateDBClient returned an error (%s)", err)
31                 }
32                 if reflect.TypeOf(DBconn) != reflect.TypeOf(expected) {
33                         t.Fatalf("CreateDBClient set DBconn as:\n result=%T\n expected=%T", DBconn, expected)
34                 }
35         })
36         t.Run("Fail to create client for unsupported DB", func(t *testing.T) {
37                 err := CreateDBClient("fakeDB")
38                 if err == nil {
39                         t.Fatal("CreateDBClient didn't return an error")
40                 }
41                 if !strings.Contains(string(err.Error()), "DB not supported") {
42                         t.Fatalf("CreateDBClient method returned an error (%s)", err)
43                 }
44         })
45 }
46
47 func TestSerialize(t *testing.T) {
48
49         inp := map[string]interface{}{
50                 "UUID":   "123e4567-e89b-12d3-a456-426655440000",
51                 "Data":   "sdaijsdiodalkfjsdlagf",
52                 "Number": 23,
53                 "Float":  34.4,
54                 "Map": map[string]interface{}{
55                         "m1": "m1",
56                         "m2": 2,
57                         "m3": 3.0,
58                 },
59         }
60
61         got, err := Serialize(inp)
62         if err != nil {
63                 t.Fatal(err)
64         }
65
66         expected := "{\"Data\":\"sdaijsdiodalkfjsdlagf\"," +
67                 "\"Float\":34.4,\"Map\":{\"m1\":\"m1\",\"m2\":2,\"m3\":3}," +
68                 "\"Number\":23,\"UUID\":\"123e4567-e89b-12d3-a456-426655440000\"}"
69
70         if expected != got {
71                 t.Errorf("Serialize returned unexpected string: %s;"+
72                         " expected %sv", got, expected)
73         }
74 }
75
76 func TestDeSerialize(t *testing.T) {
77         testCases := []struct {
78                 label    string
79                 input    string
80                 expected map[string]interface{}
81                 errMsg   string
82         }{
83                 {
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",
91                                 "Float": 34.4,
92                                 "Map": map[string]interface{}{
93                                         "m1": "m1",
94                                         "m3": 3.0,
95                                 },
96                         },
97                 },
98                 {
99                         label:  "Fail to deserialize invalid entry",
100                         input:  "{invalid}",
101                         errMsg: "Error deSerializing {invalid}: invalid character 'i' looking for beginning of object key string",
102                 },
103         }
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)
108                         if err != nil {
109                                 if testCase.errMsg == "" {
110                                         t.Fatalf("DeSerialize method return an un-expected (%s)", err)
111                                 }
112                                 if !strings.Contains(string(err.Error()), testCase.errMsg) {
113                                         t.Fatalf("DeSerialize method returned an error (%s)", err)
114                                 }
115                         } else {
116                                 if !reflect.DeepEqual(testCase.expected, got) {
117                                         t.Errorf("Serialize returned unexpected : %v;"+
118                                                 " expected %v", got, testCase.expected)
119                                 }
120                         }
121                 })
122         }
123 }