Skip to content

Commit

Permalink
test: update sql tests for pgsql - Closes phalcon#14874
Browse files Browse the repository at this point in the history
  • Loading branch information
jenovateurs committed Mar 8, 2020
1 parent 9e1de53 commit dfbe5cc
Show file tree
Hide file tree
Showing 35 changed files with 342 additions and 266 deletions.
46 changes: 43 additions & 3 deletions tests/_data/assets/schemas/pgsql.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@


drop table if exists complex_default;

create table complex_default
(
id SERIAL PRIMARY KEY,
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_null TIMESTAMP NULL DEFAULT NULL
);

CREATE OR REPLACE FUNCTION update_timestamp()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated = NOW();
NEW.updated_null = NOW();
RETURN NEW;
END;
$$ language 'plpgsql';

CREATE TRIGGER update_timestamp BEFORE UPDATE
ON complex_default FOR EACH ROW EXECUTE PROCEDURE
update_timestamp();



drop table if exists co_customers;
Expand Down Expand Up @@ -27,7 +50,7 @@ drop table if exists co_invoices;

create table co_invoices
(
inv_id serial not null constraint co_invoices_pk primary key,
inv_id serial constraint co_invoices_pk primary key,
inv_cst_id integer,
inv_status_flag smallint,
inv_title varchar(100),
Expand All @@ -46,9 +69,18 @@ create index co_invoices_inv_status_flag_index



drop table if exists objects;

create table objects
(
obj_id serial not null constraint objects_pk primary key,
obj_name varchar(100) not null,
obj_type smallint not null
);



drop table if exists co_orders;
drop table if exists co_orders;

create table co_orders
(
Expand All @@ -71,7 +103,7 @@ create table private.co_orders_x_products



drop table if exists co_products;
drop table if exists co_products;

create table co_products
(
Expand All @@ -85,5 +117,13 @@ create table co_products



drop table if exists table_with_uuid_primary;

create table table_with_uuid_primary
(
uuid char(36) not null primary key,
int_field int null
);



34 changes: 31 additions & 3 deletions tests/_data/fixtures/Migrations/ComplexDefaultMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function insert(
insert into complex_default (
id, created, updated
) values (
{$id}, "{$created}", "{$updated}"
{$id}, '{$created}', '{$updated}'
)
SQL;

Expand Down Expand Up @@ -65,11 +65,39 @@ protected function getSqlSqlite(): array

protected function getSqlPgsql(): array
{
return [];
return [
"
drop table if exists complex_default;
",
"
create table complex_default
(
id SERIAL PRIMARY KEY,
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_null TIMESTAMP NULL DEFAULT NULL
);
",
"
CREATE OR REPLACE FUNCTION update_timestamp()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated = NOW();
NEW.updated_null = NOW();
RETURN NEW;
END;
$$ language 'plpgsql';
",
"
CREATE TRIGGER update_timestamp BEFORE UPDATE
ON complex_default FOR EACH ROW EXECUTE PROCEDURE
update_timestamp();
"
];
}

protected function getSqlSqlsrv(): array
{
return [];
}
}
}
2 changes: 1 addition & 1 deletion tests/_data/fixtures/Migrations/InvoicesMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ protected function getSqlPgsql(): array
"
create table co_invoices
(
inv_id serial not null constraint co_invoices_pk primary key,
inv_id serial constraint co_invoices_pk primary key,
inv_cst_id integer,
inv_status_flag smallint,
inv_title varchar(100),
Expand Down
16 changes: 14 additions & 2 deletions tests/_data/fixtures/Migrations/ObjectsMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function insert(

$sql = <<<SQL
insert into objects (obj_id, obj_name, obj_type)
values ({$id}, "{$name}", "{$type}");
values ({$id}, '{$name}', '{$type}');
SQL;

$this->connection->exec($sql);
Expand Down Expand Up @@ -79,7 +79,19 @@ protected function getSqlSqlite(): array

protected function getSqlPgsql(): array
{
return [];
return [
"
drop table if exists objects;
",
"
create table objects
(
obj_id serial not null constraint objects_pk primary key,
obj_name varchar(100) not null,
obj_type smallint not null
);
"
];
}

protected function getSqlSqlsrv(): array
Expand Down
13 changes: 12 additions & 1 deletion tests/_data/fixtures/Migrations/StringPrimaryMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,18 @@ protected function getSqlSqlite(): array

protected function getSqlPgsql(): array
{
return [];
return [
"
drop table if exists table_with_uuid_primary;
",
"
create table table_with_uuid_primary
(
uuid char(36) not null primary key,
int_field int null
);
"
];
}

protected function getSqlSqlsrv(): array
Expand Down
2 changes: 1 addition & 1 deletion tests/_data/fixtures/Traits/RecordsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private function insertDataInvoices(
$title = uniqid($prefix . '-');
for ($counter = 1; $counter <= $count; $counter++) {
$migration->insert(
null,
'default',
$custId,
1,
$title,
Expand Down
25 changes: 25 additions & 0 deletions tests/database/Db/Adapter/Pdo/DescribeColumnsCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,29 @@ public function dbAdapterPdoDescribeColumnsOnUpdate(DatabaseTester $I)
$I->assertSame('CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP', $columns[2]->getDefault());
$I->assertSame('NULL on update CURRENT_TIMESTAMP', $columns[3]->getDefault());
}

/**
* Tests Phalcon\Db\Adapter\Pdo :: describeColumns()
*
* @author Jeremy PASTOURET <https://github.com/jenovateurs>
* @since 2019-12-07
*
* @group pgsql
*/
public function dbAdapterPdoDescribeColumnsDefaultPostgres(DatabaseTester $I)
{
$I->wantToTest('Db\Adapter\Pdo - describeColumns() - CheckPostgres Default value');

$connection = $I->getConnection();
$db = $this->container->get('db');

$now = date('Y-m-d H:i:s');
$migration = new ComplexDefaultMigration($connection);
$migration->insert(1, $now, $now);

$columns = $db->describeColumns($migration->getTable());

$I->assertSame('now()', $columns[1]->getDefault());
$I->assertSame('now()', $columns[2]->getDefault());
}
}
9 changes: 3 additions & 6 deletions tests/database/Mvc/Model/AssignCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ public function _before(DatabaseTester $I)
* @author Sid Roberts <https://github.com/SidRoberts>
* @since 2019-04-18
*
* @group mysql
* @group sqlite
* @group common
*/
public function mvcModelAssign(DatabaseTester $I)
{
Expand Down Expand Up @@ -99,8 +98,7 @@ public function mvcModelAssign(DatabaseTester $I)
* @author Phalcon Team <team@phalcon.io>
* @since 2020-01-29
*
* @group mysql
* @group sqlite
* @group common
*/
public function mvcModelAssignIncomplete(DatabaseTester $I)
{
Expand Down Expand Up @@ -141,8 +139,7 @@ public function mvcModelAssignIncomplete(DatabaseTester $I)
* @author Phalcon Team <team@phalcon.io>
* @since 2020-02-13
*
* @group mysql
* @group sqlite
* @group common
*/
public function mvcModelAssignAutoPrimary(DatabaseTester $I)
{
Expand Down
10 changes: 6 additions & 4 deletions tests/database/Mvc/Model/AverageCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@ public function _before(DatabaseTester $I)
* @since 2020-01-30
*
* @group mysql
* @group pgsql
*/
public function mvcModelAverage(DatabaseTester $I)
{
$driver = $I->getDriver();

/** @var PDO $connection */
$connection = $I->getConnection();
$migration = new InvoicesMigration($connection);
Expand Down Expand Up @@ -67,7 +66,8 @@ public function mvcModelAverage(DatabaseTester $I)
'inv_cst_id = 2',
]
);
$I->assertEquals('4.714286', $total);
//Handle Postgres need round because Postgres send (4.7142857142857143) and mysql (4.714286)
$I->assertEquals('4.714286', round($total, 6));

$total = Invoices::average(
[
Expand All @@ -86,12 +86,14 @@ public function mvcModelAverage(DatabaseTester $I)
],
]
);
$I->assertEquals('4.714286', $total);
//Handle Postgres need round because Postgres send (4.7142857142857143) and mysql (4.714286)
$I->assertEquals('4.714286', round($total, 6));

$results = Invoices::average(
[
'column' => 'inv_total',
'group' => 'inv_cst_id',
'order' => 'inv_cst_id'
]
);
$I->assertInstanceOf(Simple::class, $results);
Expand Down
3 changes: 1 addition & 2 deletions tests/database/Mvc/Model/ConstructCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ public function _before(DatabaseTester $I)
* @author Phalcon Team <team@phalcon.io>
* @since 2020-02-01
*
* @group mysql
* @group sqlite
* @group common
*/
public function mvcModelConstruct(DatabaseTester $I)
{
Expand Down
3 changes: 1 addition & 2 deletions tests/database/Mvc/Model/DeleteCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ public function _before(DatabaseTester $I)
* @author Phalcon Team <team@phalcon.io>
* @since 2020-02-01
*
* @group mysql
* @group sqlite
* @group common
*/
public function mvcModelDelete(DatabaseTester $I)
{
Expand Down
6 changes: 2 additions & 4 deletions tests/database/Mvc/Model/FindCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ public function _before(DatabaseTester $I)
* @author Phalcon Team <team@phalcon.io>
* @since 2020-02-01
*
* @group mysql
* @group sqlite
* @group common
*/
public function mvcModelFind(DatabaseTester $I)
{
Expand All @@ -75,8 +74,7 @@ public function mvcModelFind(DatabaseTester $I)
* @author Phalcon Team <team@phalcon.io>
* @since 2020-02-01
*
* @group mysql
* @group sqlite
* @group common
*/
public function mvcModelFindWithCache(DatabaseTester $I)
{
Expand Down
Loading

0 comments on commit dfbe5cc

Please sign in to comment.