From d5eb713888715e8f18d2ccf508a8eb0b1a483ad1 Mon Sep 17 00:00:00 2001 From: wei <> Date: Tue, 24 Apr 2007 06:14:56 +0000 Subject: add active record Relations --- .../ActiveRecord/ForeignKeyTestCase.php | 144 ++++++++++++++------- tests/simple_unit/ActiveRecord/fk_tests.db | Bin 0 -> 9216 bytes tests/simple_unit/ActiveRecord/sqlite.sql | 46 +++++++ .../DbCommon/CommandBuilderMysqlTest.php | 2 +- tests/simple_unit/unit.php | 1 - 5 files changed, 141 insertions(+), 52 deletions(-) create mode 100644 tests/simple_unit/ActiveRecord/fk_tests.db create mode 100644 tests/simple_unit/ActiveRecord/sqlite.sql (limited to 'tests/simple_unit') diff --git a/tests/simple_unit/ActiveRecord/ForeignKeyTestCase.php b/tests/simple_unit/ActiveRecord/ForeignKeyTestCase.php index 6c240b2f..8c39a797 100644 --- a/tests/simple_unit/ActiveRecord/ForeignKeyTestCase.php +++ b/tests/simple_unit/ActiveRecord/ForeignKeyTestCase.php @@ -2,24 +2,32 @@ Prado::using('System.Data.ActiveRecord.TActiveRecord'); -abstract class Mysql4Record extends TActiveRecord +abstract class SqliteRecord extends TActiveRecord { protected static $conn; public function getDbConnection() { if(self::$conn===null) - self::$conn = new TDbConnection('mysql:host=localhost;port=3306;dbname=tests', 'test4', 'test4'); + self::$conn = new TDbConnection('sqlite:'.dirname(__FILE__).'/fk_tests.db'); return self::$conn; } } -class Album extends Mysql4Record +class Album extends SqliteRecord { public $title; - public $Tracks = array(self::HAS_MANY, 'Track'); - public $Artists = array(self::HAS_MANY, 'Artist', 'album_artist'); + public $Tracks = array(); + public $Artists = array(); + + public $cover; + + protected static $RELATIONS = array( + 'Tracks' => array(self::HAS_MANY, 'Track'), + 'Artists' => array(self::HAS_MANY, 'Artist', 'album_artists'), + 'cover' => array(self::HAS_ONE, 'Cover') + ); public static function finder($class=__CLASS__) { @@ -27,11 +35,15 @@ class Album extends Mysql4Record } } -class Artist extends Mysql4Record +class Artist extends SqliteRecord { public $name; - public $Albums = array(self::HAS_MANY, 'Album', 'album_artist'); + public $Albums = array(); + + protected static $RELATIONS=array( + 'Albums' => array(self::HAS_MANY, 'Album', 'album_artists') + ); public static function finder($class=__CLASS__) { @@ -39,13 +51,17 @@ class Artist extends Mysql4Record } } -class Track extends Mysql4Record +class Track extends SqliteRecord { public $id; public $song_name; public $album_id; //FK -> Album.id - public $Album = array(self::BELONGS_TO, 'Album'); + public $Album; + + protected static $RELATIONS = array( + 'Album' => array(self::BELONGS_TO, 'Album'), + ); public static function finder($class=__CLASS__) { @@ -53,61 +69,89 @@ class Track extends Mysql4Record } } -abstract class SqliteRecord extends TActiveRecord +class Cover extends SqliteRecord { - protected static $conn; - - public function getDbConnection() - { - if(self::$conn===null) - self::$conn = new TDbConnection('sqlite:'.dirname(__FILE__).'/blog.db'); - return self::$conn; - } + public $album; + public $content; } -class PostRecord extends SqliteRecord +class ForeignKeyTestCase extends UnitTestCase { - const TABLE='posts'; - public $post_id; - public $author; - public $create_time; - public $title; - public $content; - public $status; + function test_has_many() + { + $albums = Album::finder()->withTracks()->findAll(); + $this->assertEqual(count($albums), 2); + + $this->assertEqual($albums[0]->title, 'Album 1'); + $this->assertEqual($albums[1]->title, 'Album 2'); + + $this->assertEqual(count($albums[0]->Artists), 0); + $this->assertEqual(count($albums[1]->Artists), 0); - public $authorRecord = array(self::HAS_ONE, 'BlogUserRecord'); + $this->assertEqual(count($albums[0]->Tracks), 3); + $this->assertEqual(count($albums[1]->Tracks), 2); - public static function finder($className=__CLASS__) + $this->assertEqual($albums[0]->Tracks[0]->song_name, 'Track 1'); + $this->assertEqual($albums[0]->Tracks[1]->song_name, 'Song 2'); + $this->assertEqual($albums[0]->Tracks[2]->song_name, 'Song 3'); + + $this->assertEqual($albums[1]->Tracks[0]->song_name, 'Track A'); + $this->assertEqual($albums[1]->Tracks[1]->song_name, 'Track B'); + } + + function test_has_one() { - return parent::finder($className); + $albums = Album::finder()->with_cover()->findAll(); + $this->assertEqual(count($albums), 2); + + $this->assertEqual($albums[0]->title, 'Album 1'); + $this->assertEqual($albums[1]->title, 'Album 2'); + + $this->assertEqual($albums[0]->cover->content, 'lalala'); + $this->assertEqual($albums[1]->cover->content, 'conver content'); + + $this->assertEqual(count($albums[0]->Artists), 0); + $this->assertEqual(count($albums[1]->Artists), 0); + + $this->assertEqual(count($albums[0]->Tracks), 0); + $this->assertEqual(count($albums[1]->Tracks), 0); } -} -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'); + function test_belongs_to() + { + $track = Track::finder()->withAlbum()->find('id = ?', 1); + + $this->assertEqual($track->id, "1"); + $this->assertEqual($track->song_name, "Track 1"); + $this->assertEqual($track->Album->title, "Album 1"); + } - public static function finder($className=__CLASS__) + function test_has_many_associate() { - return parent::finder($className); + $album = Album::finder()->withArtists()->find('title = ?', 'Album 2'); + $this->assertEqual($album->title, 'Album 2'); + $this->assertEqual(count($album->Artists), 3); + + $this->assertEqual($album->Artists[0]->name, 'Dan'); + $this->assertEqual($album->Artists[1]->name, 'Karl'); + $this->assertEqual($album->Artists[2]->name, 'Tom'); } -} -class ForeignKeyTestCase extends UnitTestCase -{ - function test() + function test_multiple_fk() { - $album = Album::finder()->withTracks()->findAll(); - //print_r($album); - //print_r(PostRecord::finder()->findAll()); - //print_r(BlogUserRecord::finder()->with_posts()->findAll()); + $album = Album::finder()->withArtists()->withTracks()->with_cover()->find('title = ?', 'Album 1'); + + $this->assertEqual($album->title, 'Album 1'); + $this->assertEqual(count($album->Artists), 2); + + $this->assertEqual($album->Artists[0]->name, 'Dan'); + $this->assertEqual($album->Artists[1]->name, 'Jenny'); + + $this->assertEqual($album->Tracks[0]->song_name, 'Track 1'); + $this->assertEqual($album->Tracks[1]->song_name, 'Song 2'); + $this->assertEqual($album->Tracks[2]->song_name, 'Song 3'); + + $this->assertEqual($album->cover->content, 'lalala'); } } diff --git a/tests/simple_unit/ActiveRecord/fk_tests.db b/tests/simple_unit/ActiveRecord/fk_tests.db new file mode 100644 index 00000000..87835c84 Binary files /dev/null and b/tests/simple_unit/ActiveRecord/fk_tests.db differ diff --git a/tests/simple_unit/ActiveRecord/sqlite.sql b/tests/simple_unit/ActiveRecord/sqlite.sql new file mode 100644 index 00000000..03e4a1ab --- /dev/null +++ b/tests/simple_unit/ActiveRecord/sqlite.sql @@ -0,0 +1,46 @@ +CREATE TABLE album ( + title varchar(100) NOT NULL PRIMARY KEY +); + +CREATE TABLE artist ( + name varchar(25) NOT NULL PRIMARY KEY +); + +CREATE TABLE album_artists ( + album_title varchar(100) NOT NULL CONSTRAINT fk_album REFERENCES album(title) ON DELETE CASCADE, + artist_name varchar(25) NOT NULL CONSTRAINT fk_artist REFERENCES artist(name) ON DELETE CASCADE +); + +CREATE TABLE track ( + id INTEGER NOT NULL PRIMARY KEY, + song_name varchar(200) NOT NULL default '', + album_id varchar(100) NOT NULL CONSTRAINT fk_album_1 REFERENCES album(title) ON DELETE CASCADE +); + +CREATE TABLE cover( + album varchar(200) NOT NULL CONSTRAINT fk_album_2 REFERENCES album(title) ON DELETE CASCADE, + content text +); + +INSERT INTO album (title) VALUES ('Album 1'); +INSERT INTO album (title) VALUES ('Album 2'); + +INSERT INTO cover(album,content) VALUES ('Album 1', 'lalala'); +INSERT INTO cover(album,content) VALUES ('Album 2', 'conver content'); + +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/CommandBuilderMysqlTest.php b/tests/simple_unit/DbCommon/CommandBuilderMysqlTest.php index 500d5277..6b744a4b 100644 --- a/tests/simple_unit/DbCommon/CommandBuilderMysqlTest.php +++ b/tests/simple_unit/DbCommon/CommandBuilderMysqlTest.php @@ -6,7 +6,7 @@ class CommandBuilderMysqlTest extends UnitTestCase { function mysql_meta_data() { - $conn = new TDbConnection('mysql:host=localhost;dbname=tests', 'test','test'); + $conn = new TDbConnection('mysql:host=localhost;dbname=tests;port=3307', 'test5','test5'); return new TMysqlMetaData($conn); } diff --git a/tests/simple_unit/unit.php b/tests/simple_unit/unit.php index b58c98c5..7e86e925 100644 --- a/tests/simple_unit/unit.php +++ b/tests/simple_unit/unit.php @@ -1,7 +1,6 @@