mssql-types 0.10.0

SQL Server to Rust type mappings and conversions
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
//! Type conversion edge case tests (TEST-008).
//!
//! Tests edge cases for:
//! - NULL handling
//! - Unicode/UTF-8 boundary conditions
//! - Large dataset handling
//! - Type conversion boundaries

#![allow(clippy::unwrap_used, clippy::expect_used, clippy::approx_constant)]

use bytes::Bytes;
use mssql_types::{FromSql, SqlValue, ToSql, TypeError};

// ============================================================================
// NULL Handling Edge Cases
// ============================================================================

mod null_handling {
    use super::*;

    #[test]
    fn test_null_to_option_i32() {
        let null_value = SqlValue::Null;
        let result: Result<Option<i32>, _> = Option::<i32>::from_sql(&null_value);
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), None);
    }

    #[test]
    fn test_null_to_non_option_fails() {
        let null_value = SqlValue::Null;
        let result: Result<i32, _> = i32::from_sql(&null_value);
        assert!(matches!(result, Err(TypeError::UnexpectedNull)));
    }

    #[test]
    fn test_null_to_option_string() {
        let null_value = SqlValue::Null;
        let result: Result<Option<String>, _> = Option::<String>::from_sql(&null_value);
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), None);
    }

    #[test]
    fn test_null_to_string_fails() {
        let null_value = SqlValue::Null;
        let result: Result<String, _> = String::from_sql(&null_value);
        assert!(matches!(result, Err(TypeError::UnexpectedNull)));
    }

    #[test]
    fn test_option_none_to_sql() {
        let none_value: Option<i32> = None;
        let result = none_value.to_sql().unwrap();
        assert!(result.is_null());
    }

    #[test]
    fn test_option_some_to_sql() {
        let some_value: Option<i32> = Some(42);
        let result = some_value.to_sql().unwrap();
        assert!(!result.is_null());
        assert_eq!(result, SqlValue::Int(42));
    }

    #[test]
    fn test_nested_option_to_sql() {
        // Option<Option<i32>> should flatten correctly
        let nested: Option<i32> = Some(42);
        let result = nested.to_sql().unwrap();
        assert_eq!(result, SqlValue::Int(42));

        let nested_none: Option<i32> = None;
        let result_none = nested_none.to_sql().unwrap();
        assert!(result_none.is_null());
    }
}

// ============================================================================
// Integer Boundary Tests
// ============================================================================

mod integer_boundaries {
    use super::*;

    #[test]
    fn test_i32_max() {
        let max_val = SqlValue::Int(i32::MAX);
        let result: Result<i32, _> = i32::from_sql(&max_val);
        assert_eq!(result.unwrap(), i32::MAX);
    }

    #[test]
    fn test_i32_min() {
        let min_val = SqlValue::Int(i32::MIN);
        let result: Result<i32, _> = i32::from_sql(&min_val);
        assert_eq!(result.unwrap(), i32::MIN);
    }

    #[test]
    fn test_i64_max() {
        let max_val = SqlValue::BigInt(i64::MAX);
        let result: Result<i64, _> = i64::from_sql(&max_val);
        assert_eq!(result.unwrap(), i64::MAX);
    }

    #[test]
    fn test_i64_min() {
        let min_val = SqlValue::BigInt(i64::MIN);
        let result: Result<i64, _> = i64::from_sql(&min_val);
        assert_eq!(result.unwrap(), i64::MIN);
    }

    #[test]
    fn test_i16_max() {
        let max_val = SqlValue::SmallInt(i16::MAX);
        let result: Result<i16, _> = i16::from_sql(&max_val);
        assert_eq!(result.unwrap(), i16::MAX);
    }

    #[test]
    fn test_i16_min() {
        let min_val = SqlValue::SmallInt(i16::MIN);
        let result: Result<i16, _> = i16::from_sql(&min_val);
        assert_eq!(result.unwrap(), i16::MIN);
    }

    #[test]
    fn test_u8_max() {
        let max_val = SqlValue::TinyInt(u8::MAX);
        let result: Result<u8, _> = u8::from_sql(&max_val);
        assert_eq!(result.unwrap(), u8::MAX);
    }

    #[test]
    fn test_u8_zero() {
        let zero_val = SqlValue::TinyInt(0);
        let result: Result<u8, _> = u8::from_sql(&zero_val);
        assert_eq!(result.unwrap(), 0);
    }
}

// ============================================================================
// Floating Point Edge Cases
// ============================================================================

mod float_boundaries {
    use super::*;

    #[test]
    fn test_f64_max() {
        let max_val = SqlValue::Double(f64::MAX);
        let result: Result<f64, _> = f64::from_sql(&max_val);
        assert_eq!(result.unwrap(), f64::MAX);
    }

    #[test]
    fn test_f64_min() {
        let min_val = SqlValue::Double(f64::MIN);
        let result: Result<f64, _> = f64::from_sql(&min_val);
        assert_eq!(result.unwrap(), f64::MIN);
    }

    #[test]
    fn test_f64_positive_infinity() {
        let inf_val = SqlValue::Double(f64::INFINITY);
        let result: Result<f64, _> = f64::from_sql(&inf_val);
        assert!(result.unwrap().is_infinite());
    }

    #[test]
    fn test_f64_negative_infinity() {
        let neg_inf_val = SqlValue::Double(f64::NEG_INFINITY);
        let result: Result<f64, _> = f64::from_sql(&neg_inf_val);
        let val = result.unwrap();
        assert!(val.is_infinite() && val.is_sign_negative());
    }

    #[test]
    fn test_f64_nan() {
        let nan_val = SqlValue::Double(f64::NAN);
        let result: Result<f64, _> = f64::from_sql(&nan_val);
        assert!(result.unwrap().is_nan());
    }

    #[test]
    fn test_f64_zero() {
        let zero_val = SqlValue::Double(0.0);
        let result: Result<f64, _> = f64::from_sql(&zero_val);
        assert_eq!(result.unwrap(), 0.0);
    }

    #[test]
    fn test_f64_negative_zero() {
        let neg_zero_val = SqlValue::Double(-0.0);
        let result: Result<f64, _> = f64::from_sql(&neg_zero_val);
        let val = result.unwrap();
        assert!(val == 0.0); // -0.0 equals 0.0
    }

    #[test]
    fn test_f32_max() {
        let max_val = SqlValue::Float(f32::MAX);
        let result: Result<f32, _> = f32::from_sql(&max_val);
        assert_eq!(result.unwrap(), f32::MAX);
    }
}

// ============================================================================
// String/Unicode Edge Cases
// ============================================================================

mod unicode_handling {
    use super::*;

    #[test]
    fn test_empty_string() {
        let empty = SqlValue::String(String::new());
        let result: Result<String, _> = String::from_sql(&empty);
        assert_eq!(result.unwrap(), "");
    }

    #[test]
    fn test_ascii_string() {
        let ascii = SqlValue::String("Hello, World!".to_string());
        let result: Result<String, _> = String::from_sql(&ascii);
        assert_eq!(result.unwrap(), "Hello, World!");
    }

    #[test]
    fn test_unicode_basic_multilingual_plane() {
        // Characters in BMP (U+0000 to U+FFFF)
        let bmp = SqlValue::String("日本語テスト".to_string());
        let result: Result<String, _> = String::from_sql(&bmp);
        assert_eq!(result.unwrap(), "日本語テスト");
    }

    #[test]
    fn test_unicode_supplementary_planes() {
        // Characters outside BMP (requires surrogate pairs in UTF-16)
        let emoji = SqlValue::String("😀🎉🚀".to_string());
        let result: Result<String, _> = String::from_sql(&emoji);
        assert_eq!(result.unwrap(), "😀🎉🚀");
    }

    #[test]
    fn test_unicode_combining_characters() {
        // é composed as e + combining acute accent
        let combining = SqlValue::String("cafe\u{0301}".to_string());
        let result: Result<String, _> = String::from_sql(&combining);
        // The combining character should be preserved - no normalization
        assert_eq!(result.unwrap(), "cafe\u{0301}");
        // Note: "cafe\u{0301}" and "café" look identical but are different byte sequences
    }

    #[test]
    fn test_unicode_zero_width_characters() {
        // Zero-width joiner and other invisible characters
        let zwj = SqlValue::String("Test\u{200B}String".to_string()); // Zero-width space
        let result: Result<String, _> = String::from_sql(&zwj);
        let s = result.unwrap();
        assert!(s.contains('\u{200B}'));
    }

    #[test]
    fn test_unicode_rtl_characters() {
        // Right-to-left text (Hebrew/Arabic)
        let rtl = SqlValue::String("שלום".to_string());
        let result: Result<String, _> = String::from_sql(&rtl);
        assert_eq!(result.unwrap(), "שלום");
    }

    #[test]
    fn test_unicode_mixed_scripts() {
        let mixed = SqlValue::String("Hello世界مرحبا".to_string());
        let result: Result<String, _> = String::from_sql(&mixed);
        assert_eq!(result.unwrap(), "Hello世界مرحبا");
    }

    #[test]
    fn test_string_with_null_byte() {
        // Embedded null byte should be preserved
        let with_null = SqlValue::String("before\0after".to_string());
        let result: Result<String, _> = String::from_sql(&with_null);
        let s = result.unwrap();
        assert!(s.contains('\0'));
        assert_eq!(s.len(), 12);
    }

    #[test]
    fn test_long_string() {
        // Very long string (10KB)
        let long_str: String = "x".repeat(10_000);
        let long_val = SqlValue::String(long_str.clone());
        let result: Result<String, _> = String::from_sql(&long_val);
        assert_eq!(result.unwrap().len(), 10_000);
    }

    #[test]
    fn test_very_long_string() {
        // Very long string (1MB)
        let very_long: String = "x".repeat(1_000_000);
        let long_val = SqlValue::String(very_long.clone());
        let result: Result<String, _> = String::from_sql(&long_val);
        assert_eq!(result.unwrap().len(), 1_000_000);
    }
}

// ============================================================================
// Binary Data Edge Cases
// ============================================================================

mod binary_handling {
    use super::*;

    #[test]
    fn test_empty_binary() {
        let empty = SqlValue::Binary(Bytes::new());
        let result: Result<Vec<u8>, _> = Vec::<u8>::from_sql(&empty);
        assert_eq!(result.unwrap(), Vec::<u8>::new());
    }

    #[test]
    fn test_single_byte() {
        let single = SqlValue::Binary(Bytes::from_static(&[0xFF]));
        let result: Result<Vec<u8>, _> = Vec::<u8>::from_sql(&single);
        assert_eq!(result.unwrap(), vec![0xFF]);
    }

    #[test]
    fn test_all_byte_values() {
        // All possible byte values 0-255
        let all_bytes: Vec<u8> = (0u8..=255u8).collect();
        let all_val = SqlValue::Binary(Bytes::from(all_bytes.clone()));
        let result: Result<Vec<u8>, _> = Vec::<u8>::from_sql(&all_val);
        assert_eq!(result.unwrap(), all_bytes);
    }

    #[test]
    fn test_large_binary() {
        // 1MB binary data
        let large: Vec<u8> = vec![0xAB; 1_000_000];
        let large_val = SqlValue::Binary(Bytes::from(large.clone()));
        let result: Result<Vec<u8>, _> = Vec::<u8>::from_sql(&large_val);
        assert_eq!(result.unwrap().len(), 1_000_000);
    }
}

// ============================================================================
// Boolean Edge Cases
// ============================================================================

mod boolean_handling {
    use super::*;

    #[test]
    fn test_bool_true() {
        let true_val = SqlValue::Bool(true);
        let result: Result<bool, _> = bool::from_sql(&true_val);
        assert!(result.unwrap());
    }

    #[test]
    fn test_bool_false() {
        let false_val = SqlValue::Bool(false);
        let result: Result<bool, _> = bool::from_sql(&false_val);
        assert!(!result.unwrap());
    }

    #[test]
    fn test_bool_to_sql_true() {
        let result = true.to_sql().unwrap();
        assert_eq!(result, SqlValue::Bool(true));
    }

    #[test]
    fn test_bool_to_sql_false() {
        let result = false.to_sql().unwrap();
        assert_eq!(result, SqlValue::Bool(false));
    }
}

// ============================================================================
// Type Coercion Edge Cases
// ============================================================================

mod type_coercion {
    use super::*;

    #[test]
    fn test_int_to_i64() {
        // i32 value should convert to i64
        let int_val = SqlValue::Int(42);
        let result: Result<i64, _> = i64::from_sql(&int_val);
        assert_eq!(result.unwrap(), 42i64);
    }

    #[test]
    fn test_smallint_to_i32() {
        let small_val = SqlValue::SmallInt(100);
        let result: Result<i32, _> = i32::from_sql(&small_val);
        assert_eq!(result.unwrap(), 100i32);
    }

    #[test]
    fn test_tinyint_to_i32() {
        let tiny_val = SqlValue::TinyInt(255);
        let result: Result<i32, _> = i32::from_sql(&tiny_val);
        assert_eq!(result.unwrap(), 255i32);
    }

    #[test]
    fn test_float_to_f64() {
        let float_val = SqlValue::Float(3.14);
        let result: Result<f64, _> = f64::from_sql(&float_val);
        // Float to f64 should preserve value (within precision)
        assert!((result.unwrap() - 3.14f64).abs() < 0.0001);
    }
}

// ============================================================================
// Error Case Tests
// ============================================================================

mod error_cases {
    use super::*;

    #[test]
    fn test_string_to_int_fails() {
        let str_val = SqlValue::String("not a number".to_string());
        let result: Result<i32, _> = i32::from_sql(&str_val);
        assert!(result.is_err());
    }

    #[test]
    fn test_int_to_string() {
        // This should work as a string representation
        let int_val = SqlValue::Int(42);
        let _result: Result<String, _> = String::from_sql(&int_val);
        // May succeed or fail depending on implementation
        // Document expected behavior here
    }

    #[test]
    fn test_binary_to_string_fails() {
        let bin_val = SqlValue::Binary(Bytes::from_static(&[0xFF, 0xFE]));
        let _result: Result<String, _> = String::from_sql(&bin_val);
        // Binary to string should fail (or return hex?)
        // Document expected behavior
    }
}