1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
<?php
Prado::using('System.Data.ActiveRecord.TActiveRecord');
require_once(dirname(__FILE__).'/records/ItemRecord.php');
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::HAS_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::HAS_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 ForeignKeyTestCase extends UnitTestCase
{
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);
}
}
?>
|