summaryrefslogtreecommitdiff
path: root/tests/simple_unit
diff options
context:
space:
mode:
authorwei <>2007-04-24 06:14:56 +0000
committerwei <>2007-04-24 06:14:56 +0000
commitd5eb713888715e8f18d2ccf508a8eb0b1a483ad1 (patch)
tree3752f12f0a9379681e13171df805e8f5760c53ec /tests/simple_unit
parent1c74ee3c07cd2b25670826d44f7a1b1959302ce3 (diff)
add active record Relations
Diffstat (limited to 'tests/simple_unit')
-rw-r--r--tests/simple_unit/ActiveRecord/ForeignKeyTestCase.php144
-rw-r--r--tests/simple_unit/ActiveRecord/fk_tests.dbbin0 -> 9216 bytes
-rw-r--r--tests/simple_unit/ActiveRecord/sqlite.sql46
-rw-r--r--tests/simple_unit/DbCommon/CommandBuilderMysqlTest.php2
-rw-r--r--tests/simple_unit/unit.php1
5 files changed, 141 insertions, 52 deletions
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
--- /dev/null
+++ b/tests/simple_unit/ActiveRecord/fk_tests.db
Binary files 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 @@
<?php
include_once '../test_tools/unit_tests.php';
-
$test_cases = dirname(__FILE__)."/";
$tester = new PradoUnitTester($test_cases);