From 75ec0c4791bfd453b76477fff9f2b4cc4e81ede2 Mon Sep 17 00:00:00 2001 From: torleif Date: Tue, 5 Aug 2014 14:46:06 +1200 Subject: [PATCH] Added tests that check comparisonClause() AS requested by tractorcow: https://github.com/silverstripe/silverstripe-postgresql/pull/26 Tested on MySQL (succeeds as expected) Tested on PostgreSQL (and fails as expected, but passes with patch). The fixes test only DataQueryTest related items for consistency, e.g. by avoiding DataObject calls. --- tests/model/DataQueryTest.php | 62 +++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/tests/model/DataQueryTest.php b/tests/model/DataQueryTest.php index f29d8a8d4..c97355369 100644 --- a/tests/model/DataQueryTest.php +++ b/tests/model/DataQueryTest.php @@ -10,6 +10,7 @@ class DataQueryTest extends SapphireTest { 'DataQueryTest_C', 'DataQueryTest_D', 'DataQueryTest_E', + 'DataQueryTest_F', ); @@ -165,6 +166,58 @@ class DataQueryTest extends SapphireTest { $query = $query->distinct(true); $this->assertContains('SELECT DISTINCT', $query->sql(), 'Query contains distinct'); + } + + public function testComparisonClauseInt() { + DB::query("INSERT INTO \"DataQueryTest_F\" (\"SortOrder\") VALUES (2)"); + $query = new DataQuery('DataQueryTest_F'); + $query->where(DB::getConn()->comparisonClause('"SortOrder"', '2')); + $this->assertGreaterThan(0, $query->count(), "Couldn't find SortOrder"); + $this->resetDBSchema(true); + } + + public function testComparisonClauseDateFull() { + DB::query("INSERT INTO \"DataQueryTest_F\" (\"MyDate\") VALUES ('1988-03-04 06:30')"); + $query = new DataQuery('DataQueryTest_F'); + $query->where(DB::getConn()->comparisonClause('"MyDate"', '1988-03-04%')); + $this->assertGreaterThan(0, $query->count(), "Couldn't find MyDate"); + $this->resetDBSchema(true); + } + + public function testComparisonClauseDateStartsWith() { + DB::query("INSERT INTO \"DataQueryTest_F\" (\"MyDate\") VALUES ('1988-03-04 06:30')"); + $query = new DataQuery('DataQueryTest_F'); + $query->where(DB::getConn()->comparisonClause('"MyDate"', '1988%')); + $this->assertGreaterThan(0, $query->count(), "Couldn't find MyDate"); + $this->resetDBSchema(true); + } + + public function testComparisonClauseDateStartsPartial() { + DB::query("INSERT INTO \"DataQueryTest_F\" (\"MyDate\") VALUES ('1988-03-04 06:30')"); + $query = new DataQuery('DataQueryTest_F'); + $query->where(DB::getConn()->comparisonClause('"MyDate"', '%03-04%')); + $this->assertGreaterThan(0, $query->count(), "Couldn't find MyDate"); + $this->resetDBSchema(true); + } + + public function testComparisonClauseTextCaseInsensitive() { + DB::query("INSERT INTO \"DataQueryTest_F\" (\"MyString\") VALUES ('HelloWorld')"); + $query = new DataQuery('DataQueryTest_F'); + $query->where(DB::getConn()->comparisonClause('"MyString"', 'helloworld')); + $this->assertGreaterThan(0, $query->count(), "Couldn't find MyString"); + $this->resetDBSchema(true); + } + + public function testComparisonClauseTextCaseSensitive() { + DB::query("INSERT INTO \"DataQueryTest_F\" (\"MyString\") VALUES ('HelloWorld')"); + $query = new DataQuery('DataQueryTest_F'); + $query->where(DB::getConn()->comparisonClause('"MyString"', 'HelloWorld', false, false, true)); + $this->assertGreaterThan(0, $query->count(), "Couldn't find MyString"); + + $query2 = new DataQuery('DataQueryTest_F'); + $query2->where(DB::getConn()->comparisonClause('"MyString"', 'helloworld', false, false, true)); + $this->assertEquals(0, $query2->count(), "Found mystring. Shouldn't be able too."); + $this->resetDBSchema(true); } } @@ -229,3 +282,12 @@ class DataQueryTest_E extends DataQueryTest_C implements TestOnly { private static $default_sort = '"DataQueryTest_E"."SortOrder" ASC'; } + +class DataQueryTest_F extends DataObject implements TestOnly { + + private static $db = array( + 'SortOrder' => 'Int', + 'MyDate' => 'SS_Datetime', + 'MyString' => 'Text' + ); +}