|
| 1 | +/* |
| 2 | + This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | + License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | + file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 5 | +*/ |
| 6 | + |
| 7 | +package repositories |
| 8 | + |
| 9 | +import ( |
| 10 | + "encoding/json" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | +) |
| 15 | + |
| 16 | +func TestParseCephVersionName(t *testing.T) { |
| 17 | + t.Parallel() |
| 18 | + |
| 19 | + tests := map[string]struct { |
| 20 | + input string |
| 21 | + expected CephVersionName |
| 22 | + expectError bool |
| 23 | + }{ |
| 24 | + "quincy": { |
| 25 | + input: "quincy", |
| 26 | + expected: CephVersionNameQuincy, |
| 27 | + }, |
| 28 | + "reef": { |
| 29 | + input: "reef", |
| 30 | + expected: CephVersionNameReef, |
| 31 | + }, |
| 32 | + "squid": { |
| 33 | + input: "squid", |
| 34 | + expected: CephVersionNameSquid, |
| 35 | + }, |
| 36 | + "invalid": { |
| 37 | + input: "invalid", |
| 38 | + expectError: true, |
| 39 | + }, |
| 40 | + "empty": { |
| 41 | + input: "", |
| 42 | + expectError: true, |
| 43 | + }, |
| 44 | + "uppercase": { |
| 45 | + input: "QUINCY", |
| 46 | + expectError: true, |
| 47 | + }, |
| 48 | + } |
| 49 | + |
| 50 | + for name, test := range tests { |
| 51 | + t.Run(name, func(t *testing.T) { |
| 52 | + t.Parallel() |
| 53 | + |
| 54 | + result, err := ParseCephVersionName(test.input) |
| 55 | + |
| 56 | + if test.expectError { |
| 57 | + require.Error(t, err) |
| 58 | + } else { |
| 59 | + require.NoError(t, err) |
| 60 | + require.Equal(t, test.expected, result) |
| 61 | + } |
| 62 | + }) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func TestCephVersionNameString(t *testing.T) { |
| 67 | + t.Parallel() |
| 68 | + |
| 69 | + tests := map[string]struct { |
| 70 | + version CephVersionName |
| 71 | + expected string |
| 72 | + }{ |
| 73 | + "quincy": { |
| 74 | + version: CephVersionNameQuincy, |
| 75 | + expected: "quincy", |
| 76 | + }, |
| 77 | + "reef": { |
| 78 | + version: CephVersionNameReef, |
| 79 | + expected: "reef", |
| 80 | + }, |
| 81 | + "squid": { |
| 82 | + version: CephVersionNameSquid, |
| 83 | + expected: "squid", |
| 84 | + }, |
| 85 | + "unknown": { |
| 86 | + version: CephVersionNameUnknown, |
| 87 | + expected: "unknown", |
| 88 | + }, |
| 89 | + } |
| 90 | + |
| 91 | + for name, test := range tests { |
| 92 | + t.Run(name, func(t *testing.T) { |
| 93 | + t.Parallel() |
| 94 | + |
| 95 | + result := test.version.String() |
| 96 | + require.Equal(t, test.expected, result) |
| 97 | + }) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +func TestCephVersionNameMarshalJSON(t *testing.T) { |
| 102 | + t.Parallel() |
| 103 | + |
| 104 | + tests := map[string]struct { |
| 105 | + version CephVersionName |
| 106 | + expected string |
| 107 | + }{ |
| 108 | + "quincy": { |
| 109 | + version: CephVersionNameQuincy, |
| 110 | + expected: `"quincy"`, |
| 111 | + }, |
| 112 | + "reef": { |
| 113 | + version: CephVersionNameReef, |
| 114 | + expected: `"reef"`, |
| 115 | + }, |
| 116 | + "squid": { |
| 117 | + version: CephVersionNameSquid, |
| 118 | + expected: `"squid"`, |
| 119 | + }, |
| 120 | + "unknown": { |
| 121 | + version: CephVersionNameUnknown, |
| 122 | + expected: `"unknown"`, |
| 123 | + }, |
| 124 | + } |
| 125 | + |
| 126 | + for name, test := range tests { |
| 127 | + t.Run(name, func(t *testing.T) { |
| 128 | + t.Parallel() |
| 129 | + |
| 130 | + result, err := json.Marshal(test.version) |
| 131 | + require.NoError(t, err) |
| 132 | + require.Equal(t, test.expected, string(result)) |
| 133 | + }) |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +func TestCephVersionNameUnmarshalJSON(t *testing.T) { |
| 138 | + t.Parallel() |
| 139 | + |
| 140 | + tests := map[string]struct { |
| 141 | + input string |
| 142 | + expected CephVersionName |
| 143 | + expectError bool |
| 144 | + }{ |
| 145 | + "quincy": { |
| 146 | + input: `"quincy"`, |
| 147 | + expected: CephVersionNameQuincy, |
| 148 | + }, |
| 149 | + "reef": { |
| 150 | + input: `"reef"`, |
| 151 | + expected: CephVersionNameReef, |
| 152 | + }, |
| 153 | + "squid": { |
| 154 | + input: `"squid"`, |
| 155 | + expected: CephVersionNameSquid, |
| 156 | + }, |
| 157 | + "invalid": { |
| 158 | + input: `"invalid"`, |
| 159 | + expectError: true, |
| 160 | + }, |
| 161 | + "malformed json": { |
| 162 | + input: `quincy`, |
| 163 | + expectError: true, |
| 164 | + }, |
| 165 | + } |
| 166 | + |
| 167 | + for name, test := range tests { |
| 168 | + t.Run(name, func(t *testing.T) { |
| 169 | + t.Parallel() |
| 170 | + |
| 171 | + var result CephVersionName |
| 172 | + err := json.Unmarshal([]byte(test.input), &result) |
| 173 | + |
| 174 | + if test.expectError { |
| 175 | + require.Error(t, err) |
| 176 | + } else { |
| 177 | + require.NoError(t, err) |
| 178 | + require.Equal(t, test.expected, result) |
| 179 | + } |
| 180 | + }) |
| 181 | + } |
| 182 | +} |
0 commit comments