| 1 | // vtest build: !windows |
| 2 | module main |
| 3 | |
| 4 | import db.pg |
| 5 | import db.sqlite |
| 6 | |
| 7 | fn test_pg_connection_user_aliases() { |
| 8 | assert pg.Config{ |
| 9 | user: 'alice' |
| 10 | }.connection_user()! == 'alice' |
| 11 | assert pg.Config{ |
| 12 | username: 'alice' |
| 13 | }.connection_user()! == 'alice' |
| 14 | assert pg.Config{ |
| 15 | user: 'alice' |
| 16 | username: 'alice' |
| 17 | }.connection_user()! == 'alice' |
| 18 | if _ := pg.Config{ |
| 19 | user: 'alice' |
| 20 | username: 'bob' |
| 21 | }.connection_user() |
| 22 | { |
| 23 | assert false |
| 24 | } else { |
| 25 | assert err.msg().contains('must match') |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | fn test_pg_row_value_helpers() { |
| 30 | mut vals := []?string{} |
| 31 | vals << none |
| 32 | vals << 'hello' |
| 33 | vals << '' |
| 34 | row := pg.Row{ |
| 35 | vals: vals |
| 36 | } |
| 37 | assert row.val(0) == '' |
| 38 | assert row.val(1) == 'hello' |
| 39 | assert row.values() == ['', 'hello', ''] |
| 40 | assert row.val_opt(0) == none |
| 41 | assert row.val_opt(1) or { '' } == 'hello' |
| 42 | } |
| 43 | |
| 44 | fn test_sqlite_row_value_helpers() { |
| 45 | sqlite_row := sqlite.Row{ |
| 46 | vals: ['hello', ''] |
| 47 | names: ['first', 'second'] |
| 48 | } |
| 49 | assert sqlite_row.val(0) == 'hello' |
| 50 | assert sqlite_row.values() == ['hello', ''] |
| 51 | } |
| 52 | |
| 53 | fn test_pg_result_empty_rows_synthetic() { |
| 54 | // Construct a Result directly with column names but no rows. |
| 55 | // This simulates what exec_result() should return for queries like |
| 56 | // "SELECT 1 AS id WHERE false" and verifies the downstream |
| 57 | // contract of Result with zero tuples. |
| 58 | res := pg.Result{ |
| 59 | cols: { |
| 60 | 'id': 0 |
| 61 | } |
| 62 | names: ['id'] |
| 63 | rows: [] |
| 64 | } |
| 65 | |
| 66 | assert res.names.len == 1 |
| 67 | assert res.names[0] == 'id' |
| 68 | assert res.cols == { |
| 69 | 'id': 0 |
| 70 | } |
| 71 | assert res.rows.len == 0 |
| 72 | } |
| 73 | |
| 74 | fn test_pg_result_multi_col_empty_rows() { |
| 75 | // Multiple columns, zero rows — the fix scenario with more than one column |
| 76 | res := pg.Result{ |
| 77 | cols: { |
| 78 | 'id': 0 |
| 79 | 'name': 1 |
| 80 | } |
| 81 | names: ['id', 'name'] |
| 82 | rows: [] |
| 83 | } |
| 84 | |
| 85 | assert res.names.len == 2 |
| 86 | assert res.names[0] == 'id' |
| 87 | assert res.names[1] == 'name' |
| 88 | assert res.cols == { |
| 89 | 'id': 0 |
| 90 | 'name': 1 |
| 91 | } |
| 92 | assert res.rows.len == 0 |
| 93 | |
| 94 | // Verify column lookup via cols map |
| 95 | assert res.cols['id'] == 0 |
| 96 | assert res.cols['name'] == 1 |
| 97 | } |
| 98 | |
| 99 | fn test_pg_result_no_cols_no_rows() { |
| 100 | // Zero columns, zero rows — edge case for DDL / command results |
| 101 | res := pg.Result{ |
| 102 | cols: map[string]int{} |
| 103 | names: []string{} |
| 104 | rows: [] |
| 105 | } |
| 106 | |
| 107 | assert res.names.len == 0 |
| 108 | assert res.cols.len == 0 |
| 109 | assert res.rows.len == 0 |
| 110 | } |
| 111 | |
| 112 | fn test_pg_result_as_structs_empty_rows() { |
| 113 | // as_structs must return an empty slice when there are no rows, |
| 114 | // even when column metadata is present. |
| 115 | res := pg.Result{ |
| 116 | cols: { |
| 117 | 'id': 0 |
| 118 | } |
| 119 | names: ['id'] |
| 120 | rows: [] |
| 121 | } |
| 122 | |
| 123 | structs := res.as_structs[map[string]string](fn (res pg.Result, row pg.Row) !map[string]string { |
| 124 | return map[string]string{} |
| 125 | })! |
| 126 | |
| 127 | assert structs.len == 0 |
| 128 | } |
| 129 | |