github.com/rajatvaryani/mattermost-server@v5.11.1+incompatible/cmd/mattermost/commands/utils_test.go (about) 1 // Copyright (c) 2019-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package commands 5 6 import ( 7 "reflect" 8 "sort" 9 "strings" 10 "testing" 11 ) 12 13 func TestStructToMap(t *testing.T) { 14 cases := []struct { 15 Name string 16 Input interface{} 17 Expected map[string]interface{} 18 }{ 19 { 20 Name: "Struct with one string field", 21 Input: struct { 22 Test string 23 }{ 24 Test: "test", 25 }, 26 Expected: map[string]interface{}{ 27 "Test": "test", 28 }, 29 }, 30 { 31 Name: "String with multiple fields of different ", 32 Input: struct { 33 Test1 string 34 Test2 int 35 Test3 string 36 Test4 bool 37 }{ 38 Test1: "test1", 39 Test2: 21, 40 Test3: "test2", 41 Test4: false, 42 }, 43 Expected: map[string]interface{}{ 44 "Test1": "test1", 45 "Test2": 21, 46 "Test3": "test2", 47 "Test4": false, 48 }, 49 }, 50 { 51 Name: "Nested fields", 52 Input: TestConfig{ 53 TestServiceSettings{"abc", "def", "ghi"}, 54 TestTeamSettings{"abc", 1}, 55 TestClientRequirements{"abc", "def", "ghi"}, 56 TestMessageExportSettings{true, "abc", TestGlobalRelaySettings{"abc", "def", "ghi"}}, 57 }, 58 Expected: map[string]interface{}{ 59 "TestServiceSettings": map[string]interface{}{ 60 "Siteurl": "abc", 61 "Websocketurl": "def", 62 "Licensedfieldlocation": "ghi", 63 }, 64 "TestTeamSettings": map[string]interface{}{ 65 "Sitename": "abc", 66 "Maxuserperteam": 1, 67 }, 68 "TestClientRequirements": map[string]interface{}{ 69 "Androidlatestversion": "abc", 70 "Androidminversion": "def", 71 "Desktoplatestversion": "ghi", 72 }, 73 "TestMessageExportSettings": map[string]interface{}{ 74 "Enableexport": true, 75 "Exportformat": "abc", 76 "TestGlobalRelaySettings": map[string]interface{}{ 77 "Customertype": "abc", 78 "Smtpusername": "def", 79 "Smtppassword": "ghi", 80 }, 81 }, 82 }, 83 }, 84 } 85 86 for _, test := range cases { 87 t.Run(test.Name, func(t *testing.T) { 88 res := structToMap(test.Input) 89 90 if !reflect.DeepEqual(res, test.Expected) { 91 t.Errorf("got %v want %v ", res, test.Expected) 92 } 93 }) 94 } 95 } 96 97 func TestPrintMap(t *testing.T) { 98 inputCases := []interface{}{ 99 map[string]interface{}{ 100 "CustomerType": "A9", 101 "SmtpUsername": "", 102 "SmtpPassword": "", 103 "EmailAddress": "", 104 }, 105 map[string]interface{}{ 106 "EnableExport": false, 107 "ExportFormat": "actiance", 108 "DailyRunTime": "01:00", 109 "GlobalRelaySettings": map[string]interface{}{ 110 "CustomerType": "A9", 111 "SmtpUsername": "", 112 "SmtpPassword": "", 113 "EmailAddress": "", 114 }, 115 }, 116 } 117 118 outputCases := []string{ 119 "CustomerType: \"A9\"\nSmtpUsername: \"\"\nSmtpPassword: \"\"\nEmailAddress: \"\"\n", 120 "EnableExport: \"false\"\nExportFormat: \"actiance\"\nDailyRunTime: \"01:00\"\nGlobalRelaySettings:\n\t CustomerType: \"A9\"\n\tSmtpUsername: \"\"\n\tSmtpPassword: \"\"\n\tEmailAddress: \"\"\n", 121 } 122 123 cases := []struct { 124 Name string 125 Input reflect.Value 126 Expected string 127 }{ 128 { 129 Name: "Basic print", 130 Input: reflect.ValueOf(inputCases[0]), 131 Expected: outputCases[0], 132 }, 133 { 134 Name: "Complex print", 135 Input: reflect.ValueOf(inputCases[1]), 136 Expected: outputCases[1], 137 }, 138 } 139 140 for _, test := range cases { 141 t.Run(test.Name, func(t *testing.T) { 142 res := printMap(test.Input, 0) 143 144 // create two slice of string formed by splitting our strings on \n 145 slice1 := strings.Split(res, "\n") 146 slice2 := strings.Split(res, "\n") 147 148 sort.Strings(slice1) 149 sort.Strings(slice2) 150 151 if !reflect.DeepEqual(slice1, slice2) { 152 t.Errorf("got '%#v' want '%#v", slice1, slice2) 153 } 154 155 }) 156 } 157 }