public function testExplainPlanFromObject() { BookstoreDataPopulator::depopulate($this->con); BookstoreDataPopulator::populate($this->con); $db = Propel::getDb(BookPeer::DATABASE_NAME); $c = new ModelCriteria('bookstore', 'Book'); $c->join('Book.Author'); $c->where('Author.FirstName = ?', 'Neal'); $c->select('Title'); $explain = $c->explain($this->con); if ($db instanceof DBMySQL) { $this->assertEquals(sizeof($explain), 2, 'Explain plan return two lines'); // explain can change sometime, test can't be strict $this->assertArrayHasKey('select_type', $explain[0], 'Line 1, select_type key exist'); $this->assertArrayHasKey('table', $explain[0], 'Line 1, table key exist'); $this->assertArrayHasKey('type', $explain[0], 'Line 1, type key exist'); $this->assertArrayHasKey('possible_keys', $explain[0], 'Line 1, possible_keys key exist'); $this->assertArrayHasKey('select_type', $explain[1], 'Line 2, select_type key exist'); $this->assertArrayHasKey('table', $explain[1], 'Line 2, table key exist'); $this->assertArrayHasKey('type', $explain[1], 'Line 2, type key exist'); $this->assertArrayHasKey('possible_keys', $explain[1], 'Line 2, possible_keys key exist'); } elseif ($db instanceof DBOracle) { $this->assertTrue(sizeof($explain) > 2, 'Explain plan return more than 2 lines'); } else { $this->markTestSkipped('Cannot test explain plan on adapter ' . get_class($db)); } }
public static function getChartDatasetData(\ModelCriteria $q) { $data = array('raw' => array(), 'daily' => array(), 'weekly' => array(), 'monthly' => array()); $cache = self::getGemCache(); $rates = $q->select(array('rateDatetime', 'rate'))->filterByRateDatetime(date("Y-m-d 00:00:00", strtotime("-1 week")), \Criteria::GREATER_EQUAL)->find(); /* * use these 3 arrays to maintain the values over which we calculate the moving average * every value is added to the array, but we pop off values older then the specified threshold (day, week, month) */ $dailyValues = array(); $weeklyValues = array(); $monthlyValues = array(); foreach ($rates as $rateEntry) { $date = new DateTime("{$rateEntry['rateDatetime']}"); $date->setTimezone(new DateTimeZone('UTC')); $timestamp = $date->getTimestamp(); $dailyValues[$timestamp] = $rateEntry['rate']; $weeklyValues[$timestamp] = $rateEntry['rate']; $monthlyValues[$timestamp] = $rateEntry['rate']; $rateEntry['rate'] = round($rateEntry['rate'], 2); foreach ($dailyValues as $keyTimestamp => $value) { if ($timestamp - $keyTimestamp > 86400) { unset($dailyValues[$keyTimestamp]); } else { break; } } foreach ($weeklyValues as $keyTimestamp => $value) { if ($timestamp - $keyTimestamp > 604800) { unset($weeklyValues[$keyTimestamp]); } else { break; } } foreach ($monthlyValues as $keyTimestamp => $value) { if ($timestamp - $keyTimestamp > 18144000) { unset($monthlyValues[$keyTimestamp]); } else { break; } } $data['raw'][] = array($timestamp * 1000, $rateEntry['rate']); $data['daily'][] = array($timestamp * 1000, round(array_sum($dailyValues) / count($dailyValues), 2)); $data['weekly'][] = array($timestamp * 1000, round(array_sum($weeklyValues) / count($weeklyValues), 2)); $data['monthly'][] = array($timestamp * 1000, round(array_sum($monthlyValues) / count($monthlyValues), 2)); } return $data; }
public function testSelectArrayWithColumn() { BookstoreDataPopulator::depopulate($this->con); BookstoreDataPopulator::populate($this->con); $c = new ModelCriteria('bookstore', 'Book'); $c->join('Book.Author'); $c->withColumn('LOWER(Book.Title)', 'LowercaseTitle'); $c->select(array('LowercaseTitle', 'Book.Title')); $c->orderBy('Book.Title'); $rows = $c->find($this->con); $expectedSQL = 'SELECT LOWER(book.TITLE) AS LowercaseTitle, book.TITLE AS "Book.Title" FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID) ORDER BY book.TITLE ASC'; $this->assertEquals($expectedSQL, $this->con->getLastExecutedQuery(), 'find() called after select(array) can cope with a column added with withColumn()'); $expectedRows = array(array('LowercaseTitle' => 'don juan', 'Book.Title' => 'Don Juan'), array('LowercaseTitle' => 'harry potter and the order of the phoenix', 'Book.Title' => 'Harry Potter and the Order of the Phoenix'), array('LowercaseTitle' => 'quicksilver', 'Book.Title' => 'Quicksilver'), array('LowercaseTitle' => 'the tin drum', 'Book.Title' => 'The Tin Drum')); $this->assertEquals(serialize($rows->getData()), serialize($expectedRows), 'find() called after select(array) can cope with a column added with withColumn()'); }
public function testGetSelectReturnsArrayWhenSelectingAllColumns() { $c = new ModelCriteria('bookstore', 'Book'); $c->select('*'); $this->assertEquals(array('Book.Id', 'Book.Title', 'Book.ISBN', 'Book.Price', 'Book.PublisherId', 'Book.AuthorId'), $c->getSelect()); }
public function testWithColumnAndSelect() { $c = new ModelCriteria('bookstore', 'Author'); $c->join('Book'); $c->withColumn('COUNT(Book.Id)', 'NbBooks'); $c->select(array('FirstName', 'LastName')); $collection = $c->find(); $this->assertThat($collection, $this->isInstanceOf('PropelCollection')); foreach ($collection as $array) { $this->assertArrayHasKey('FirstName', $array); $this->assertArrayHasKey('LastName', $array); $this->assertArrayHasKey('NbBooks', $array); } }
public function testQuotingAliases() { $c = new ModelCriteria('bookstore', 'Book'); $c->withColumn('book.title', 'Book Title (*.)'); $c->select('Book Title (*.)'); $c->where('Book.Isbn = ?', '0380977427'); $title = $c->findOne(); $expectedSQL = "SELECT book.title AS `Book Title (*.)` FROM `book` WHERE book.isbn = '0380977427' LIMIT 1"; $this->assertEquals($expectedSQL, $this->con->getLastExecutedQuery()); $this->assertEquals('Quicksilver', $title); }