Skip to content

Commit d59f417

Browse files
committed
test: refactor with dataProvider
1 parent afe3bdd commit d59f417

File tree

1 file changed

+95
-143
lines changed

1 file changed

+95
-143
lines changed

tests/system/Database/Live/UpdateTest.php

Lines changed: 95 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,18 @@ public function testUpdateWithWhereAndLimit(): void
111111
}
112112
}
113113

114-
public function testUpdateBatchConstraintsVarchar(): void
114+
/**
115+
* @dataProvider provideUpdateBatch
116+
*/
117+
public function testUpdateBatch(string $constraints, array $data, array $expected): void
115118
{
116119
$table = 'type_test';
117120

118121
// Prepares test data.
119122
$builder = $this->db->table($table);
120123
$builder->truncate();
121124

122-
for ($i = 0; $i < 3; $i++) {
125+
for ($i = 1; $i < 4; $i++) {
123126
$builder->insert([
124127
'type_varchar' => 'test' . $i,
125128
'type_char' => 'char',
@@ -129,164 +132,113 @@ public function testUpdateBatchConstraintsVarchar(): void
129132
'type_bigint' => 9_223_372_036_854_775_807,
130133
'type_float' => 10.1,
131134
'type_numeric' => 123.23,
132-
'type_date' => '2023-12-21',
135+
'type_date' => '2023-12-0' . $i,
133136
'type_datetime' => '2023-12-21 12:00:00',
134137
]);
135138
}
136139

137-
$data = [
138-
[
139-
'type_varchar' => 'test1', // Key
140-
'type_text' => 'updated',
141-
'type_smallint' => 9999,
142-
'type_integer' => 9_999_999,
143-
'type_bigint' => 9_999_999,
144-
'type_float' => 999999.9,
145-
'type_numeric' => 999999.99,
146-
'type_date' => '2024-01-01',
147-
'type_datetime' => '2024-01-01 09:00:00',
148-
],
149-
[
150-
'type_varchar' => 'test2', // Key
151-
'type_text' => 'updated',
152-
'type_smallint' => 9999,
153-
'type_integer' => 9_999_999,
154-
'type_bigint' => 9_999_999,
155-
'type_float' => 999999.9,
156-
'type_numeric' => 999999.99,
157-
'type_date' => '2024-01-01',
158-
'type_datetime' => '2024-01-01 09:00:00',
159-
],
160-
];
161-
$this->db->table($table)->updateBatch($data, 'type_varchar');
140+
$this->db->table($table)->updateBatch($data, $constraints);
162141

163142
if ($this->db->DBDriver === 'SQLSRV') {
164143
// We cannot compare `text` and `varchar` with `=`. It causes the error:
165144
// [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]The data types text and varchar are incompatible in the equal to operator.
166145
// And data type `text`, `ntext`, `image` are deprecated in SQL Server 2016
167146
// See https://github.com/codeigniter4/CodeIgniter4/pull/8439#issuecomment-1902535909
168-
$this->seeInDatabase($table, [
169-
'type_varchar' => 'test1',
170-
// 'type_text' => 'updated',
171-
'type_smallint' => 9999,
172-
'type_integer' => 9_999_999,
173-
'type_bigint' => 9_999_999,
174-
'type_float' => 999999.9,
175-
'type_numeric' => 999999.99,
176-
'type_date' => '2024-01-01',
177-
'type_datetime' => '2024-01-01 09:00:00',
178-
]);
179-
$this->seeInDatabase($table, [
180-
'type_varchar' => 'test2',
181-
// 'type_text' => 'updated',
182-
'type_smallint' => 9999,
183-
'type_integer' => 9_999_999,
184-
'type_bigint' => 9_999_999,
185-
'type_float' => 999999.9,
186-
'type_numeric' => 999999.99,
187-
'type_date' => '2024-01-01',
188-
'type_datetime' => '2024-01-01 09:00:00',
189-
]);
190-
} else {
191-
$this->seeInDatabase($table, [
192-
'type_varchar' => 'test1',
193-
'type_text' => 'updated',
194-
'type_smallint' => 9999,
195-
'type_integer' => 9_999_999,
196-
'type_bigint' => 9_999_999,
197-
'type_float' => 999999.9,
198-
'type_numeric' => 999999.99,
199-
'type_date' => '2024-01-01',
200-
'type_datetime' => '2024-01-01 09:00:00',
201-
]);
202-
$this->seeInDatabase($table, [
203-
'type_varchar' => 'test2',
204-
'type_text' => 'updated',
205-
'type_smallint' => 9999,
206-
'type_integer' => 9_999_999,
207-
'type_bigint' => 9_999_999,
208-
'type_float' => 999999.9,
209-
'type_numeric' => 999999.99,
210-
'type_date' => '2024-01-01',
211-
'type_datetime' => '2024-01-01 09:00:00',
212-
]);
147+
unset($expected[0]['type_text'], $expected[1]['type_text']);
213148
}
149+
150+
$this->seeInDatabase($table, $expected[0]);
151+
$this->seeInDatabase($table, $expected[1]);
214152
}
215153

216-
public function testUpdateBatchConstraintsDate(): void
154+
public static function provideUpdateBatch(): iterable
217155
{
218-
$table = 'type_test';
219-
220-
// Prepares test data.
221-
$builder = $this->db->table($table);
222-
$builder->truncate();
223-
224-
for ($i = 1; $i < 4; $i++) {
225-
$builder->insert([
226-
'type_varchar' => 'test' . $i,
227-
'type_char' => 'char',
228-
'type_text' => 'text',
229-
'type_smallint' => 32767,
230-
'type_integer' => 2_147_483_647,
231-
'type_bigint' => 9_223_372_036_854_775_807,
232-
'type_float' => 10.1,
233-
'type_numeric' => 123.23,
234-
'type_date' => '2023-12-0' . $i,
235-
'type_datetime' => '2023-12-21 12:00:00',
236-
]);
237-
}
238-
239-
$data = [
240-
[
241-
'type_text' => 'updated',
242-
'type_bigint' => 9_999_999,
243-
'type_date' => '2023-12-01', // Key
244-
'type_datetime' => '2024-01-01 09:00:00',
156+
yield from [
157+
'constraints varchar' => [
158+
'type_varchar',
159+
[
160+
[
161+
'type_varchar' => 'test1', // Key
162+
'type_text' => 'updated',
163+
'type_smallint' => 9999,
164+
'type_integer' => 9_999_999,
165+
'type_bigint' => 9_999_999,
166+
'type_float' => 999999.9,
167+
'type_numeric' => 999999.99,
168+
'type_date' => '2024-01-01',
169+
'type_datetime' => '2024-01-01 09:00:00',
170+
],
171+
[
172+
'type_varchar' => 'test2', // Key
173+
'type_text' => 'updated',
174+
'type_smallint' => 9999,
175+
'type_integer' => 9_999_999,
176+
'type_bigint' => 9_999_999,
177+
'type_float' => 999999.9,
178+
'type_numeric' => 999999.99,
179+
'type_date' => '2024-01-01',
180+
'type_datetime' => '2024-01-01 09:00:00',
181+
],
182+
],
183+
[
184+
[
185+
'type_varchar' => 'test1',
186+
'type_text' => 'updated',
187+
'type_smallint' => 9999,
188+
'type_integer' => 9_999_999,
189+
'type_bigint' => 9_999_999,
190+
'type_float' => 999999.9,
191+
'type_numeric' => 999999.99,
192+
'type_date' => '2024-01-01',
193+
'type_datetime' => '2024-01-01 09:00:00',
194+
],
195+
[
196+
'type_varchar' => 'test2',
197+
'type_text' => 'updated',
198+
'type_smallint' => 9999,
199+
'type_integer' => 9_999_999,
200+
'type_bigint' => 9_999_999,
201+
'type_float' => 999999.9,
202+
'type_numeric' => 999999.99,
203+
'type_date' => '2024-01-01',
204+
'type_datetime' => '2024-01-01 09:00:00',
205+
],
206+
],
245207
],
246-
[
247-
'type_text' => 'updated',
248-
'type_bigint' => 9_999_999,
249-
'type_date' => '2023-12-02', // Key
250-
'type_datetime' => '2024-01-01 09:00:00',
208+
'constraints date' => [
209+
'type_date',
210+
[
211+
[
212+
'type_text' => 'updated',
213+
'type_bigint' => 9_999_999,
214+
'type_date' => '2023-12-01', // Key
215+
'type_datetime' => '2024-01-01 09:00:00',
216+
],
217+
[
218+
'type_text' => 'updated',
219+
'type_bigint' => 9_999_999,
220+
'type_date' => '2023-12-02', // Key
221+
'type_datetime' => '2024-01-01 09:00:00',
222+
],
223+
],
224+
[
225+
[
226+
'type_varchar' => 'test1',
227+
'type_text' => 'updated',
228+
'type_bigint' => 9_999_999,
229+
'type_date' => '2023-12-01',
230+
'type_datetime' => '2024-01-01 09:00:00',
231+
],
232+
[
233+
'type_varchar' => 'test2',
234+
'type_text' => 'updated',
235+
'type_bigint' => 9_999_999,
236+
'type_date' => '2023-12-02',
237+
'type_datetime' => '2024-01-01 09:00:00',
238+
],
239+
],
251240
],
252241
];
253-
$this->db->table($table)->updateBatch($data, 'type_date');
254-
255-
if ($this->db->DBDriver === 'SQLSRV') {
256-
// We cannot compare `text` and `varchar` with `=`. It causes the error:
257-
// [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]The data types text and varchar are incompatible in the equal to operator.
258-
// And data type `text`, `ntext`, `image` are deprecated in SQL Server 2016
259-
// See https://github.com/codeigniter4/CodeIgniter4/pull/8439#issuecomment-1902535909
260-
$this->seeInDatabase($table, [
261-
'type_varchar' => 'test1',
262-
// 'type_text' => 'updated',
263-
'type_bigint' => 9_999_999,
264-
'type_date' => '2023-12-01',
265-
'type_datetime' => '2024-01-01 09:00:00',
266-
]);
267-
$this->seeInDatabase($table, [
268-
'type_varchar' => 'test2',
269-
// 'type_text' => 'updated',
270-
'type_bigint' => 9_999_999,
271-
'type_date' => '2023-12-02',
272-
'type_datetime' => '2024-01-01 09:00:00',
273-
]);
274-
} else {
275-
$this->seeInDatabase($table, [
276-
'type_varchar' => 'test1',
277-
'type_text' => 'updated',
278-
'type_bigint' => 9_999_999,
279-
'type_date' => '2023-12-01',
280-
'type_datetime' => '2024-01-01 09:00:00',
281-
]);
282-
$this->seeInDatabase($table, [
283-
'type_varchar' => 'test2',
284-
'type_text' => 'updated',
285-
'type_bigint' => 9_999_999,
286-
'type_date' => '2023-12-02',
287-
'type_datetime' => '2024-01-01 09:00:00',
288-
]);
289-
}
290242
}
291243

292244
public function testUpdateWithWhereSameColumn(): void

0 commit comments

Comments
 (0)