-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathUpdateQueryTest.php
More file actions
131 lines (115 loc) · 4.97 KB
/
UpdateQueryTest.php
File metadata and controls
131 lines (115 loc) · 4.97 KB
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
<?hh // strict
namespace Slack\SQLFake;
use type Facebook\HackTest\HackTest;
use function Facebook\FBExpect\expect;
final class UpdateQueryTest extends HackTest {
private static ?AsyncMysqlConnection $conn;
public async function testUpdateSingleRow(): Awaitable<void> {
$conn = static::$conn as nonnull;
await $conn->query("UPDATE table3 SET name='updated' WHERE id=1");
$results = await $conn->query('SELECT * FROM table3');
expect($results->rows())->toBeSame(vec[
dict['id' => 1, 'group_id' => 12345, 'name' => 'updated'],
dict['id' => 2, 'group_id' => 12345, 'name' => 'name2'],
dict['id' => 3, 'group_id' => 12345, 'name' => 'name3'],
dict['id' => 4, 'group_id' => 6, 'name' => 'name4'],
dict['id' => 6, 'group_id' => 6, 'name' => 'name5'],
]);
}
public async function testUpdateMultipleRows(): Awaitable<void> {
$conn = static::$conn as nonnull;
await $conn->query("UPDATE table3 set name=CONCAT(name, id, 'updated'), group_id = 13 WHERE group_id=6");
$results = await $conn->query('SELECT * FROM table3 WHERE group_id=13');
expect($results->rows())->toBeSame(vec[
dict['id' => 4, 'group_id' => 13, 'name' => 'name44updated'],
dict['id' => 6, 'group_id' => 13, 'name' => 'name56updated'],
]);
}
public async function testUpdateWithLimit(): Awaitable<void> {
$conn = static::$conn as nonnull;
await $conn->query("UPDATE table3 set name='updated', group_id = 13 WHERE group_id=6 LIMIT 1");
$results = await $conn->query('SELECT * FROM table3 WHERE group_id=13');
expect($results->rows())->toBeSame(vec[
dict['id' => 4, 'group_id' => 13, 'name' => 'updated'],
]);
}
public async function testUpdateWithLimitAndOrderBy(): Awaitable<void> {
$conn = static::$conn as nonnull;
await $conn->query("UPDATE table3 set name='updated', group_id = 13 WHERE group_id=6 ORDER BY id desc LIMIT 1");
$results = await $conn->query('SELECT * FROM table3 WHERE group_id=13');
expect($results->rows())->toBeSame(vec[
dict['id' => 6, 'group_id' => 13, 'name' => 'updated'],
]);
$results = await $conn->query('SELECT * FROM table3 WHERE id=6');
expect($results->rows())->toBeSame(vec[
dict['id' => 6, 'group_id' => 13, 'name' => 'updated'],
]);
}
public async function testQualifiedTable(): Awaitable<void> {
$conn = static::$conn as nonnull;
$expected = vec[
dict['id' => 4, 'group_id' => 13, 'name' => 'name44updated'],
dict['id' => 6, 'group_id' => 13, 'name' => 'name56updated'],
];
await $conn->query("UPDATE db2.table3 set name=CONCAT(name, id, 'updated'), group_id = 13 WHERE group_id=6");
$results = await $conn->query('SELECT * FROM table3 WHERE group_id=13');
expect($results->rows())->toBeSame($expected, 'no backticks');
}
public async function testQualifiedTableBackticks(): Awaitable<void> {
$conn = static::$conn as nonnull;
$expected = vec[
dict['id' => 4, 'group_id' => 13, 'name' => 'name44updated'],
dict['id' => 6, 'group_id' => 13, 'name' => 'name56updated'],
];
await $conn->query("UPDATE `db2`.`table3` set name=CONCAT(name, id, 'updated'), group_id = 13 WHERE group_id=6");
$results = await $conn->query('SELECT * FROM table3 WHERE group_id=13');
expect($results->rows())->toBeSame($expected, 'with backticks');
}
public async function testPrimaryKeyViolation(): Awaitable<void> {
$conn = static::$conn as nonnull;
expect(() ==> $conn->query('UPDATE table3 set id=1'))->toThrow(SQLFakeUniqueKeyViolation::class);
}
public async function testUpdateIgnore(): Awaitable<void> {
$conn = static::$conn as nonnull;
await $conn->query("INSERT INTO table3 (id, name) VALUES (7, 'test'), (77, 'testupdated')");
expect(() ==> $conn->query("UPDATE IGNORE table3 SET name='testupdated' WHERE id=7"))->notToThrow(
SQLFakeUniqueKeyViolation::class,
);
$results = await $conn->query("SELECT * FROM table3 WHERE name='testupdated'");
expect($results->rows())->toBeSame(
vec[
dict['id' => 77, 'group_id' => 0, 'name' => 'testupdated'],
],
);
}
public async function testTypeCoercion(): Awaitable<void> {
$conn = static::$conn as nonnull;
await $conn->query('UPDATE table3 set name=1 WHERE id=6');
$results = await $conn->query('SELECT * FROM table3 WHERE id=6');
expect($results->rows())->toBeSame(
vec[
dict['id' => 6, 'group_id' => 6, 'name' => '1'],
],
);
}
public async function testTypeCoercionStrict(): Awaitable<void> {
$conn = static::$conn as nonnull;
QueryContext::$strictSQLMode = true;
expect(() ==> $conn->query('UPDATE table3 set name=1 WHERE id=6'))->toThrow(
SQLFakeRuntimeException::class,
"Invalid value '1' for column 'name' on 'table3', expected string",
);
}
<<__Override>>
public static async function beforeFirstTestAsync(): Awaitable<void> {
static::$conn = await SharedSetup::initAsync();
// block hole logging
Logger::setHandle(new \HH\Lib\IO\MemoryHandle());
}
<<__Override>>
public async function beforeEachTestAsync(): Awaitable<void> {
restore('setup');
QueryContext::$strictSchemaMode = false;
QueryContext::$strictSQLMode = false;
}
}