activerecord 1.9.1 → 1.10.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of activerecord might be problematic. Click here for more details.
- data/CHANGELOG +78 -0
- data/README +1 -1
- data/install.rb +7 -42
- data/lib/active_record.rb +2 -0
- data/lib/active_record/acts/list.rb +28 -4
- data/lib/active_record/acts/nested_set.rb +212 -0
- data/lib/active_record/associations.rb +203 -21
- data/lib/active_record/associations/association_proxy.rb +10 -2
- data/lib/active_record/associations/belongs_to_association.rb +0 -1
- data/lib/active_record/associations/has_and_belongs_to_many_association.rb +15 -9
- data/lib/active_record/associations/has_many_association.rb +25 -25
- data/lib/active_record/associations/has_one_association.rb +2 -2
- data/lib/active_record/base.rb +134 -110
- data/lib/active_record/connection_adapters/abstract_adapter.rb +9 -9
- data/lib/active_record/connection_adapters/mysql_adapter.rb +4 -0
- data/lib/active_record/connection_adapters/oci_adapter.rb +2 -2
- data/lib/active_record/connection_adapters/sqlserver_adapter.rb +1 -2
- data/lib/active_record/deprecated_associations.rb +1 -19
- data/lib/active_record/deprecated_finders.rb +41 -0
- data/lib/active_record/fixtures.rb +24 -11
- data/lib/active_record/observer.rb +17 -11
- data/lib/active_record/reflection.rb +5 -1
- data/lib/active_record/transactions.rb +7 -0
- data/lib/active_record/validations.rb +32 -33
- data/rakefile +30 -6
- data/test/associations_go_eager_test.rb +55 -0
- data/test/associations_test.rb +72 -15
- data/test/base_test.rb +15 -21
- data/test/deprecated_associations_test.rb +0 -24
- data/test/deprecated_finder_test.rb +147 -0
- data/test/finder_test.rb +37 -37
- data/test/fixtures/author.rb +3 -0
- data/test/fixtures/authors.yml +7 -0
- data/test/fixtures/categories.yml +7 -0
- data/test/fixtures/categories_posts.yml +11 -0
- data/test/fixtures/category.rb +3 -0
- data/test/fixtures/comment.rb +5 -0
- data/test/fixtures/comments.yml +17 -0
- data/test/fixtures/company.rb +3 -0
- data/test/fixtures/courses.yml +4 -4
- data/test/fixtures/db_definitions/db2.drop.sql +6 -0
- data/test/fixtures/db_definitions/db2.sql +46 -0
- data/test/fixtures/db_definitions/mysql.drop.sql +6 -1
- data/test/fixtures/db_definitions/mysql.sql +60 -12
- data/test/fixtures/db_definitions/mysql2.sql +1 -1
- data/test/fixtures/db_definitions/oci.drop.sql +5 -0
- data/test/fixtures/db_definitions/oci.sql +45 -0
- data/test/fixtures/db_definitions/postgresql.drop.sql +6 -0
- data/test/fixtures/db_definitions/postgresql.sql +45 -0
- data/test/fixtures/db_definitions/sqlite.drop.sql +6 -1
- data/test/fixtures/db_definitions/sqlite.sql +46 -0
- data/test/fixtures/db_definitions/sqlserver.drop.sql +7 -1
- data/test/fixtures/db_definitions/sqlserver.sql +46 -0
- data/test/fixtures/fk_test_has_fk.yml +3 -0
- data/test/fixtures/fk_test_has_pk.yml +2 -0
- data/test/fixtures/mixin.rb +18 -0
- data/test/fixtures/mixins.yml +30 -0
- data/test/fixtures/post.rb +8 -0
- data/test/fixtures/posts.yml +20 -0
- data/test/fixtures/task.rb +3 -0
- data/test/fixtures/tasks.yml +7 -0
- data/test/fixtures_test.rb +34 -2
- data/test/mixin_nested_set_test.rb +184 -0
- data/test/mixin_test.rb +28 -3
- data/test/validations_test.rb +16 -0
- metadata +21 -5
- data/test/fixtures/db_definitions/drop_oracle_tables.sql +0 -35
- data/test/fixtures/db_definitions/drop_oracle_tables2.sql +0 -3
@@ -14,5 +14,10 @@ drop table colnametests;
|
|
14
14
|
drop table mixins;
|
15
15
|
drop table people;
|
16
16
|
drop table binaries;
|
17
|
+
drop table posts;
|
18
|
+
drop table comments;
|
19
|
+
drop table authors;
|
17
20
|
drop table computers;
|
21
|
+
drop table categories;
|
22
|
+
drop table categories_posts;
|
18
23
|
drop sequence rails_sequence;
|
@@ -166,3 +166,48 @@ create table computers (
|
|
166
166
|
id integer not null primary key,
|
167
167
|
developer integer not null references developers initially deferred disable
|
168
168
|
);
|
169
|
+
|
170
|
+
create table posts (
|
171
|
+
id integer not null primary key,
|
172
|
+
author_id integer default null,
|
173
|
+
title varchar(255) default null,
|
174
|
+
type varchar(255) default null,
|
175
|
+
body varchar(3000) default null
|
176
|
+
);
|
177
|
+
|
178
|
+
create table comments (
|
179
|
+
id integer not null primary key,
|
180
|
+
post_id integer default null,
|
181
|
+
type varchar(255) default null,
|
182
|
+
body varchar(3000) default null
|
183
|
+
);
|
184
|
+
|
185
|
+
create table authors (
|
186
|
+
id integer not null primary key,
|
187
|
+
name varchar(255) default null
|
188
|
+
);
|
189
|
+
|
190
|
+
create table tasks (
|
191
|
+
id integer not null primary key,
|
192
|
+
starting date default null,
|
193
|
+
ending date default null
|
194
|
+
);
|
195
|
+
|
196
|
+
create table categories (
|
197
|
+
id integer not null primary key,
|
198
|
+
name varchar(255) default null
|
199
|
+
);
|
200
|
+
|
201
|
+
create table categories_posts (
|
202
|
+
category_id integer not null references developers initially deferred disable,
|
203
|
+
post_id int integer not null references developers initially deferred disable
|
204
|
+
);
|
205
|
+
|
206
|
+
create table fk_test_has_pk (
|
207
|
+
id integer not null primary key
|
208
|
+
);
|
209
|
+
|
210
|
+
create table fk_test_has_fk (
|
211
|
+
id integer not null primary key,
|
212
|
+
fk_id integer not null references fk_test_has_fk initially deferred disable,
|
213
|
+
);
|
@@ -147,3 +147,48 @@ CREATE TABLE computers (
|
|
147
147
|
developer integer NOT NULL
|
148
148
|
);
|
149
149
|
|
150
|
+
CREATE TABLE posts (
|
151
|
+
id serial,
|
152
|
+
author_id integer,
|
153
|
+
title varchar(255),
|
154
|
+
type varchar(255),
|
155
|
+
body text
|
156
|
+
);
|
157
|
+
|
158
|
+
CREATE TABLE comments (
|
159
|
+
id serial,
|
160
|
+
post_id integer,
|
161
|
+
type varchar(255),
|
162
|
+
body text
|
163
|
+
);
|
164
|
+
|
165
|
+
CREATE TABLE authors (
|
166
|
+
id serial,
|
167
|
+
name varchar(255) default NULL
|
168
|
+
);
|
169
|
+
|
170
|
+
CREATE TABLE tasks (
|
171
|
+
id serial,
|
172
|
+
starting timestamp,
|
173
|
+
ending timestamp,
|
174
|
+
PRIMARY KEY (id)
|
175
|
+
);
|
176
|
+
|
177
|
+
CREATE TABLE categories (
|
178
|
+
id serial,
|
179
|
+
name varchar(255)
|
180
|
+
);
|
181
|
+
|
182
|
+
CREATE TABLE categories_posts (
|
183
|
+
category_id integer NOT NULL,
|
184
|
+
post_id integer NOT NULL
|
185
|
+
);
|
186
|
+
|
187
|
+
CREATE TABLE fk_test_has_pk (
|
188
|
+
id INTEGER NOT NULL PRIMARY KEY
|
189
|
+
);
|
190
|
+
|
191
|
+
CREATE TABLE fk_test_has_fk (
|
192
|
+
id INTEGER NOT NULL PRIMARY KEY,
|
193
|
+
fk_id INTEGER NOT NULL REFERENCES fk_test_has_fk(id)
|
194
|
+
);
|
@@ -116,3 +116,49 @@ CREATE TABLE 'computers' (
|
|
116
116
|
'developer' INTEGER NOT NULL
|
117
117
|
);
|
118
118
|
|
119
|
+
CREATE TABLE 'posts' (
|
120
|
+
'id' INTEGER NOT NULL PRIMARY KEY,
|
121
|
+
'author_id' INTEGER,
|
122
|
+
'title' VARCHAR(255) NOT NULL,
|
123
|
+
'type' VARCHAR(255) NOT NULL,
|
124
|
+
'body' TEXT NOT NULL
|
125
|
+
);
|
126
|
+
|
127
|
+
CREATE TABLE 'comments' (
|
128
|
+
'id' INTEGER NOT NULL PRIMARY KEY,
|
129
|
+
'post_id' INTEGER NOT NULL,
|
130
|
+
'type' VARCHAR(255) NOT NULL,
|
131
|
+
'body' TEXT NOT NULL
|
132
|
+
);
|
133
|
+
|
134
|
+
CREATE TABLE 'authors' (
|
135
|
+
'id' INTEGER NOT NULL PRIMARY KEY,
|
136
|
+
'name' VARCHAR(255) NOT NULL
|
137
|
+
);
|
138
|
+
|
139
|
+
CREATE TABLE 'tasks' (
|
140
|
+
'id' INTEGER NOT NULL PRIMARY KEY,
|
141
|
+
'starting' DATETIME DEFAULT NULL,
|
142
|
+
'ending' DATETIME DEFAULT NULL
|
143
|
+
);
|
144
|
+
|
145
|
+
CREATE TABLE 'categories' (
|
146
|
+
'id' INTEGER NOT NULL PRIMARY KEY,
|
147
|
+
'name' VARCHAR(255) NOT NULL
|
148
|
+
);
|
149
|
+
|
150
|
+
CREATE TABLE 'categories_posts' (
|
151
|
+
'category_id' INTEGER NOT NULL,
|
152
|
+
'post_id' INTEGER NOT NULL
|
153
|
+
);
|
154
|
+
|
155
|
+
CREATE TABLE 'fk_test_has_pk' (
|
156
|
+
'id' INTEGER NOT NULL PRIMARY KEY
|
157
|
+
);
|
158
|
+
|
159
|
+
CREATE TABLE 'fk_test_has_fk' (
|
160
|
+
'id' INTEGER NOT NULL PRIMARY KEY,
|
161
|
+
'fk_id' INTEGER NOT NULL,
|
162
|
+
|
163
|
+
FOREIGN KEY ('fk_id') REFERENCES 'fk_test_has_pk'('id')
|
164
|
+
);
|
@@ -14,4 +14,10 @@ DROP TABLE colnametests;
|
|
14
14
|
DROP TABLE mixins;
|
15
15
|
DROP TABLE people;
|
16
16
|
DROP TABLE binaries;
|
17
|
-
DROP TABLE computers;
|
17
|
+
DROP TABLE computers;
|
18
|
+
DROP TABLE posts;
|
19
|
+
DROP TABLE comments;
|
20
|
+
DROP TABLE authors;
|
21
|
+
DROP TABLE tasks;
|
22
|
+
DROP TABLE categories;
|
23
|
+
DROP TABLE categories_posts;
|
@@ -116,3 +116,49 @@ CREATE TABLE computers (
|
|
116
116
|
developer int NOT NULL
|
117
117
|
);
|
118
118
|
|
119
|
+
CREATE TABLE posts (
|
120
|
+
id int NOT NULL IDENTITY(1, 1) PRIMARY KEY,
|
121
|
+
author_id int default NULL,
|
122
|
+
title varchar(255) default NULL,
|
123
|
+
type varchar(255) default NULL,
|
124
|
+
body text default NULL
|
125
|
+
);
|
126
|
+
|
127
|
+
CREATE TABLE comments (
|
128
|
+
id int NOT NULL IDENTITY(1, 1) PRIMARY KEY,
|
129
|
+
post_id int default NULL,
|
130
|
+
type varchar(255) default NULL,
|
131
|
+
body text default NULL
|
132
|
+
);
|
133
|
+
|
134
|
+
CREATE TABLE authors (
|
135
|
+
id int NOT NULL IDENTITY(1, 1) PRIMARY KEY,
|
136
|
+
name varchar(255) default NULL
|
137
|
+
);
|
138
|
+
|
139
|
+
CREATE TABLE tasks (
|
140
|
+
id int NOT NULL IDENTITY(1, 1) PRIMARY KEY,
|
141
|
+
starting datetime default NULL,
|
142
|
+
ending datetime default NULL
|
143
|
+
);
|
144
|
+
|
145
|
+
CREATE TABLE categories (
|
146
|
+
id int NOT NULL IDENTITY(1, 1) PRIMARY KEY,
|
147
|
+
name varchar(255)
|
148
|
+
);
|
149
|
+
|
150
|
+
CREATE TABLE categories_posts (
|
151
|
+
category_id int NOT NULL,
|
152
|
+
post_id int NOT NULL
|
153
|
+
);
|
154
|
+
|
155
|
+
CREATE TABLE fk_test_has_pk (
|
156
|
+
id INTEGER NOT NULL PRIMARY KEY
|
157
|
+
);
|
158
|
+
|
159
|
+
CREATE TABLE fk_test_has_fk (
|
160
|
+
id INTEGER NOT NULL PRIMARY KEY,
|
161
|
+
fk_id INTEGER NOT NULL,
|
162
|
+
|
163
|
+
FOREIGN KEY (fk_id) REFERENCES fk_test_has_pk(id)
|
164
|
+
);
|
data/test/fixtures/mixin.rb
CHANGED
@@ -16,5 +16,23 @@ end
|
|
16
16
|
class ListWithStringScopeMixin < ActiveRecord::Base
|
17
17
|
acts_as_list :column => "pos", :scope => 'parent_id = #{parent_id}'
|
18
18
|
|
19
|
+
def self.table_name() "mixins" end
|
20
|
+
end
|
21
|
+
|
22
|
+
class NestedSet < Mixin
|
23
|
+
acts_as_nested_set :scope => "ROOT_ID IS NULL"
|
24
|
+
|
25
|
+
def self.table_name() "mixins" end
|
26
|
+
end
|
27
|
+
|
28
|
+
class NestedSetWithStringScope < Mixin
|
29
|
+
acts_as_nested_set :scope => 'root_id = #{root_id}'
|
30
|
+
|
31
|
+
def self.table_name() "mixins" end
|
32
|
+
end
|
33
|
+
|
34
|
+
class NestedSetWithSymbolScope < Mixin
|
35
|
+
acts_as_nested_set :scope => :root
|
36
|
+
|
19
37
|
def self.table_name() "mixins" end
|
20
38
|
end
|
data/test/fixtures/mixins.yml
CHANGED
@@ -28,3 +28,33 @@ list_<%= counter %>:
|
|
28
28
|
type: ListMixin
|
29
29
|
parent_id: 5
|
30
30
|
<% end %>
|
31
|
+
|
32
|
+
# Nested set mixins
|
33
|
+
|
34
|
+
<% (1..10).each do |counter| %>
|
35
|
+
set_<%= counter %>:
|
36
|
+
id: <%= counter+3000 %>
|
37
|
+
type: NestedSet
|
38
|
+
<% end %>
|
39
|
+
|
40
|
+
# Big old set
|
41
|
+
<%
|
42
|
+
[[4001, 0, 1, 20],
|
43
|
+
[4002, 4001, 2, 7],
|
44
|
+
[4003, 4002, 3, 4],
|
45
|
+
[4004, 4002, 5, 6],
|
46
|
+
[4005, 4001, 8, 13],
|
47
|
+
[4006, 4005, 9, 10],
|
48
|
+
[4007, 4005, 11, 12],
|
49
|
+
[4008, 4001, 14, 19],
|
50
|
+
[4009, 4008, 15, 16],
|
51
|
+
[4010, 4008, 17, 18]].each do |set| %>
|
52
|
+
tree_<%= set[0] %>:
|
53
|
+
id: <%= set[0]%>
|
54
|
+
parent_id: <%= set[1]%>
|
55
|
+
type: NestedSetWithStringScope
|
56
|
+
lft: <%= set[2]%>
|
57
|
+
rgt: <%= set[3]%>
|
58
|
+
root_id: 42
|
59
|
+
|
60
|
+
<% end %>
|
@@ -0,0 +1,20 @@
|
|
1
|
+
welcome:
|
2
|
+
id: 1
|
3
|
+
author_id: 1
|
4
|
+
title: Welcome to the weblog
|
5
|
+
body: Such a lovely day
|
6
|
+
type: Post
|
7
|
+
|
8
|
+
thinking:
|
9
|
+
id: 2
|
10
|
+
author_id: 1
|
11
|
+
title: So I was thinking
|
12
|
+
body: Like I hopefully always am
|
13
|
+
type: SpecialPost
|
14
|
+
|
15
|
+
authorless:
|
16
|
+
id: 3
|
17
|
+
author_id: 0
|
18
|
+
title: I don't have any comments
|
19
|
+
body: I just don't want to
|
20
|
+
type: Post
|
data/test/fixtures_test.rb
CHANGED
@@ -2,13 +2,14 @@ require 'abstract_unit'
|
|
2
2
|
require 'fixtures/topic'
|
3
3
|
require 'fixtures/developer'
|
4
4
|
require 'fixtures/company'
|
5
|
+
require 'fixtures/task'
|
5
6
|
|
6
7
|
class FixturesTest < Test::Unit::TestCase
|
7
|
-
fixtures :topics, :developers, :accounts
|
8
|
+
fixtures :topics, :developers, :accounts, :tasks
|
8
9
|
|
9
10
|
FIXTURES = %w( accounts companies customers
|
10
11
|
developers developers_projects entrants
|
11
|
-
movies projects subscribers topics )
|
12
|
+
movies projects subscribers topics tasks )
|
12
13
|
MATCH_ATTRIBUTE_NAME = /[a-zA-Z][-_\w]*/
|
13
14
|
|
14
15
|
def test_clean_fixtures
|
@@ -46,6 +47,13 @@ class FixturesTest < Test::Unit::TestCase
|
|
46
47
|
assert_nil(secondRow["author_email_address"])
|
47
48
|
end
|
48
49
|
|
50
|
+
def test_insert_with_datetime
|
51
|
+
topics = create_fixtures("tasks")
|
52
|
+
first = Task.find(1)
|
53
|
+
assert first
|
54
|
+
end
|
55
|
+
|
56
|
+
|
49
57
|
def test_bad_format
|
50
58
|
path = File.join(File.dirname(__FILE__), 'fixtures', 'bad_fixtures')
|
51
59
|
Dir.entries(path).each do |file|
|
@@ -171,3 +179,27 @@ class OverlappingFixturesTest < Test::Unit::TestCase
|
|
171
179
|
assert_equal([:topics, :developers, :accounts], fixture_table_names)
|
172
180
|
end
|
173
181
|
end
|
182
|
+
|
183
|
+
|
184
|
+
class ForeignKeyFixturesTest < Test::Unit::TestCase
|
185
|
+
fixtures :fk_test_has_pk, :fk_test_has_fk
|
186
|
+
|
187
|
+
# if foreign keys are implemented and fixtures
|
188
|
+
# are not deleted in reverse order then this test
|
189
|
+
# case will raise StatementInvalid
|
190
|
+
|
191
|
+
def test_number1
|
192
|
+
assert true
|
193
|
+
end
|
194
|
+
|
195
|
+
def test_number2
|
196
|
+
assert true
|
197
|
+
end
|
198
|
+
|
199
|
+
end
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
|
@@ -0,0 +1,184 @@
|
|
1
|
+
require 'abstract_unit'
|
2
|
+
require 'active_record/acts/nested_set'
|
3
|
+
require 'fixtures/mixin'
|
4
|
+
require 'pp'
|
5
|
+
|
6
|
+
class MixinNestedSetTest < Test::Unit::TestCase
|
7
|
+
fixtures :mixins
|
8
|
+
|
9
|
+
def test_mixing_in_methods
|
10
|
+
ns = NestedSet.new
|
11
|
+
assert( ns.respond_to?( :all_children ) )
|
12
|
+
assert_equal( ns.scope_condition, "ROOT_ID IS NULL" )
|
13
|
+
|
14
|
+
check_method_mixins ns
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_string_scope
|
18
|
+
ns = NestedSetWithStringScope.new
|
19
|
+
|
20
|
+
ns.root_id = 1
|
21
|
+
assert_equal( ns.scope_condition, "root_id = 1" )
|
22
|
+
ns.root_id = 42
|
23
|
+
assert_equal( ns.scope_condition, "root_id = 42" )
|
24
|
+
check_method_mixins ns
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_symbol_scope
|
28
|
+
ns = NestedSetWithSymbolScope.new
|
29
|
+
ns.root_id = 1
|
30
|
+
assert_equal( ns.scope_condition, "root_id = 1" )
|
31
|
+
ns.root_id = 42
|
32
|
+
assert_equal( ns.scope_condition, "root_id = 42" )
|
33
|
+
check_method_mixins ns
|
34
|
+
end
|
35
|
+
|
36
|
+
def check_method_mixins( obj )
|
37
|
+
[:scope_condition, :left_col_name, :right_col_name, :parent_column, :root?, :add_child,
|
38
|
+
:children_count, :full_set, :all_children, :direct_children].each { |symbol| assert( obj.respond_to?(symbol)) }
|
39
|
+
end
|
40
|
+
|
41
|
+
def set( id )
|
42
|
+
NestedSet.find( 3000 + id )
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_adding_children
|
46
|
+
assert( set(1).unknown? )
|
47
|
+
assert( set(2).unknown? )
|
48
|
+
set(1).add_child set(2)
|
49
|
+
|
50
|
+
# Did we maintain adding the parent_ids?
|
51
|
+
assert( set(1).root? )
|
52
|
+
assert( set(2).child? )
|
53
|
+
assert( set(2).parent_id == set(1).id )
|
54
|
+
|
55
|
+
# Check boundies
|
56
|
+
assert_equal( set(1).lft, 1 )
|
57
|
+
assert_equal( set(2).lft, 2 )
|
58
|
+
assert_equal( set(2).rgt, 3 )
|
59
|
+
assert_equal( set(1).rgt, 4 )
|
60
|
+
|
61
|
+
# Check children cound
|
62
|
+
assert_equal( set(1).children_count, 1 )
|
63
|
+
|
64
|
+
set(1).add_child set(3)
|
65
|
+
|
66
|
+
#check boundries
|
67
|
+
assert_equal( set(1).lft, 1 )
|
68
|
+
assert_equal( set(2).lft, 2 )
|
69
|
+
assert_equal( set(2).rgt, 3 )
|
70
|
+
assert_equal( set(3).lft, 4 )
|
71
|
+
assert_equal( set(3).rgt, 5 )
|
72
|
+
assert_equal( set(1).rgt, 6 )
|
73
|
+
|
74
|
+
# How is the count looking?
|
75
|
+
assert_equal( set(1).children_count, 2 )
|
76
|
+
|
77
|
+
set(2).add_child set(4)
|
78
|
+
|
79
|
+
# boundries
|
80
|
+
assert_equal( set(1).lft, 1 )
|
81
|
+
assert_equal( set(2).lft, 2 )
|
82
|
+
assert_equal( set(4).lft, 3 )
|
83
|
+
assert_equal( set(4).rgt, 4 )
|
84
|
+
assert_equal( set(2).rgt, 5 )
|
85
|
+
assert_equal( set(3).lft, 6 )
|
86
|
+
assert_equal( set(3).rgt, 7 )
|
87
|
+
assert_equal( set(1).rgt, 8 )
|
88
|
+
|
89
|
+
# Children count
|
90
|
+
assert_equal( set(1).children_count, 3 )
|
91
|
+
assert_equal( set(2).children_count, 1 )
|
92
|
+
assert_equal( set(3).children_count, 0 )
|
93
|
+
assert_equal( set(4).children_count, 0 )
|
94
|
+
|
95
|
+
set(2).add_child set(5)
|
96
|
+
set(4).add_child set(6)
|
97
|
+
|
98
|
+
assert_equal( set(2).children_count, 3 )
|
99
|
+
|
100
|
+
|
101
|
+
# Children accessors
|
102
|
+
assert_equal( set(1).full_set.length, 6 )
|
103
|
+
assert_equal( set(2).full_set.length, 4 )
|
104
|
+
assert_equal( set(4).full_set.length, 2 )
|
105
|
+
|
106
|
+
assert_equal( set(1).all_children.length, 5 )
|
107
|
+
assert_equal( set(6).all_children.length, 0 )
|
108
|
+
|
109
|
+
assert_equal( set(1).direct_children.length, 2 )
|
110
|
+
|
111
|
+
end
|
112
|
+
|
113
|
+
def test_snipping_tree
|
114
|
+
big_tree = NestedSetWithStringScope.find( 4001 )
|
115
|
+
|
116
|
+
# Make sure we have the right one
|
117
|
+
assert_equal( 3, big_tree.direct_children.length )
|
118
|
+
assert_equal( 10, big_tree.full_set.length )
|
119
|
+
|
120
|
+
NestedSetWithStringScope.find( 4005 ).destroy
|
121
|
+
|
122
|
+
big_tree = NestedSetWithStringScope.find( 4001 )
|
123
|
+
|
124
|
+
assert_equal( 7, big_tree.full_set.length )
|
125
|
+
assert_equal( 2, big_tree.direct_children.length )
|
126
|
+
|
127
|
+
assert_equal( 1, NestedSetWithStringScope.find(4001).lft )
|
128
|
+
assert_equal( 2, NestedSetWithStringScope.find(4002).lft )
|
129
|
+
assert_equal( 3, NestedSetWithStringScope.find(4003).lft )
|
130
|
+
assert_equal( 4, NestedSetWithStringScope.find(4003).rgt )
|
131
|
+
assert_equal( 5, NestedSetWithStringScope.find(4004).lft )
|
132
|
+
assert_equal( 6, NestedSetWithStringScope.find(4004).rgt )
|
133
|
+
assert_equal( 7, NestedSetWithStringScope.find(4002).rgt )
|
134
|
+
assert_equal( 8, NestedSetWithStringScope.find(4008).lft )
|
135
|
+
assert_equal( 9, NestedSetWithStringScope.find(4009).lft )
|
136
|
+
assert_equal(10, NestedSetWithStringScope.find(4009).rgt )
|
137
|
+
assert_equal(11, NestedSetWithStringScope.find(4010).lft )
|
138
|
+
assert_equal(12, NestedSetWithStringScope.find(4010).rgt )
|
139
|
+
assert_equal(13, NestedSetWithStringScope.find(4008).rgt )
|
140
|
+
assert_equal(14, NestedSetWithStringScope.find(4001).rgt )
|
141
|
+
end
|
142
|
+
|
143
|
+
def test_deleting_root
|
144
|
+
NestedSetWithStringScope.find(4001).destroy
|
145
|
+
|
146
|
+
assert( NestedSetWithStringScope.find_all.length == 0 )
|
147
|
+
end
|
148
|
+
|
149
|
+
def test_common_usage
|
150
|
+
@set_1.add_child( @set_2 )
|
151
|
+
assert_equal( 1, @set_1.direct_children.length )
|
152
|
+
|
153
|
+
@set_2.add_child( @set_3 )
|
154
|
+
assert_equal( 1, @set_1.direct_children.length )
|
155
|
+
|
156
|
+
# Local cache is now out of date!
|
157
|
+
# Problem: the update_alls update all objects up the tree
|
158
|
+
@set_1.reload
|
159
|
+
assert_equal( 2, @set_1.all_children.length )
|
160
|
+
|
161
|
+
assert_equal( 1, @set_1.lft )
|
162
|
+
assert_equal( 2, @set_2.lft )
|
163
|
+
assert_equal( 3, @set_3.lft )
|
164
|
+
assert_equal( 4, @set_3.rgt )
|
165
|
+
assert_equal( 5, @set_2.rgt )
|
166
|
+
assert_equal( 6, @set_1.rgt )
|
167
|
+
|
168
|
+
assert( @set_1.root? )
|
169
|
+
|
170
|
+
begin
|
171
|
+
@set_4.add_child( @set_1 )
|
172
|
+
fail
|
173
|
+
rescue
|
174
|
+
end
|
175
|
+
|
176
|
+
assert_equal( 2, @set_1.all_children.length )
|
177
|
+
|
178
|
+
@set_1.add_child @set_4
|
179
|
+
|
180
|
+
assert_equal( 3, @set_1.all_children.length )
|
181
|
+
|
182
|
+
|
183
|
+
end
|
184
|
+
end
|