|
| 1 | +package goeql |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "reflect" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +// Test EncryptedText Serialization |
| 10 | +func TestEncryptedText_Serialize(t *testing.T) { |
| 11 | + et := EncryptedText("Hello, World!") |
| 12 | + table := "test_table" |
| 13 | + column := "test_column" |
| 14 | + |
| 15 | + serializedData, err := et.Serialize(table, column) |
| 16 | + if err != nil { |
| 17 | + t.Fatalf("Serialize returned error: %v", err) |
| 18 | + } |
| 19 | + |
| 20 | + desearlizedData, err := et.Deserialize(serializedData) |
| 21 | + if err != nil { |
| 22 | + t.Fatalf("Deserialize returned error: %v", err) |
| 23 | + } |
| 24 | + |
| 25 | + if !reflect.DeepEqual(desearlizedData, et) { |
| 26 | + t.Errorf("Expected deserialized value to be '%s', got '%s'", et, desearlizedData) |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +// Test EncryptedText Deserialization |
| 31 | +func TestEncryptedText_Deserialize(t *testing.T) { |
| 32 | + ec := EncryptedColumn{ |
| 33 | + K: "pt", |
| 34 | + P: "Hello, World!", |
| 35 | + I: TableColumn{T: "test_table", C: "test_column"}, |
| 36 | + V: 1, |
| 37 | + } |
| 38 | + |
| 39 | + data, err := json.Marshal(ec) |
| 40 | + if err != nil { |
| 41 | + t.Fatalf("Error marshaling EncryptedColumn: %v", err) |
| 42 | + } |
| 43 | + |
| 44 | + var et EncryptedText |
| 45 | + deserialized, err := et.Deserialize(data) |
| 46 | + if err != nil { |
| 47 | + t.Fatalf("Deserialize returned error: %v", err) |
| 48 | + } |
| 49 | + |
| 50 | + if deserialized != EncryptedText("Hello, World!") { |
| 51 | + t.Errorf("Expected deserialized value to be 'Hello, World!', got '%s'", deserialized) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +// Test EncryptedJsonb Serialization |
| 56 | +func TestEncryptedJsonb_Serialize(t *testing.T) { |
| 57 | + // You must cast any int to float64 to get the correct JSON output |
| 58 | + // Deserialization will always return a float64 for ints as json.Unmarshal will |
| 59 | + // convert them to float64 by default |
| 60 | + ej := EncryptedJsonb{ |
| 61 | + "name": "Alice", |
| 62 | + "age": float64(30), |
| 63 | + "is_member": true, |
| 64 | + } |
| 65 | + |
| 66 | + table := "test_table" |
| 67 | + column := "test_column" |
| 68 | + |
| 69 | + serializedData, err := ej.Serialize(table, column) |
| 70 | + if err != nil { |
| 71 | + t.Fatalf("Serialize returned error: %v", err) |
| 72 | + } |
| 73 | + |
| 74 | + desearlizedData, err := ej.Deserialize(serializedData) |
| 75 | + if err != nil { |
| 76 | + t.Fatalf("Deserialize returned error: %v", err) |
| 77 | + } |
| 78 | + |
| 79 | + if !reflect.DeepEqual(desearlizedData, ej) { |
| 80 | + t.Errorf("Expected deserialized value to be '%s', got '%s'", ej, desearlizedData) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +// Test EncryptedJsonb Deserialization |
| 85 | +func TestEncryptedJsonb_Deserialize(t *testing.T) { |
| 86 | + originalData := map[string]interface{}{ |
| 87 | + "name": "Alice", |
| 88 | + "age": float64(30), |
| 89 | + "is_member": true, |
| 90 | + } |
| 91 | + |
| 92 | + jsonString, err := json.Marshal(originalData) |
| 93 | + if err != nil { |
| 94 | + t.Fatalf("Error marshaling original data: %v", err) |
| 95 | + } |
| 96 | + |
| 97 | + ec := EncryptedColumn{ |
| 98 | + K: "pt", |
| 99 | + P: string(jsonString), |
| 100 | + I: TableColumn{T: "test_table", C: "test_column"}, |
| 101 | + V: 1, |
| 102 | + } |
| 103 | + |
| 104 | + data, err := json.Marshal(ec) |
| 105 | + if err != nil { |
| 106 | + t.Fatalf("Error marshaling EncryptedColumn: %v", err) |
| 107 | + } |
| 108 | + |
| 109 | + var ej EncryptedJsonb |
| 110 | + deserialized, err := ej.Deserialize(data) |
| 111 | + if err != nil { |
| 112 | + t.Fatalf("Deserialize returned error: %v", err) |
| 113 | + } |
| 114 | + |
| 115 | + if !reflect.DeepEqual(deserialized, EncryptedJsonb(originalData)) { |
| 116 | + t.Errorf("Deserialized data does not match original data") |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +// Test EncryptedInt Serialization |
| 121 | +func TestEncryptedInt_Serialize(t *testing.T) { |
| 122 | + ei := EncryptedInt(42) |
| 123 | + table := "test_table" |
| 124 | + column := "test_column" |
| 125 | + |
| 126 | + serializedData, err := ei.Serialize(table, column) |
| 127 | + if err != nil { |
| 128 | + t.Fatalf("Serialize returned error: %v", err) |
| 129 | + } |
| 130 | + |
| 131 | + var ec EncryptedColumn |
| 132 | + if err := json.Unmarshal(serializedData, &ec); err != nil { |
| 133 | + t.Fatalf("Error unmarshaling serialized data: %v", err) |
| 134 | + } |
| 135 | + |
| 136 | + expectedP := "42" |
| 137 | + if ec.P != expectedP { |
| 138 | + t.Errorf("Expected P to be '%s', got '%s'", expectedP, ec.P) |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +// Test EncryptedInt Deserialization |
| 143 | +func TestEncryptedInt_Deserialize(t *testing.T) { |
| 144 | + ec := EncryptedColumn{ |
| 145 | + K: "pt", |
| 146 | + P: "42", |
| 147 | + I: TableColumn{T: "test_table", C: "test_column"}, |
| 148 | + V: 1, |
| 149 | + } |
| 150 | + |
| 151 | + data, err := json.Marshal(ec) |
| 152 | + if err != nil { |
| 153 | + t.Fatalf("Error marshaling EncryptedColumn: %v", err) |
| 154 | + } |
| 155 | + |
| 156 | + var ei EncryptedInt |
| 157 | + deserialized, err := ei.Deserialize(data) |
| 158 | + if err != nil { |
| 159 | + t.Fatalf("Deserialize returned error: %v", err) |
| 160 | + } |
| 161 | + |
| 162 | + if deserialized != EncryptedInt(42) { |
| 163 | + t.Errorf("Expected deserialized value to be 42, got %d", deserialized) |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +// Test EncryptedBool Serialization |
| 168 | +func TestEncryptedBool_Serialize(t *testing.T) { |
| 169 | + eb := EncryptedBool(true) |
| 170 | + table := "test_table" |
| 171 | + column := "test_column" |
| 172 | + |
| 173 | + serializedData, err := eb.Serialize(table, column) |
| 174 | + if err != nil { |
| 175 | + t.Fatalf("Serialize returned error: %v", err) |
| 176 | + } |
| 177 | + |
| 178 | + var ec EncryptedColumn |
| 179 | + if err := json.Unmarshal(serializedData, &ec); err != nil { |
| 180 | + t.Fatalf("Error unmarshaling serialized data: %v", err) |
| 181 | + } |
| 182 | + |
| 183 | + if ec.P != "true" { |
| 184 | + t.Errorf("Expected P to be 'true', got '%s'", ec.P) |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +// Test EncryptedBool Deserialization |
| 189 | +func TestEncryptedBool_Deserialize(t *testing.T) { |
| 190 | + ec := EncryptedColumn{ |
| 191 | + K: "pt", |
| 192 | + P: "true", |
| 193 | + I: TableColumn{T: "test_table", C: "test_column"}, |
| 194 | + V: 1, |
| 195 | + } |
| 196 | + |
| 197 | + data, err := json.Marshal(ec) |
| 198 | + if err != nil { |
| 199 | + t.Fatalf("Error marshaling EncryptedColumn: %v", err) |
| 200 | + } |
| 201 | + |
| 202 | + var eb EncryptedBool |
| 203 | + deserialized, err := eb.Deserialize(data) |
| 204 | + if err != nil { |
| 205 | + t.Fatalf("Deserialize returned error: %v", err) |
| 206 | + } |
| 207 | + |
| 208 | + if deserialized != EncryptedBool(true) { |
| 209 | + t.Errorf("Expected deserialized value to be true, got %v", deserialized) |
| 210 | + } |
| 211 | +} |
| 212 | + |
| 213 | +// Test SerializeQuery Function |
| 214 | +func TestSerializeQuery(t *testing.T) { |
| 215 | + tests := []struct { |
| 216 | + value interface{} |
| 217 | + table string |
| 218 | + column string |
| 219 | + expectedP string |
| 220 | + }{ |
| 221 | + {value: "test_string", table: "table1", column: "column1", expectedP: "test_string"}, |
| 222 | + {value: 123, table: "table2", column: "column2", expectedP: "123"}, |
| 223 | + {value: true, table: "table3", column: "column3", expectedP: "true"}, |
| 224 | + {value: map[string]interface{}{"key": "value"}, table: "table4", column: "column4", expectedP: `{"key":"value"}`}, |
| 225 | + } |
| 226 | + |
| 227 | + for _, tt := range tests { |
| 228 | + serializedData, err := SerializeQuery(tt.value, tt.table, tt.column) |
| 229 | + if err != nil { |
| 230 | + t.Fatalf("SerializeQuery returned error: %v", err) |
| 231 | + } |
| 232 | + |
| 233 | + var ec EncryptedColumn |
| 234 | + if err := json.Unmarshal(serializedData, &ec); err != nil { |
| 235 | + t.Fatalf("Error unmarshaling serialized data: %v", err) |
| 236 | + } |
| 237 | + |
| 238 | + if ec.P != tt.expectedP { |
| 239 | + t.Errorf("Expected P to be '%s', got '%s'", tt.expectedP, ec.P) |
| 240 | + } |
| 241 | + } |
| 242 | +} |
| 243 | + |
| 244 | +// Test ToEncryptedColumn Function |
| 245 | +func TestToEncryptedColumn(t *testing.T) { |
| 246 | + tests := []struct { |
| 247 | + value interface{} |
| 248 | + table string |
| 249 | + column string |
| 250 | + expectedP string |
| 251 | + }{ |
| 252 | + {value: "test_string", table: "table1", column: "column1", expectedP: "test_string"}, |
| 253 | + {value: 123, table: "table2", column: "column2", expectedP: "123"}, |
| 254 | + {value: 123.456, table: "table3", column: "column3", expectedP: "123.456000"}, |
| 255 | + {value: true, table: "table4", column: "column4", expectedP: "true"}, |
| 256 | + {value: map[string]interface{}{"key": "value"}, table: "table5", column: "column5", expectedP: `{"key":"value"}`}, |
| 257 | + } |
| 258 | + |
| 259 | + for _, tt := range tests { |
| 260 | + ec, err := ToEncryptedColumn(tt.value, tt.table, tt.column) |
| 261 | + if err != nil { |
| 262 | + t.Fatalf("ToEncryptedColumn returned error: %v", err) |
| 263 | + } |
| 264 | + |
| 265 | + if ec.P != tt.expectedP { |
| 266 | + t.Errorf("Expected P to be '%s', got '%s'", tt.expectedP, ec.P) |
| 267 | + } |
| 268 | + } |
| 269 | +} |
| 270 | + |
| 271 | +// Test convertToString Function |
| 272 | +func TestConvertToString(t *testing.T) { |
| 273 | + tests := []struct { |
| 274 | + value interface{} |
| 275 | + expectedStr string |
| 276 | + expectError bool |
| 277 | + }{ |
| 278 | + {value: "test_string", expectedStr: "test_string", expectError: false}, |
| 279 | + {value: 123, expectedStr: "123", expectError: false}, |
| 280 | + {value: 123.456, expectedStr: "123.456000", expectError: false}, |
| 281 | + {value: true, expectedStr: "true", expectError: false}, |
| 282 | + {value: map[string]interface{}{"key": "value"}, expectedStr: `{"key":"value"}`, expectError: false}, |
| 283 | + {value: []int{1, 2, 3}, expectedStr: "", expectError: true}, // Unsupported type |
| 284 | + } |
| 285 | + |
| 286 | + for _, tt := range tests { |
| 287 | + str, err := convertToString(tt.value) |
| 288 | + if tt.expectError { |
| 289 | + if err == nil { |
| 290 | + t.Errorf("Expected error for value: %v, but got none", tt.value) |
| 291 | + } |
| 292 | + } else { |
| 293 | + if err != nil { |
| 294 | + t.Errorf("Unexpected error for value: %v, error: %v", tt.value, err) |
| 295 | + } else if str != tt.expectedStr { |
| 296 | + t.Errorf("Expected '%s', got '%s' for value: %v", tt.expectedStr, str, tt.value) |
| 297 | + } |
| 298 | + } |
| 299 | + } |
| 300 | +} |
| 301 | + |
| 302 | +// Test EncryptedInt Deserialization Error |
| 303 | +func TestEncryptedInt_Deserialize_Error(t *testing.T) { |
| 304 | + ec := EncryptedColumn{ |
| 305 | + K: "pt", |
| 306 | + P: "not_an_integer", |
| 307 | + I: TableColumn{T: "test_table", C: "test_column"}, |
| 308 | + V: 1, |
| 309 | + } |
| 310 | + |
| 311 | + data, err := json.Marshal(ec) |
| 312 | + if err != nil { |
| 313 | + t.Fatalf("Error marshaling EncryptedColumn: %v", err) |
| 314 | + } |
| 315 | + |
| 316 | + var ei EncryptedInt |
| 317 | + _, err = ei.Deserialize(data) |
| 318 | + if err == nil { |
| 319 | + t.Errorf("Expected error during Deserialize, but got none") |
| 320 | + } |
| 321 | +} |
| 322 | + |
| 323 | +// Test EncryptedBool Deserialization Error |
| 324 | +func TestEncryptedBool_Deserialize_Error(t *testing.T) { |
| 325 | + ec := EncryptedColumn{ |
| 326 | + K: "pt", |
| 327 | + P: "not_a_boolean", |
| 328 | + I: TableColumn{T: "test_table", C: "test_column"}, |
| 329 | + V: 1, |
| 330 | + } |
| 331 | + |
| 332 | + data, err := json.Marshal(ec) |
| 333 | + if err != nil { |
| 334 | + t.Fatalf("Error marshaling EncryptedColumn: %v", err) |
| 335 | + } |
| 336 | + |
| 337 | + var eb EncryptedBool |
| 338 | + _, err = eb.Deserialize(data) |
| 339 | + if err == nil { |
| 340 | + t.Errorf("Expected error during Deserialize, but got none") |
| 341 | + } |
| 342 | +} |
| 343 | + |
| 344 | +// Test EncryptedJsonb Deserialization Error |
| 345 | +func TestEncryptedJsonb_Deserialize_Error(t *testing.T) { |
| 346 | + ec := EncryptedColumn{ |
| 347 | + K: "pt", |
| 348 | + P: "invalid_json", |
| 349 | + I: TableColumn{T: "test_table", C: "test_column"}, |
| 350 | + V: 1, |
| 351 | + } |
| 352 | + |
| 353 | + data, err := json.Marshal(ec) |
| 354 | + if err != nil { |
| 355 | + t.Fatalf("Error marshaling EncryptedColumn: %v", err) |
| 356 | + } |
| 357 | + |
| 358 | + var ej EncryptedJsonb |
| 359 | + _, err = ej.Deserialize(data) |
| 360 | + if err == nil { |
| 361 | + t.Errorf("Expected error during Deserialize, but got none") |
| 362 | + } |
| 363 | +} |
0 commit comments