packages
The Louise packages, each released on its own
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#28 · main · 3 min 34 s
0Étoiles
0Forks
3Issues
| 1 | // The pool and transactions, against a real PostgreSQL when |
| 2 | // LOUISE_PG_URL is set (skipped otherwise). |
| 3 | |
| 4 | import postgres as pg |
| 5 | import postgres.{sql} |
| 6 | import std/os |
| 7 | import std/process.{type proc} |
| 8 | import std/random.{type random} |
| 9 | import std/result |
| 10 | import std/test.{assert, assert_equal} |
| 11 | |
| 12 | @test |
| 13 | fn the_pool_serves_statements :: -> () / <io, proc, random> |
| 14 | { |
| 15 | match os.env "LOUISE_PG_URL" |
| 16 | { None -> () |
| 17 | , Some(url) -> |
| 18 | { |
| 19 | let config = result.expect (pg.config url) "config" |
| 20 | let pool = result.expect (pg.start_pool config 1) "the pool" |
| 21 | // A size-1 pool, twice in a row: the connection comes back. |
| 22 | // with_connection gives the action's own Result: unwrap both. |
| 23 | let first = |
| 24 | result.expect |
| 25 | ( result.expect |
| 26 | ( pg.with_connection |
| 27 | pool |
| 28 | fn { pg.query "select $1::int as n" [pg.int 41] } |
| 29 | ) |
| 30 | "first statement" |
| 31 | ) |
| 32 | "the query" |
| 33 | match first.rows |
| 34 | { [row] -> assert_equal (pg.row_int row "n") Ok(Some(41)) |
| 35 | , _ -> panic "one row" |
| 36 | } |
| 37 | let second = |
| 38 | result.expect |
| 39 | ( result.expect |
| 40 | ( pg.with_connection |
| 41 | pool |
| 42 | fn { pg.query_statement sql`select {41}::int + 1 as n` } |
| 43 | ) |
| 44 | "second statement" |
| 45 | ) |
| 46 | "the query" |
| 47 | match second.rows |
| 48 | { [row] -> assert_equal (pg.row_int row "n") Ok(Some(42)) |
| 49 | , _ -> panic "one row" |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | @test |
| 56 | fn a_transaction_commits_and_rolls_back :: -> () / <io, proc, random> |
| 57 | { |
| 58 | match os.env "LOUISE_PG_URL" |
| 59 | { None -> () |
| 60 | , Some(url) -> |
| 61 | { |
| 62 | let config = result.expect (pg.config url) "config" |
| 63 | let pool = result.expect (pg.start_pool config 1) "the pool" |
| 64 | let _ = |
| 65 | result.expect |
| 66 | ( result.expect |
| 67 | ( pg.with_connection |
| 68 | pool |
| 69 | fn |
| 70 | { |
| 71 | pg.execute |
| 72 | "create table if not exists louise_pool (id int primary key)" |
| 73 | [] |
| 74 | } |
| 75 | ) |
| 76 | "create table" |
| 77 | ) |
| 78 | "the create" |
| 79 | let _ = |
| 80 | result.expect |
| 81 | ( result.expect |
| 82 | ( pg.with_connection |
| 83 | pool |
| 84 | fn { pg.execute "delete from louise_pool" [] } |
| 85 | ) |
| 86 | "delete" |
| 87 | ) |
| 88 | "the delete" |
| 89 | // A failing transaction rolls its write back. |
| 90 | match pg.transaction |
| 91 | pool |
| 92 | fn |
| 93 | { |
| 94 | use _ <- |
| 95 | result.try |
| 96 | (pg.execute "insert into louise_pool (id) values ($1)" [pg.int 1]) |
| 97 | // A duplicate key ends the block with an Error. |
| 98 | pg.execute "insert into louise_pool (id) values ($1)" [pg.int 1] |
| 99 | } |
| 100 | { Ok(_) -> panic "the duplicate key must fail" |
| 101 | , Error(e) -> assert_equal (pg.sqlstate e) Some("23505") |
| 102 | } |
| 103 | // Rolled back: the table is empty. |
| 104 | let count = |
| 105 | result.expect |
| 106 | ( result.expect |
| 107 | ( pg.with_connection |
| 108 | pool |
| 109 | fn { pg.query "select count(*) as n from louise_pool" [] } |
| 110 | ) |
| 111 | "count" |
| 112 | ) |
| 113 | "the query" |
| 114 | match count.rows |
| 115 | { [row] -> assert_equal (pg.row_int row "n") Ok(Some(0)) |
| 116 | , _ -> panic "one row" |
| 117 | } |
| 118 | // A succeeding one commits. |
| 119 | result.expect |
| 120 | ( pg.transaction |
| 121 | pool |
| 122 | fn |
| 123 | { |
| 124 | use _ <- |
| 125 | result.try |
| 126 | ( pg.execute_statement |
| 127 | sql`insert into louise_pool (id) values (1)` |
| 128 | ) |
| 129 | use _ <- |
| 130 | result.try |
| 131 | ( pg.execute_statement |
| 132 | sql`insert into louise_pool (id) values (2)` |
| 133 | ) |
| 134 | Ok(()) |
| 135 | } |
| 136 | ) |
| 137 | "the committing transaction" |
| 138 | let committed = |
| 139 | result.expect |
| 140 | ( result.expect |
| 141 | ( pg.with_connection |
| 142 | pool |
| 143 | fn { pg.query "select count(*) as n from louise_pool" [] } |
| 144 | ) |
| 145 | "count again" |
| 146 | ) |
| 147 | "the query" |
| 148 | match committed.rows |
| 149 | { [row] -> assert_equal (pg.row_int row "n") Ok(Some(2)) |
| 150 | , _ -> panic "one row" |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | @test |
| 157 | fn savepoints_roll_back_to_themselves :: -> () / <io, proc, random> |
| 158 | { |
| 159 | match os.env "LOUISE_PG_URL" |
| 160 | { None -> () |
| 161 | , Some(url) -> |
| 162 | { |
| 163 | let config = result.expect (pg.config url) "config" |
| 164 | let pool = result.expect (pg.start_pool config 1) "the pool" |
| 165 | // A failing savepoint keeps the transaction's earlier writes. |
| 166 | result.expect |
| 167 | ( pg.transaction |
| 168 | pool |
| 169 | fn |
| 170 | { |
| 171 | use _ <- |
| 172 | result.try |
| 173 | (pg.execute "insert into louise_pool (id) values (100)" []) |
| 174 | match pg.savepoint |
| 175 | fn |
| 176 | { |
| 177 | use _ <- |
| 178 | result.try |
| 179 | ( pg.execute |
| 180 | "insert into louise_pool (id) values (101)" |
| 181 | [] |
| 182 | ) |
| 183 | // A duplicate key rolls back to the savepoint, not the transaction. |
| 184 | pg.execute "insert into louise_pool (id) values (100)" [] |
| 185 | } |
| 186 | { Ok(_) -> panic "the duplicate key must fail" |
| 187 | , Error(e) -> assert_equal (pg.sqlstate e) Some("23505") |
| 188 | } |
| 189 | Ok(()) |
| 190 | } |
| 191 | ) |
| 192 | "the outer transaction" |
| 193 | let count = |
| 194 | result.expect |
| 195 | ( result.expect |
| 196 | ( pg.with_connection |
| 197 | pool |
| 198 | fn |
| 199 | { |
| 200 | pg.query |
| 201 | "select count(*) as n from louise_pool where id >= 100" |
| 202 | [] |
| 203 | } |
| 204 | ) |
| 205 | "the count" |
| 206 | ) |
| 207 | "the query" |
| 208 | match count.rows |
| 209 | { [row] -> |
| 210 | assert_equal |
| 211 | (pg.row_int row "n") |
| 212 | Ok(Some(1)) // 100 stayed, 101 did not |
| 213 | , _ -> panic "one row" |
| 214 | } |
| 215 | let _ = |
| 216 | result.expect |
| 217 | ( result.expect |
| 218 | ( pg.with_connection |
| 219 | pool |
| 220 | fn { pg.execute "delete from louise_pool where id >= 100" [] } |
| 221 | ) |
| 222 | "cleanup" |
| 223 | ) |
| 224 | "the delete" |
| 225 | } |
| 226 | } |
| 227 | } |