MINOR Using SQLQuery wrapper in DbDatetimeTest to provide better cross-db support, specifically correct LIMIT syntax in MSSQL (previously broken in a9b13509)

This commit is contained in:
Ingo Schommer 2011-05-19 11:38:43 +12:00
parent cfe0066a3b
commit 11b409aac8

View File

@ -54,8 +54,8 @@ class DbDatetimeTest extends FunctionalTest {
function testCorrectNow() {
if($this->supportDbDatetime) {
$query = 'SELECT ' . $this->adapter->formattedDatetimeClause('now', '%U');
$result = DB::query($query)->value();
$query = new SQLQuery($this->adapter->formattedDatetimeClause('now', '%U'));
$result = $query->execute()->value();
$this->assertRegExp('/^\d*$/', (string) $result);
$this->assertTrue($result>0);
}
@ -63,16 +63,16 @@ class DbDatetimeTest extends FunctionalTest {
function testDbDatetimeFormat() {
if($this->supportDbDatetime) {
$query = 'SELECT ' . $this->adapter->formattedDatetimeClause('1973-10-14 10:30:00', '%H:%i, %d/%m/%Y');
$result = DB::query($query)->value();
$query = new SQLQuery($this->adapter->formattedDatetimeClause('1973-10-14 10:30:00', '%H:%i, %d/%m/%Y'));
$result = $query->execute()->value();
$this->matchesRoughly($result, date('H:i, d/m/Y', strtotime('1973-10-14 10:30:00')), 'nice literal time');
$query = 'SELECT ' . $this->adapter->formattedDatetimeClause('now', '%d');
$result = DB::query($query)->value();
$query = new SQLQuery($this->adapter->formattedDatetimeClause('now', '%d'));
$result = $query->execute()->value();
$this->matchesRoughly($result, date('d', $this->getDbNow()), 'todays day');
$query = 'SELECT ' . $this->adapter->formattedDatetimeClause('"Created"', '%U') . ' AS test FROM "DbDateTimeTest_Team"';
$result = DB::query($query)->value();
$query = new SQLQuery($this->adapter->formattedDatetimeClause('"Created"', '%U') . ' AS test FROM "DbDateTimeTest_Team"');
$result = $query->execute()->value();
$this->matchesRoughly($result, strtotime(DataObject::get_one('DbDateTimeTest_Team')->Created), 'fixture ->Created as timestamp');
}
@ -81,16 +81,19 @@ class DbDatetimeTest extends FunctionalTest {
function testDbDatetimeInterval() {
if($this->supportDbDatetime) {
$query = 'SELECT ' . $this->adapter->datetimeIntervalClause('1973-10-14 10:30:00', '+18 Years');
$result = DB::query($query)->value();
$query = new SQLQuery($this->adapter->datetimeIntervalClause('1973-10-14 10:30:00', '+18 Years'));
$result = $query->execute()->value();
$this->matchesRoughly($result, '1991-10-14 10:30:00', 'add 18 years');
$query = 'SELECT ' . $this->adapter->datetimeIntervalClause('now', '+1 Day');
$result = DB::query($query)->value();
$query = new SQLQuery($this->adapter->datetimeIntervalClause('now', '+1 Day'));
$result = $query->execute()->value();
$this->matchesRoughly($result, date('Y-m-d H:i:s', strtotime('+1 Day', $this->getDbNow())), 'tomorrow');
$query = 'SELECT ' . $this->adapter->datetimeIntervalClause('"Created"', '-15 Minutes') . ' AS "test" FROM "DbDateTimeTest_Team" LIMIT 1';
$result = DB::query($query)->value();
$query = new SQLQuery();
$query->select($this->adapter->datetimeIntervalClause('"Created"', '-15 Minutes') . ' AS "test"')
->from("DbDateTimeTest_Team")
->limit(1);
$result = $query->execute()->value();
$this->matchesRoughly($result, date('Y-m-d H:i:s', strtotime(Dataobject::get_one('DbDateTimeTest_Team')->Created) - 900), '15 Minutes before creating fixture');
}
@ -99,20 +102,24 @@ class DbDatetimeTest extends FunctionalTest {
function testDbDatetimeDifference() {
if($this->supportDbDatetime) {
$query = 'SELECT ' . $this->adapter->datetimeDifferenceClause('1974-10-14 10:30:00', '1973-10-14 10:30:00');
$result = DB::query($query)->value();
$query = new SQLQuery($this->adapter->datetimeDifferenceClause('1974-10-14 10:30:00', '1973-10-14 10:30:00'));
$result = $query->execute()->value();
$this->matchesRoughly($result/86400, 365, '1974 - 1973 = 365 * 86400 sec');
$query = 'SELECT ' . $this->adapter->datetimeDifferenceClause(date('Y-m-d H:i:s', strtotime('-15 seconds')), 'now');
$result = DB::query($query)->value();
$query = new SQLQuery($this->adapter->datetimeDifferenceClause(date('Y-m-d H:i:s', strtotime('-15 seconds')), 'now'));
$result = $query->execute()->value();
$this->matchesRoughly($result, -15, '15 seconds ago - now');
$query = 'SELECT ' . $this->adapter->datetimeDifferenceClause('now', $this->adapter->datetimeIntervalClause('now', '+45 Minutes'));
$result = DB::query($query)->value();
$query = new SQLQuery($this->adapter->datetimeDifferenceClause('now', $this->adapter->datetimeIntervalClause('now', '+45 Minutes')));
$result = $query->execute()->value();
$this->matchesRoughly($result, -45 * 60, 'now - 45 minutes ahead');
$query = 'SELECT ' . $this->adapter->datetimeDifferenceClause('"LastEdited"', '"Created"') . ' AS "test" FROM "DbDateTimeTest_Team" LIMIT 1';
$result = DB::query($query)->value();
$query = new SQLQuery();
$query->select($this->adapter->datetimeDifferenceClause('"LastEdited"', '"Created"') . ' AS "test"')
->from("DbDateTimeTest_Team")
->limit(1);
$result = $query->execute()->value();
$lastedited = Dataobject::get_one('DbDateTimeTest_Team')->LastEdited;
$created = Dataobject::get_one('DbDateTimeTest_Team')->Created;
$this->matchesRoughly($result, strtotime($lastedited) - strtotime($created), 'age of HomePage record in seconds since unix epoc');