From 38c18b2d740f61e342f00bc33791f0f3c014e126 Mon Sep 17 00:00:00 2001 From: wei <> Date: Sun, 22 Apr 2007 00:28:36 +0000 Subject: Update to Active Record to use Mysql 4. Add TActiveRecordRelation --- .../ActiveRecord/ActiveRecordMySql5TestCase.php | 5 +- .../ActiveRecord/ForeignKeyTestCase.php | 114 +++++++++ .../ActiveRecord/RecordEventTestCase.php | 10 +- tests/simple_unit/ActiveRecord/blog.db | Bin 0 -> 4096 bytes tests/simple_unit/ActiveRecord/mysql4text.sql | 52 +++++ tests/simple_unit/DbCommon/Mysql4ColumnTest.php | 254 +++++++++++++++++++++ tests/simple_unit/DbCommon/MysqlColumnTest.php | 5 +- 7 files changed, 429 insertions(+), 11 deletions(-) create mode 100644 tests/simple_unit/ActiveRecord/ForeignKeyTestCase.php create mode 100644 tests/simple_unit/ActiveRecord/blog.db create mode 100644 tests/simple_unit/ActiveRecord/mysql4text.sql create mode 100644 tests/simple_unit/DbCommon/Mysql4ColumnTest.php (limited to 'tests') diff --git a/tests/simple_unit/ActiveRecord/ActiveRecordMySql5TestCase.php b/tests/simple_unit/ActiveRecord/ActiveRecordMySql5TestCase.php index b02de3de..b0d7ccf4 100644 --- a/tests/simple_unit/ActiveRecord/ActiveRecordMySql5TestCase.php +++ b/tests/simple_unit/ActiveRecord/ActiveRecordMySql5TestCase.php @@ -1,5 +1,4 @@ -setDbConnection($conn); } diff --git a/tests/simple_unit/ActiveRecord/ForeignKeyTestCase.php b/tests/simple_unit/ActiveRecord/ForeignKeyTestCase.php new file mode 100644 index 00000000..6c240b2f --- /dev/null +++ b/tests/simple_unit/ActiveRecord/ForeignKeyTestCase.php @@ -0,0 +1,114 @@ + Album.id + + public $Album = array(self::BELONGS_TO, 'Album'); + + public static function finder($class=__CLASS__) + { + return parent::finder($class); + } +} + +abstract class SqliteRecord extends TActiveRecord +{ + protected static $conn; + + public function getDbConnection() + { + if(self::$conn===null) + self::$conn = new TDbConnection('sqlite:'.dirname(__FILE__).'/blog.db'); + return self::$conn; + } +} + +class PostRecord extends SqliteRecord +{ + const TABLE='posts'; + public $post_id; + public $author; + public $create_time; + public $title; + public $content; + public $status; + + public $authorRecord = array(self::HAS_ONE, 'BlogUserRecord'); + + public static function finder($className=__CLASS__) + { + return parent::finder($className); + } +} +class BlogUserRecord extends SqliteRecord +{ + const TABLE='users'; + public $username; + public $email; + public $password; + public $role; + public $first_name; + public $last_name; + + public $posts = array(self::HAS_MANY, 'PostRecord'); + + public static function finder($className=__CLASS__) + { + return parent::finder($className); + } +} + +class ForeignKeyTestCase extends UnitTestCase +{ + function test() + { + $album = Album::finder()->withTracks()->findAll(); + //print_r($album); + //print_r(PostRecord::finder()->findAll()); + //print_r(BlogUserRecord::finder()->with_posts()->findAll()); + } +} + +?> \ No newline at end of file diff --git a/tests/simple_unit/ActiveRecord/RecordEventTestCase.php b/tests/simple_unit/ActiveRecord/RecordEventTestCase.php index fad54eb0..f6009608 100644 --- a/tests/simple_unit/ActiveRecord/RecordEventTestCase.php +++ b/tests/simple_unit/ActiveRecord/RecordEventTestCase.php @@ -9,13 +9,13 @@ class RecordEventTestCase extends UnitTestCase $conn = new TDbConnection('pgsql:host=localhost;dbname=test', 'test','test'); TActiveRecordManager::getInstance()->setDbConnection($conn); } -/* + function testFindByPk() { $user1 = UserRecord::finder()->findByPk('admin'); $this->assertNotNull($user1); } -*/ + function test_same_data_returns_same_object() { $criteria = new TActiveRecordCriteria('username = ?', 'admin'); @@ -23,14 +23,14 @@ class RecordEventTestCase extends UnitTestCase $finder->OnCreateCommand[] = array($this, 'logger'); $finder->OnExecuteCommand[] = array($this, 'logger'); $user1 = $finder->find($criteria); - var_dump($user1); + //var_dump($user1); - var_dump(UserRecord::finder()->find($criteria)); + //var_dump(UserRecord::finder()->find($criteria)); } function logger($sender, $param) { - var_dump($param); + //var_dump($param); } } diff --git a/tests/simple_unit/ActiveRecord/blog.db b/tests/simple_unit/ActiveRecord/blog.db new file mode 100644 index 00000000..30a9cb7a Binary files /dev/null and b/tests/simple_unit/ActiveRecord/blog.db differ diff --git a/tests/simple_unit/ActiveRecord/mysql4text.sql b/tests/simple_unit/ActiveRecord/mysql4text.sql new file mode 100644 index 00000000..4d61bc81 --- /dev/null +++ b/tests/simple_unit/ActiveRecord/mysql4text.sql @@ -0,0 +1,52 @@ +CREATE TABLE album ( + title varchar(100) NOT NULL default '', + PRIMARY KEY (title) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +CREATE TABLE artist ( + name varchar(25) NOT NULL default '', + PRIMARY KEY (name) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +CREATE TABLE album_artists ( + album_title varchar(100) NOT NULL default '', + artist_name varchar(25) NOT NULL default '', + PRIMARY KEY (album_title,artist_name), + KEY FK_album_artists_2 (artist_name) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +CREATE TABLE track ( + id int(11) NOT NULL auto_increment, + song_name varchar(200) NOT NULL default '', + album_id varchar(100) NOT NULL default '', + PRIMARY KEY (id), + KEY album_id (album_id) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +ALTER TABLE album_artists + ADD CONSTRAINT FK_album_artists_2 FOREIGN KEY (artist_name) REFERENCES artist (name), + ADD CONSTRAINT FK_album_artists_1 FOREIGN KEY (album_title) REFERENCES album (title); + +ALTER TABLE track + ADD CONSTRAINT track_ibfk_1 FOREIGN KEY (album_id) REFERENCES album (title); + + +INSERT INTO album (title) VALUES ('Album 1'); +INSERT INTO album (title) VALUES ('Album 2'); + +INSERT INTO artist (name) VALUES ('Dan'); +INSERT INTO artist (name) VALUES ('Jenny'); +INSERT INTO artist (name) VALUES ('Karl'); +INSERT INTO artist (name) VALUES ('Tom'); + +INSERT INTO album_artists (album_title, artist_name) VALUES ('Album 1', 'Dan'); +INSERT INTO album_artists (album_title, artist_name) VALUES ('Album 2', 'Dan'); +INSERT INTO album_artists (album_title, artist_name) VALUES ('Album 1', 'Jenny'); +INSERT INTO album_artists (album_title, artist_name) VALUES ('Album 2', 'Karl'); +INSERT INTO album_artists (album_title, artist_name) VALUES ('Album 2', 'Tom'); + +INSERT INTO track (id, song_name, album_id) VALUES (1, 'Track 1', 'Album 1'); +INSERT INTO track (id, song_name, album_id) VALUES (2, 'Song 2', 'Album 1'); +INSERT INTO track (id, song_name, album_id) VALUES (3, 'Track A', 'Album 2'); +INSERT INTO track (id, song_name, album_id) VALUES (4, 'Track B', 'Album 2'); +INSERT INTO track (id, song_name, album_id) VALUES (5, 'Song 3', 'Album 1'); \ No newline at end of file diff --git a/tests/simple_unit/DbCommon/Mysql4ColumnTest.php b/tests/simple_unit/DbCommon/Mysql4ColumnTest.php new file mode 100644 index 00000000..d69f3093 --- /dev/null +++ b/tests/simple_unit/DbCommon/Mysql4ColumnTest.php @@ -0,0 +1,254 @@ +create_meta_data()->getTableInfo('table1'); + $this->assertEqual(count($table->getColumns()), 18); + + $columns['id'] = array( + 'ColumnName' => '`id`', + 'ColumnSize' => 10, + 'ColumnIndex' => 0, + 'DbType' => 'int unsigned', + 'AllowNull' => false, + 'DefaultValue' => TDbTableColumn::UNDEFINED_VALUE, + 'NumericPrecision' => null, + 'NumericScale' => null, + 'IsPrimaryKey' => true, + 'IsForeignKey' => false, + 'SequenceName' => null, + 'AutoIncrement' => true, + ); + + $columns['name'] = array( + 'ColumnName' => '`name`', + 'ColumnSize' => 45, + 'ColumnIndex' => 1, + 'DbType' => 'varchar', + 'AllowNull' => false, + 'DefaultValue' => TDbTableColumn::UNDEFINED_VALUE, + 'NumericPrecision' => null, + 'NumericScale' => null, + 'IsPrimaryKey' => true, + 'IsForeignKey' => false, + 'SequenceName' => null, + 'AutoIncrement' => false, + ); + + $columns['field1'] = array( + 'ColumnName' => '`field1`', + 'ColumnSize' => 4, + 'ColumnIndex' => 2, + 'DbType' => 'tinyint', + 'AllowNull' => false, + 'DefaultValue' => '0', + 'NumericPrecision' => null, + 'NumericScale' => null, + 'IsPrimaryKey' => false, + 'IsForeignKey' => false, + 'SequenceName' => null, + 'AutoIncrement' => false, + ); + + $columns['field2_text'] = array( + 'ColumnName' => '`field2_text`', + 'ColumnSize' => null, + 'ColumnIndex' => 3, + 'DbType' => 'text', + 'AllowNull' => true, + 'DefaultValue' => TDbTableColumn::UNDEFINED_VALUE, + 'NumericPrecision' => null, + 'NumericScale' => null, + 'IsPrimaryKey' => false, + 'IsForeignKey' => false, + 'SequenceName' => null, + 'AutoIncrement' => false, + ); + + $columns['field3_date'] = array( + 'ColumnName' => '`field3_date`', + 'ColumnSize' => null, + 'ColumnIndex' => 4, + 'DbType' => 'date', + 'AllowNull' => true, + 'DefaultValue' => '2007-02-25', + 'NumericPrecision' => null, + 'NumericScale' => null, + 'IsPrimaryKey' => false, + 'IsForeignKey' => false, + 'SequenceName' => null, + 'AutoIncrement' => false, + ); + + $columns['field4_float'] = array( + 'ColumnName' => '`field4_float`', + 'ColumnSize' => null, + 'ColumnIndex' => 5, + 'DbType' => 'float', + 'AllowNull' => false, + 'DefaultValue' => 10, + 'NumericPrecision' => null, + 'NumericScale' => null, + 'IsPrimaryKey' => false, + 'IsForeignKey' => false, + 'SequenceName' => null, + 'AutoIncrement' => false, + ); + + $columns['field5_float'] = array( + 'ColumnName' => '`field5_float`', + 'ColumnSize' => null, + 'ColumnIndex' => 6, + 'DbType' => 'float', + 'AllowNull' => false, + 'DefaultValue' => '0.0000', + 'NumericPrecision' => 5, + 'NumericScale' => 4, + 'IsPrimaryKey' => false, + 'IsForeignKey' => false, + 'SequenceName' => null, + 'AutoIncrement' => false, + ); + + $columns['field6_double'] = array( + 'ColumnName' => '`field6_double`', + 'ColumnSize' => null, + 'ColumnIndex' => 7, + 'DbType' => 'double', + 'AllowNull' => false, + 'DefaultValue' => '0', + 'NumericPrecision' => null, + 'NumericScale' => null, + 'IsPrimaryKey' => false, + 'IsForeignKey' => false, + 'SequenceName' => null, + 'AutoIncrement' => false, + ); + + $columns['field7_datetime'] = array( + 'ColumnName' => '`field7_datetime`', + 'ColumnSize' => null, + 'ColumnIndex' => 8, + 'DbType' => 'datetime', + 'AllowNull' => false, + 'DefaultValue' => '0000-00-00 00:00:00', + 'NumericPrecision' => null, + 'NumericScale' => null, + 'IsPrimaryKey' => false, + 'IsForeignKey' => false, + 'SequenceName' => null, + 'AutoIncrement' => false, + ); + + $columns['field8_timestamp'] = array( + 'ColumnName' => '`field8_timestamp`', + 'ColumnSize' => null, + 'ColumnIndex' => 9, + 'DbType' => 'timestamp', + 'AllowNull' => true, + 'DefaultValue' => 'CURRENT_TIMESTAMP', + 'NumericPrecision' => null, + 'NumericScale' => null, + 'IsPrimaryKey' => false, + 'IsForeignKey' => false, + 'SequenceName' => null, + 'AutoIncrement' => false, + ); + + $columns['field9_time'] = array( + 'ColumnName' => '`field9_time`', + 'ColumnSize' => null, + 'ColumnIndex' => 10, + 'DbType' => 'time', + 'AllowNull' => false, + 'DefaultValue' => '00:00:00', + 'NumericPrecision' => null, + 'NumericScale' => null, + 'IsPrimaryKey' => false, + 'IsForeignKey' => false, + 'SequenceName' => null, + 'AutoIncrement' => false, + ); + + $columns['field10_year'] = array( + 'ColumnName' => '`field10_year`', + 'ColumnSize' => 4, + 'ColumnIndex' => 11, + 'DbType' => 'year', + 'AllowNull' => false, + 'DefaultValue' => '0000', + 'NumericPrecision' => null, + 'NumericScale' => null, + 'IsPrimaryKey' => false, + 'IsForeignKey' => false, + 'SequenceName' => null, + 'AutoIncrement' => false, + ); + + $columns['field11_enum'] = array( + 'ColumnName' => '`field11_enum`', + 'ColumnSize' => null, + 'ColumnIndex' => 12, + 'DbType' => 'enum', + 'AllowNull' => false, + 'DefaultValue' => 'one', + 'NumericPrecision' => null, + 'NumericScale' => null, + 'IsPrimaryKey' => false, + 'IsForeignKey' => false, + 'SequenceName' => null, + 'AutoIncrement' => false, + 'DbTypeValues' => array('one', 'two', 'three'), + ); + + $columns['field12_SET'] = array( + 'ColumnName' => '`field12_SET`', + 'ColumnSize' => null, + 'ColumnIndex' => 13, + 'DbType' => 'set', + 'AllowNull' => false, + 'DefaultValue' => TDbTableColumn::UNDEFINED_VALUE, + 'NumericPrecision' => null, + 'NumericScale' => null, + 'IsPrimaryKey' => false, + 'IsForeignKey' => false, + 'SequenceName' => null, + 'AutoIncrement' => false, + 'DbTypeValues' => array('blue', 'red', 'green'), + ); + + $this->assertColumn($columns, $table); + + $this->assertNull($table->getSchemaName()); + $this->assertEqual('table1', $table->getTableName()); + $this->assertEqual(array('id', 'name'), $table->getPrimaryKeys()); + } + + function assertColumn($columns, $table) + { + foreach($columns as $id=>$asserts) + { + $column = $table->Columns[$id]; + foreach($asserts as $property=>$assert) + { + $ofAssert= var_export($assert,true); + $value = $column->{$property}; + $ofValue = var_export($value, true); + $this->assertEqual($value, $assert, + "Column [{$id}] {$property} value {$ofValue} did not match {$ofAssert}"); + } + } + } +} + +?> \ No newline at end of file diff --git a/tests/simple_unit/DbCommon/MysqlColumnTest.php b/tests/simple_unit/DbCommon/MysqlColumnTest.php index 17eb6063..1508f2c6 100644 --- a/tests/simple_unit/DbCommon/MysqlColumnTest.php +++ b/tests/simple_unit/DbCommon/MysqlColumnTest.php @@ -1,5 +1,4 @@ -