diff options
author | tag <[email protected]> | 2012-11-23 22:51:33 -0500 |
---|---|---|
committer | tag <[email protected]> | 2012-11-23 22:51:33 -0500 |
commit | 7c95314624d427e99a5213e2868d305f421b6962 (patch) | |
tree | de4ba72e89a9177582f3a824260f094ee9f87a6f /test/test_queries.php | |
parent | 13d291f4442d268ba170b6635e44cd11d3f1049b (diff) |
Multiple connection support for Idiorm
Mutiple connections code, including documentation and unit tests.
Utilizes key names to distinguish connections, but uses a default
connection if none specified. I don't (yet) use multiple connections in
my work (it's pending), so this has not been tested "in the wild".
Added unit tests with additional connections, ran unit tests for Paris
against this build too, so an unmodified Paris is forward-compatible
with this commit (mulitple connections support not yet coded for Paris).
Does *NOT* add support for queries across multiple connections. (I
don't even want to go there ...)
##### Edge-case compatibility breaks:
* ORM::_setup_identifier_quote_character visibility was changed to
protected (which was likely original intent, judging by prefixed name)
* May break compatibility if ORM has been extended, **and** subclasses
directly utilize `::_config`, `::_db`, `::_query_log`, or
`::_query_cache` instead of using pre-existing accessor methods. (Paris
does not do this; all Paris tests pass)
* Re-use of `Tester` class outside of Idiorm repo, as
`Tester::check_equal()` was renamed to `Tester::check_equal_query()`
##### Other notes
New method: `ORM::get_connection_keys()`.
New `Tester` method: `Tester::check_equal_string()`.
TODO: Consider adding methods to get (connection-specific)
configuration info.
Diffstat (limited to 'test/test_queries.php')
-rwxr-xr-x | test/test_queries.php | 199 |
1 files changed, 124 insertions, 75 deletions
diff --git a/test/test_queries.php b/test/test_queries.php index 8cc26d6..a16194e 100755 --- a/test/test_queries.php +++ b/test/test_queries.php @@ -18,229 +18,229 @@ ORM::for_table('widget')->find_many(); $expected = "SELECT * FROM `widget`"; - Tester::check_equal("Basic unfiltered find_many query", $expected); + Tester::check_equal_query("Basic unfiltered find_many query", $expected); ORM::for_table('widget')->find_one(); $expected = "SELECT * FROM `widget` LIMIT 1"; - Tester::check_equal("Basic unfiltered find_one query", $expected); + Tester::check_equal_query("Basic unfiltered find_one query", $expected); ORM::for_table('widget')->where_id_is(5)->find_one(); $expected = "SELECT * FROM `widget` WHERE `id` = '5' LIMIT 1"; - Tester::check_equal("where_id_is method", $expected); + Tester::check_equal_query("where_id_is method", $expected); ORM::for_table('widget')->find_one(5); $expected = "SELECT * FROM `widget` WHERE `id` = '5' LIMIT 1"; - Tester::check_equal("Filtering on ID passed into find_one method", $expected); + Tester::check_equal_query("Filtering on ID passed into find_one method", $expected); ORM::for_table('widget')->count(); $expected = "SELECT COUNT(*) AS `count` FROM `widget` LIMIT 1"; - Tester::check_equal("COUNT query", $expected); + Tester::check_equal_query("COUNT query", $expected); ORM::for_table('person')->max('height'); $expected = "SELECT MAX(`height`) AS `max` FROM `person` LIMIT 1"; - Tester::check_equal("MAX query", $expected); + Tester::check_equal_query("MAX query", $expected); ORM::for_table('person')->min('height'); $expected = "SELECT MIN(`height`) AS `min` FROM `person` LIMIT 1"; - Tester::check_equal("MIN query", $expected); + Tester::check_equal_query("MIN query", $expected); ORM::for_table('person')->avg('height'); $expected = "SELECT AVG(`height`) AS `avg` FROM `person` LIMIT 1"; - Tester::check_equal("AVG query", $expected); + Tester::check_equal_query("AVG query", $expected); ORM::for_table('person')->sum('height'); $expected = "SELECT SUM(`height`) AS `sum` FROM `person` LIMIT 1"; - Tester::check_equal("SUM query", $expected); + Tester::check_equal_query("SUM query", $expected); ORM::for_table('widget')->where('name', 'Fred')->find_one(); $expected = "SELECT * FROM `widget` WHERE `name` = 'Fred' LIMIT 1"; - Tester::check_equal("Single where clause", $expected); + Tester::check_equal_query("Single where clause", $expected); ORM::for_table('widget')->where('name', 'Fred')->where('age', 10)->find_one(); $expected = "SELECT * FROM `widget` WHERE `name` = 'Fred' AND `age` = '10' LIMIT 1"; - Tester::check_equal("Multiple WHERE clauses", $expected); + Tester::check_equal_query("Multiple WHERE clauses", $expected); ORM::for_table('widget')->where_not_equal('name', 'Fred')->find_many(); $expected = "SELECT * FROM `widget` WHERE `name` != 'Fred'"; - Tester::check_equal("where_not_equal method", $expected); + Tester::check_equal_query("where_not_equal method", $expected); ORM::for_table('widget')->where_like('name', '%Fred%')->find_one(); $expected = "SELECT * FROM `widget` WHERE `name` LIKE '%Fred%' LIMIT 1"; - Tester::check_equal("where_like method", $expected); + Tester::check_equal_query("where_like method", $expected); ORM::for_table('widget')->where_not_like('name', '%Fred%')->find_one(); $expected = "SELECT * FROM `widget` WHERE `name` NOT LIKE '%Fred%' LIMIT 1"; - Tester::check_equal("where_not_like method", $expected); + Tester::check_equal_query("where_not_like method", $expected); ORM::for_table('widget')->where_in('name', array('Fred', 'Joe'))->find_many(); $expected = "SELECT * FROM `widget` WHERE `name` IN ('Fred', 'Joe')"; - Tester::check_equal("where_in method", $expected); + Tester::check_equal_query("where_in method", $expected); ORM::for_table('widget')->where_not_in('name', array('Fred', 'Joe'))->find_many(); $expected = "SELECT * FROM `widget` WHERE `name` NOT IN ('Fred', 'Joe')"; - Tester::check_equal("where_not_in method", $expected); + Tester::check_equal_query("where_not_in method", $expected); ORM::for_table('widget')->limit(5)->find_many(); $expected = "SELECT * FROM `widget` LIMIT 5"; - Tester::check_equal("LIMIT clause", $expected); + Tester::check_equal_query("LIMIT clause", $expected); ORM::for_table('widget')->limit(5)->offset(5)->find_many(); $expected = "SELECT * FROM `widget` LIMIT 5 OFFSET 5"; - Tester::check_equal("LIMIT and OFFSET clause", $expected); + Tester::check_equal_query("LIMIT and OFFSET clause", $expected); ORM::for_table('widget')->order_by_desc('name')->find_one(); $expected = "SELECT * FROM `widget` ORDER BY `name` DESC LIMIT 1"; - Tester::check_equal("ORDER BY DESC", $expected); + Tester::check_equal_query("ORDER BY DESC", $expected); ORM::for_table('widget')->order_by_asc('name')->find_one(); $expected = "SELECT * FROM `widget` ORDER BY `name` ASC LIMIT 1"; - Tester::check_equal("ORDER BY ASC", $expected); + Tester::check_equal_query("ORDER BY ASC", $expected); ORM::for_table('widget')->order_by_expr('SOUNDEX(`name`)')->find_one(); $expected = "SELECT * FROM `widget` ORDER BY SOUNDEX(`name`) LIMIT 1"; - Tester::check_equal("ORDER BY expression", $expected); + Tester::check_equal_query("ORDER BY expression", $expected); ORM::for_table('widget')->order_by_asc('name')->order_by_desc('age')->find_one(); $expected = "SELECT * FROM `widget` ORDER BY `name` ASC, `age` DESC LIMIT 1"; - Tester::check_equal("Multiple ORDER BY", $expected); + Tester::check_equal_query("Multiple ORDER BY", $expected); ORM::for_table('widget')->group_by('name')->find_many(); $expected = "SELECT * FROM `widget` GROUP BY `name`"; - Tester::check_equal("GROUP BY", $expected); + Tester::check_equal_query("GROUP BY", $expected); ORM::for_table('widget')->group_by('name')->group_by('age')->find_many(); $expected = "SELECT * FROM `widget` GROUP BY `name`, `age`"; - Tester::check_equal("Multiple GROUP BY", $expected); + Tester::check_equal_query("Multiple GROUP BY", $expected); ORM::for_table('widget')->group_by_expr("FROM_UNIXTIME(`time`, '%Y-%m')")->find_many(); $expected = "SELECT * FROM `widget` GROUP BY FROM_UNIXTIME(`time`, '%Y-%m')"; - Tester::check_equal("GROUP BY expression", $expected); + Tester::check_equal_query("GROUP BY expression", $expected); ORM::for_table('widget')->where('name', 'Fred')->limit(5)->offset(5)->order_by_asc('name')->find_many(); $expected = "SELECT * FROM `widget` WHERE `name` = 'Fred' ORDER BY `name` ASC LIMIT 5 OFFSET 5"; - Tester::check_equal("Complex query", $expected); + Tester::check_equal_query("Complex query", $expected); ORM::for_table('widget')->where_lt('age', 10)->where_gt('age', 5)->find_many(); $expected = "SELECT * FROM `widget` WHERE `age` < '10' AND `age` > '5'"; - Tester::check_equal("Less than and greater than", $expected); + Tester::check_equal_query("Less than and greater than", $expected); ORM::for_table('widget')->where_lte('age', 10)->where_gte('age', 5)->find_many(); $expected = "SELECT * FROM `widget` WHERE `age` <= '10' AND `age` >= '5'"; - Tester::check_equal("Less than or equal and greater than or equal", $expected); + Tester::check_equal_query("Less than or equal and greater than or equal", $expected); ORM::for_table('widget')->where_null('name')->find_many(); $expected = "SELECT * FROM `widget` WHERE `name` IS NULL"; - Tester::check_equal("where_null method", $expected); + Tester::check_equal_query("where_null method", $expected); ORM::for_table('widget')->where_not_null('name')->find_many(); $expected = "SELECT * FROM `widget` WHERE `name` IS NOT NULL"; - Tester::check_equal("where_not_null method", $expected); + Tester::check_equal_query("where_not_null method", $expected); ORM::for_table('widget')->where_raw('`name` = ? AND (`age` = ? OR `age` = ?)', array('Fred', 5, 10))->find_many(); $expected = "SELECT * FROM `widget` WHERE `name` = 'Fred' AND (`age` = '5' OR `age` = '10')"; - Tester::check_equal("Raw WHERE clause", $expected); + Tester::check_equal_query("Raw WHERE clause", $expected); ORM::for_table('widget')->where_raw('STRFTIME("%Y", "now") = ?', array(2012))->find_many(); $expected = "SELECT * FROM `widget` WHERE STRFTIME(\"%Y\", \"now\") = '2012'"; - Tester::check_equal("Raw WHERE clause with '%'", $expected); + Tester::check_equal_query("Raw WHERE clause with '%'", $expected); ORM::for_table('widget')->where_raw('`name` = "Fred"')->find_many(); $expected = "SELECT * FROM `widget` WHERE `name` = \"Fred\""; - Tester::check_equal("Raw WHERE clause with no parameters", $expected); + Tester::check_equal_query("Raw WHERE clause with no parameters", $expected); ORM::for_table('widget')->where('age', 18)->where_raw('(`name` = ? OR `name` = ?)', array('Fred', 'Bob'))->where('size', 'large')->find_many(); $expected = "SELECT * FROM `widget` WHERE `age` = '18' AND (`name` = 'Fred' OR `name` = 'Bob') AND `size` = 'large'"; - Tester::check_equal("Raw WHERE clause in method chain", $expected); + Tester::check_equal_query("Raw WHERE clause in method chain", $expected); ORM::for_table('widget')->raw_query('SELECT `w`.* FROM `widget` w')->find_many(); $expected = "SELECT `w`.* FROM `widget` w"; - Tester::check_equal("Raw query", $expected); + Tester::check_equal_query("Raw query", $expected); ORM::for_table('widget')->raw_query('SELECT `w`.* FROM `widget` w WHERE `name` = ? AND `age` = ?', array('Fred', 5))->find_many(); $expected = "SELECT `w`.* FROM `widget` w WHERE `name` = 'Fred' AND `age` = '5'"; - Tester::check_equal("Raw query with parameters", $expected); + Tester::check_equal_query("Raw query with parameters", $expected); ORM::for_table('widget')->select('name')->find_many(); $expected = "SELECT `name` FROM `widget`"; - Tester::check_equal("Simple result column", $expected); + Tester::check_equal_query("Simple result column", $expected); ORM::for_table('widget')->select('name')->select('age')->find_many(); $expected = "SELECT `name`, `age` FROM `widget`"; - Tester::check_equal("Multiple simple result columns", $expected); + Tester::check_equal_query("Multiple simple result columns", $expected); ORM::for_table('widget')->select('widget.name')->find_many(); $expected = "SELECT `widget`.`name` FROM `widget`"; - Tester::check_equal("Specify table name and column in result columns", $expected); + Tester::check_equal_query("Specify table name and column in result columns", $expected); ORM::for_table('widget')->select('widget.name', 'widget_name')->find_many(); $expected = "SELECT `widget`.`name` AS `widget_name` FROM `widget`"; - Tester::check_equal("Aliases in result columns", $expected); + Tester::check_equal_query("Aliases in result columns", $expected); ORM::for_table('widget')->select_expr('COUNT(*)', 'count')->find_many(); $expected = "SELECT COUNT(*) AS `count` FROM `widget`"; - Tester::check_equal("Literal expression in result columns", $expected); + Tester::check_equal_query("Literal expression in result columns", $expected); ORM::for_table('widget')->select_many(array('widget_name' => 'widget.name'), 'widget_handle')->find_many(); $expected = "SELECT `widget`.`name` AS `widget_name`, `widget_handle` FROM `widget`"; - Tester::check_equal("Aliases in select many result columns", $expected); + Tester::check_equal_query("Aliases in select many result columns", $expected); ORM::for_table('widget')->select_many_expr(array('count' => 'COUNT(*)'), 'SUM(widget_order)')->find_many(); $expected = "SELECT COUNT(*) AS `count`, SUM(widget_order) FROM `widget`"; - Tester::check_equal("Literal expression in select many result columns", $expected); + Tester::check_equal_query("Literal expression in select many result columns", $expected); ORM::for_table('widget')->join('widget_handle', array('widget_handle.widget_id', '=', 'widget.id'))->find_many(); $expected = "SELECT * FROM `widget` JOIN `widget_handle` ON `widget_handle`.`widget_id` = `widget`.`id`"; - Tester::check_equal("Simple join", $expected); + Tester::check_equal_query("Simple join", $expected); ORM::for_table('widget')->join('widget_handle', array('widget_handle.widget_id', '=', 'widget.id'))->find_one(5); $expected = "SELECT * FROM `widget` JOIN `widget_handle` ON `widget_handle`.`widget_id` = `widget`.`id` WHERE `widget`.`id` = '5' LIMIT 1"; - Tester::check_equal("Simple join with where_id_is method", $expected); + Tester::check_equal_query("Simple join with where_id_is method", $expected); ORM::for_table('widget')->inner_join('widget_handle', array('widget_handle.widget_id', '=', 'widget.id'))->find_many(); $expected = "SELECT * FROM `widget` INNER JOIN `widget_handle` ON `widget_handle`.`widget_id` = `widget`.`id`"; - Tester::check_equal("Inner join", $expected); + Tester::check_equal_query("Inner join", $expected); ORM::for_table('widget')->left_outer_join('widget_handle', array('widget_handle.widget_id', '=', 'widget.id'))->find_many(); $expected = "SELECT * FROM `widget` LEFT OUTER JOIN `widget_handle` ON `widget_handle`.`widget_id` = `widget`.`id`"; - Tester::check_equal("Left outer join", $expected); + Tester::check_equal_query("Left outer join", $expected); ORM::for_table('widget')->right_outer_join('widget_handle', array('widget_handle.widget_id', '=', 'widget.id'))->find_many(); $expected = "SELECT * FROM `widget` RIGHT OUTER JOIN `widget_handle` ON `widget_handle`.`widget_id` = `widget`.`id`"; - Tester::check_equal("Right outer join", $expected); + Tester::check_equal_query("Right outer join", $expected); ORM::for_table('widget')->full_outer_join('widget_handle', array('widget_handle.widget_id', '=', 'widget.id'))->find_many(); $expected = "SELECT * FROM `widget` FULL OUTER JOIN `widget_handle` ON `widget_handle`.`widget_id` = `widget`.`id`"; - Tester::check_equal("Full outer join", $expected); + Tester::check_equal_query("Full outer join", $expected); ORM::for_table('widget') ->join('widget_handle', array('widget_handle.widget_id', '=', 'widget.id')) ->join('widget_nozzle', array('widget_nozzle.widget_id', '=', 'widget.id')) ->find_many(); $expected = "SELECT * FROM `widget` JOIN `widget_handle` ON `widget_handle`.`widget_id` = `widget`.`id` JOIN `widget_nozzle` ON `widget_nozzle`.`widget_id` = `widget`.`id`"; - Tester::check_equal("Multiple join sources", $expected); + Tester::check_equal_query("Multiple join sources", $expected); ORM::for_table('widget')->table_alias('w')->find_many(); $expected = "SELECT * FROM `widget` `w`"; - Tester::check_equal("Main table alias", $expected); + Tester::check_equal_query("Main table alias", $expected); ORM::for_table('widget')->join('widget_handle', array('wh.widget_id', '=', 'widget.id'), 'wh')->find_many(); $expected = "SELECT * FROM `widget` JOIN `widget_handle` `wh` ON `wh`.`widget_id` = `widget`.`id`"; - Tester::check_equal("Join with alias", $expected); + Tester::check_equal_query("Join with alias", $expected); ORM::for_table('widget')->join('widget_handle', "widget_handle.widget_id = widget.id")->find_many(); $expected = "SELECT * FROM `widget` JOIN `widget_handle` ON widget_handle.widget_id = widget.id"; - Tester::check_equal("Join with string constraint", $expected); + Tester::check_equal_query("Join with string constraint", $expected); ORM::for_table('widget')->distinct()->select('name')->find_many(); $expected = "SELECT DISTINCT `name` FROM `widget`"; - Tester::check_equal("Select with DISTINCT", $expected); + Tester::check_equal_query("Select with DISTINCT", $expected); $widget = ORM::for_table('widget')->create(); $widget->name = "Fred"; $widget->age = 10; $widget->save(); $expected = "INSERT INTO `widget` (`name`, `age`) VALUES ('Fred', '10')"; - Tester::check_equal("Insert data", $expected); + Tester::check_equal_query("Insert data", $expected); $widget = ORM::for_table('widget')->create(); $widget->name = "Fred"; @@ -248,14 +248,14 @@ $widget->set_expr('added', 'NOW()'); $widget->save(); $expected = "INSERT INTO `widget` (`name`, `age`, `added`) VALUES ('Fred', '10', NOW())"; - Tester::check_equal("Insert data containing an expression", $expected); + Tester::check_equal_query("Insert data containing an expression", $expected); $widget = ORM::for_table('widget')->find_one(1); $widget->name = "Fred"; $widget->age = 10; $widget->save(); $expected = "UPDATE `widget` SET `name` = 'Fred', `age` = '10' WHERE `id` = '1'"; - Tester::check_equal("Update data", $expected); + Tester::check_equal_query("Update data", $expected); $widget = ORM::for_table('widget')->find_one(1); $widget->name = "Fred"; @@ -263,20 +263,20 @@ $widget->set_expr('added', 'NOW()'); $widget->save(); $expected = "UPDATE `widget` SET `name` = 'Fred', `age` = '10', `added` = NOW() WHERE `id` = '1'"; - Tester::check_equal("Update data containing an expression", $expected); + Tester::check_equal_query("Update data containing an expression", $expected); $widget = ORM::for_table('widget')->find_one(1); $widget->set(array("name" => "Fred", "age" => 10)); $widget->save(); $expected = "UPDATE `widget` SET `name` = 'Fred', `age` = '10' WHERE `id` = '1'"; - Tester::check_equal("Update multiple fields", $expected); + Tester::check_equal_query("Update multiple fields", $expected); $widget = ORM::for_table('widget')->find_one(1); $widget->set(array("name" => "Fred", "age" => 10)); $widget->set_expr(array("added" => "NOW()", "lat_long" => "GeomFromText('POINT(1.2347 2.3436)')")); $widget->save(); $expected = "UPDATE `widget` SET `name` = 'Fred', `age` = '10', `added` = NOW(), `lat_long` = GeomFromText('POINT(1.2347 2.3436)') WHERE `id` = '1'"; - Tester::check_equal("Update multiple fields containing an expression", $expected); + Tester::check_equal_query("Update multiple fields containing an expression", $expected); $widget = ORM::for_table('widget')->find_one(1); $widget->set(array("name" => "Fred", "age" => 10)); @@ -284,41 +284,72 @@ $widget->lat_long = 'unknown'; $widget->save(); $expected = "UPDATE `widget` SET `name` = 'Fred', `age` = '10', `added` = NOW(), `lat_long` = 'unknown' WHERE `id` = '1'"; - Tester::check_equal("Update multiple fields containing an expression (override previously set expression with plain value)", $expected); + Tester::check_equal_query("Update multiple fields containing an expression (override previously set expression with plain value)", $expected); $widget = ORM::for_table('widget')->find_one(1); $widget->delete(); $expected = "DELETE FROM `widget` WHERE `id` = '1'"; - Tester::check_equal("Delete data", $expected); + Tester::check_equal_query("Delete data", $expected); $widget = ORM::for_table('widget')->where_equal('age', 10)->delete_many(); $expected = "DELETE FROM `widget` WHERE `age` = '10'"; - Tester::check_equal("Delete many", $expected); + Tester::check_equal_query("Delete many", $expected); ORM::raw_execute("INSERT OR IGNORE INTO `widget` (`id`, `name`) VALUES (?, ?)", array(1, 'Tolstoy')); $expected = "INSERT OR IGNORE INTO `widget` (`id`, `name`) VALUES ('1', 'Tolstoy')"; - Tester::check_equal("Raw execute", $expected); // A bit of a silly test, as query is passed through + Tester::check_equal_query("Raw execute", $expected); // A bit of a silly test, as query is passed through + // Tests of muliple connections + define('ALTERNATE', 'alternate'); + ORM::set_db(new DummyDifferentPDO('sqlite::memory:'), 'alternate'); + ORM::configure('logging', true, ALTERNATE); + + $person1 = ORM::for_table('person')->find_one(); + $person2 = ORM::for_table('person', ALTERNATE)->find_one(); + $expected = "SELECT * FROM `person` LIMIT 1"; + + Tester::check_equal_string("Multiple connection (1)", $person1->name, 'Fred'); + Tester::check_equal_string("Multiple connection (2)", $person2->name, 'Steve'); + + $expectedToo = "SELECT * FROM `widget`"; + ORM::raw_execute("SELECT * FROM `widget`", array(), ALTERNATE); + + Tester::check_equal_string( + "Multiple connection log (1)", + ORM::get_last_query(ORM::DEFAULT_CONNECTION), + $expected + ); + Tester::check_equal_string( + "Multiple connection query log (2)", + ORM::get_last_query(), + $expectedToo + ); + Tester::check_equal_string( + "Multiple connection query log (3)", + ORM::get_last_query(ALTERNATE), + $expectedToo + ); + // Regression tests $widget = ORM::for_table('widget')->select('widget.*')->find_one(); $expected = "SELECT `widget`.* FROM `widget` LIMIT 1"; - Tester::check_equal("Issue #12 - incorrect quoting of column wildcard", $expected); + Tester::check_equal_query("Issue #12 - incorrect quoting of column wildcard", $expected); $widget = ORM::for_table('widget')->where_raw('username LIKE "ben%"')->find_many(); $expected = 'SELECT * FROM `widget` WHERE username LIKE "ben%"'; - Tester::check_equal('Issue #57 - _log_query method raises a warning when query contains "%"', $expected); + Tester::check_equal_query('Issue #57 - _log_query method raises a warning when query contains "%"', $expected); $widget = ORM::for_table('widget')->where_raw('comments LIKE "has been released?%"')->find_many(); $expected = 'SELECT * FROM `widget` WHERE comments LIKE "has been released?%"'; - Tester::check_equal('Issue #57 - _log_query method raises a warning when query contains "?"', $expected); + Tester::check_equal_query('Issue #57 - _log_query method raises a warning when query contains "?"', $expected); // Tests that alter Idiorm's config are done last ORM::configure('id_column', 'primary_key'); ORM::for_table('widget')->find_one(5); $expected = "SELECT * FROM `widget` WHERE `primary_key` = '5' LIMIT 1"; - Tester::check_equal("Setting: id_column", $expected); + Tester::check_equal_query("Setting: id_column", $expected); ORM::configure('id_column_overrides', array( 'widget' => 'widget_id', @@ -327,27 +358,27 @@ ORM::for_table('widget')->find_one(5); $expected = "SELECT * FROM `widget` WHERE `widget_id` = '5' LIMIT 1"; - Tester::check_equal("Setting: id_column_overrides, first test", $expected); + Tester::check_equal_query("Setting: id_column_overrides, first test", $expected); ORM::for_table('widget_handle')->find_one(5); $expected = "SELECT * FROM `widget_handle` WHERE `widget_handle_id` = '5' LIMIT 1"; - Tester::check_equal("Setting: id_column_overrides, second test", $expected); + Tester::check_equal_query("Setting: id_column_overrides, second test", $expected); ORM::for_table('widget_nozzle')->find_one(5); $expected = "SELECT * FROM `widget_nozzle` WHERE `primary_key` = '5' LIMIT 1"; - Tester::check_equal("Setting: id_column_overrides, third test", $expected); + Tester::check_equal_query("Setting: id_column_overrides, third test", $expected); ORM::for_table('widget')->use_id_column('new_id')->find_one(5); $expected = "SELECT * FROM `widget` WHERE `new_id` = '5' LIMIT 1"; - Tester::check_equal("Instance ID column, first test", $expected); + Tester::check_equal_query("Instance ID column, first test", $expected); ORM::for_table('widget_handle')->use_id_column('new_id')->find_one(5); $expected = "SELECT * FROM `widget_handle` WHERE `new_id` = '5' LIMIT 1"; - Tester::check_equal("Instance ID column, second test", $expected); + Tester::check_equal_query("Instance ID column, second test", $expected); ORM::for_table('widget_nozzle')->use_id_column('new_id')->find_one(5); $expected = "SELECT * FROM `widget_nozzle` WHERE `new_id` = '5' LIMIT 1"; - Tester::check_equal("Instance ID column, third test", $expected); + Tester::check_equal_query("Instance ID column, third test", $expected); // Test caching. This is a bit of a hack. ORM::configure('caching', true); @@ -355,7 +386,25 @@ ORM::for_table('widget')->where('name', 'Bob')->where('age', 42)->find_one(); $expected = ORM::get_last_query(); ORM::for_table('widget')->where('name', 'Fred')->where('age', 17)->find_one(); // this shouldn't run a query! - Tester::check_equal("Caching, same query not run twice", $expected); + Tester::check_equal_query("Caching, same query not run twice", $expected); + + // Test caching with multiple connections (also a bit of a hack) + ORM::configure('caching', true, ALTERNATE); + ORM::for_table('widget', ALTERNATE)->where('name', 'Steve')->where('age', 80)->find_one(); + ORM::for_table('widget', ALTERNATE)->where('name', 'Tom')->where('age', 120)->find_one(); + $expectedToo = ORM::get_last_query(); + ORM::for_table('widget', ALTERNATE)->where('name', 'Steve')->where('age', 80)->find_one(); // this shouldn't run a query! + Tester::check_equal_query( + "Multi-connection caching, same query not run twice, on alternate connection", + $expectedToo + ); + + ORM::for_table('widget')->where('name', 'Fred')->where('age', 17)->find_one(); // this still shouldn't run a query! + Tester::check_equal_string( + "Multi-conneciton caching, same query not run twice across connections", + ORM::get_last_query(ORM::DEFAULT_CONNECTION), + $expected + ); Tester::report(); |