Skip to content

Commit

Permalink
Merge pull request mevdschee#244 from karllhughes/master
Browse files Browse the repository at this point in the history
Making mysql and postgres test suites adaptable
  • Loading branch information
mevdschee authored Apr 15, 2017
2 parents b58f4ed + 0529347 commit 4a517b9
Show file tree
Hide file tree
Showing 11 changed files with 2,496 additions and 2 deletions.
118 changes: 117 additions & 1 deletion tests/MysqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ public function getCapabilities($db)
public function seedDatabase($db, $capabilities)
{
$fixture = __DIR__.'/data/blog_mysql.sql';

$contents = file_get_contents($fixture);

if (!($capabilities & self::GIS)) {
Expand All @@ -99,4 +98,121 @@ public function seedDatabase($db, $capabilities)
die("Loading '$fixture' failed on statemement #$i with error:\n".mysqli_error($db)."\n");
}
}

/**
* Gets the path to the seed file based on the version of MySQL
*
* @return string
*/
protected static function getSeedFile()
{
if (static::$mysql_version >= self::MYSQL_57) {
return __DIR__.'/data/blog_'.strtolower(static::$config['dbengine']).'_57.sql';
} elseif (static::$mysql_version >= self::MYSQL_56) {
return __DIR__.'/data/blog_'.strtolower(static::$config['dbengine']).'_56.sql';
}
return __DIR__.'/data/blog_'.strtolower(static::$config['dbengine']).'_55.sql';
}

public function testHidingPasswordColumn()
{
parent::testHidingPasswordColumn();
}

public function testMissingIntermediateTable()
{
$test = new API($this, static::$config);
$test->get('/users?include=posts,tags');
$test->expect('{"users":{"columns":["id","username","location"],"records":[[1,"user1",null]]},"posts":{"relations":{"user_id":"users.id"},"columns":["id","user_id","category_id","content"],"records":[[1,1,1,"blog started"],[2,1,2,"It works!"]]},"post_tags":{"relations":{"post_id":"posts.id"},"columns":["id","post_id","tag_id"],"records":[[1,1,1],[2,1,2],[3,2,1],[4,2,2]]},"tags":{"relations":{"id":"post_tags.tag_id"},"columns":["id","name"],"records":[[1,"funny"],[2,"important"]]}}');
}

public function testEditUserPassword()
{
parent::testEditUserPassword();
}

public function testEditUserLocation()
{
parent::testEditUserLocation();
}

public function testListUserLocations()
{
parent::testListUserLocations();
}

public function testEditUserWithId()
{
parent::testEditUserWithId();
}

public function testReadOtherUser()
{
parent::testReadOtherUser();
}

public function testEditOtherUser()
{
parent::testEditOtherUser();
}

public function testSpatialFilterWithin()
{
if (static::$mysql_version < self::MYSQL_56) {
$this->markTestSkipped("MySQL < 5.6 does not support JSON fields.");
}
parent::testSpatialFilterWithin();
}


public function testListProductsProperties()
{
if (static::$mysql_version < self::MYSQL_57) {
$this->markTestSkipped("MySQL < 5.7 does not support JSON fields.");
}
parent::testListProductsProperties();
}

public function testReadProductProperties()
{
if (static::$mysql_version < self::MYSQL_57) {
$this->markTestSkipped("MySQL < 5.7 does not support JSON fields.");
}
parent::testReadProductProperties();
}

public function testWriteProductProperties()
{
if (static::$mysql_version < self::MYSQL_57) {
$this->markTestSkipped("MySQL < 5.7 does not support JSON fields.");
}
parent::testWriteProductProperties();
}

public function testListProducts()
{
parent::testListProducts();
}

public function testAddProducts()
{
if (static::$mysql_version < self::MYSQL_57) {
$this->markTestSkipped("MySQL < 5.7 does not support JSON fields.");
}
parent::testAddProducts();
}

public function testSoftDeleteProducts()
{
if (static::$mysql_version < self::MYSQL_57) {
$test = new API($this, static::$config);
$test->delete('/products/1');
$test->expect('1');
$test->get('/products?columns=id,deleted_at');
$test->expect('{"products":{"columns":["id","deleted_at"],"records":[[1,"2013-12-11 11:10:09"]]}}');
} else {
parent::testSoftDeleteProducts();
}
}

}
102 changes: 101 additions & 1 deletion tests/PostgresqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,108 @@ public function seedDatabase($db,$capabilities)
foreach ($queries as $i=>$query) {
if (!pg_query($db, $query.';')) {
$i++;
die("Loading '$fixture' failed on statemement #$i with error:\n".print_r( pg_last_error($db), true)."\n");
die("Loading '$seed_file' failed on statemement #$i with error:\n".print_r( pg_last_error($db), true)."\n");
}
}
}

/**
* Gets the path to the seed file based on the version of Postgres and GIS extension
*
* @return string
*/
protected function getSeedFile()
{
$filepath = __DIR__.'/data/blog_'.strtolower(static::$config['dbengine']);

if (version_compare(static::$pg_server_version, '9.4.0') >= 0 ) {
$filepath .= '_94';
} elseif (version_compare(static::$pg_server_version, '9.2.0') >= 0 ) {
$filepath .= '_92';
} else {
$filepath .= '_91';
}
if (static::$gis_installed) {
$filepath .= '_gis';
}
return $filepath.'.sql';
}

/**
* Determines whether the GIS extension is installed or not based on array of extensions.
*
* @return boolean
*/
protected function isGisInstalled($extensions = [])
{
static::$gis_installed = false;
if ($extensions) {
foreach ($extensions as $extension) {
if ($extension['extname'] === 'postgis') {
static::$gis_installed = true;
break;
}
}
}
return static::$gis_installed;
}

public function testSpatialFilterWithin()
{
if (!static::$gis_installed) {
$this->markTestSkipped("Postgis not installed");
}
parent::testSpatialFilterWithin();
}

public function testListProductsProperties()
{
if (version_compare(static::$pg_server_version, '9.2.0') < 0) {
$this->markTestSkipped("Postgres < 9.2.0 does not support JSON fields.");
}
parent::testListProductsProperties();
}

public function testReadProductProperties()
{
if (version_compare(static::$pg_server_version, '9.2.0') < 0) {
$this->markTestSkipped("Postgres < 9.2.0 does not support JSON fields.");
}
parent::testReadProductProperties();
}

public function testWriteProductProperties()
{
if (version_compare(static::$pg_server_version, '9.2.0') < 0) {
$this->markTestSkipped("Postgres < 9.2.0 does not support JSON fields.");
}
parent::testWriteProductProperties();
}

public function testListProducts()
{
parent::testListProducts();
}

public function testAddProducts()
{
if (version_compare(static::$pg_server_version, '9.2.0') < 0) {
$this->markTestSkipped("Postgres < 9.2.0 does not support JSON fields.");
}
parent::testAddProducts();
}

public function testSoftDeleteProducts()
{
if (version_compare(static::$pg_server_version, '9.2.0') < 0) {
$test = new API($this, static::$config);
$test->delete('/products/1');
$test->expect('1');
$test->get('/products?columns=id,deleted_at');
$test->expect('{"products":{"columns":["id","deleted_at"],"records":[[1,"2013-12-11 11:10:09"]]}}');
} else {
parent::testSoftDeleteProducts();
}
}

}
148 changes: 148 additions & 0 deletions tests/data/blog_mysql_55.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
-- Adminer 4.2.4 MySQL dump

SET NAMES utf8;
SET time_zone = '+00:00';
SET foreign_key_checks = 0;
SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO';

DROP TABLE IF EXISTS `categories`;
CREATE TABLE `categories` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`icon` blob NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

INSERT INTO `categories` (`id`, `name`, `icon`) VALUES
(1, 'announcement', NULL),
(2, 'article', NULL);

DROP TABLE IF EXISTS `comments`;
CREATE TABLE `comments` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`post_id` int(11) NOT NULL,
`message` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
KEY `post_id` (`post_id`),
CONSTRAINT `comments_ibfk_1` FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

INSERT INTO `comments` (`id`, `post_id`, `message`) VALUES
(1, 1, 'great'),
(2, 1, 'fantastic'),
(3, 2, 'thank you'),
(4, 2, 'awesome');

DROP TABLE IF EXISTS `posts`;
CREATE TABLE `posts` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`category_id` int(11) NOT NULL,
`content` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
KEY `category_id` (`category_id`),
KEY `user_id` (`user_id`),
CONSTRAINT `posts_ibfk_3` FOREIGN KEY (`category_id`) REFERENCES `categories` (`id`),
CONSTRAINT `posts_ibfk_4` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

INSERT INTO `posts` (`id`, `user_id`, `category_id`, `content`) VALUES
(1, 1, 1, 'blog started'),
(2, 1, 2, 'It works!');

DROP TABLE IF EXISTS `post_tags`;
CREATE TABLE `post_tags` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`post_id` int(11) NOT NULL,
`tag_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `post_id` (`post_id`),
KEY `tag_id` (`tag_id`),
CONSTRAINT `post_tags_ibfk_1` FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`),
CONSTRAINT `post_tags_ibfk_2` FOREIGN KEY (`tag_id`) REFERENCES `tags` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

INSERT INTO `post_tags` (`id`, `post_id`, `tag_id`) VALUES
(1, 1, 1),
(2, 1, 2),
(3, 2, 1),
(4, 2, 2);

DROP TABLE IF EXISTS `tags`;
CREATE TABLE `tags` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

INSERT INTO `tags` (`id`, `name`) VALUES
(1, 'funny'),
(2, 'important');

DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`location` text NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

INSERT INTO `users` (`id`, `username`, `password`, `location`) VALUES
(1, 'user1', 'pass1', null),
(2, 'user2', 'pass2', null);

DROP TABLE IF EXISTS `countries`;
CREATE TABLE `countries` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

INSERT INTO `countries` (`id`, `name`) VALUES
(1, 'Left'),
(2, 'Right');

DROP TABLE IF EXISTS `events`;
CREATE TABLE `events` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`datetime` datetime NOT NULL,
`visitors` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

INSERT INTO `events` (`id`, `name`, `datetime`, `visitors`) VALUES
(1, 'Launch', '2016-01-01 13:01:01', 0);

DROP VIEW IF EXISTS `tag_usage`;
CREATE VIEW `tag_usage` AS select `name`, count(`name`) AS `count` from `tags`, `post_tags` where `tags`.`id` = `post_tags`.`tag_id` group by `name` order by `count` desc, `name`;

DROP TABLE IF EXISTS `products`;
CREATE TABLE `products` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`price` decimal(10,2) NOT NULL,
`properties` text NOT NULL,
`created_at` datetime NOT NULL,
`deleted_at` datetime NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

INSERT INTO `products` (`id`, `name`, `price`, `properties`, `created_at`) VALUES
(1, 'Calculator', '23.01', '{"depth":false,"model":"TRX-120","width":100,"height":null}', '1970-01-01 01:01:01');

DROP TABLE IF EXISTS `barcodes`;
CREATE TABLE `barcodes` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`product_id` int(11) NOT NULL,
`hex` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
`bin` varbinary(255) NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `barcodes_ibfk_1` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

INSERT INTO `barcodes` (`id`, `product_id`, `hex`, `bin`) VALUES
(1, 1, '00ff01', UNHEX('00ff01'));

-- 2016-11-05 13:11:47
Loading

0 comments on commit 4a517b9

Please sign in to comment.