From 8395abb04ffc76394b194eb344b3bb001ae23f72 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Mon, 12 Jan 2015 05:33:41 +0100 Subject: [PATCH] added new tests --- tests/dibi/DataSource.phpt | 159 ++++++ tests/dibi/DibiConnection.affectedRows.phpt | 35 ++ tests/dibi/DibiConnection.connect.phpt | 37 ++ tests/dibi/DibiConnection.fetch.phpt | 329 ++++++++++++ tests/dibi/DibiConnection.substitutions.phpt | 26 + tests/dibi/DibiConnection.transactions.phpt | 48 ++ tests/dibi/DibiFluent.delete.phpt | 45 ++ tests/dibi/DibiFluent.fetch.phpt | 59 +++ tests/dibi/DibiFluent.insert.phpt | 58 +++ tests/dibi/DibiFluent.select.phpt | 139 +++++ tests/dibi/DibiFluent.update.phpt | 30 ++ tests/dibi/DibiResult.meta.phpt | 57 +++ tests/dibi/DibiResult.types.phpt | 18 + tests/dibi/DibiTranslator.conditions.phpt | 76 +++ tests/dibi/DibiTranslator.identifiers.phpt | 47 ++ tests/dibi/DibiTranslator.phpt | 510 +++++++++++++++++++ tests/dibi/bootstrap.php | 7 + tests/dibi/data/mysql.sql | 52 ++ tests/dibi/data/odbc.mdb | Bin 0 -> 65536 bytes tests/dibi/data/odbc.sql | 32 ++ tests/dibi/data/pgsql.sql | 52 ++ tests/dibi/data/sqlite.sql | 34 ++ tests/dibi/meta.phpt | 75 +++ 23 files changed, 1925 insertions(+) create mode 100644 tests/dibi/DataSource.phpt create mode 100644 tests/dibi/DibiConnection.affectedRows.phpt create mode 100644 tests/dibi/DibiConnection.connect.phpt create mode 100644 tests/dibi/DibiConnection.fetch.phpt create mode 100644 tests/dibi/DibiConnection.substitutions.phpt create mode 100644 tests/dibi/DibiConnection.transactions.phpt create mode 100644 tests/dibi/DibiFluent.delete.phpt create mode 100644 tests/dibi/DibiFluent.fetch.phpt create mode 100644 tests/dibi/DibiFluent.insert.phpt create mode 100644 tests/dibi/DibiFluent.select.phpt create mode 100644 tests/dibi/DibiFluent.update.phpt create mode 100644 tests/dibi/DibiResult.meta.phpt create mode 100644 tests/dibi/DibiResult.types.phpt create mode 100644 tests/dibi/DibiTranslator.conditions.phpt create mode 100644 tests/dibi/DibiTranslator.identifiers.phpt create mode 100644 tests/dibi/DibiTranslator.phpt create mode 100644 tests/dibi/data/mysql.sql create mode 100644 tests/dibi/data/odbc.mdb create mode 100644 tests/dibi/data/odbc.sql create mode 100644 tests/dibi/data/pgsql.sql create mode 100644 tests/dibi/data/sqlite.sql create mode 100644 tests/dibi/meta.phpt diff --git a/tests/dibi/DataSource.phpt b/tests/dibi/DataSource.phpt new file mode 100644 index 00000000..1725afbd --- /dev/null +++ b/tests/dibi/DataSource.phpt @@ -0,0 +1,159 @@ +loadFile(__DIR__ . "/data/$config[system].sql"); + + +$ds = $conn->dataSource('SELECT * FROM products'); +Assert::match( + reformat(" +SELECT * +FROM (SELECT * FROM products) t"), + (string) $ds +); + + +Assert::same(3, $ds->count()); +Assert::same(3, $ds->getTotalCount()); +Assert::same( + reformat('SELECT COUNT(*) FROM (SELECT * FROM products) t'), + dibi::$sql +); + + +$ds->select('title'); +$ds->orderBy('title', dibi::DESC); +$ds->where('title like "%a%"'); +Assert::match( + reformat(" +SELECT [title] +FROM (SELECT * FROM products) t + WHERE (title like '%a%') + ORDER BY [title] DESC + "), + (string) $ds +); + + +$ds->select('product_id'); +$ds->orderBy('product_id', dibi::ASC); +$ds->where('product_id = %i', 1); +Assert::match( + reformat(" +SELECT [title], [product_id] +FROM (SELECT * FROM products) t + WHERE (title like '%a%') AND (product_id = 1) + ORDER BY [title] DESC, [product_id] ASC + "), + (string) $ds +); + + +$ds->select(array('product_id')); +$ds->orderBy(array('product_id' => dibi::ASC)); +$ds->where(array('product_id = 1')); +Assert::match( + reformat(" +SELECT [product_id] +FROM (SELECT * FROM products) t + WHERE (title like '%a%') AND (product_id = 1) AND (product_id = 1) + ORDER BY [product_id] ASC + "), + (string) $ds +); + + +Assert::same(1, $ds->count()); +Assert::same(3, $ds->getTotalCount()); +Assert::match(reformat("SELECT COUNT(*) FROM ( +SELECT [product_id] +FROM (SELECT * FROM products) t + WHERE (title like '%a%') AND (product_id = 1) AND (product_id = 1) + ORDER BY [product_id] ASC + ) t"), dibi::$sql); +Assert::same(1, $ds->toDataSource()->count()); + + +Assert::equal(array( + new DibiRow(array( + 'product_id' => 1, + )), +), iterator_to_array($ds)); + +Assert::match( + reformat(" +SELECT [product_id] +FROM (SELECT * FROM products) t + WHERE (title like '%a%') AND (product_id = 1) AND (product_id = 1) + ORDER BY [product_id] ASC + "), + dibi::$sql +); + + +$fluent = $ds->toFluent(); +Assert::same(1, $fluent->count()); +Assert::match( + reformat("SELECT * FROM ( +SELECT [product_id] +FROM (SELECT * FROM products) t + WHERE (title like '%a%') AND (product_id = 1) AND (product_id = 1) + ORDER BY [product_id] ASC + ) t"), + (string) $fluent +); + + +$ds = $conn->select('title')->from('products')->toDataSource(); +Assert::match( + reformat(" +SELECT * +FROM (SELECT [title] FROM [products]) t"), + (string) $ds +); + +Assert::equal(new DibiRow(array( + 'product_id' => 1, + 'title' => 'Chair', +)), $conn->dataSource('SELECT * FROM products ORDER BY product_id')->fetch()); + +Assert::same(1, $conn->dataSource('SELECT * FROM products ORDER BY product_id')->fetchSingle()); + +Assert::same( + array(1 => 'Chair', 'Table', 'Computer'), + $conn->dataSource('SELECT * FROM products ORDER BY product_id')->fetchPairs() +); + +Assert::equal(array( + 1 => new DibiRow(array( + 'product_id' => 1, + 'title' => 'Chair', + )), + new DibiRow(array( + 'product_id' => 2, + 'title' => 'Table', + )), + new DibiRow(array( + 'product_id' => 3, + 'title' => 'Computer', + )), +), $conn->dataSource('SELECT * FROM products ORDER BY product_id')->fetchAssoc('product_id')); + + +$ds = new DibiDataSource('products', $conn); + +Assert::match( + reformat(" +SELECT * +FROM [products]"), + (string) $ds +); + +Assert::same(3, $ds->count()); +Assert::same(3, $ds->getTotalCount()); +Assert::same(reformat('SELECT COUNT(*) FROM [products]'), dibi::$sql); diff --git a/tests/dibi/DibiConnection.affectedRows.phpt b/tests/dibi/DibiConnection.affectedRows.phpt new file mode 100644 index 00000000..4aba92e3 --- /dev/null +++ b/tests/dibi/DibiConnection.affectedRows.phpt @@ -0,0 +1,35 @@ +loadFile(__DIR__ . "/data/$config[system].sql"); + + +$conn->query('INSERT INTO products', array( + 'title' => 'Test product', +)); +Assert::same(1, $conn->getAffectedRows()); + + +$conn->query('UPDATE products SET title="xxx" WHERE product_id > 100'); +Assert::same(0, $conn->getAffectedRows()); + + +$conn->query('UPDATE products SET title="xxx"'); +Assert::same(4, $conn->getAffectedRows()); + + +$conn->query('DELETE FROM orders'); +$conn->query('DELETE FROM products WHERE product_id > 100'); +Assert::same(0, $conn->getAffectedRows()); + + +$conn->query('DELETE FROM products WHERE product_id < 3'); +Assert::same(2, $conn->getAffectedRows()); diff --git a/tests/dibi/DibiConnection.connect.phpt b/tests/dibi/DibiConnection.connect.phpt new file mode 100644 index 00000000..f7517456 --- /dev/null +++ b/tests/dibi/DibiConnection.connect.phpt @@ -0,0 +1,37 @@ +isConnected()); + + $conn->disconnect(); + Assert::false($conn->isConnected()); +}); + + +test(function() use ($config) { // lazy + $conn = new DibiConnection($config + array('lazy' => TRUE)); + Assert::false($conn->isConnected()); + + $conn->query('SELECT 1'); + Assert::true($conn->isConnected()); +}); + + +test(function() use ($config) { // query string + $conn = new DibiConnection(http_build_query($config, NULL, '&')); + Assert::true($conn->isConnected()); + + Assert::null($conn->getConfig('lazy')); + Assert::same($config['driver'], $conn->getConfig('driver')); + Assert::type('IDibiDriver', $conn->getDriver()); +}); diff --git a/tests/dibi/DibiConnection.fetch.phpt b/tests/dibi/DibiConnection.fetch.phpt new file mode 100644 index 00000000..c8723e40 --- /dev/null +++ b/tests/dibi/DibiConnection.fetch.phpt @@ -0,0 +1,329 @@ +loadFile(__DIR__ . "/data/$config[system].sql"); + + +function num($n) +{ + global $config; + if (substr(@$config['dsn'], 0, 5) === 'odbc:' || $config['driver'] === 'sqlite') { + $n = is_float($n) ? "$n.0" : (string) $n; + } + return $n; +} + + +// fetch a single value +$res = $conn->query('SELECT [title] FROM [products]'); +Assert::same('Chair', $res->fetchSingle()); + + +// fetch complete result set +$res = $conn->query('SELECT * FROM [products] ORDER BY product_id'); +Assert::equal(array( + new DibiRow(array('product_id' => num(1), 'title' => 'Chair')), + new DibiRow(array('product_id' => num(2), 'title' => 'Table')), + new DibiRow(array('product_id' => num(3), 'title' => 'Computer')), +), $res->fetchAll()); + + +// fetch complete result set like pairs key => value +$res = $conn->query('SELECT * FROM [products] ORDER BY product_id'); +Assert::same( + array(1 => 'Chair', 'Table', 'Computer'), + $res->fetchPairs('product_id', 'title') +); + +$res = $conn->query('SELECT * FROM [products] ORDER BY product_id'); +Assert::same( + array(1 => 'Chair', 'Table', 'Computer'), + $res->fetchPairs() +); + + +// fetch row by row +$res = $conn->query('SELECT * FROM [products] ORDER BY product_id'); +Assert::equal(array( + new DibiRow(array('product_id' => num(1), 'title' => 'Chair')), + new DibiRow(array('product_id' => num(2), 'title' => 'Table')), + new DibiRow(array('product_id' => num(3), 'title' => 'Computer')), +), iterator_to_array($res)); + + +// fetch complete result set like association array +$res = $conn->query('SELECT * FROM [products] ORDER BY product_id'); +Assert::equal(array( + 'Chair' => new DibiRow(array('product_id' => num(1), 'title' => 'Chair')), + 'Table' => new DibiRow(array('product_id' => num(2), 'title' => 'Table')), + 'Computer' => new DibiRow(array('product_id' => num(3), 'title' => 'Computer')), +), $res->fetchAssoc('title')); + + + +// more complex association array +function query($conn) { + + return $conn->query($conn->getConfig('system') === 'odbc' ? ' + SELECT products.title, customers.name, orders.amount + FROM ([products] + INNER JOIN [orders] ON [products.product_id] = [orders.product_id]) + INNER JOIN [customers] ON [orders.customer_id] = [customers.customer_id] + ORDER BY orders.order_id + ' : ' + SELECT products.title AS title, customers.name AS name, orders.amount AS amount + FROM [products] + INNER JOIN [orders] USING ([product_id]) + INNER JOIN [customers] USING ([customer_id]) + ORDER BY orders.order_id + '); +} + + +Assert::equal(array( + 'Arnold Rimmer' => array( + 'Chair' => new DibiRow(array('title' => 'Chair', 'name' => 'Arnold Rimmer', 'amount' => num(7.0))), + 'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Arnold Rimmer', 'amount' => num(2.0))), + ), + 'Dave Lister' => array( + 'Table' => new DibiRow(array('title' => 'Table', 'name' => 'Dave Lister', 'amount' => num(3.0))), + ), + 'Kristine Kochanski' => array( + 'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Kristine Kochanski', 'amount' => num(5.0))), + ), +), query($conn)->fetchAssoc('name,title')); + + +Assert::equal(array( + 'Arnold Rimmer' => array( + array( + 'Chair' => new DibiRow(array('title' => 'Chair', 'name' => 'Arnold Rimmer', 'amount' => num(7.0))), + ), + array( + 'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Arnold Rimmer', 'amount' => num(2.0))), + ), + ), + 'Dave Lister' => array( + array( + 'Table' => new DibiRow(array('title' => 'Table', 'name' => 'Dave Lister', 'amount' => num(3.0))), + ), + ), + 'Kristine Kochanski' => array( + array( + 'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Kristine Kochanski', 'amount' => num(5.0))), + ), + ), +), query($conn)->fetchAssoc('name,#,title')); + + +Assert::equal(array( + 'Arnold Rimmer' => array( + 'title' => array( + 'Chair' => new DibiRow(array('title' => 'Chair', 'name' => 'Arnold Rimmer', 'amount' => num(7.0))), + 'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Arnold Rimmer', 'amount' => num(2.0))), + ), + 'name' => 'Arnold Rimmer', + 'amount' => num(7.0), + ), + 'Dave Lister' => array( + 'title' => array( + 'Table' => new DibiRow(array('title' => 'Table', 'name' => 'Dave Lister', 'amount' => num(3.0))), + ), + 'name' => 'Dave Lister', + 'amount' => num(3.0), + ), + 'Kristine Kochanski' => array( + 'title' => array( + 'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Kristine Kochanski', 'amount' => num(5.0))), + ), + 'name' => 'Kristine Kochanski', + 'amount' => num(5.0), + ), +), query($conn)->fetchAssoc('name,=,title')); + + +Assert::equal(array( + 'Arnold Rimmer' => new DibiRow(array( + 'title' => array( + 'Chair' => new DibiRow(array('title' => 'Chair', 'name' => 'Arnold Rimmer', 'amount' => num(7.0))), + 'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Arnold Rimmer', 'amount' => num(2.0))), + ), + 'name' => 'Arnold Rimmer', + 'amount' => num(7.0), + )), + 'Dave Lister' => new DibiRow(array( + 'title' => array( + 'Table' => new DibiRow(array('title' => 'Table', 'name' => 'Dave Lister', 'amount' => num(3.0))), + ), + 'name' => 'Dave Lister', + 'amount' => num(3.0), + )), + 'Kristine Kochanski' => new DibiRow(array( + 'title' => array( + 'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Kristine Kochanski', 'amount' => num(5.0))), + ), + 'name' => 'Kristine Kochanski', + 'amount' => num(5.0), + )), +), query($conn)->fetchAssoc('name,@,title')); + + +Assert::equal(array( + new DibiRow(array('title' => 'Chair', 'name' => 'Arnold Rimmer', 'amount' => num(7.0))), + new DibiRow(array( + 'title' => 'Computer', 'name' => 'Arnold Rimmer', 'amount' => num(2.0))), + new DibiRow(array( + 'title' => 'Table', 'name' => 'Dave Lister', 'amount' => num(3.0))), + new DibiRow(array( + 'title' => 'Computer', 'name' => 'Kristine Kochanski', 'amount' => num(5.0))), +), query($conn)->fetchAssoc('@,=')); + + +Assert::equal(array( + 'Arnold Rimmer' => array( + 'title' => array( + 'Chair' => new DibiRow(array('title' => 'Chair', 'name' => 'Arnold Rimmer', 'amount' => num(7.0))), + 'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Arnold Rimmer', 'amount' => num(2.0))), + ), + 'name' => 'Arnold Rimmer', + 'amount' => num(7.0), + ), + 'Dave Lister' => array( + 'title' => array( + 'Table' => new DibiRow(array('title' => 'Table', 'name' => 'Dave Lister', 'amount' => num(3.0))), + ), + 'name' => 'Dave Lister', + 'amount' => num(3.0), + ), + 'Kristine Kochanski' => array( + 'title' => array( + 'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Kristine Kochanski', 'amount' => num(5.0))), + ), + 'name' => 'Kristine Kochanski', + 'amount' => num(5.0), + ), +), query($conn)->fetchAssoc('name,=,title,@')); + + +// old syntax +Assert::equal(array( + 'Arnold Rimmer' => array( + 'Chair' => new DibiRow(array('title' => 'Chair', 'name' => 'Arnold Rimmer', 'amount' => num(7.0))), + 'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Arnold Rimmer', 'amount' => num(2.0))), + ), + 'Dave Lister' => array( + 'Table' => new DibiRow(array('title' => 'Table', 'name' => 'Dave Lister', 'amount' => num(3.0))), + ), + 'Kristine Kochanski' => array( + 'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Kristine Kochanski', 'amount' => num(5.0))), + ), +), query($conn)->fetchAssoc('name|title')); + + +Assert::equal(array( + 'Arnold Rimmer' => array( + array( + 'Chair' => new DibiRow(array('title' => 'Chair', 'name' => 'Arnold Rimmer', 'amount' => num(7.0))), + ), + array( + 'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Arnold Rimmer', 'amount' => num(2.0))), + ), + ), + 'Dave Lister' => array( + array( + 'Table' => new DibiRow(array('title' => 'Table', 'name' => 'Dave Lister', 'amount' => num(3.0))), + ), + ), + 'Kristine Kochanski' => array( + array( + 'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Kristine Kochanski', 'amount' => num(5.0))), + ), + ), +), query($conn)->fetchAssoc('name[]title')); + + +Assert::equal(array( + 'Arnold Rimmer' => new DibiRow(array( + 'title' => array( + 'Chair' => new DibiRow(array('title' => 'Chair', 'name' => 'Arnold Rimmer', 'amount' => num(7.0))), + 'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Arnold Rimmer', 'amount' => num(2.0))), + ), + 'name' => 'Arnold Rimmer', + 'amount' => num(7.0), + )), + 'Dave Lister' => new DibiRow(array( + 'title' => array( + 'Table' => new DibiRow(array('title' => 'Table', 'name' => 'Dave Lister', 'amount' => num(3.0))), + ), + 'name' => 'Dave Lister', + 'amount' => num(3.0), + )), + 'Kristine Kochanski' => new DibiRow(array( + 'title' => array( + 'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Kristine Kochanski', 'amount' => num(5.0))), + ), + 'name' => 'Kristine Kochanski', + 'amount' => num(5.0), + )), +), query($conn)->fetchAssoc('name->title')); + + +Assert::equal(array( + 'Arnold Rimmer' => new DibiRow(array( + 'title' => array('Chair' => 'Arnold Rimmer', 'Computer' => 'Arnold Rimmer'), + 'name' => 'Arnold Rimmer', + 'amount' => num(7.0), + )), + 'Dave Lister' => new DibiRow(array( + 'title' => array('Table' => 'Dave Lister'), + 'name' => 'Dave Lister', + 'amount' => num(3.0), + )), + 'Kristine Kochanski' => new DibiRow(array( + 'title' => array('Computer' => 'Kristine Kochanski'), + 'name' => 'Kristine Kochanski', + 'amount' => num(5.0), + )), +), query($conn)->fetchAssoc('name->title=name')); + + +Assert::equal(array( + new DibiRow(array('title' => 'Chair', 'name' => 'Arnold Rimmer', 'amount' => num(7.0))), + new DibiRow(array('title' => 'Computer', 'name' => 'Arnold Rimmer', 'amount' => num(2.0))), + new DibiRow(array('title' => 'Table', 'name' => 'Dave Lister', 'amount' => num(3.0))), + new DibiRow(array('title' => 'Computer', 'name' => 'Kristine Kochanski', 'amount' => num(5.0))), +), query($conn)->fetchAssoc('[]')); + + +Assert::equal(array( + 'Arnold Rimmer' => new DibiRow(array( + 'title' => array( + 'Chair' => new DibiRow(array('title' => 'Chair', 'name' => 'Arnold Rimmer', 'amount' => num(7.0))), + 'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Arnold Rimmer', 'amount' => num(2.0))), + ), + 'name' => 'Arnold Rimmer', + 'amount' => num(7.0), + )), + 'Dave Lister' => new DibiRow(array( + 'title' => array( + 'Table' => new DibiRow(array('title' => 'Table', 'name' => 'Dave Lister', 'amount' => num(3.0))), + ), + 'name' => 'Dave Lister', + 'amount' => num(3.0), + )), + 'Kristine Kochanski' => new DibiRow(array( + 'title' => array( + 'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Kristine Kochanski', 'amount' => num(5.0))), + ), + 'name' => 'Kristine Kochanski', + 'amount' => num(5.0), + )), +), query($conn)->fetchAssoc('name->title->')); diff --git a/tests/dibi/DibiConnection.substitutions.phpt b/tests/dibi/DibiConnection.substitutions.phpt new file mode 100644 index 00000000..236c860a --- /dev/null +++ b/tests/dibi/DibiConnection.substitutions.phpt @@ -0,0 +1,26 @@ + wp_ +$conn->getSubstitutes()->blog = 'wp_'; + +Assert::same( + reformat('UPDATE wp_items SET [text]=\'Hello World\''), + $conn->translate("UPDATE :blog:items SET [text]='Hello World'") +); + +Assert::same( + reformat('UPDATE \'wp_\' SET [text]=\'Hello World\''), + $conn->translate("UPDATE :blog: SET [text]='Hello World'") +); + +Assert::same( + reformat('UPDATE \':blg:\' SET [text]=\'Hello World\''), + $conn->translate("UPDATE :blg: SET [text]='Hello World'") +); diff --git a/tests/dibi/DibiConnection.transactions.phpt b/tests/dibi/DibiConnection.transactions.phpt new file mode 100644 index 00000000..a11f96d2 --- /dev/null +++ b/tests/dibi/DibiConnection.transactions.phpt @@ -0,0 +1,48 @@ +loadFile(__DIR__ . "/data/$config[system].sql"); + + +/*Assert::exception(function() use ($conn) { + $conn->rollback(); +}, 'DibiException'); + +Assert::exception(function() use ($conn) { + $conn->commit(); +}, 'DibiException'); + +$conn->begin(); +Assert::exception(function() use ($conn) { + $conn->begin(); +}, 'DibiException'); +*/ + + +$conn->begin(); +Assert::same(3, (int) $conn->query('SELECT COUNT(*) FROM [products]')->fetchSingle()); +$conn->query('INSERT INTO [products]', array( + 'title' => 'Test product', +)); +Assert::same(4, (int) $conn->query('SELECT COUNT(*) FROM [products]')->fetchSingle()); +$conn->rollback(); +Assert::same(3, (int) $conn->query('SELECT COUNT(*) FROM [products]')->fetchSingle()); + + + + +$conn->begin(); +$conn->query('INSERT INTO [products]', array( + 'title' => 'Test product', +)); +$conn->commit(); +Assert::same(4, (int) $conn->query('SELECT COUNT(*) FROM [products]')->fetchSingle()); diff --git a/tests/dibi/DibiFluent.delete.phpt b/tests/dibi/DibiFluent.delete.phpt new file mode 100644 index 00000000..72db5ed4 --- /dev/null +++ b/tests/dibi/DibiFluent.delete.phpt @@ -0,0 +1,45 @@ +delete('table')->as('bAlias') + ->setFlag('IGNORE'); + +Assert::same( + reformat('DELETE IGNORE FROM [table] AS [bAlias]'), + (string) $fluent +); + +$fluent->removeClause('from')->from('anotherTable'); + +Assert::same( + reformat('DELETE IGNORE FROM [anotherTable]'), + (string) $fluent +); + +$fluent->using('thirdTable'); + +Assert::same( + reformat('DELETE IGNORE FROM [anotherTable] USING [thirdTable]'), + (string) $fluent +); + +$fluent->setFlag('IGNORE', FALSE); + +Assert::same( + reformat('DELETE FROM [anotherTable] USING [thirdTable]'), + (string) $fluent +); + +$fluent->limit(10); + +Assert::same( + reformat('DELETE FROM [anotherTable] USING [thirdTable] LIMIT 10'), + (string) $fluent +); diff --git a/tests/dibi/DibiFluent.fetch.phpt b/tests/dibi/DibiFluent.fetch.phpt new file mode 100644 index 00000000..088b019a --- /dev/null +++ b/tests/dibi/DibiFluent.fetch.phpt @@ -0,0 +1,59 @@ +loadFile(__DIR__ . "/data/$config[system].sql"); + + +function num($n) +{ + global $config; + if (substr(@$config['dsn'], 0, 5) === 'odbc:' || $config['driver'] === 'sqlite') { + $n = is_float($n) ? "$n.0" : (string) $n; + } + return $n; +} + + +// fetch a single value +$res = $conn->select('title')->from('products')->orderBy('product_id'); +Assert::equal('Chair', $res->fetchSingle()); + + +// fetch complete result set +$res = $conn->select('*')->from('products')->orderBy('product_id'); +Assert::equal(array( + new DibiRow(array('product_id' => num(1), 'title' => 'Chair')), + new DibiRow(array('product_id' => num(2), 'title' => 'Table')), + new DibiRow(array('product_id' => num(3), 'title' => 'Computer')), +), $res->fetchAll()); + + +// more complex association array +if ($config['system'] !== 'odbc') { + $res = $conn->select(array('products.title' => 'title', 'customers.name' => 'name'))->select('orders.amount')->as('amount') + ->from('products') + ->innerJoin('orders')->using('(product_id)') + ->innerJoin('customers')->using('([customer_id])') + ->orderBy('order_id'); + + Assert::equal(array( + 'Arnold Rimmer' => array( + 'Chair' => new DibiRow(array('title' => 'Chair', 'name' => 'Arnold Rimmer', 'amount' => num(7.0))), + 'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Arnold Rimmer', 'amount' => num(2.0))), + ), + 'Dave Lister' => array( + 'Table' => new DibiRow(array('title' => 'Table', 'name' => 'Dave Lister', 'amount' => num(3.0))), + ), + 'Kristine Kochanski' => array( + 'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Kristine Kochanski', 'amount' => num(5.0))), + ), + ), $res->fetchAssoc('name,title')); +} diff --git a/tests/dibi/DibiFluent.insert.phpt b/tests/dibi/DibiFluent.insert.phpt new file mode 100644 index 00000000..698324f5 --- /dev/null +++ b/tests/dibi/DibiFluent.insert.phpt @@ -0,0 +1,58 @@ + 'Super Product', + 'price' => 12, + 'brand' => NULL, +); + +$fluent = $conn->insert('table', $arr) + ->setFlag('IGNORE')->setFlag('DELAYED'); + +Assert::same( + reformat('INSERT IGNORE DELAYED INTO [table] ([title], [price], [brand]) VALUES (\'Super Product\', 12, NULL)'), + (string) $fluent +); + +$fluent->setFlag('IGNORE', FALSE); + +Assert::same( + reformat('INSERT DELAYED INTO [table] ([title], [price], [brand]) VALUES (\'Super Product\', 12, NULL)'), + (string) $fluent +); + +$fluent->setFlag('HIGH_priority'); + +Assert::same( + reformat('INSERT DELAYED HIGH_PRIORITY INTO [table] ([title], [price], [brand]) VALUES (\'Super Product\', 12, NULL)'), + (string) $fluent +); + +$fluent->into('anotherTable'); + +Assert::same( + reformat('INSERT DELAYED HIGH_PRIORITY INTO [anotherTable] VALUES (\'Super Product\', 12, NULL)'), + (string) $fluent +); + +$fluent->values('%l', $arr); + +Assert::same( + reformat('INSERT DELAYED HIGH_PRIORITY INTO [anotherTable] VALUES (\'Super Product\', 12, NULL) , (\'Super Product\', 12, NULL)'), + (string) $fluent +); + +$fluent->values($arr); + +Assert::same( + reformat('INSERT DELAYED HIGH_PRIORITY INTO [anotherTable] VALUES (\'Super Product\', 12, NULL) , (\'Super Product\', 12, NULL) , (\'Super Product\', 12, NULL)'), + (string) $fluent +); diff --git a/tests/dibi/DibiFluent.select.phpt b/tests/dibi/DibiFluent.select.phpt new file mode 100644 index 00000000..bdb67eeb --- /dev/null +++ b/tests/dibi/DibiFluent.select.phpt @@ -0,0 +1,139 @@ +select('*') + ->select('a') + ->select('b')->as('bAlias') + ->select(array('c', 'd', 'e')) + ->select('%n', 'd'); + +Assert::same( + reformat('SELECT * , [a] , [b] AS [bAlias] , [c], [d], [e] , [d]'), + (string) $fluent +); + +$fluent->from('table')->as('tableAlias') + ->innerJoin('table1')->on('table.col = table1.col') + ->innerJoin('table2')->on('table.col = table2.col'); + +Assert::same( + reformat('SELECT * , [a] , [b] AS [bAlias] , [c], [d], [e] , [d] FROM [table] AS [tableAlias] INNER JOIN [table1] ON table.col = table1.col INNER JOIN [table2] ON table.col = table2.col'), + (string) $fluent +); + +$fluent->from('anotherTable'); + +Assert::same( + reformat('SELECT * , [a] , [b] AS [bAlias] , [c], [d], [e] , [d] FROM [table] AS [tableAlias] INNER JOIN [table1] ON table.col = table1.col INNER JOIN [table2] ON table.col = table2.col , [anotherTable]'), + (string) $fluent +); + +$fluent->removeClause('from')->from('anotherTable'); + +Assert::same( + reformat('SELECT * , [a] , [b] AS [bAlias] , [c], [d], [e] , [d] FROM [anotherTable]'), + (string) $fluent +); + +$fluent->as('anotherAlias') + ->clause('from') + ->innerJoin('table3') + ->on('table.col = table3.col'); + +Assert::same( + reformat('SELECT * , [a] , [b] AS [bAlias] , [c], [d], [e] , [d] FROM [anotherTable] AS [anotherAlias] INNER JOIN [table3] ON table.col = table3.col'), + (string) $fluent +); + +$fluent->where('col > %i', $max) + ->or('col < %i', $min) + ->where('active = 1') + ->where('col')->in(array(1,2,3)) + ->orderBy('val')->asc() + ->orderBy('[val2] DESC') + ->orderBy(array('val3' => -1)); + +Assert::same( + reformat('SELECT * , [a] , [b] AS [bAlias] , [c], [d], [e] , [d] FROM [anotherTable] AS [anotherAlias] INNER JOIN [table3] ON table.col = table3.col WHERE col > 10 OR col < 5 AND active = 1 AND [col] IN (1, 2, 3) ORDER BY [val] ASC , [val2] DESC , [val3] DESC'), + (string) $fluent +); + +$fluent->orderBy(DibiFluent::REMOVE); + +Assert::same( + reformat('SELECT * , [a] , [b] AS [bAlias] , [c], [d], [e] , [d] FROM [anotherTable] AS [anotherAlias] INNER JOIN [table3] ON table.col = table3.col WHERE col > 10 OR col < 5 AND active = 1 AND [col] IN (1, 2, 3)'), + (string) $fluent +); + + +try { + $fluent = $conn->select('*')->from('table')->fetch(); +} catch (Exception $e) { +} +Assert::same( + reformat(' SELECT * FROM [table] LIMIT 1'), + dibi::$sql +); + + +$fluent = $conn->select('*') + ->select( + $conn->select('count(*)') + ->from('precteni')->as('P') + ->where('P.id_clanku', '=', 'C.id_clanku') + ) + ->from('clanky')->as('C') + ->where('id_clanku=%i', 123) + ->limit(1) + ->offset(0); + +Assert::same( + reformat('SELECT * , (SELECT count(*) FROM [precteni] AS [P] WHERE P.id_clanku = C.id_clanku) FROM [clanky] AS [C] WHERE id_clanku=123 LIMIT 1 OFFSET 0'), + (string) $fluent +); + + +$fluent = $conn->select('*') + ->select(array('x' => 'xAlias')) + ->from('products') + ->innerJoin('orders')->using('(product_id)') + ->innerJoin('customers')->using('([customer_id])') + ->innerJoin('items')->using('(%n)', array('customer_id', 'order_id')); + +Assert::same( + reformat('SELECT * , [x] AS [xAlias] FROM [products] INNER JOIN [orders] USING (product_id) INNER JOIN [customers] USING ([customer_id]) INNER JOIN [items] USING ([customer_id], [order_id])'), + (string) $fluent +); + + + +$fluent = $conn->command()->select() + ->from('products') + ->select('*') + ->innerJoin('orders')->using('(product_id)'); + +Assert::same( + reformat('SELECT * FROM [products] INNER JOIN [orders] USING (product_id)'), + (string) $fluent +); + + +$fluent = $conn->select('*') + ->from(array('me' => 't')) + ->where('col > %i', $max) + ->where(array('x' => 'a', 'b', 'c')); + +Assert::same( + reformat('SELECT * FROM [me] AS [t] WHERE col > 10 AND ([x] = \'a\') AND (b) AND (c)'), + (string) $fluent +); diff --git a/tests/dibi/DibiFluent.update.phpt b/tests/dibi/DibiFluent.update.phpt new file mode 100644 index 00000000..fde198f0 --- /dev/null +++ b/tests/dibi/DibiFluent.update.phpt @@ -0,0 +1,30 @@ + 'Super Product', + 'price' => 12, + 'brand' => NULL, +); + +$fluent = $conn->update('table', $arr) + ->setFlag('IGNORE')->setFlag('DELAYED'); + +Assert::same( + reformat('UPDATE IGNORE DELAYED [table] SET [title]=\'Super Product\', [price]=12, [brand]=NULL'), + (string) $fluent +); + +$fluent->set(array('another' => 123)); + +Assert::same( + reformat('UPDATE IGNORE DELAYED [table] SET [title]=\'Super Product\', [price]=12, [brand]=NULL , [another]=123'), + (string) $fluent +); diff --git a/tests/dibi/DibiResult.meta.phpt b/tests/dibi/DibiResult.meta.phpt new file mode 100644 index 00000000..46d95e49 --- /dev/null +++ b/tests/dibi/DibiResult.meta.phpt @@ -0,0 +1,57 @@ +loadFile(__DIR__ . "/data/$config[system].sql"); + +$info = $conn->query(' + SELECT products.product_id, orders.order_id, customers.name, products.product_id + 1 AS xxx + FROM products + INNER JOIN orders USING (product_id) + INNER JOIN customers USING (customer_id) +')->getInfo(); + + +Assert::same( + array('product_id', 'order_id', 'name', 'xxx'), + $info->getColumnNames() +); + + +if ($config['driver'] !== 'sqlite3' && $config['driver'] !== 'pdo') { + Assert::same( + array('products.product_id', 'orders.order_id', 'customers.name', 'xxx'), + $info->getColumnNames(TRUE) + ); +} + + +$columns = $info->getColumns(); + +Assert::same('product_id', $columns[0]->name); +if ($config['driver'] !== 'sqlite3' && $config['driver'] !== 'pdo') { + Assert::same('products', $columns[0]->tableName); +} +Assert::null($columns[0]->getVendorInfo('xxx')); +if ($config['system'] !== 'sqlite') { + Assert::same('i', $columns[0]->type); +} +Assert::null($columns[0]->nullable); + +Assert::same('xxx', $columns[3]->name); +Assert::null($columns[3]->tableName); +if ($config['system'] !== 'sqlite') { + Assert::same('i', $columns[0]->type); +} +Assert::null($columns[3]->nullable); diff --git a/tests/dibi/DibiResult.types.phpt b/tests/dibi/DibiResult.types.phpt new file mode 100644 index 00000000..fa8e20da --- /dev/null +++ b/tests/dibi/DibiResult.types.phpt @@ -0,0 +1,18 @@ +loadFile(__DIR__ . "/data/$config[system].sql"); + +$res = $conn->query('SELECT * FROM [customers]'); + +// auto-converts this column to integer +$res->setType('customer_id', Dibi::DATETIME, 'H:i j.n.Y'); + +Assert::equal(new DibiRow(array( + 'customer_id' => new DibiDateTime('1970-01-01 01:00:01'), + 'name' => 'Dave Lister', +)), $res->fetch()); diff --git a/tests/dibi/DibiTranslator.conditions.phpt b/tests/dibi/DibiTranslator.conditions.phpt new file mode 100644 index 00000000..364a07c5 --- /dev/null +++ b/tests/dibi/DibiTranslator.conditions.phpt @@ -0,0 +1,76 @@ +translate(' +SELECT * +FROM [customers] +%if', isset($name), 'WHERE [name] LIKE %s', 'xxx', '%end' +)); + + +// if & else & end (last end is optional) +Assert::same( + reformat(" +SELECT * +FROM [customers] /* ... */"), + + $conn->translate(' +SELECT * +FROM %if', TRUE, '[customers] %else [products]' +)); + + +// if & else & (optional) end +Assert::match( + reformat(" +SELECT * +FROM [people] +WHERE [id] > 0 + /* AND ...=... + */ AND [bar]=1 +"), + + $conn->translate(" +SELECT * +FROM [people] +WHERE [id] > 0 + %if", FALSE, "AND [foo]=%i", 1, " + %else %if", TRUE, "AND [bar]=%i", 1, " +")); + + +// nested condition +Assert::match( + reformat(" +SELECT * +FROM [customers] +WHERE + [name] LIKE 'xxx' + /* AND ...=1 */ + /* 1 LIMIT 10 */"), + + $conn->translate(' +SELECT * +FROM [customers] +WHERE + %if', TRUE, '[name] LIKE %s', 'xxx', ' + %if', FALSE, 'AND [admin]=1 %end + %else 1 LIMIT 10 %end' +)); diff --git a/tests/dibi/DibiTranslator.identifiers.phpt b/tests/dibi/DibiTranslator.identifiers.phpt new file mode 100644 index 00000000..b3302a82 --- /dev/null +++ b/tests/dibi/DibiTranslator.identifiers.phpt @@ -0,0 +1,47 @@ +translate('SELECT * FROM where WHERE select < 2') +); + + +Assert::same( + reformat('SELECT * FROM [where] WHERE where.select < 2'), + $conn->translate('SELECT * FROM [where] WHERE where.select < 2') +); + + +Assert::same( + reformat('SELECT * FROM [where] WHERE [where].[select] < 2'), + $conn->translate('SELECT * FROM [where] WHERE [where.select] < 2') +); + + +Assert::same( + reformat('SELECT * FROM [where] as [temp] WHERE [temp].[select] < 2'), + $conn->translate('SELECT * FROM [where] as [temp] WHERE [temp.select] < 2') +); + + +Assert::same( + reformat('SELECT * FROM [where] WHERE [quot\'n\' space] > 2'), + $conn->translate("SELECT * FROM [where] WHERE [quot'n' space] > 2") +); + + +Assert::same( + reformat('SELECT * FROM [where] WHERE [where].[quot\'n\' space] > 2'), + $conn->translate("SELECT * FROM [where] WHERE [where.quot'n' space] > 2") +); diff --git a/tests/dibi/DibiTranslator.phpt b/tests/dibi/DibiTranslator.phpt new file mode 100644 index 00000000..fce89076 --- /dev/null +++ b/tests/dibi/DibiTranslator.phpt @@ -0,0 +1,510 @@ + "'Y-m-d H:i:s'", 'formatDate' => "'Y-m-d'")); + + +// dibi detects INSERT or REPLACE command & booleans +Assert::same( + reformat("REPLACE INTO [products] ([title], [price]) VALUES ('Drticka', 318)"), + $conn->translate('REPLACE INTO [products]', array( + 'title' => 'Drticka', + 'price' => 318, +))); + + +// multiple INSERT command +$array = array( + 'title' => 'Super Product', + 'price' => 12, + 'brand' => NULL, +); +Assert::same( + reformat('INSERT INTO [products] ([title], [price], [brand]) VALUES (\'Super Product\', 12, NULL) , (\'Super Product\', 12, NULL) , (\'Super Product\', 12, NULL)'), + $conn->translate("INSERT INTO [products]", $array, $array, $array) +); + + +// multiple INSERT command II +$array = array( + array('pole' => 'hodnota1', 'bit' => 1), + array('pole' => 'hodnota2', 'bit' => 1), + array('pole' => 'hodnota3', 'bit' => 1) +); +Assert::same( + reformat('INSERT INTO [products] ([pole], [bit]) VALUES (\'hodnota1\', 1) , (\'hodnota2\', 1) , (\'hodnota3\', 1)'), + $conn->translate("INSERT INTO [products] %ex", $array) +); + + +// dibi detects UPDATE command +Assert::same( + reformat("UPDATE [colors] SET [color]='blue', [order]=12 WHERE [id]=123"), + $conn->translate('UPDATE [colors] SET', array( + 'color' => 'blue', + 'order' => 12, +), "WHERE [id]=%i", 123)); + + +// IN array +$array = array(1, 2, 3); +Assert::same( + reformat('SELECT * FROM [people] WHERE [id] IN ( 1, 2, 3 )'), + $conn->translate("SELECT * FROM [people] WHERE [id] IN (", $array, ")") +); + + +// long numbers +Assert::same( + reformat('SELECT -123456789123456789123456789'), + $conn->translate("SELECT %i", '-123456789123456789123456789') +); + +// long float numbers +Assert::same( + reformat('SELECT -.12345678912345678912345678e10'), + $conn->translate("SELECT %f", '-.12345678912345678912345678e10') +); + +// hex numbers +Assert::same( + reformat('SELECT 17'), + $conn->translate("SELECT %i", '0x11') +); + +// invalid input +$e = Assert::exception(function() use ($conn) { + $conn->translate("SELECT %s", (object) array(123), ', %m', 123); +}, 'DibiException', 'SQL translate error'); +Assert::same('SELECT **Unexpected type object** , **Unknown or invalid modifier %m**', $e->getSql()); + +Assert::same( + reformat('SELECT * FROM [table] WHERE id=10 AND name=\'ahoj\''), + $conn->translate('SELECT * FROM [table] WHERE id=%i AND name=%s', 10, 'ahoj') +); + +Assert::same( + reformat('TEST ([cond] > 2) OR ([cond2] = \'3\') OR (cond3 < RAND())'), + $conn->translate('TEST %or', array('[cond] > 2', '[cond2] = "3"', 'cond3 < RAND()')) +); + +Assert::same( + reformat('TEST ([cond] > 2) AND ([cond2] = \'3\') AND (cond3 < RAND())'), + $conn->translate('TEST %and', array('[cond] > 2', '[cond2] = "3"', 'cond3 < RAND()')) +); + +// +$where = array(); +$where[] = '[age] > 20'; +$where[] = '[email] IS NOT NULL'; +Assert::same( + reformat('SELECT * FROM [table] WHERE ([age] > 20) AND ([email] IS NOT NULL)'), + $conn->translate('SELECT * FROM [table] WHERE %and', $where) +); + + +$where = array(); +$where['age'] = NULL; +$where['email'] = 'ahoj'; +$where['id%l'] = array(10, 20, 30); +Assert::same( + reformat('SELECT * FROM [table] WHERE ([age] IS NULL) AND ([email] = \'ahoj\') AND ([id] IN (10, 20, 30))'), + $conn->translate('SELECT * FROM [table] WHERE %and', $where) +); + + +$where = array(); +Assert::same( + reformat('SELECT * FROM [table] WHERE 1=1'), + $conn->translate('SELECT * FROM [table] WHERE %and', $where) +); + + +// ORDER BY array +$order = array( + 'field1' => 'asc', + 'field2' => 'desc', + 'field3' => 1, + 'field4' => -1, + 'field5' => TRUE, + 'field6' => FALSE, +); +Assert::same( + reformat("SELECT * FROM [people] ORDER BY [field1] ASC, [field2] DESC, [field3] ASC, [field4] DESC, [field5] ASC, [field6] DESC"), + $conn->translate("SELECT * FROM [people] ORDER BY %by", $order) +); + + +// with limit = 2 +Assert::same( + reformat(array( + 'odbc' => 'SELECT TOP 2 * FROM (SELECT * FROM [products] ) t', + 'SELECT * FROM [products] LIMIT 2', + )), + $conn->translate('SELECT * FROM [products] %lmt', 2) +); + +if ($config['system'] === 'odbc') { + Assert::exception(function() use ($conn) { + $conn->translate('SELECT * FROM [products] %lmt %ofs', 2, 1); + }, 'DibiException'); +} else { + // with limit = 2, offset = 1 + Assert::same( + reformat('SELECT * FROM [products] LIMIT 2 OFFSET 1'), + $conn->translate('SELECT * FROM [products] %lmt %ofs', 2, 1) + ); + + // with offset = 50 + Assert::same( + reformat(array( + 'mysql' => 'SELECT * FROM `products` LIMIT 18446744073709551615 OFFSET 50', + 'pgsql' => 'SELECT * FROM "products" OFFSET 50', + 'SELECT * FROM [products] LIMIT -1 OFFSET 50', + )), + $conn->translate('SELECT * FROM [products] %ofs', 50) + ); +} + + + + +Assert::same( + reformat(array( + 'odbc' => 'INSERT INTO test ([a2], [a4], [b1], [b2], [b3], [b4], [b5], [b6], [b7], [b8], [b9]) VALUES (#09/26/1212 00:00:00#, #12/31/1969 22:13:20#, #09/26/1212#, #09/26/1212 00:00:00#, #12/31/1969#, #12/31/1969 22:13:20#, #09/26/1212 00:00:00#, #09/26/1212#, #09/26/1212 00:00:00#, NULL, NULL)', + "INSERT INTO test ([a2], [a4], [b1], [b2], [b3], [b4], [b5], [b6], [b7], [b8], [b9]) VALUES ('1212-09-26 00:00:00', '1969-12-31 22:13:20', '1212-09-26', '1212-09-26 00:00:00', '1969-12-31', '1969-12-31 22:13:20', '1212-09-26 00:00:00', '1212-09-26', '1212-09-26 00:00:00', NULL, NULL)", + )), + $conn->translate("INSERT INTO test", array( + 'a2' => new DibiDateTime('1212-09-26'), + 'a4' => new DibiDateTime(-10000), + 'b1%d' => '1212-09-26', + 'b2%t' => '1212-09-26', + 'b3%d' => -10000, + 'b4%t' => -10000, + 'b5' => new DateTime('1212-09-26'), + 'b6%d' => new DateTime('1212-09-26'), + 'b7%t' => new DateTime('1212-09-26'), + 'b8%d' => NULL, + 'b9%t' => NULL, +))); + + + +// like +if ($config['driver'] !== 'sqlite') { // sqlite2 + Assert::same( + reformat(array( + 'sqlite' => "SELECT * FROM products WHERE (title LIKE 'C%' ESCAPE '\\' AND title LIKE '%r' ESCAPE '\\') OR title LIKE '%a\n\\%\\_\\\\''\"%' ESCAPE '\\'", + 'odbc' => "SELECT * FROM products WHERE (title LIKE 'C%' AND title LIKE '%r') OR title LIKE '%a\n[%][_]\\''\"%'", + 'pgsql' => "SELECT * FROM products WHERE (title LIKE 'C%' AND title LIKE '%r') OR title LIKE '%a\n\\\\%\\\\_\\''\"%'", + "SELECT * FROM products WHERE (title LIKE 'C%' AND title LIKE '%r') OR title LIKE '%a\\n\\%\\_\\\\\\\\\'\"%'", + )), + $conn->translate("SELECT * FROM products WHERE (title LIKE %like~ AND title LIKE %~like) OR title LIKE %~like~", 'C', 'r', "a\n%_\\'\"") + ); +} + + +$e = Assert::exception(function() use ($conn) { + $conn->translate("SELECT '"); +}, 'DibiException', 'SQL translate error'); +Assert::same('SELECT **Alone quote**', $e->getSql()); + +Assert::match( + reformat(array( + 'mysql' => "SELECT DISTINCT HIGH_PRIORITY SQL_BUFFER_RESULT +CONCAT(last_name, ', ', first_name) AS full_name +GROUP BY `user` +HAVING MAX(salary) > %i 123 +INTO OUTFILE '/tmp/result\'.txt' +FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\\\"' +LINES TERMINATED BY '\\\\n' +", + "SELECT DISTINCT HIGH_PRIORITY SQL_BUFFER_RESULT +CONCAT(last_name, ', ', first_name) AS full_name +GROUP BY [user] +HAVING MAX(salary) > %i 123 +INTO OUTFILE '/tmp/result''.txt' +FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' +LINES TERMINATED BY '\\n' +" + )), + $conn->translate('%sql', "SELECT DISTINCT HIGH_PRIORITY SQL_BUFFER_RESULT +CONCAT(last_name, \", \", first_name) AS full_name +GROUP BY [user] +HAVING MAX(salary) > %i", 123, " +INTO OUTFILE '/tmp/result''.txt' +FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' +LINES TERMINATED BY '\\n' +") +); + + + + + +$array1 = array(1, 2, 3); +$array2 = array('one', 'two', 'three'); +$array3 = array( + 'col1' => 'one', + 'col2' => 'two', + 'col3' => 'three', +); +$array4 = array( + 'a' => 12, + 'b' => NULL, + 'c' => new DibiDateTime('12.3.2007'), + 'd' => 'any string', +); + +$array5 = array('RAND()', '[col1] > [col2]'); + + +Assert::match( + reformat(array( + 'mysql' => "SELECT * +FROM `db`.`table` +WHERE (`test`.`a` LIKE '1995-03-01' + OR `b1` IN ( 1, 2, 3 ) + OR `b2` IN ('1', '2', '3' ) + OR `b3` IN ( ) + OR `b4` IN ( 'one', 'two', 'three' ) + OR `b5` IN (`col1` AS `one`, `col2` AS `two`, `col3` AS `three` ) + OR `b6` IN ('one', 'two', 'three') + OR `b7` IN (NULL) + OR `b8` IN (RAND() `col1` > `col2` ) + OR `b9` IN ( ) + AND `c` = 'embedded \' string' + OR `d`=10 + OR `e`=NULL + OR `true`= 1 + OR `false`= 0 + OR `str_null`=NULL + OR `str_not_null`='hello' +LIMIT 10", + 'pgsql' => 'SELECT * +FROM "db"."table" +WHERE ("test"."a" LIKE \'1995-03-01\' + OR "b1" IN ( 1, 2, 3 ) + OR "b2" IN (\'1\', \'2\', \'3\' ) + OR "b3" IN ( ) + OR "b4" IN ( \'one\', \'two\', \'three\' ) + OR "b5" IN ("col1" AS "one", "col2" AS "two", "col3" AS "three" ) + OR "b6" IN (\'one\', \'two\', \'three\') + OR "b7" IN (NULL) + OR "b8" IN (RAND() "col1" > "col2" ) + OR "b9" IN ( ) + AND "c" = \'embedded \'\' string\' + OR "d"=10 + OR "e"=NULL + OR "true"= TRUE + OR "false"= FALSE + OR "str_null"=NULL + OR "str_not_null"=\'hello\' +LIMIT 10', + 'odbc' => "SELECT * +FROM [db].[table] +WHERE ([test].[a] LIKE #03/01/1995# + OR [b1] IN ( 1, 2, 3 ) + OR [b2] IN ('1', '2', '3' ) + OR [b3] IN ( ) + OR [b4] IN ( 'one', 'two', 'three' ) + OR [b5] IN ([col1] AS [one], [col2] AS [two], [col3] AS [three] ) + OR [b6] IN ('one', 'two', 'three') + OR [b7] IN (NULL) + OR [b8] IN (RAND() [col1] > [col2] ) + OR [b9] IN ( ) + AND [c] = 'embedded '' string' + OR [d]=10 + OR [e]=NULL + OR [true]= 1 + OR [false]= 0 + OR [str_null]=NULL + OR [str_not_null]='hello' +LIMIT 10", + "SELECT * +FROM [db].[table] +WHERE ([test].[a] LIKE '1995-03-01' + OR [b1] IN ( 1, 2, 3 ) + OR [b2] IN ('1', '2', '3' ) + OR [b3] IN ( ) + OR [b4] IN ( 'one', 'two', 'three' ) + OR [b5] IN ([col1] AS [one], [col2] AS [two], [col3] AS [three] ) + OR [b6] IN ('one', 'two', 'three') + OR [b7] IN (NULL) + OR [b8] IN (RAND() [col1] > [col2] ) + OR [b9] IN ( ) + AND [c] = 'embedded '' string' + OR [d]=10 + OR [e]=NULL + OR [true]= 1 + OR [false]= 0 + OR [str_null]=NULL + OR [str_not_null]='hello' +LIMIT 10", + )), + + $conn->translate("SELECT * +FROM [db.table] +WHERE ([test.a] LIKE %d", '1995-03-01', " + OR [b1] IN (", $array1, ") + OR [b2] IN (%s", $array1, ") + OR [b3] IN (%s", array(), ") + OR [b4] IN (", $array2, ") + OR [b5] IN (%n", $array3, ") + OR [b6] IN %l", $array3, " + OR [b7] IN %in", array(), " + OR [b8] IN (%sql", $array5, ") + OR [b9] IN (", array(), ") + AND [c] = 'embedded '' string' + OR [d]=%i", 10.3, " + OR [e]=%i", NULL, " + OR [true]=", TRUE, " + OR [false]=", FALSE, " + OR [str_null]=%sn", '', " + OR [str_not_null]=%sn", 'hello', " +LIMIT 10") +); + + +Assert::same( + reformat('TEST [cond] > 2 [cond2] = \'3\' cond3 < RAND() 123'), + $conn->translate('TEST %ex', array('[cond] > 2', '[cond2] = "3"', 'cond3 < RAND()'), 123) +); + + +Assert::same( + reformat('TEST ([cond] > 2) OR ([cond2] > 3) OR ([cond3] = 10 + 1)'), + $conn->translate('TEST %or', array('`cond` > 2', array('[cond2] > %i', '3'), 'cond3%sql' => array('10 + 1'))) +); + + +Assert::same( + reformat('TEST ([cond] = 2) OR ([cond3] = RAND())'), + $conn->translate('TEST %or', array('cond' => 2, 'cond3%sql' => 'RAND()')) +); + + +Assert::same( + reformat('TEST ([cond1] 3) OR ([cond2] RAND()) OR ([cond3] LIKE \'string\')'), + $conn->translate('TEST %or', array('cond1%ex' => 3, 'cond2%ex' => 'RAND()', 'cond3%ex' => array('LIKE %s', 'string'))) +); + + +Assert::same( + reformat(array( + 'odbc' => 'SELECT TOP 10 * FROM (SELECT * FROM [test] WHERE [id] LIKE \'%d%t\' ) t', + 'SELECT * FROM [test] WHERE [id] LIKE \'%d%t\' LIMIT 10', + )), + $conn->translate("SELECT * FROM [test] WHERE %n LIKE '%d%t' %lmt", 'id', 10) +); + + +$where = array( + 'tablename.column' => 1, +); +Assert::same( + reformat('SELECT * FROM [tablename] WHERE ([tablename].[column] = 1)'), + $conn->translate('SELECT * FROM [tablename] WHERE %and', $where) +); + + +Assert::same( + reformat('SELECT FROM ... '), + $conn->translate('SELECT FROM ... %lmt', NULL) +); + +Assert::same( + reformat('SELECT \'%i\''), + $conn->translate("SELECT '%i'") +); + +Assert::same( + reformat('SELECT \'%i\''), + $conn->translate('SELECT "%i"') +); + + +Assert::same( + reformat('INSERT INTO [products] ([product_id], [title]) VALUES (1, SHA1(\'Test product\')) , (1, SHA1(\'Test product\'))'), + $conn->translate('INSERT INTO [products]', array( + 'product_id' => 1, + 'title' => array('SHA1(%s)', 'Test product'), +), array( + 'product_id' => 1, + 'title' => array('SHA1(%s)', 'Test product'), +)) +); + +Assert::same( + reformat('UPDATE [products] [product_id]=1, [title]=SHA1(\'Test product\')'), + $conn->translate('UPDATE [products]', array( + 'product_id' => 1, + 'title' => array('SHA1(%s)', 'Test product'), +)) +); + + +$e = Assert::exception(function() use ($conn) { + $array6 = array( + 'id' => array(1, 2, 3, 4), + 'text' => array('ahoj', 'jak', 'se', array('SUM(%i)', '5')), + 'num%i' => array('1', ''), + ); + $conn->translate('INSERT INTO test %m', $array6); +}, 'DibiException', 'SQL translate error'); +Assert::same('INSERT INTO test **Multi-insert array "num%i" is different.**', $e->getSql()); + +$array6 = array( + 'id' => array(1, 2, 3, 4), + 'text' => array('ahoj', 'jak', 'se', array('SUM(%i)', '5')), + 'num%i' => array('1', '', 10.3, 1), +); + +Assert::same( + reformat('INSERT INTO test ([id], [text], [num]) VALUES (1, \'ahoj\', 1), (2, \'jak\', 0), (3, \'se\', 10), (4, SUM(5), 1)'), + $conn->translate('INSERT INTO test %m', $array6) +); + + +$by = array ( + array('funkce(nazev_pole) ASC'), + 'jine_pole' => 'DESC' +); + +Assert::same( + reformat('SELECT * FROM table ORDER BY funkce(nazev_pole) ASC, [jine_pole] DESC'), + $conn->translate("SELECT * FROM table ORDER BY %by", $by) +); + +Assert::same( + reformat('INSERT INTO [test].*'), + $conn->translate('INSERT INTO [test.*]') +); + +Assert::same( + reformat('INSERT INTO 0'), + $conn->translate('INSERT INTO %f', 'ahoj') +); + + +setLocale(LC_ALL, 'czech'); + +Assert::same( + reformat("UPDATE [colors] SET [color]='blue', [price]=-12.4, [spec]=-9E-005, [spec2]=1000, [spec3]=10000, [spec4]=10000 WHERE [price]=123.5"), + + $conn->translate("UPDATE [colors] SET", array( + 'color' => 'blue', + 'price' => -12.4, + 'spec%f' => '-9E-005', + 'spec2%f' => 1000.00, + 'spec3%i' => 10000, + 'spec4' => 10000, +), "WHERE [price]=%f", 123.5) +); diff --git a/tests/dibi/bootstrap.php b/tests/dibi/bootstrap.php index 30fa8539..15a08683 100644 --- a/tests/dibi/bootstrap.php +++ b/tests/dibi/bootstrap.php @@ -29,6 +29,13 @@ define('TEMP_DIR', __DIR__ . '/../tmp'); Tester\Environment::lock($config['system'], TEMP_DIR); +// ODBC +if ($config['system'] === 'odbc') { + copy(__DIR__ . '/data/odbc.mdb', TEMP_DIR . '/odbc.mdb'); + $config['dsn'] = str_replace('data/odbc.mdb', TEMP_DIR . '/odbc.mdb', $config['dsn']); +} + + try { new DibiConnection($config); } catch (DibiNotSupportedException $e) { diff --git a/tests/dibi/data/mysql.sql b/tests/dibi/data/mysql.sql new file mode 100644 index 00000000..c91ca37f --- /dev/null +++ b/tests/dibi/data/mysql.sql @@ -0,0 +1,52 @@ +/*!40102 SET storage_engine = InnoDB */; + +DROP DATABASE IF EXISTS dibi_test; +CREATE DATABASE dibi_test; +USE dibi_test; + +DROP TABLE IF EXISTS `products`; +CREATE TABLE `products` ( + `product_id` int(11) NOT NULL AUTO_INCREMENT, + `title` varchar(100) DEFAULT NULL, + PRIMARY KEY (`product_id`), + KEY `title` (`title`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +INSERT INTO `products` (`product_id`, `title`) VALUES +(1, 'Chair'), +(3, 'Computer'), +(2, 'Table'); + +DROP TABLE IF EXISTS `customers`; +CREATE TABLE `customers` ( + `customer_id` int(11) NOT NULL AUTO_INCREMENT, + `name` varchar(100) DEFAULT NULL, + PRIMARY KEY (`customer_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +INSERT INTO `customers` (`customer_id`, `name`) VALUES +(1, 'Dave Lister'), +(2, 'Arnold Rimmer'), +(3, 'The Cat'), +(4, 'Holly'), +(5, 'Kryten'), +(6, 'Kristine Kochanski'); + +DROP TABLE IF EXISTS `orders`; +CREATE TABLE `orders` ( + `order_id` int(11) NOT NULL AUTO_INCREMENT, + `customer_id` int(11) NOT NULL, + `product_id` int(11) NOT NULL, + `amount` float NOT NULL, + PRIMARY KEY (`order_id`), + KEY `customer_id` (`customer_id`), + KEY `product_id` (`product_id`), + CONSTRAINT `orders_ibfk_4` FOREIGN KEY (`product_id`) REFERENCES `products` (`product_id`) ON UPDATE CASCADE, + CONSTRAINT `orders_ibfk_3` FOREIGN KEY (`customer_id`) REFERENCES `customers` (`customer_id`) ON UPDATE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +INSERT INTO `orders` (`order_id`, `customer_id`, `product_id`, `amount`) VALUES +(1, 2, 1, 7), +(2, 2, 3, 2), +(3, 1, 2, 3), +(4, 6, 3, 5); diff --git a/tests/dibi/data/odbc.mdb b/tests/dibi/data/odbc.mdb new file mode 100644 index 0000000000000000000000000000000000000000..8c8cd5aa778ae0d4acbd68388c3013d8e6ecc6e0 GIT binary patch literal 65536 zcmeI5U2I%O701tAUwgmrT{|I!kP-`3fDlcSv?^7FgcG|FSaux8i6Sdf8-J{0$FUu4 zoY1rdyUD8fxln!LTf1L5|I*c6FJJlP(_?>l&1)Zf z^RAAkj?eFUlqS4{qxINB)JoHotQI*!SBX{dnhVkA5?=;~W1?Z+rf=xtAWF zc<}50e)Y?@KXKycJ3iX;o4^0Me6^aswEx+MwiK?ub;sy$54VZjy>k~v!bAcjKmsH{ z0wh2JBtQZrKmsJN76cpz?jEoAECI(E_toFml#(C$=lr0|oCHXK1W14cNPq-LfCNZ@ z1W14cNMJn?sIDrdoXdHg%{EtL7<=xSaB5LZAe!k=yX2)EAp#8(Afc&)TT`TxEEm*P zR$5S(h2;*FSu#P!Y}5-fs^Qw%0~Ktt);SnAL2HAc0|Hky(y^&PV;gMbRFFd<*?EsF zIi?Yeqt=J^npce?j3qU+p$SSbmK3}22Z+WPG)hHWlZL! zBt0^MpCdAg`wVI~hc#uitl+guleI_Ww2X(fIoU6#P(A_8Ehx{K#uQ%9$^q2RK+~1a zqP;79at^&~e!n~fn-$1Q_PQT><8l_3%2KeJgOJXlpG<5eCj;_18+8R*2cfHDn}r>< zzC{k9FD=i?1T_0_&teo?8|sH-5|Rp9X5_G(M-OHB65a&}2d2cqa#pLemQPuZm-?>+ zmikuF^qA$U1FdAEhH^V}qFc*uRdrGUJM+mjsfJZ`+Fkf(76!->bh0tt`+36KB@kN^pg011%5 z1|X1fp7V4X1MoZ4>nMeDGB+eKzae$a1NU4-9ca1wvQd2D_B}`OyI_c0cwKl#~2#-;{~QV z%>UO7=_y1K&LS3|+ugcJecCpe&tP+V$~KfU@pgCPMs_aVsNRjk1JTy(o@JsFx{+N$ zltSYe;Wl#9?(@SIs+d6j0U*}H2xVwIrqJsaEfJxxb@*^oEJPz4^)qXg_rvB^+>Blr zP-Y}R0wh2JBtQZrKmsH{0wl0838b7~6m({H;Hdr${Qp+?|9YTb?``lGKZyHg(u(H- zOrTF*NV@-e9Nz>rFF!i19=iV;AP6)_4xXj zdG>lFKhj&dRnpe>*p?o@pAX{`XK=iI3Q>z2A5Y(owY1NHVB%l2$*T^6Hx_x&&_MIQ z+n{6bojM(SkfFJ0T=OC5SiC9LiGoHJuxL18p)y@-i_$D3vw>>XAp3t<-#kT3f{6r3 zfCNZ@1W14cNPq-LfCO$`0**5reUw>t*z^B(`)_;S^G3bfyw{6gD;_9ji$5zY6z(hh zGyi;kI{%UUpK}*;M{^yyUuB=nc4wC|Kg`T$?#%o({hip(txJO=Cjk;50TLjAbw{9c zEk+c5zHe2wqEGy-%E&)rV|K(GsNS7Yz$A22|Cm(+%7o#WGE#IEC>yE&}S23i8f!>+kzTWA< zY5f!CIYH&r-~+ug!{ejVM~5c@K`IcS?E_&`(Hdvr`1HBy-u|$zwN^LYcWkhKCQx>3 z)su6BQ^R5Fwpw*_hiMh2%=|!~jZF`iFp&TWkN^pg011!)36KB@kN^p+JptEweMSF2 z)Pdz;B5P1>3wwO|ZF-La_Odf@fRzC}gyt25qNb3l>n&f**zt z{A^UvXBRS&011!)36KB@kN^pg011!)3A_sgT&L566UamYBtQZrKmsH{0wh2JBtQZr zKmsJNfeEPpk1by&5+DH*AOR8}0TLhq5+DH*AOR8}felQ6{{IGETuzMyNPq-LfCNZ@ z1W14cNPq-LfCNGU^#A!*AOR8}0TLhq5+DH*AOR8}0TLjAjZeUFJ`+FxKO-;t<9^=% zu{Z5)_bwGDiW`6aoFNI2011!)36KB@kN^pg012!ufp&eYwtkPA??~UVYuzo6ztI)_ zgkZ5>hUC1Qkwa3JQ!*m+G9_~|i<(K4PUGk06Ft&=?UPQ)OI4&@pX7ZL9Sq7@bWwrP zvKhTsny-6mM)45un=rapy5)W|w?~?F8@kRY+;}5+DH*AOR8}0TLhq5+DH*AOR9s2LxREDza+Tm&@gXFCVWP dYC|ja^&46RPwexOJ?c=U$$Vp#PP?wv{{Wa-3e*4q literal 0 HcmV?d00001 diff --git a/tests/dibi/data/odbc.sql b/tests/dibi/data/odbc.sql new file mode 100644 index 00000000..86669f5c --- /dev/null +++ b/tests/dibi/data/odbc.sql @@ -0,0 +1,32 @@ +CREATE TABLE products ( + product_id COUNTER, + title TEXT(50) +); + +INSERT INTO products (product_id, title) VALUES (1, 'Chair'); +INSERT INTO products (product_id, title) VALUES (2, 'Table'); +INSERT INTO products (product_id, title) VALUES (3, 'Computer'); + +CREATE TABLE [customers] ( + [customer_id] COUNTER, + [name] TEXT(50) +); + +INSERT INTO `customers` (`customer_id`, `name`) VALUES (1, 'Dave Lister'); +INSERT INTO `customers` (`customer_id`, `name`) VALUES (2, 'Arnold Rimmer'); +INSERT INTO `customers` (`customer_id`, `name`) VALUES (3, 'The Cat'); +INSERT INTO `customers` (`customer_id`, `name`) VALUES (4, 'Holly'); +INSERT INTO `customers` (`customer_id`, `name`) VALUES (5, 'Kryten'); +INSERT INTO `customers` (`customer_id`, `name`) VALUES (6, 'Kristine Kochanski'); + +CREATE TABLE [orders] ( + [order_id] INTEGER, + [customer_id] INTEGER, + [product_id] INTEGER, + [amount] FLOAT +); + +INSERT INTO `orders` (`order_id`, `customer_id`, `product_id`, `amount`) VALUES (1, 2, 1, 7); +INSERT INTO `orders` (`order_id`, `customer_id`, `product_id`, `amount`) VALUES (2, 2, 3, 2); +INSERT INTO `orders` (`order_id`, `customer_id`, `product_id`, `amount`) VALUES (3, 1, 2, 3); +INSERT INTO `orders` (`order_id`, `customer_id`, `product_id`, `amount`) VALUES (4, 6, 3, 5); diff --git a/tests/dibi/data/pgsql.sql b/tests/dibi/data/pgsql.sql new file mode 100644 index 00000000..9ca52a79 --- /dev/null +++ b/tests/dibi/data/pgsql.sql @@ -0,0 +1,52 @@ +SET client_encoding = 'UTF8'; +DROP SCHEMA IF EXISTS public CASCADE; +CREATE SCHEMA public; + +CREATE TABLE products ( + product_id serial NOT NULL, + title varchar(100) DEFAULT NULL, + PRIMARY KEY (product_id) +); + +INSERT INTO products (product_id, title) VALUES +(1, 'Chair'), +(2, 'Table'), +(3, 'Computer'); +SELECT setval('products_product_id_seq', 3, TRUE); + +CREATE INDEX title ON products USING btree (title); + +CREATE TABLE customers ( + customer_id serial NOT NULL, + name varchar(100) DEFAULT NULL, + PRIMARY KEY (customer_id) +); + +INSERT INTO customers (customer_id, name) VALUES +(1, 'Dave Lister'), +(2, 'Arnold Rimmer'), +(3, 'The Cat'), +(4, 'Holly'), +(5, 'Kryten'), +(6, 'Kristine Kochanski'); +SELECT setval('customers_customer_id_seq', 6, TRUE); + +CREATE TABLE orders ( + order_id serial NOT NULL, + customer_id integer NOT NULL, + product_id integer NOT NULL, + amount real NOT NULL +); + +INSERT INTO orders (order_id, customer_id, product_id, amount) VALUES +(1, 2, 1, 7), +(2, 2, 3, 2), +(3, 1, 2, 3), +(4, 6, 3, 5); +SELECT setval('orders_order_id_seq', 4, TRUE); + +ALTER TABLE ONLY orders + ADD CONSTRAINT orders_customer_id_fkey FOREIGN KEY (customer_id) REFERENCES customers(customer_id) ON UPDATE CASCADE ON DELETE RESTRICT; + +ALTER TABLE ONLY orders + ADD CONSTRAINT orders_product_id_fkey FOREIGN KEY (product_id) REFERENCES products(product_id) ON UPDATE CASCADE ON DELETE RESTRICT; diff --git a/tests/dibi/data/sqlite.sql b/tests/dibi/data/sqlite.sql new file mode 100644 index 00000000..b736ed29 --- /dev/null +++ b/tests/dibi/data/sqlite.sql @@ -0,0 +1,34 @@ +CREATE TABLE [products] ( + [product_id] INTEGER NOT NULL PRIMARY KEY, + [title] VARCHAR(100) NULL +); + +CREATE INDEX "title" ON "products" ("title"); + +INSERT INTO "products" ("product_id", "title") VALUES (1, 'Chair'); +INSERT INTO "products" ("product_id", "title") VALUES (2, 'Table'); +INSERT INTO "products" ("product_id", "title") VALUES (3, 'Computer'); + +CREATE TABLE [customers] ( + [customer_id] INTEGER PRIMARY KEY NOT NULL, + [name] VARCHAR(100) NULL +); + +INSERT INTO "customers" ("customer_id", "name") VALUES (1, 'Dave Lister'); +INSERT INTO "customers" ("customer_id", "name") VALUES (2, 'Arnold Rimmer'); +INSERT INTO "customers" ("customer_id", "name") VALUES (3, 'The Cat'); +INSERT INTO "customers" ("customer_id", "name") VALUES (4, 'Holly'); +INSERT INTO "customers" ("customer_id", "name") VALUES (5, 'Kryten'); +INSERT INTO "customers" ("customer_id", "name") VALUES (6, 'Kristine Kochanski'); + +CREATE TABLE [orders] ( + [order_id] INTEGER NOT NULL PRIMARY KEY, + [customer_id] INTEGER NOT NULL, + [product_id] INTEGER NOT NULL, + [amount] FLOAT NOT NULL +); + +INSERT INTO "orders" ("order_id", "customer_id", "product_id", "amount") VALUES (1, 2, 1, '7.0'); +INSERT INTO "orders" ("order_id", "customer_id", "product_id", "amount") VALUES (2, 2, 3, '2.0'); +INSERT INTO "orders" ("order_id", "customer_id", "product_id", "amount") VALUES (3, 1, 2, '3.0'); +INSERT INTO "orders" ("order_id", "customer_id", "product_id", "amount") VALUES (4, 6, 3, '5.0'); diff --git a/tests/dibi/meta.phpt b/tests/dibi/meta.phpt new file mode 100644 index 00000000..88454961 --- /dev/null +++ b/tests/dibi/meta.phpt @@ -0,0 +1,75 @@ +loadFile(__DIR__ . "/data/$config[system].sql"); + + +$meta = $conn->getDatabaseInfo(); + +Assert::same(3, count($meta->getTables())); + +$names = $meta->getTableNames(); +sort($names); +Assert::equal(array('customers', 'orders', 'products'), $names); + +Assert::false($meta->hasTable('xxxx')); + +$table = $meta->getTable('products'); +Assert::same('products', $table->name); +Assert::false($table->isView()); + +Assert::same(2, count($table->getColumns())); +Assert::false($table->hasColumn('xxxx')); +Assert::true($table->hasColumn('product_id')); +Assert::true($table->hasColumn('Product_id')); +Assert::same('product_id', $table->getColumn('Product_id')->name); + +$column = $table->getColumn('product_id'); +Assert::same('product_id', $column->name); +Assert::same('products', $column->table->name); +Assert::same('i', $column->type); +Assert::type('string', $column->nativeType); +Assert::false($column->nullable); +Assert::true($column->autoIncrement); + +$column = $table->getColumn('title'); +Assert::same('title', $column->name); +Assert::same('products', $column->table->name); +Assert::same('s', $column->type); +Assert::type('string', $column->nativeType); +Assert::same(100, $column->size); +Assert::true($column->nullable); +Assert::false($column->autoIncrement); +//Assert::null($column->default); + + +$indexes = $table->getIndexes(); +$index = reset($indexes); + +if ($config['system'] !== 'sqlite') { + Assert::same(2, count($indexes)); + Assert::true($index->primary); + Assert::true($index->unique); + Assert::same(1, count($index->getColumns())); + Assert::same('product_id', $index->columns[0]->name); + + $index = next($indexes); +} + +Assert::same('title', $index->name); +Assert::false($index->primary); +Assert::false($index->unique); +Assert::same(1, count($index->getColumns())); +Assert::same('title', $index->columns[0]->name);