Skip to content

Commit 4f0bba1

Browse files
Add default value tests. (#200)
1 parent 466c741 commit 4f0bba1

3 files changed

Lines changed: 414 additions & 2 deletions

File tree

.scrutinizer.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,15 @@ build:
4747
override:
4848
- curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
4949
- curl https://packages.microsoft.com/config/ubuntu/18.04/prod.list | sudo tee /etc/apt/sources.list.d/msprod.list
50+
- sudo apt-get install apt-transport-https ca-certificates -y
51+
- sudo apt-get install make -y
5052
- sudo apt-get update -y
51-
- sudo ACCEPT_EULA=Y apt-get install mssql-tools unixodbc-dev -y
53+
- sudo apt-get install unixodbc-dev=2.3.7 unixodbc=2.3.7 odbcinst1debian2=2.3.7 odbcinst=2.3.7 -y
54+
- sudo ACCEPT_EULA=Y apt-get install mssql-tools -y
5255
- sudo ls /opt/mssql-tools/bin/sqlcmd*
5356
- /opt/mssql-tools/bin/sqlcmd -S 127.0.0.1 -U SA -P 'YourStrong!Passw0rd' -Q 'CREATE DATABASE yiitest'
54-
- pecl -q install pdo_sqlsrv
57+
- pecl channel-update pecl.php.net
58+
- pecl install pdo_sqlsrv
5559
- composer config preferred-install.yiisoft/db source
5660
- composer update --no-interaction --no-progress --optimize-autoloader --ansi
5761

tests/DefaultValuesTest.php

Lines changed: 297 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,297 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\Db\Mssql\Tests;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Yiisoft\Db\Mssql\Tests\Support\TestTrait;
9+
10+
/**
11+
* @group mssql
12+
*
13+
* @psalm-suppress PropertyNotSetInConstructor
14+
*
15+
* @link https://learn.microsoft.com/en-us/sql/t-sql/data-types/data-types-transact-sql?view=sql-server-ver16
16+
*/
17+
final class DefaultValuesTest extends TestCase
18+
{
19+
use TestTrait;
20+
21+
/**
22+
* @link https://learn.microsoft.com/en-us/sql/t-sql/data-types/int-bigint-smallint-and-tinyint-transact-sql?view=sql-server-ver16
23+
*/
24+
public function testBigInt(): void
25+
{
26+
$db = $this->getConnection(true);
27+
28+
$command = $db->createCommand();
29+
30+
$command->insert('bigint_default', [])->execute();
31+
32+
$this->assertSame(
33+
[
34+
'id' => '1',
35+
'Mybigint' => '9223372036854775807',
36+
'Myint' => '2147483647',
37+
'Mysmallint' => '32767',
38+
'Mytinyint' => '255',
39+
],
40+
$command->setSql(
41+
<<<SQL
42+
SELECT * FROM bigint_default WHERE id = 1
43+
SQL
44+
)->queryOne()
45+
);
46+
}
47+
48+
/**
49+
* @link https://learn.microsoft.com/en-us/sql/t-sql/data-types/binary-and-varbinary-transact-sql?view=sql-server-ver16
50+
*/
51+
public function testBinaryVarbinary(): void
52+
{
53+
$db = $this->getConnection(true);
54+
55+
$command = $db->createCommand();
56+
57+
$command->insert('binary_varbinary_default', [])->execute();
58+
59+
$this->assertSame(
60+
[
61+
'id' => '1',
62+
'Mybinary1' => '0x62696E61727900000000',
63+
'Mybinary2' => 'b',
64+
'Myvarbinary1' => 'varbinary',
65+
'Myvarbinary2' => 'v',
66+
],
67+
$command->setSql(
68+
<<<SQL
69+
SELECT id, CONVERT(VARCHAR(100), Mybinary1, 1) AS Mybinary1, Mybinary2, Myvarbinary1, Myvarbinary2 FROM binary_varbinary_default WHERE id = 1
70+
SQL
71+
)->queryOne()
72+
);
73+
}
74+
75+
/**
76+
* @link https://learn.microsoft.com/en-us/sql/t-sql/data-types/bit-transact-sql?view=sql-server-ver16
77+
*/
78+
public function testBit(): void
79+
{
80+
$db = $this->getConnection(true);
81+
82+
$command = $db->createCommand();
83+
84+
$command->insert('bit_default', [])->execute();
85+
86+
$this->assertSame(
87+
[
88+
'id' => '1',
89+
'Mybit1' => '0',
90+
'Mybit2' => '1',
91+
'Mybit3' => '1',
92+
],
93+
$command->setSql(
94+
<<<SQL
95+
SELECT * FROM bit_default WHERE id = 1
96+
SQL
97+
)->queryOne()
98+
);
99+
100+
$command->insert('bit_default', ['Mybit1' => true, 'Mybit2' => false, 'Mybit3' => '1'])->execute();
101+
102+
$this->assertSame(
103+
[
104+
'id' => '2',
105+
'Mybit1' => '1',
106+
'Mybit2' => '0',
107+
'Mybit3' => '1',
108+
],
109+
$command->setSql(
110+
<<<SQL
111+
SELECT * FROM bit_default WHERE id = 2
112+
SQL
113+
)->queryOne()
114+
);
115+
}
116+
117+
/**
118+
* https://learn.microsoft.com/en-us/sql/t-sql/data-types/char-and-varchar-transact-sql?view=sql-server-ver16
119+
*/
120+
public function testCharVarchar(): void
121+
{
122+
$db = $this->getConnection(true);
123+
124+
$command = $db->createCommand();
125+
126+
$command->insert('char_varchar_default', [])->execute();
127+
128+
$this->assertSame(
129+
[
130+
'id' => '1',
131+
'Mychar1' => 'char ',
132+
'Mychar2' => 'c',
133+
'Myvarchar1' => 'varchar',
134+
'Myvarchar2' => 'v',
135+
],
136+
$command->setSql(
137+
<<<SQL
138+
SELECT * FROM char_varchar_default WHERE id = 1
139+
SQL
140+
)->queryOne()
141+
);
142+
}
143+
144+
/**
145+
* @link https://learn.microsoft.com/en-us/sql/t-sql/data-types/ntext-text-and-image-transact-sql?view=sql-server-ver16
146+
*/
147+
public function testNtextTextImage(): void
148+
{
149+
$db = $this->getConnection(true);
150+
151+
$command = $db->createCommand();
152+
153+
$command->insert('ntext_text_image_default', [])->execute();
154+
155+
$this->assertSame(
156+
[
157+
'id' => '1',
158+
'Mytext' => 'text',
159+
'Myntext' => 'ntext',
160+
'Myimage' => 'image',
161+
],
162+
$command->setSql(
163+
<<<SQL
164+
SELECT * FROM ntext_text_image_default WHERE id = 1
165+
SQL
166+
)->queryOne()
167+
);
168+
}
169+
170+
/**
171+
* @link https://learn.microsoft.com/en-us/sql/t-sql/data-types/date-transact-sql?view=sql-server-ver16
172+
*/
173+
public function testDate(): void
174+
{
175+
$db = $this->getConnection(true);
176+
177+
$command = $db->createCommand();
178+
179+
$command->insert('date_default', [])->execute();
180+
181+
$this->assertSame(
182+
[
183+
'id' => '1',
184+
'Mydate1' => '2007-05-08',
185+
'Mydate2' => date('Y-m-d'),
186+
'Mydatetime' => '2007-05-08 12:35:29.123',
187+
'Mydatetime2' => '2007-05-08 12:35:29.1234567',
188+
'Mydatetimeoffset' => '2007-05-08 12:35:29.1234567 +12:15',
189+
'Mytime' => '12:35:29.1234567',
190+
],
191+
$command->setSql(
192+
<<<SQL
193+
SELECT * FROM date_default WHERE id = 1
194+
SQL
195+
)->queryOne()
196+
);
197+
}
198+
199+
/**
200+
* @link https://learn.microsoft.com/en-us/sql/t-sql/data-types/float-and-real-transact-sql?view=sql-server-ver16
201+
*/
202+
public function testFloatReal(): void
203+
{
204+
$db = $this->getConnection(true);
205+
206+
$command = $db->createCommand();
207+
208+
$command->insert('float_real_default', [])->execute();
209+
210+
$this->assertSame(
211+
[
212+
'id' => '1',
213+
'Myfloat' => '123.45',
214+
'Myreal' => '38.502998',
215+
],
216+
$command->setSql(
217+
<<<SQL
218+
SELECT * FROM float_real_default WHERE id = 1
219+
SQL
220+
)->queryOne()
221+
);
222+
}
223+
224+
/**
225+
* @link https://learn.microsoft.com/en-us/sql/t-sql/data-types/money-and-smallmoney-transact-sql?view=sql-server-ver16
226+
*/
227+
public function testMoneySmallMoney(): void
228+
{
229+
$db = $this->getConnection(true);
230+
231+
$command = $db->createCommand();
232+
233+
$command->insert('money_small_money_default', [])->execute();
234+
235+
$this->assertSame(
236+
[
237+
'id' => '1',
238+
'Mymoney' => '922337203685477.5807',
239+
'Mysmallmoney' => '123.4500',
240+
],
241+
$command->setSql(
242+
<<<SQL
243+
SELECT * FROM money_small_money_default WHERE id = 1
244+
SQL
245+
)->queryOne()
246+
);
247+
}
248+
249+
/**
250+
* @link https://learn.microsoft.com/en-us/sql/t-sql/data-types/decimal-and-numeric-transact-sql?view=sql-server-ver16
251+
*/
252+
public function testNumeric(): void
253+
{
254+
$db = $this->getConnection(true);
255+
256+
$command = $db->createCommand();
257+
258+
$command->insert('numeric_default', [])->execute();
259+
260+
$this->assertSame(
261+
[
262+
'id' => '1',
263+
'Mydecimal' => '123.00',
264+
'Mynumeric' => '12345.12000',
265+
],
266+
$command->setSql(
267+
<<<SQL
268+
SELECT * FROM numeric_default WHERE id = 1
269+
SQL
270+
)->queryOne()
271+
);
272+
}
273+
274+
/**
275+
* @link https://learn.microsoft.com/en-us/sql/t-sql/data-types/uniqueidentifier-transact-sql?view=sql-server-ver16
276+
*/
277+
public function testUniqueidentifier(): void
278+
{
279+
$db = $this->getConnection(true);
280+
281+
$command = $db->createCommand();
282+
283+
$command->insert('uniqueidentifier_default', [])->execute();
284+
285+
$this->assertSame(
286+
[
287+
'id' => '1',
288+
'Myuniqueidentifier' => '12345678-1234-1234-1234-123456789012',
289+
],
290+
$command->setSql(
291+
<<<SQL
292+
SELECT * FROM uniqueidentifier_default WHERE id = 1
293+
SQL
294+
)->queryOne()
295+
);
296+
}
297+
}

0 commit comments

Comments
 (0)