diff options
Diffstat (limited to 'tests/unit/Data/ActiveRecord')
29 files changed, 1496 insertions, 0 deletions
diff --git a/tests/unit/Data/ActiveRecord/ActiveRecordDynamicCallTest.php b/tests/unit/Data/ActiveRecord/ActiveRecordDynamicCallTest.php new file mode 100644 index 00000000..1bf98415 --- /dev/null +++ b/tests/unit/Data/ActiveRecord/ActiveRecordDynamicCallTest.php @@ -0,0 +1,70 @@ +<?php +Prado::using('System.Data.ActiveRecord.TActiveRecord'); +require_once(dirname(__FILE__).'/records/DepartmentRecord.php'); +require_once(dirname(__FILE__).'/records/DepSections.php'); + +/** + * @package System.Data.ActiveRecord + */ +class ActiveRecordDynamicCallTest extends PHPUnit_Framework_TestCase +{ + function setup() + { + $conn = new TDbConnection('pgsql:host=localhost;dbname=test', 'test','test'); + TActiveRecordManager::getInstance()->setDbConnection($conn); + } + + function test_multiple_field_and_or() + { + $finder = DepartmentRecord::finder(); + $r2 = $finder->findAllByName_And_Description_Or_Active_Or_Order('Facilities', null, false, 1); + $this->assertNotNull($r2); + } + + function test_dynamic_call() + { + $finder = DepartmentRecord::finder(); + $r2 = $finder->findByName('Facilities'); + $this->assertNotNull($r2); + } + + function test_dynamic_multiple_field_call() + { + $finder = DepartmentRecord::finder(); + $rs = $finder->findByNameAndActive('Marketing',true); + $this->assertNotNull($rs); + } + + function test_dynamic_call_missing_parameters_throws_exception() + { + $finder = DepartmentRecord::finder(); + try + { + $rs = $finder->findByNameAndActive('Marketing'); + $this->fail(); + } + catch(TDbException $e) + { + $this->pass(); + } + } + + function test_dynamic_call_extras_parameters_ok() + { + $finder = DepartmentRecord::finder(); + $rs = $finder->findByNameAndActive('Marketing',true,true); + $this->assertNotNull($rs); + } + + function test_dynamic_delete_by() + { + $finder = DepartmentRecord::finder(); + //$finder->RecordManager->OnDelete[] = array($this, 'assertDeleteSql'); + $this->assertEqual($finder->deleteByName('tasds'), 0); + } + + function assertDeleteSql($sender, $param) + { + var_dump($param); + } +} diff --git a/tests/unit/Data/ActiveRecord/ActiveRecordFinderTest.php b/tests/unit/Data/ActiveRecord/ActiveRecordFinderTest.php new file mode 100644 index 00000000..2209fc6a --- /dev/null +++ b/tests/unit/Data/ActiveRecord/ActiveRecordFinderTest.php @@ -0,0 +1,46 @@ +<?php +Prado::using('System.Data.ActiveRecord.TActiveRecord'); +require_once(dirname(__FILE__).'/records/DepartmentRecord.php'); + +/** + * @package System.Data.ActiveRecord + */ +class ActiveRecordFinderTest extends PHPUnit_Framework_TestCase +{ + function setup() + { + $conn = new TDbConnection('pgsql:host=localhost;dbname=test', 'test','test'); + TActiveRecordManager::getInstance()->setDbConnection($conn); + } + + function testFindBySQL_returns_one_record() + { + $department = DepartmentRecord::finder()->find('department_id < ?', 5); + $this->assertNotNull($department); + } + + function testFindBySQL_returns_zero_records() + { + $department = DepartmentRecord::finder()->find('department_id > ?', 15); + $this->assertNull($department); + } + + function test_find_by_sql_returns_iterator() + { + $deps = DepartmentRecord::finder()->findAll('department_id < :id', array('id'=>5)); + $this->assertEqual(count($deps),4); + } + + function test_find_by_multiple_parameters() + { + $department = DepartmentRecord::finder()->find('department_id < ? AND "order" > ?', 5,2); + $this->assertNotNull($department); + } + + function test_find_by_array_parameter() + { + $department = DepartmentRecord::finder()->find('department_id < ? AND "order" > ?', array(5,2)); + $this->assertNotNull($department); + } + +} diff --git a/tests/unit/Data/ActiveRecord/ActiveRecordMySql5Test.php b/tests/unit/Data/ActiveRecord/ActiveRecordMySql5Test.php new file mode 100644 index 00000000..31cbed5c --- /dev/null +++ b/tests/unit/Data/ActiveRecord/ActiveRecordMySql5Test.php @@ -0,0 +1,48 @@ +<?php +Prado::using('System.Data.ActiveRecord.TActiveRecord'); +require_once(dirname(__FILE__).'/records/Blogs.php'); + +/** + * @package System.Data.ActiveRecord + */ +class ActiveRecordMySql5Test extends PHPUnit_Framework_TestCase +{ + function setup() + { + $conn = new TDbConnection('mysql:host=localhost;dbname=ar_test;port=3307', 'test5','test5'); + TActiveRecordManager::getInstance()->setDbConnection($conn); + } + + function test_find_first_blog() + { + $blog = Blogs::finder()->findByPk(1); + $this->assertNotNull($blog); + } + + function test_insert_new_blog() + { + $blog = new Blogs(); + $blog->blog_name = 'test1'; + $blog->blog_author = 'wei'; + + $this->assertTrue($blog->save()); + + $blog->blog_name = 'test2'; + + $this->assertTrue($blog->save()); + + $check = Blogs::finder()->findByPk($blog->blog_id); + + $this->assertSameBlog($check,$blog); + + $this->assertTrue($blog->delete()); + } + + function assertSameBlog($check, $blog) + { + $props = array('blog_id', 'blog_name', 'blog_author'); + foreach($props as $prop) + $this->assertEqual($check->{$prop}, $blog->{$prop}); + } + +}
\ No newline at end of file diff --git a/tests/unit/Data/ActiveRecord/BaseActiveRecordTest.php b/tests/unit/Data/ActiveRecord/BaseActiveRecordTest.php new file mode 100644 index 00000000..258fe70c --- /dev/null +++ b/tests/unit/Data/ActiveRecord/BaseActiveRecordTest.php @@ -0,0 +1,34 @@ +<?php +Prado::using('System.Data.ActiveRecord.TActiveRecord'); + +/** + * @package System.Data.ActiveRecord + */ +class BaseRecordTest extends TActiveRecord +{ + +} + +class BaseActiveRecordTest extends PHPUnit_Framework_TestCase +{ + function test_finder_returns_same_instance() + { + $obj1 = TActiveRecord::finder('BaseRecordTest'); + $obj2 = TActiveRecord::finder('BaseRecordTest'); + $this->assertIdentical($obj1,$obj2); + } + + function test_finder_throw_exception_when_save() + { + $obj = TActiveRecord::finder('BaseRecordTest'); + try + { + $obj->save(); + $this->fail(); + } + catch(TActiveRecordException $e) + { + $this->pass(); + } + } +} diff --git a/tests/unit/Data/ActiveRecord/CountRecordsTest.php b/tests/unit/Data/ActiveRecord/CountRecordsTest.php new file mode 100644 index 00000000..b97f83ed --- /dev/null +++ b/tests/unit/Data/ActiveRecord/CountRecordsTest.php @@ -0,0 +1,36 @@ +<?php + +Prado::using('System.Data.ActiveRecord.TActiveRecord'); +require_once(dirname(__FILE__).'/records/DepartmentRecord.php'); + +/** + * @package System.Data.ActiveRecord + */ +class CountRecordsTest extends PHPUnit_Framework_TestCase +{ + function setup() + { + $conn = new TDbConnection('pgsql:host=localhost;dbname=test', 'test','test'); + TActiveRecordManager::getInstance()->setDbConnection($conn); + } + + function test_count() + { + $finder = DepartmentRecord::finder(); + $count = $finder->count('"order" > ?', 2); + $this->assertTrue($count > 0); + } + + function test_count_zero() + { + $finder = DepartmentRecord::finder(); + $count = $finder->count('"order" > ?', 11); + $this->assertEqual($count,0); + } + + function test_count_without_parameter() + { + $finder = DepartmentRecord::finder(); + $this->assertEqual($finder->count(), 8); + } +} diff --git a/tests/unit/Data/ActiveRecord/CriteriaTest.php b/tests/unit/Data/ActiveRecord/CriteriaTest.php new file mode 100644 index 00000000..3c4c15d7 --- /dev/null +++ b/tests/unit/Data/ActiveRecord/CriteriaTest.php @@ -0,0 +1,51 @@ +<?php + +Prado::using('System.Data.ActiveRecord.TActiveRecord'); +require_once(dirname(__FILE__).'/records/DepartmentRecord.php'); +require_once(dirname(__FILE__).'/records/DepSections.php'); + +/** + * @package System.Data.ActiveRecord + */ +class CriteriaTest extends PHPUnit_Framework_TestCase +{ + function setup() + { + $conn = new TDbConnection('pgsql:host=localhost;dbname=test', 'test','test'); + TActiveRecordManager::getInstance()->setDbConnection($conn); + } + + function test_orderby_only() + { + $criteria = new TActiveRecordCriteria; + $criteria->OrdersBy['name'] = 'asc'; + $records = DepartmentRecord::finder()->findAll($criteria); + $this->assertEqual(count($records), 8); + $this->assertEqual($records[0]->name, '+GX Service'); + $this->assertEqual($records[7]->name, 'Marketing'); + } + + function test_orderby_only_desc() + { + $criteria = new TActiveRecordCriteria; + $criteria->OrdersBy['name'] = 'desc'; + $records = DepartmentRecord::finder()->findAll($criteria); + $this->assertEqual(count($records), 8); + $this->assertEqual($records[7]->name, '+GX Service'); + $this->assertEqual($records[0]->name, 'Marketing'); + } + + function test_criteria_parameters() + { + $criteria = new TActiveRecordCriteria('sql', "One", "two", 3); + $expect = array("One", "two", 3); + $this->assertEqual($criteria->getParameters()->toArray(), $expect); + } + + function test_criteria_parameters_array() + { + $expect = array("One", "two", 3); + $criteria = new TActiveRecordCriteria('sql', $expect); + $this->assertEqual($criteria->getParameters()->toArray(), $expect); + } +} diff --git a/tests/unit/Data/ActiveRecord/DeleteByPkTest.php b/tests/unit/Data/ActiveRecord/DeleteByPkTest.php new file mode 100644 index 00000000..2f2c09c9 --- /dev/null +++ b/tests/unit/Data/ActiveRecord/DeleteByPkTest.php @@ -0,0 +1,33 @@ +<?php + +Prado::using('System.Data.ActiveRecord.TActiveRecord'); +require_once(dirname(__FILE__).'/records/DepartmentRecord.php'); +require_once(dirname(__FILE__).'/records/DepSections.php'); + +/** + * @package System.Data.ActiveRecord + */ +class DeleteByPkTest extends PHPUnit_Framework_TestCase +{ + function setup() + { + $conn = new TDbConnection('pgsql:host=localhost;dbname=test', 'test','test'); + TActiveRecordManager::getInstance()->setDbConnection($conn); + } + + function test_delete_by_pks() + { + $finder = DepartmentRecord::finder(); + $this->assertEqual($finder->deleteByPk(100),0); + $this->assertEqual($finder->deleteByPk(100, 101),0); + $this->assertEqual($finder->deleteByPk(array(100, 101)),0); + } + + function test_delete_by_composite_pks() + { + $finder = DepSections::finder(); + $this->assertEqual($finder->deleteByPk(array(100,101)),0); + $this->assertEqual($finder->deleteByPk(array(100, 101), array(102, 103)),0); + $this->assertEqual($finder->deleteByPk(array(array(100, 101), array(102, 103))),0); + } +}
\ No newline at end of file diff --git a/tests/unit/Data/ActiveRecord/FindByPksTest.php b/tests/unit/Data/ActiveRecord/FindByPksTest.php new file mode 100644 index 00000000..8235352c --- /dev/null +++ b/tests/unit/Data/ActiveRecord/FindByPksTest.php @@ -0,0 +1,65 @@ +<?php +Prado::using('System.Data.ActiveRecord.TActiveRecord'); +require_once(dirname(__FILE__).'/records/DepartmentRecord.php'); +require_once(dirname(__FILE__).'/records/DepSections.php'); + +/** + * @package System.Data.ActiveRecord + */ +class FindByPksTest extends PHPUnit_Framework_TestCase +{ + function setup() + { + $conn = new TDbConnection('pgsql:host=localhost;dbname=test', 'test','test'); + TActiveRecordManager::getInstance()->setDbConnection($conn); + } + + function test_find_by_1pk() + { + $dep = DepartmentRecord::finder()->findByPk(1); + $this->assertNotNull($dep); + $this->assertEqual($dep->department_id, 1); + } + + function test_find_by_1pk_array() + { + $dep = DepartmentRecord::finder()->findByPk(array(1)); + $this->assertNotNull($dep); + $this->assertEqual($dep->department_id, 1); + } + + function test_find_by_pks() + { + $deps = DepartmentRecord::finder()->findAllByPks(1,2,4); + $this->assertEqual(count($deps), 3); + + $this->assertEqual($deps[0]->department_id, 1); + $this->assertEqual($deps[1]->department_id, 2); + $this->assertEqual($deps[2]->department_id, 4); + } + + function test_find_by_pks_with_invalid() + { + $deps = DepartmentRecord::finder()->findAllByPks(4,2,14); + $this->assertEqual(count($deps), 2); + + $this->assertEqual($deps[0]->department_id, 2); + $this->assertEqual($deps[1]->department_id, 4); + } + + function test_find_by_composite_pks() + { + $ds = DepSections::finder()->findAllByPks(array(1,1), array(2,5)); + $this->assertEqual(count($ds), 2); + + $this->assertIsDepSection($ds[0], 1, 1); + $this->assertIsDepSection($ds[1], 2, 5); + } + + function assertIsDepSection($dep, $dep_id, $sec_id) + { + $this->assertTrue($dep instanceof DepSections); + $this->assertEqual($dep->department_id, $dep_id); + $this->assertEqual($dep->section_id, $sec_id); + } +} diff --git a/tests/unit/Data/ActiveRecord/FindBySqlTest.php b/tests/unit/Data/ActiveRecord/FindBySqlTest.php new file mode 100644 index 00000000..98113b06 --- /dev/null +++ b/tests/unit/Data/ActiveRecord/FindBySqlTest.php @@ -0,0 +1,47 @@ +<?php +Prado::using('System.Data.ActiveRecord.TActiveRecord'); +require_once(dirname(__FILE__).'/records/DepartmentRecord.php'); +require_once(dirname(__FILE__).'/records/UserRecord.php'); + +/** + * @package System.Data.ActiveRecord + */ +class UserRecord2 extends UserRecord +{ + public $another_value; +} + +class SqlTest extends TActiveRecord +{ + public $category; + public $item; + + const TABLE='items'; +} + +class FindBySqlTest extends PHPUnit_Framework_TestCase +{ + function setup() + { + $conn = new TDbConnection('pgsql:host=localhost;dbname=test', 'test','test'); + TActiveRecordManager::getInstance()->setDbConnection($conn); + } + + function test_find_by_sql() + { + $deps = DepartmentRecord::finder()->findBySql('SELECT * FROM departments'); + $this->assertTrue(count($deps) > 0); + } + + function test_find_by_sql_arb() + { + $sql = 'SELECT c.name as category, i.name as item + FROM items i, categories c + WHERE i.category_id = c.category_id LIMIT 2'; + $items = TActiveRecord::finder('SqlTest')->findBySql($sql); + + $sql = "SELECT users.*, 'hello' as another_value FROM users LIMIT 2"; + $users = TActiveRecord::finder('UserRecord2')->findBySql($sql); + var_dump($users); + } +} diff --git a/tests/unit/Data/ActiveRecord/ForeignKeyTest.php b/tests/unit/Data/ActiveRecord/ForeignKeyTest.php new file mode 100644 index 00000000..99c8b527 --- /dev/null +++ b/tests/unit/Data/ActiveRecord/ForeignKeyTest.php @@ -0,0 +1,176 @@ +<?php + +Prado::using('System.Data.ActiveRecord.TActiveRecord'); +require_once(dirname(__FILE__).'/records/ItemRecord.php'); + +/** + * @package System.Data.ActiveRecord + */ +abstract class SqliteRecord extends TActiveRecord +{ + protected static $conn; + + public function getDbConnection() + { + if(self::$conn===null) + self::$conn = new TDbConnection('sqlite:'.dirname(__FILE__).'/fk_tests.db'); + return self::$conn; + } +} + +class Album extends SqliteRecord +{ + public $title; + + public $Tracks = array(); + public $Artists = array(); + + public $cover; + + public static $RELATIONS = array( + 'Tracks' => array(self::HAS_MANY, 'Track'), + 'Artists' => array(self::MANY_TO_MANY, 'Artist', 'album_artists'), + 'cover' => array(self::HAS_ONE, 'Cover') + ); + + public static function finder($class=__CLASS__) + { + return parent::finder($class); + } +} + +class Artist extends SqliteRecord +{ + public $name; + + public $Albums = array(); + + public static $RELATIONS=array + ( + 'Albums' => array(self::MANY_TO_MANY, 'Album', 'album_artists') + ); + + public static function finder($class=__CLASS__) + { + return parent::finder($class); + } +} + +class Track extends SqliteRecord +{ + public $id; + public $song_name; + public $album_id; //FK -> Album.id + + public $Album; + + public static $RELATIONS = array( + 'Album' => array(self::BELONGS_TO, 'Album'), + ); + + public static function finder($class=__CLASS__) + { + return parent::finder($class); + } +} + +class Cover extends SqliteRecord +{ + public $album; + public $content; +} + +class ForeignKeyTest extends PHPUnit_Framework_TestCase +{ + 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); + + $this->assertEqual(count($albums[0]->Tracks), 3); + $this->assertEqual(count($albums[1]->Tracks), 2); + + $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() + { + $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); + } + + 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"); + } + + function test_has_many_associate() + { + $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'); + } + + function test_multiple_fk() + { + $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'); + } + + function test_self_reference_fk() + { + $item = ItemRecord::finder()->withRelated_Items()->findByPk(1); + $this->assertNotNull($item); + $this->assertEqual($item->name, "Professional Work Attire"); + + $this->assertEqual(count($item->related_items),2); + $this->assertEqual($item->related_items[0]->name, "Nametags"); + $this->assertEqual($item->related_items[0]->item_id, 2); + + $this->assertEqual($item->related_items[1]->name, "Grooming and Hygiene"); + $this->assertEqual($item->related_items[1]->item_id, 3); + } + +} diff --git a/tests/unit/Data/ActiveRecord/ForeignObjectUpdateTest.php b/tests/unit/Data/ActiveRecord/ForeignObjectUpdateTest.php new file mode 100644 index 00000000..8b9d0673 --- /dev/null +++ b/tests/unit/Data/ActiveRecord/ForeignObjectUpdateTest.php @@ -0,0 +1,243 @@ +<?php +Prado::using('System.Data.ActiveRecord.TActiveRecord'); + +/** + * @package System.Data.ActiveRecord + */ +class BaseFkRecord extends TActiveRecord +{ + public function getDbConnection() + { + static $conn; + if($conn===null) + { + $conn = new TDbConnection('pgsql:host=localhost;dbname=test', 'test','test'); + //$this->OnExecuteCommand[] = array($this,'logger'); + } + return $conn; + } + + function logger($sender,$param) + { + } +} + +class TeamRecord extends BaseFkRecord +{ + const TABLE='teams'; + public $name; + public $location; + + public $players=array(); + + //define the $player member having has many relationship with PlayerRecord + public static $RELATIONS=array + ( + 'players' => array(self::HAS_MANY, 'PlayerRecord'), + ); + + public static function finder($className=__CLASS__) + { + return parent::finder($className); + } +} + +class PlayerRecord extends BaseFkRecord +{ + const TABLE='players'; + public $player_id; + public $age; + public $team_name; + + public $team; + private $_skills; + public $profile; + + public static $RELATIONS=array + ( + 'skills' => array(self::MANY_TO_MANY, 'SkillRecord', 'player_skills'), + 'team' => array(self::BELONGS_TO, 'TeamRecord'), + 'profile' => array(self::HAS_ONE, 'ProfileRecord'), + ); + + public static function finder($className=__CLASS__) + { + return parent::finder($className); + } + + public function getSkills() + { + if($this->_skills===null && $this->player_id !==null) + { + //lazy load the skill records + $this->setSkills($this->withSkills()->findByPk($this->player_id)->skills); + } + else if($this->_skills===null) + { + //create new TList; + $this->setSkills(new TList()); + } + return $this->_skills; + } + + public function setSkills($value) + { + $this->_skills = $value instanceof TList ? $value : new TList($value); + } +} + +class ProfileRecord extends BaseFkRecord +{ + const TABLE='profiles'; + public $fk_player_id; + public $salary; + + public $player; + + public static $RELATIONS=array + ( + 'player' => array(self::BELONGS_TO, 'PlayerRecord'), + ); + + public static function finder($className=__CLASS__) + { + return parent::finder($className); + } +} + +class SkillRecord extends BaseFkRecord +{ + const TABLE='skills'; + public $skill_id; + public $name; + + public $players=array(); + + public static $RELATIONS=array + ( + 'players' => array(self::MANY_TO_MANY, 'PlayerRecord', 'player_skills'), + ); + + public static function finder($className=__CLASS__) + { + return parent::finder($className); + } + + +} + +class ForeignObjectUpdateTest extends PHPUnit_Framework_TestCase +{ + function test_add_has_one() + { + ProfileRecord::finder()->deleteByPk(3); + + $player = PlayerRecord::finder()->findByPk(3); + $player->profile = new ProfileRecord(array('salary'=>50000)); + $player->save(); + + //test insert + $player2 = PlayerRecord::finder()->withProfile()->findByPk(3); + $this->assertEqual($player2->profile->salary,50000); + + $player2->profile->salary = 45000; + $player2->save(); + $this->assertEqual($player2->profile->salary,45000); + + //test update + $player3 = PlayerRecord::finder()->withProfile()->findByPk(3); + $this->assertEqual($player3->profile->salary,45000); + } + + function test_add_many() + { + PlayerRecord::finder()->deleteAll("player_id > ?", 3); + + $team = TeamRecord::finder()->findByPk('Team b'); + $team->players[] = new PlayerRecord(array('age'=>20)); + $team->players[] = new PlayerRecord(array('age'=>25)); + $team->save(); + + //test insert + $team1 = TeamRecord::finder()->withPlayers()->findByPk('Team b'); + $this->assertEqual(count($team1->players),3); + $this->assertEqual($team1->players[0]->age, 18); + $this->assertEqual($team1->players[1]->age, 20); + $this->assertEqual($team1->players[2]->age, 25); + + //test update + $team1->players[1]->age = 55; + $team1->save(); + + $this->assertEqual($team1->players[0]->age, 18); + $this->assertEqual($team1->players[1]->age, 55); + $this->assertEqual($team1->players[2]->age, 25); + + $criteria = new TActiveRecordCriteria(); + $criteria->OrdersBy['age'] = 'desc'; + $team2 = TeamRecord::finder()->withPlayers($criteria)->findByPk('Team b'); + $this->assertEqual(count($team2->players),3); + //ordered by age + $this->assertEqual($team2->players[0]->age, 55); + $this->assertEqual($team2->players[1]->age, 25); + $this->assertEqual($team2->players[2]->age, 18); + } + + function test_add_belongs_to() + { + TeamRecord::finder()->deleteByPk('Team c'); + PlayerRecord::finder()->deleteAll("player_id > ?", 3); + + $player = new PlayerRecord(array('age'=>27)); + $player->team = new TeamRecord(array('name'=>'Team c', 'location'=>'Sydney')); + $player->save(); + + //test insert + $player1 = PlayerRecord::finder()->withTeam()->findByAge(27); + $this->assertNotNull($player1); + $this->assertNotNull($player1->team); + $this->assertEqual($player1->team->name, 'Team c'); + $this->assertEqual($player1->team->location, 'Sydney'); + } + + function test_add_many_via_association() + { + PlayerRecord::finder()->deleteAll("player_id > ?", 3); + SkillRecord::finder()->deleteAll("skill_id > ?", 3); + + $player = new PlayerRecord(array('age'=>37)); + $player->skills[] = new SkillRecord(array('name'=>'Bash')); + $player->skills[] = new SkillRecord(array('name'=>'Jump')); + $player->save(); + + //test insert + $player2 = PlayerRecord::finder()->withSkills()->findByAge(37); + $this->assertNotNull($player2); + $this->assertEqual(count($player2->skills), 2); + $this->assertEqual($player2->skills[0]->name, 'Bash'); + $this->assertEqual($player2->skills[1]->name, 'Jump'); + + //test update + $player2->skills[1]->name = "Skip"; + $player2->skills[] = new SkillRecord(array('name'=>'Push')); + $player2->save(); + + $criteria = new TActiveRecordCriteria(); + $criteria->OrdersBy['name'] = 'asc'; + $player3 = PlayerRecord::finder()->withSkills($criteria)->findByAge(37); + $this->assertNotNull($player3); + $this->assertEqual(count($player3->skills), 3); + $this->assertEqual($player3->skills[0]->name, 'Bash'); + $this->assertEqual($player3->skills[1]->name, 'Push'); + $this->assertEqual($player3->skills[2]->name, 'Skip'); + + //test lazy load + $player4 = PlayerRecord::finder()->findByAge(37); + $this->assertEqual(count($player4->skills), 3); + + $this->assertEqual($player4->skills[0]->name, 'Bash'); + $this->assertEqual($player4->skills[1]->name, 'Skip'); + $this->assertEqual($player4->skills[2]->name, 'Push'); + } +//*/ +} diff --git a/tests/unit/Data/ActiveRecord/MultipleForeignKeyTest.php b/tests/unit/Data/ActiveRecord/MultipleForeignKeyTest.php new file mode 100644 index 00000000..d8c54b03 --- /dev/null +++ b/tests/unit/Data/ActiveRecord/MultipleForeignKeyTest.php @@ -0,0 +1,193 @@ +<?php + +Prado::using('System.Data.ActiveRecord.TActiveRecord'); + +/** + * @package System.Data.ActiveRecord + */ +abstract class MultipleFKSqliteRecord extends TActiveRecord +{ + protected static $conn; + + public function getDbConnection() + { + if(self::$conn===null) + self::$conn = new TDbConnection('sqlite:'.dirname(__FILE__).'/test1.sqlite'); + return self::$conn; + } +} + +/** + * +CREATE TABLE table1 ( +id integer PRIMARY KEY AUTOINCREMENT, +field1 varchar, +fk1 integer CONSTRAINT fk_id1 REFERENCES table2(id) ON DELETE CASCADE, +fk2 integer CONSTRAINT fk_id2 REFERENCES table2(id) ON DELETE CASCADE, +fk3 integer CONSTRAINT fk_id3 REFERENCES table2(id) ON DELETE CASCADE) + */ +class Table1 extends MultipleFKSqliteRecord +{ + public $id; + public $field1; + public $fk1; + public $fk2; + public $fk3; + + public $object1; + //public $object2; //commented out for testing __get/__set + public $object3; + + public static $RELATIONS = array + ( + 'object1' => array(self::BELONGS_TO, 'Table2', 'fk1'), + 'object2' => array(self::BELONGS_TO, 'Table2', 'fk2'), + 'object3' => array(self::BELONGS_TO, 'Table2', 'fk3'), + ); + + public static function finder($class=__CLASS__) + { + return parent::finder($class); + } +} + +/** + * CREATE TABLE table2 (id integer PRIMARY KEY AUTOINCREMENT,field1 varchar) + */ +class Table2 extends MultipleFKSqliteRecord +{ + public $id; + public $field1; + + private $_state1; + //public $state2; //commented out for testing __get/__set + public $state3; + + public static $RELATIONS = array + ( + 'state1' => array(self::HAS_MANY, 'Table1', 'fk1'), + 'state2' => array(self::HAS_MANY, 'Table1', 'fk2'), + 'state3' => array(self::HAS_ONE, 'Table1', 'fk3'), + ); + + public function setState1($obj) + { + $this->_state1 = $obj; + } + + public function getState1() + { + if(is_null($this->_state1)) + $this->fetchResultsFor('state1'); + return $this->_state1; + } + + public static function finder($class=__CLASS__) + { + return parent::finder($class); + } +} + + +class CategoryX extends MultipleFKSqliteRecord +{ + public $cat_id; + public $category_name; + public $parent_cat; + + public $parent_category; + public $child_categories=array(); + + public static $RELATIONS=array + ( + 'parent_category' => array(self::BELONGS_TO, 'CategoryX'), + 'child_categories' => array(self::HAS_MANY, 'CategoryX'), + ); + + public static function finder($class=__CLASS__) + { + return parent::finder($class); + } +} + +class MultipleForeignKeyTest extends PHPUnit_Framework_TestCase +{ + function testBelongsTo() + { + $obj = Table1::finder()->withObject1()->findAll(); + $this->assertEqual(count($obj), 3); + $this->assertEqual($obj[0]->id, '1'); + $this->assertEqual($obj[1]->id, '2'); + $this->assertEqual($obj[2]->id, '3'); + + $this->assertEqual($obj[0]->object1->id, '1'); + $this->assertEqual($obj[1]->object1->id, '2'); + $this->assertEqual($obj[2]->object1->id, '2'); + } + + function testHasMany() + { + $obj = Table2::finder()->withState1()->findAll(); + $this->assertEqual(count($obj), 5); + + $this->assertEqual(count($obj[0]->state1), 1); + $this->assertEqual($obj[0]->state1[0]->id, '1'); + + $this->assertEqual(count($obj[1]->state1), 2); + $this->assertEqual($obj[1]->state1[0]->id, '2'); + $this->assertEqual($obj[1]->state1[1]->id, '3'); + + $this->assertEqual(count($obj[2]->state1), 0); + $this->assertEqual($obj[2]->id, '3'); + + $this->assertEqual(count($obj[3]->state1), 0); + $this->assertEqual($obj[3]->id, '4'); + } + + function testHasOne() + { + $obj = Table2::finder()->withState3('id = 3')->findAll(); + + $this->assertEqual(count($obj), 5); + + $this->assertEqual($obj[0]->id, '1'); + $this->assertNull($obj[0]->state3); + + $this->assertEqual($obj[1]->id, '2'); + $this->assertNull($obj[1]->state3); + + $this->assertEqual($obj[2]->id, '3'); + $this->assertNotNull($obj[2]->state3); + $this->assertEqual($obj[2]->state3->id, '3'); + + $this->assertEqual($obj[3]->id, '4'); + $this->assertNull($obj[3]->state3); + } + + function testParentChild() + { + $obj = CategoryX::finder()->withChild_Categories()->withParent_Category()->findByPk(2); + + $this->assertEqual($obj->cat_id, '2'); + $this->assertEqual(count($obj->child_categories), 2); + $this->assertNotNull($obj->parent_category); + + $this->assertEqual($obj->child_categories[0]->cat_id, 3); + $this->assertEqual($obj->child_categories[1]->cat_id, 4); + + $this->assertEqual($obj->parent_category->cat_id, 1); + } + + function testLazyLoadingGetterSetter_hasMany() + { + $arr = Table2::finder()->findByPk(2); + + $this->assertNotNull($arr->state2); //lazy load + $this->assertEqual(count($arr->state2), 1); + $this->assertEqual($arr->state2[0]->id, "1"); + $this->assertNotNull($arr->state2[0]->object2); + $this->assertEqual($arr->state2[0]->object2->id, "2"); + + $this->assertNotIdentical($arr, $arr->state2[0]->object2); + } +} diff --git a/tests/unit/Data/ActiveRecord/RecordEventTest.php b/tests/unit/Data/ActiveRecord/RecordEventTest.php new file mode 100644 index 00000000..0114e0a4 --- /dev/null +++ b/tests/unit/Data/ActiveRecord/RecordEventTest.php @@ -0,0 +1,38 @@ +<?php +Prado::using('System.Data.ActiveRecord.TActiveRecord'); +require_once(dirname(__FILE__).'/records/UserRecord.php'); + +/** + * @package System.Data.ActiveRecord + */ +class RecordEventTest extends PHPUnit_Framework_TestCase +{ + function setup() + { + $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'); + $finder = new UserRecord(); + $finder->OnCreateCommand[] = array($this, 'logger'); + $finder->OnExecuteCommand[] = array($this, 'logger'); + $user1 = $finder->find($criteria); + //var_dump($user1); + + //var_dump(UserRecord::finder()->find($criteria)); + } + + function logger($sender, $param) + { + //var_dump($param); + } +} diff --git a/tests/unit/Data/ActiveRecord/SqliteTest.php b/tests/unit/Data/ActiveRecord/SqliteTest.php new file mode 100644 index 00000000..c7f8f515 --- /dev/null +++ b/tests/unit/Data/ActiveRecord/SqliteTest.php @@ -0,0 +1,22 @@ +<?php +Prado::using('System.Data.ActiveRecord.TActiveRecord'); +require_once(dirname(__FILE__).'/records/SqliteUsers.php'); + +/** + * @package System.Data.ActiveRecord + */ +class SqliteTest extends PHPUnit_Framework_TestCase +{ + function setup() + { + $conn = new TDbConnection('sqlite2:'.dirname(__FILE__).'/ar_test.db'); + TActiveRecordManager::getInstance()->setDbConnection($conn); + } + + function test_finder() + { + $finder = SqliteUsers::finder(); + $user = $finder->findByPk('test'); + $this->assertNotNull($user); + } +} diff --git a/tests/unit/Data/ActiveRecord/UserRecordTest.php b/tests/unit/Data/ActiveRecord/UserRecordTest.php new file mode 100644 index 00000000..c582e95b --- /dev/null +++ b/tests/unit/Data/ActiveRecord/UserRecordTest.php @@ -0,0 +1,67 @@ +<?php +Prado::using('System.Data.ActiveRecord.TActiveRecord'); +require_once(dirname(__FILE__).'/records/UserRecord.php'); + +/** + * @package System.Data.ActiveRecord + */ +class UserRecordTest extends PHPUnit_Framework_TestCase +{ + function setup() + { + $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_different_instance() + { + $user1 = UserRecord::finder()->findByPk('admin'); + $this->assertNotNull($user1); + + $user2 = UserRecord::finder()->findByPk('admin'); + $this->assertFalse($user1===$user2); + } + + function testFindByPk_returns_null() + { + $user = UserRecord::finder()->findByPk('me'); + $this->assertNull($user); + } + + function test_Create_new_user_returns_true() + { + $user = new UserRecord; + $user->username = 'hello'; + $user->password = md5('asd'); + $user->email = 'asdasd'; + $user->first_name = 'wei'; + $user->last_name = 'zhuo'; + + $this->assertTrue($user->save()); + + $user->password = md5('more'); + + $this->assertTrue($user->save()); + + $check = UserRecord::finder()->findByPk('hello'); + + $this->assertSameUser($user, $check); + + $this->assertTrue($user->delete()); + } + + function assertSameUser($user,$check) + { + $props = array('username', 'password', 'email', 'first_name', 'last_name', 'job_title', + 'work_phone', 'work_fax', 'active', 'department_id', 'salutation', + 'hint_question', 'hint_answer'); + foreach($props as $prop) + $this->assertEqual($user->$prop,$check->$prop); + } +} diff --git a/tests/unit/Data/ActiveRecord/ViewRecordTest.php b/tests/unit/Data/ActiveRecord/ViewRecordTest.php new file mode 100644 index 00000000..f319e45f --- /dev/null +++ b/tests/unit/Data/ActiveRecord/ViewRecordTest.php @@ -0,0 +1,78 @@ +<?php + + +Prado::using('System.Data.ActiveRecord.TActiveRecord'); +require_once(dirname(__FILE__).'/records/SimpleUser.php'); + +/** + * @package System.Data.ActiveRecord + */ +class ViewRecordTest extends PHPUnit_Framework_TestCase +{ + function setup() + { + $conn = new TDbConnection('pgsql:host=localhost;dbname=test', 'test','test'); + TActiveRecordManager::getInstance()->setDbConnection($conn); + } + + function test_view_record() + { + $users = SimpleUser::finder()->findAll(); + $this->assertTrue(count($users) > 0); + } + + function test_save_view_record_throws_exception() + { + $user = new SimpleUser(); + try + { + $user->save(); + $this->fail(); + } + catch(TActiveRecordException $e) + { + $this->pass(); + } + } + + function test_update_view_record_throws_exception() + { + $user = SimpleUser::finder()->findByUsername('admin'); + $user->username = 'ads'; + try + { + $user->save(); + $this->fail(); + } + catch(TActiveRecordException $e) + { + $this->pass(); + } + } + + function test_find_by_pk_throws_exception() + { + try + { + $user = SimpleUser::finder()->findByPk('admin'); + $this->fail(); + } + catch(TDbException $e) + { + $this->pass(); + } + } + + function test_delete_by_pk_throws_exception() + { + try + { + SimpleUser::finder()->deleteByPk('admin'); + $this->fail(); + } + catch(TDbException $e) + { + $this->pass(); + } + } +}
\ No newline at end of file diff --git a/tests/unit/Data/ActiveRecord/ar_test.db b/tests/unit/Data/ActiveRecord/ar_test.db Binary files differnew file mode 100644 index 00000000..7549bb66 --- /dev/null +++ b/tests/unit/Data/ActiveRecord/ar_test.db diff --git a/tests/unit/Data/ActiveRecord/blog.db b/tests/unit/Data/ActiveRecord/blog.db Binary files differnew file mode 100644 index 00000000..30a9cb7a --- /dev/null +++ b/tests/unit/Data/ActiveRecord/blog.db diff --git a/tests/unit/Data/ActiveRecord/fk_tests.db b/tests/unit/Data/ActiveRecord/fk_tests.db Binary files differnew file mode 100644 index 00000000..87835c84 --- /dev/null +++ b/tests/unit/Data/ActiveRecord/fk_tests.db diff --git a/tests/unit/Data/ActiveRecord/mysql4text.sql b/tests/unit/Data/ActiveRecord/mysql4text.sql new file mode 100644 index 00000000..03d2c010 --- /dev/null +++ b/tests/unit/Data/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/unit/Data/ActiveRecord/records/Blogs.php b/tests/unit/Data/ActiveRecord/records/Blogs.php new file mode 100644 index 00000000..6523f029 --- /dev/null +++ b/tests/unit/Data/ActiveRecord/records/Blogs.php @@ -0,0 +1,12 @@ +<?php +class Blogs extends TActiveRecord +{ + public $blog_id; + public $blog_name; + public $blog_author; + + public static function finder($className=__CLASS__) + { + return parent::finder($className); + } +} diff --git a/tests/unit/Data/ActiveRecord/records/DepSections.php b/tests/unit/Data/ActiveRecord/records/DepSections.php new file mode 100644 index 00000000..bf01ed12 --- /dev/null +++ b/tests/unit/Data/ActiveRecord/records/DepSections.php @@ -0,0 +1,14 @@ +<?php +class DepSections extends TActiveRecord +{ + public $department_id; + public $section_id; + public $order; + + const TABLE='department_sections'; + + public static function finder($className=__CLASS__) + { + return parent::finder($className); + } +} diff --git a/tests/unit/Data/ActiveRecord/records/DepartmentRecord.php b/tests/unit/Data/ActiveRecord/records/DepartmentRecord.php new file mode 100644 index 00000000..b60c7930 --- /dev/null +++ b/tests/unit/Data/ActiveRecord/records/DepartmentRecord.php @@ -0,0 +1,16 @@ +<?php +class DepartmentRecord extends TActiveRecord +{ + public $department_id; + public $name; + public $description; + public $active; + public $order; + + const TABLE = 'departments'; + + public static function finder($className=__CLASS__) + { + return parent::finder($className); + } +} diff --git a/tests/unit/Data/ActiveRecord/records/ItemRecord.php b/tests/unit/Data/ActiveRecord/records/ItemRecord.php new file mode 100644 index 00000000..e6707cde --- /dev/null +++ b/tests/unit/Data/ActiveRecord/records/ItemRecord.php @@ -0,0 +1,47 @@ +<?php + +class ItemRecord extends TActiveRecord +{ + const TABLE='items'; + public $item_id; + public $name; + public $brand_specific; + public $description; + public $meta; + public $active; + public $need_review; + public $category_id; + public $type_id; + public $content; + public $standard_id; + public $timestamp; + + public $related_items = array(); + public $related_item_id; + + public static $RELATIONS=array + ( + 'related_items' => array(self::MANY_TO_MANY, 'ItemRecord', 'related_items.related_item_id'), + ); + + public function getDbConnection() + { + static $conn; + if($conn===null) + { + $conn = new TDbConnection('pgsql:host=localhost;dbname=test', 'test','test'); + $this->OnExecuteCommand[] = array($this,'logger'); + } + return $conn; + } + + public function logger($sender,$param) + { + //var_dump($param->Command->Text); + } + + public static function finder($className=__CLASS__) + { + return parent::finder($className); + } +} diff --git a/tests/unit/Data/ActiveRecord/records/SimpleUser.php b/tests/unit/Data/ActiveRecord/records/SimpleUser.php new file mode 100644 index 00000000..b6748857 --- /dev/null +++ b/tests/unit/Data/ActiveRecord/records/SimpleUser.php @@ -0,0 +1,12 @@ +<?php +class SimpleUser extends TActiveRecord +{ + public $username; + + const TABLE='simple_users'; + + public static function finder($className=__CLASS__) + { + return parent::finder($className); + } +} diff --git a/tests/unit/Data/ActiveRecord/records/SqliteUsers.php b/tests/unit/Data/ActiveRecord/records/SqliteUsers.php new file mode 100644 index 00000000..f8fa12c5 --- /dev/null +++ b/tests/unit/Data/ActiveRecord/records/SqliteUsers.php @@ -0,0 +1,14 @@ +<?php +class SqliteUsers extends TActiveRecord +{ + public $username; + public $password; + public $email; + + const TABLE='users'; + + public static function finder($className=__CLASS__) + { + return parent::finder($className); + } +} diff --git a/tests/unit/Data/ActiveRecord/records/UserRecord.php b/tests/unit/Data/ActiveRecord/records/UserRecord.php new file mode 100644 index 00000000..4ef98baf --- /dev/null +++ b/tests/unit/Data/ActiveRecord/records/UserRecord.php @@ -0,0 +1,36 @@ +<?php +class UserRecord extends TActiveRecord +{ + public $username; + public $password; + public $email; + public $first_name; + public $last_name; + public $job_title; + public $work_phone; + public $work_fax; + public $active=true; + public $department_id; + public $salutation; + public $hint_question; + public $hint_answer; + + private $_level=-1; + + const TABLE='users'; + + public function getLevel() + { + return $this->_level; + } + + public function setLevel($level) + { + $this->_level=TPropertyValue::ensureInteger($level); + } + + public static function finder($className=__CLASS__) + { + return parent::finder($className); + } +} diff --git a/tests/unit/Data/ActiveRecord/sqlite.sql b/tests/unit/Data/ActiveRecord/sqlite.sql new file mode 100644 index 00000000..7854fc43 --- /dev/null +++ b/tests/unit/Data/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/unit/Data/ActiveRecord/test1.sqlite b/tests/unit/Data/ActiveRecord/test1.sqlite Binary files differnew file mode 100644 index 00000000..1e056b52 --- /dev/null +++ b/tests/unit/Data/ActiveRecord/test1.sqlite |