vantage-sql 0.5.3

Vantage extension for SQL databases (Postgres, MySQL, SQLite)
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
//! Numeric type matrix tests via Table<MysqlDB, Entity>.
//!
//! Tests Decimal, i64, and f64 across column types (VARCHAR, DECIMAL, DOUBLE,
//! FLOAT, BIGINT). Tables pre-created by v5.sql (same shape: id, name, value).

use rust_decimal::Decimal;
use std::str::FromStr;

#[allow(unused_imports)]
use vantage_sql::mysql::MysqlType;
use vantage_sql::mysql::{AnyMysqlType, MysqlDB};
use vantage_table::table::Table;
use vantage_types::entity;

use vantage_dataset::{ReadableDataSet, WritableDataSet};

const MYSQL_URL: &str = "mysql://vantage:vantage@localhost:3306/vantage_v5";

async fn db() -> MysqlDB {
    MysqlDB::connect(MYSQL_URL).await.unwrap()
}

#[entity(MysqlType)]
#[derive(Debug, Clone, PartialEq, Default)]
struct ValDecimal {
    name: String,
    value: Decimal,
}

macro_rules! table_for {
    ($table:expr, $db:expr) => {
        Table::<MysqlDB, ValDecimal>::new($table, $db)
            .with_id_column("id")
            .with_column_of::<String>("name")
            .with_column_of::<Decimal>("value")
    };
}

/// Try replace + read-back.
async fn try_round_trip(
    table: &Table<MysqlDB, ValDecimal>,
    id: &str,
    entity: &ValDecimal,
) -> Result<ValDecimal, String> {
    table
        .replace(&id.to_string(), entity)
        .await
        .map_err(|e| e.to_string())?;
    table
        .get(id)
        .await
        .map_err(|e| e.to_string())?
        .ok_or_else(|| "row missing after replace".to_string())
}

fn dec(s: &str) -> Decimal {
    Decimal::from_str(s).unwrap()
}

// ═════════════════════════════════════════════════════════════════════════
// 1. VARCHAR — exact string round-trip
// ═════════════════════════════════════════════════════════════════════════

#[tokio::test]
async fn test_varchar_decimal() {
    let t = table_for!("decimal_varchar", db().await);
    let orig = ValDecimal {
        name: "d".into(),
        value: dec("123456.789012"),
    };
    let fetched = try_round_trip(&t, "vc_d", &orig).await.unwrap();
    assert_eq!(fetched, orig);
}

#[tokio::test]
async fn test_varchar_high_precision() {
    let t = table_for!("decimal_varchar", db().await);
    let orig = ValDecimal {
        name: "hp".into(),
        value: dec("99999999999999999.123456789012345"),
    };
    let fetched = try_round_trip(&t, "vc_hp", &orig).await.unwrap();
    assert_eq!(fetched, orig);
}

// ═════════════════════════════════════════════════════════════════════════
// 2. DECIMAL(20,6) — precision limited to 6 decimal places
// ═════════════════════════════════════════════════════════════════════════

#[tokio::test]
async fn test_decimal_exact() {
    let t = table_for!("decimal_decimal", db().await);
    let orig = ValDecimal {
        name: "d".into(),
        value: dec("123456.789012"),
    };
    let fetched = try_round_trip(&t, "dd_exact", &orig).await.unwrap();
    assert_eq!(fetched, orig);
}

#[tokio::test]
async fn test_decimal_truncated() {
    // More than 6 decimal places — MySQL truncates
    let t = table_for!("decimal_decimal", db().await);
    let orig = ValDecimal {
        name: "trunc".into(),
        value: dec("123.123456789"),
    };
    let fetched = try_round_trip(&t, "dd_trunc", &orig).await.unwrap();
    // MySQL DECIMAL(20,6) rounds to 6 places
    assert_eq!(fetched.value, dec("123.123457"));
}

#[tokio::test]
async fn test_decimal_integer() {
    let t = table_for!("decimal_decimal", db().await);
    let orig = ValDecimal {
        name: "int".into(),
        value: dec("42"),
    };
    let fetched = try_round_trip(&t, "dd_int", &orig).await.unwrap();
    // MySQL stores as 42.000000
    assert_eq!(fetched.value, dec("42.000000"));
}

// ═════════════════════════════════════════════════════════════════════════
// 2b. DECIMAL(38,15) — wide enough for full rust_decimal precision
// ═════════════════════════════════════════════════════════════════════════

#[tokio::test]
async fn test_decimal_wide_high_precision() {
    let t = table_for!("decimal_decimal_wide", db().await);
    let orig = ValDecimal {
        name: "hp".into(),
        value: dec("99999999999999999.123456789012345"),
    };
    let fetched = try_round_trip(&t, "dw_hp", &orig).await.unwrap();
    assert_eq!(fetched, orig);
}

#[tokio::test]
async fn test_decimal_wide_negative() {
    let t = table_for!("decimal_decimal_wide", db().await);
    let orig = ValDecimal {
        name: "neg".into(),
        value: dec("-12345678901234.567890123456789"),
    };
    let fetched = try_round_trip(&t, "dw_neg", &orig).await.unwrap();
    assert_eq!(fetched, orig);
}

#[tokio::test]
async fn test_decimal_wide_tiny() {
    let t = table_for!("decimal_decimal_wide", db().await);
    let orig = ValDecimal {
        name: "tiny".into(),
        value: dec("0.000000000000001"),
    };
    let fetched = try_round_trip(&t, "dw_tiny", &orig).await.unwrap();
    assert_eq!(fetched, orig);
}

// ═════════════════════════════════════════════════════════════════════════
// 3. DOUBLE — f64 precision (~15 significant digits)
// ═════════════════════════════════════════════════════════════════════════

#[tokio::test]
async fn test_double_simple() {
    let t = table_for!("decimal_double", db().await);
    let orig = ValDecimal {
        name: "d".into(),
        value: dec("123.456"),
    };
    let fetched = try_round_trip(&t, "dbl_s", &orig).await.unwrap();
    // DOUBLE may lose trailing precision
    let diff = (fetched.value - orig.value).abs();
    assert!(diff < dec("0.001"), "diff too large: {}", diff);
}

// ═════════════════════════════════════════════════════════════════════════
// 4. FLOAT — f32 precision (~7 significant digits)
// ═════════════════════════════════════════════════════════════════════════

#[tokio::test]
async fn test_float_simple() {
    let t = table_for!("decimal_float", db().await);
    let orig = ValDecimal {
        name: "d".into(),
        value: dec("123.456"),
    };
    let fetched = try_round_trip(&t, "flt_s", &orig).await.unwrap();
    // FLOAT has ~7 significant digits
    let diff = (fetched.value - orig.value).abs();
    assert!(diff < dec("0.01"), "diff too large: {}", diff);
}

// ═════════════════════════════════════════════════════════════════════════
// 5. BIGINT — integer only, fractional part lost
// ═════════════════════════════════════════════════════════════════════════

#[tokio::test]
async fn test_bigint_integer() {
    let t = table_for!("decimal_bigint", db().await);
    let orig = ValDecimal {
        name: "d".into(),
        value: dec("42"),
    };
    let fetched = try_round_trip(&t, "bi_int", &orig).await.unwrap();
    assert_eq!(fetched.value, dec("42"));
}

#[tokio::test]
async fn test_bigint_truncates_fraction() {
    let t = table_for!("decimal_bigint", db().await);
    let orig = ValDecimal {
        name: "frac".into(),
        value: dec("123.999"),
    };
    let fetched = try_round_trip(&t, "bi_frac", &orig).await.unwrap();
    // BIGINT truncates fractional part
    assert_eq!(fetched.value, dec("124"));
}

// ═════════════════════════════════════════════════════════════════════════
// i64 across column types
// ═════════════════════════════════════════════════════════════════════════

#[entity(MysqlType)]
#[derive(Debug, Clone, PartialEq, Default)]
struct ValI64 {
    name: String,
    value: i64,
}

macro_rules! table_i64 {
    ($table:expr, $db:expr) => {
        Table::<MysqlDB, ValI64>::new($table, $db)
            .with_id_column("id")
            .with_column_of::<String>("name")
            .with_column_of::<i64>("value")
    };
}

async fn try_i64(
    table: &Table<MysqlDB, ValI64>,
    id: &str,
    entity: &ValI64,
) -> Result<ValI64, String> {
    table
        .replace(&id.to_string(), entity)
        .await
        .map_err(|e| e.to_string())?;
    table
        .get(id)
        .await
        .map_err(|e| e.to_string())?
        .ok_or_else(|| "row missing after replace".to_string())
}

#[tokio::test]
async fn test_i64_varchar() {
    let t = table_i64!("decimal_varchar", db().await);
    let orig = ValI64 {
        name: "i".into(),
        value: 42,
    };
    let fetched = try_i64(&t, "i64_vc", &orig).await.unwrap();
    assert_eq!(fetched, orig);
}

#[tokio::test]
async fn test_i64_bigint() {
    let t = table_i64!("decimal_bigint", db().await);
    let orig = ValI64 {
        name: "i".into(),
        value: 9_223_372_036_854_775_807,
    }; // i64::MAX
    let fetched = try_i64(&t, "i64_bi", &orig).await.unwrap();
    assert_eq!(fetched, orig);
}

#[tokio::test]
async fn test_i64_double() {
    // DOUBLE returns Float — no Float→Integer cross-conversion
    let t = table_i64!("decimal_double", db().await);
    let orig = ValI64 {
        name: "i".into(),
        value: 42,
    };
    assert!(try_i64(&t, "i64_dbl", &orig).await.is_err());
}

#[tokio::test]
async fn test_i64_double_large() {
    // i64 beyond f64 exact range (~2^53) — precision lost
    let t = table_i64!("decimal_double", db().await);
    let orig = ValI64 {
        name: "big".into(),
        value: 9_007_199_254_740_993,
    }; // 2^53 + 1
    // DOUBLE returns Float — no Float→Integer cross-conversion
    assert!(try_i64(&t, "i64_dbl_big", &orig).await.is_err());
}

#[tokio::test]
async fn test_i64_decimal() {
    // DECIMAL returns Tag(10, Text) — no Decimal→Integer cross-conversion
    let t = table_i64!("decimal_decimal", db().await);
    let orig = ValI64 {
        name: "i".into(),
        value: 42,
    };
    assert!(try_i64(&t, "i64_dec", &orig).await.is_err());
}

#[tokio::test]
async fn test_i64_float() {
    // FLOAT returns Float — no Float→Integer cross-conversion
    let t = table_i64!("decimal_float", db().await);
    let orig = ValI64 {
        name: "i".into(),
        value: 42,
    };
    assert!(try_i64(&t, "i64_flt", &orig).await.is_err());
}

#[tokio::test]
async fn test_i64_decimal_wide() {
    // DECIMAL(38,15) returns Tag(10, Text) — no Decimal→Integer cross-conversion
    let t = table_i64!("decimal_decimal_wide", db().await);
    let orig = ValI64 {
        name: "i".into(),
        value: 42,
    };
    assert!(try_i64(&t, "i64_dw", &orig).await.is_err());
}

// ═════════════════════════════════════════════════════════════════════════
// f64 across column types
// ═════════════════════════════════════════════════════════════════════════

#[entity(MysqlType)]
#[derive(Debug, Clone, PartialEq, Default)]
struct ValF64 {
    name: String,
    value: f64,
}

macro_rules! table_f64 {
    ($table:expr, $db:expr) => {
        Table::<MysqlDB, ValF64>::new($table, $db)
            .with_id_column("id")
            .with_column_of::<String>("name")
            .with_column_of::<f64>("value")
    };
}

async fn try_f64(
    table: &Table<MysqlDB, ValF64>,
    id: &str,
    entity: &ValF64,
) -> Result<ValF64, String> {
    table
        .replace(&id.to_string(), entity)
        .await
        .map_err(|e| e.to_string())?;
    table
        .get(id)
        .await
        .map_err(|e| e.to_string())?
        .ok_or_else(|| "row missing after replace".to_string())
}

#[tokio::test]
async fn test_f64_varchar() {
    let t = table_f64!("decimal_varchar", db().await);
    let orig = ValF64 {
        name: "f".into(),
        value: 123.456,
    };
    let fetched = try_f64(&t, "f64_vc", &orig).await.unwrap();
    assert_eq!(fetched, orig);
}

#[tokio::test]
async fn test_f64_double() {
    let t = table_f64!("decimal_double", db().await);
    let orig = ValF64 {
        name: "f".into(),
        value: 123.456,
    };
    let fetched = try_f64(&t, "f64_dbl", &orig).await.unwrap();
    assert_eq!(fetched, orig);
}

#[tokio::test]
async fn test_f64_float() {
    // FLOAT is f32 — precision loss
    let t = table_f64!("decimal_float", db().await);
    let orig = ValF64 {
        name: "f".into(),
        value: 123.456,
    };
    let fetched = try_f64(&t, "f64_flt", &orig).await.unwrap();
    assert!((fetched.value - orig.value).abs() < 0.01, "diff too large");
}

#[tokio::test]
async fn test_f64_bigint() {
    // BIGINT returns Integer — no Integer→Float cross-conversion
    let t = table_f64!("decimal_bigint", db().await);
    let orig = ValF64 {
        name: "f".into(),
        value: 123.999,
    };
    assert!(try_f64(&t, "f64_bi", &orig).await.is_err());
}

#[tokio::test]
async fn test_f64_decimal() {
    // DECIMAL returns Tag(10, Text) — no Decimal→Float cross-conversion
    let t = table_f64!("decimal_decimal", db().await);
    let orig = ValF64 {
        name: "f".into(),
        value: 123.456,
    };
    assert!(try_f64(&t, "f64_dec", &orig).await.is_err());
}

#[tokio::test]
async fn test_f64_decimal_wide() {
    // DECIMAL(38,15) returns Tag(10, Text) — no Decimal→Float cross-conversion
    let t = table_f64!("decimal_decimal_wide", db().await);
    let orig = ValF64 {
        name: "f".into(),
        value: 123.456,
    };
    assert!(try_f64(&t, "f64_dw", &orig).await.is_err());
}