property 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +8 -0
- data/Rakefile +1 -1
- data/lib/property/column.rb +19 -5
- data/lib/property/db.rb +13 -4
- data/lib/property/index.rb +19 -10
- data/lib/property/role_module.rb +3 -9
- data/lib/property/serialization/json.rb +13 -9
- data/lib/property/serialization/marshal.rb +17 -10
- data/lib/property/serialization/yaml.rb +15 -7
- data/lib/property/stored_role.rb +1 -1
- data/lib/property/version.rb +3 -0
- data/lib/property.rb +0 -2
- data/property.gemspec +8 -3
- data/test/database.rb +103 -0
- data/test/fixtures.rb +23 -103
- data/test/shoulda_macros/index.rb +12 -23
- data/test/shoulda_macros/role.rb +4 -0
- data/test/shoulda_macros/serialization.rb +17 -1
- data/test/test_helper.rb +4 -0
- data/test/unit/property/attribute_test.rb +5 -5
- data/test/unit/property/column_test.rb +66 -0
- data/test/unit/property/declaration_test.rb +4 -4
- data/test/unit/property/index_complex_test.rb +26 -32
- data/test/unit/property/index_foreign_test.rb +21 -33
- data/test/unit/property/index_simple_test.rb +22 -22
- data/test/unit/property/validation_test.rb +3 -3
- metadata +8 -3
@@ -1,18 +1,7 @@
|
|
1
1
|
module IndexMacros
|
2
|
-
class IndexedStringEmp < ActiveRecord::Base
|
3
|
-
set_table_name :i_string_employees
|
4
|
-
end
|
5
|
-
|
6
|
-
class MLIndexedStringEmp < ActiveRecord::Base
|
7
|
-
set_table_name :i_ml_string_employees
|
8
|
-
end
|
9
|
-
|
10
|
-
class IndexedIntegerEmp < ActiveRecord::Base
|
11
|
-
set_table_name :i_integer_employees
|
12
|
-
end
|
13
|
-
|
14
2
|
# Simple class
|
15
|
-
class
|
3
|
+
class Client < ActiveRecord::Base
|
4
|
+
set_table_name :employees
|
16
5
|
include Property
|
17
6
|
|
18
7
|
def index_reader(group_name)
|
@@ -31,7 +20,7 @@ class Test::Unit::TestCase
|
|
31
20
|
|
32
21
|
context "assigned to an instance of Dummy" do
|
33
22
|
subject do
|
34
|
-
dummy = IndexMacros::
|
23
|
+
dummy = IndexMacros::Client.new
|
35
24
|
dummy.has_role @poet
|
36
25
|
dummy
|
37
26
|
end
|
@@ -41,22 +30,22 @@ class Test::Unit::TestCase
|
|
41
30
|
end
|
42
31
|
|
43
32
|
should 'create multilingual string indices on save' do
|
44
|
-
assert_difference('
|
33
|
+
assert_difference('IdxEmployeesMlString.count', 2) do
|
45
34
|
subject.poem = 'Hyperions Schicksalslied'
|
46
35
|
subject.save
|
47
36
|
end
|
48
37
|
end
|
49
38
|
|
50
39
|
should 'create integer indices on save' do
|
51
|
-
assert_difference('
|
40
|
+
assert_difference('IdxEmployeesInteger.count', 1) do
|
52
41
|
subject.year = 1770
|
53
42
|
subject.save
|
54
43
|
end
|
55
44
|
end
|
56
45
|
|
57
46
|
should 'not create blank indices on save' do
|
58
|
-
assert_difference('
|
59
|
-
assert_difference('
|
47
|
+
assert_difference('IdxEmployeesString.count', 0) do
|
48
|
+
assert_difference('IdxEmployeesInteger.count', 0) do
|
60
49
|
subject.save
|
61
50
|
end
|
62
51
|
end
|
@@ -68,14 +57,14 @@ class Test::Unit::TestCase
|
|
68
57
|
|
69
58
|
context "assigned to an instance of Dummy" do
|
70
59
|
subject do
|
71
|
-
dummy = IndexMacros::
|
60
|
+
dummy = IndexMacros::Client.new
|
72
61
|
dummy.has_role @poet
|
73
62
|
dummy
|
74
63
|
end
|
75
64
|
|
76
65
|
should 'not create indices on save' do
|
77
|
-
assert_difference('
|
78
|
-
assert_difference('
|
66
|
+
assert_difference('IdxEmployeesString.count', 0) do
|
67
|
+
assert_difference('IdxEmployeesInteger.count', 0) do
|
79
68
|
subject.year = 1770
|
80
69
|
subject.poem = 'Hyperions Schicksalslied'
|
81
70
|
subject.save
|
@@ -84,8 +73,8 @@ class Test::Unit::TestCase
|
|
84
73
|
end
|
85
74
|
|
86
75
|
should 'not create blank indices on save' do
|
87
|
-
assert_difference('
|
88
|
-
assert_difference('
|
76
|
+
assert_difference('IdxEmployeesString.count', 0) do
|
77
|
+
assert_difference('IdxEmployeesInteger.count', 0) do
|
89
78
|
subject.save
|
90
79
|
end
|
91
80
|
end
|
data/test/shoulda_macros/role.rb
CHANGED
@@ -139,6 +139,10 @@ class Test::Unit::TestCase
|
|
139
139
|
assert @klass.has_role?(@poet)
|
140
140
|
end
|
141
141
|
|
142
|
+
should 'return role from column' do
|
143
|
+
@klass.has_role @poet
|
144
|
+
assert_equal (@poet.kind_of?(Class) ? @poet.schema.role : @poet), @klass.schema.columns['poem'].role
|
145
|
+
end
|
142
146
|
end
|
143
147
|
|
144
148
|
context 'to an instance' do
|
@@ -20,7 +20,7 @@ class Test::Unit::TestCase
|
|
20
20
|
setup do
|
21
21
|
@properties = Property::Properties[
|
22
22
|
'string' => "one\ntwo",
|
23
|
-
'serialized' =>
|
23
|
+
'serialized' => Cat.new('Pavlov', 'Freud'),
|
24
24
|
'datetime' => Time.utc(2010, 02, 12, 21, 31, 25),
|
25
25
|
'float' => 4.3432,
|
26
26
|
'integer' => 4
|
@@ -38,6 +38,22 @@ class Test::Unit::TestCase
|
|
38
38
|
assert_equal @properties, properties
|
39
39
|
end
|
40
40
|
|
41
|
+
context 'with ascii 18' do
|
42
|
+
subject do
|
43
|
+
Property::Properties[
|
44
|
+
'string' => (1..255).to_a.map{|n| n.chr}.join('') # "AB"
|
45
|
+
]
|
46
|
+
end
|
47
|
+
|
48
|
+
should 'encode and decode ascii 18' do
|
49
|
+
string = @obj.encode_properties(subject)
|
50
|
+
properties = @obj.decode_properties(string)
|
51
|
+
assert_equal Property::Properties, properties.class
|
52
|
+
assert_equal subject, properties
|
53
|
+
end
|
54
|
+
end # with ascii 18
|
55
|
+
|
56
|
+
|
41
57
|
should 'not include instance variables' do
|
42
58
|
@properties.instance_eval do
|
43
59
|
@baz = 'some data'
|
data/test/test_helper.rb
CHANGED
@@ -1,12 +1,16 @@
|
|
1
1
|
require 'pathname'
|
2
2
|
$LOAD_PATH.unshift((Pathname(__FILE__).dirname + '..' + 'lib').expand_path)
|
3
|
+
|
3
4
|
require 'rubygems'
|
4
5
|
require 'test/unit'
|
5
6
|
require 'shoulda'
|
6
7
|
require 'active_record'
|
8
|
+
|
9
|
+
require 'database'
|
7
10
|
require 'property'
|
8
11
|
require 'shoulda_macros/serialization'
|
9
12
|
require 'shoulda_macros/role'
|
10
13
|
require 'shoulda_macros/index'
|
14
|
+
|
11
15
|
require 'fixtures'
|
12
16
|
require 'active_support/test_case'
|
@@ -217,26 +217,26 @@ class AttributeTest < Test::Unit::TestCase
|
|
217
217
|
|
218
218
|
context 'a saved serialized class' do
|
219
219
|
setup do
|
220
|
-
@
|
220
|
+
@cat = Cat.new('Joann', 'Sfar')
|
221
221
|
end
|
222
222
|
|
223
223
|
subject do
|
224
224
|
klass = Class.new(ActiveRecord::Base) do
|
225
225
|
include Property
|
226
226
|
set_table_name :dummies
|
227
|
-
property.serialize 'myserialized',
|
227
|
+
property.serialize 'myserialized', Cat
|
228
228
|
end
|
229
229
|
|
230
|
-
obj = klass.create('myserialized' => @
|
230
|
+
obj = klass.create('myserialized' => @cat)
|
231
231
|
klass.find(obj)
|
232
232
|
end
|
233
233
|
|
234
234
|
should 'find class back' do
|
235
|
-
assert_kind_of
|
235
|
+
assert_kind_of Cat, subject.prop['myserialized']
|
236
236
|
end
|
237
237
|
|
238
238
|
should 'find same value' do
|
239
|
-
assert_equal @
|
239
|
+
assert_equal @cat, subject.prop['myserialized']
|
240
240
|
end
|
241
241
|
end
|
242
242
|
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
class AttributeTest < Test::Unit::TestCase
|
3
|
+
|
4
|
+
context 'Creating Column instances' do
|
5
|
+
context 'without index' do
|
6
|
+
subject do
|
7
|
+
Property::Column.new('name', nil, 'string', {})
|
8
|
+
end
|
9
|
+
|
10
|
+
should 'set index to nil' do
|
11
|
+
assert_nil subject.index
|
12
|
+
end
|
13
|
+
end # without index
|
14
|
+
|
15
|
+
context 'with index true' do
|
16
|
+
subject do
|
17
|
+
Property::Column.new('foo', nil, 'string', {:index => true})
|
18
|
+
end
|
19
|
+
|
20
|
+
should 'set index to type' do
|
21
|
+
assert_equal 'string', subject.index
|
22
|
+
end
|
23
|
+
|
24
|
+
context 'and an index_group' do
|
25
|
+
subject do
|
26
|
+
Property::Column.new('foo', nil, 'string', {:index => true, :index_group => :integer})
|
27
|
+
end
|
28
|
+
|
29
|
+
should 'set index to index_group' do
|
30
|
+
assert_equal 'integer', subject.index
|
31
|
+
end
|
32
|
+
end # and an index_group
|
33
|
+
end # with index true
|
34
|
+
|
35
|
+
|
36
|
+
context 'with an index name' do
|
37
|
+
subject do
|
38
|
+
Property::Column.new('foo', nil, 'string', {:index => :special})
|
39
|
+
end
|
40
|
+
|
41
|
+
should 'set index to index name' do
|
42
|
+
assert_equal 'special', subject.index
|
43
|
+
end
|
44
|
+
end # with an index name
|
45
|
+
|
46
|
+
context 'with an index proc' do
|
47
|
+
subject do
|
48
|
+
Property::Column.new('foo', nil, 'string', {:index => Proc.new{}})
|
49
|
+
end
|
50
|
+
|
51
|
+
should 'set index to type' do
|
52
|
+
assert_equal 'string', subject.index
|
53
|
+
end
|
54
|
+
|
55
|
+
context 'and an index_group' do
|
56
|
+
subject do
|
57
|
+
Property::Column.new('foo', nil, 'string', {:index => Proc.new{}, :index_group => :ml_string})
|
58
|
+
end
|
59
|
+
|
60
|
+
should 'set index to index_group' do
|
61
|
+
assert_equal 'ml_string', subject.index
|
62
|
+
end
|
63
|
+
end # and an index_group
|
64
|
+
end # with an index proc
|
65
|
+
end # Creating Column instances
|
66
|
+
end
|
@@ -167,9 +167,9 @@ class DeclarationTest < Test::Unit::TestCase
|
|
167
167
|
end
|
168
168
|
|
169
169
|
should 'allow serialized columns' do
|
170
|
-
|
170
|
+
Cat = Struct.new(:name, :toy) do
|
171
171
|
def self.json_create(data)
|
172
|
-
|
172
|
+
Cat.new(data['name'], data['toy'])
|
173
173
|
end
|
174
174
|
def to_json(*args)
|
175
175
|
{ 'json_class' => self.class.to_s,
|
@@ -178,10 +178,10 @@ class DeclarationTest < Test::Unit::TestCase
|
|
178
178
|
end
|
179
179
|
end
|
180
180
|
|
181
|
-
subject.property.serialize('pet',
|
181
|
+
subject.property.serialize('pet', Cat)
|
182
182
|
column = subject.schema.columns['pet']
|
183
183
|
assert_equal 'pet', column.name
|
184
|
-
assert_equal
|
184
|
+
assert_equal Cat, column.klass
|
185
185
|
assert_equal nil, column.type
|
186
186
|
end
|
187
187
|
|
@@ -2,18 +2,6 @@ require 'test_helper'
|
|
2
2
|
require 'fixtures'
|
3
3
|
|
4
4
|
class IndexComplexTest < ActiveSupport::TestCase
|
5
|
-
class IndexedStringEmp < ActiveRecord::Base
|
6
|
-
set_table_name :i_string_employees
|
7
|
-
end
|
8
|
-
|
9
|
-
class IndexedIntegerEmp < ActiveRecord::Base
|
10
|
-
set_table_name :i_integer_employees
|
11
|
-
end
|
12
|
-
|
13
|
-
class IndexedTextEmp < ActiveRecord::Base
|
14
|
-
set_table_name :i_text_employees
|
15
|
-
end
|
16
|
-
|
17
5
|
# Complex index definition class
|
18
6
|
class Person < ActiveRecord::Base
|
19
7
|
include Property
|
@@ -31,14 +19,14 @@ class IndexComplexTest < ActiveSupport::TestCase
|
|
31
19
|
property do |p|
|
32
20
|
p.string 'name'
|
33
21
|
# only runs if 'age' is not blank
|
34
|
-
p.integer 'age',
|
35
|
-
p.string 'gender'
|
22
|
+
p.integer 'age', :index => Proc.new {|r| {'age' => r.age == 0 ? nil : r.age + 10}}
|
23
|
+
p.string 'gender', :index => Proc.new {|r| {'gender' => r.gender[0]}}, :index_group => :integer
|
36
24
|
p.string 'lang'
|
37
25
|
|
38
26
|
p.index(:text) do |r| # r = record
|
39
27
|
{
|
40
28
|
"high" => "gender:#{r.gender} age:#{r.age} name:#{r.name}",
|
41
|
-
"name_#{r.lang}" => r.name, # multi-lingual index
|
29
|
+
"name_#{r.lang}" => r.name, # multi-lingual index only matching existing object in required lang
|
42
30
|
}
|
43
31
|
end
|
44
32
|
end
|
@@ -65,35 +53,41 @@ class IndexComplexTest < ActiveSupport::TestCase
|
|
65
53
|
|
66
54
|
context 'on record creation' do
|
67
55
|
should 'create index entries' do
|
68
|
-
assert_difference('
|
56
|
+
assert_difference('IdxEmployeesText.count', 2) do
|
69
57
|
Person.create('name' => 'Juan', 'lang' => 'es', 'gender' => 'M', 'age' => 34)
|
70
58
|
end
|
71
59
|
end
|
72
60
|
|
73
61
|
should 'not create index entries for blank values' do
|
74
|
-
assert_difference('
|
62
|
+
assert_difference('IdxEmployeesInteger.count', 0) do
|
75
63
|
Person.create('name' => 'Pavlov')
|
76
64
|
end
|
77
65
|
end
|
78
66
|
|
79
67
|
should 'store key and value pairs linked to the model' do
|
80
68
|
person = Person.create('name' => 'Juan', 'lang' => 'es', 'gender' => 'M', 'age' => 34)
|
81
|
-
high_index, name_index =
|
69
|
+
high_index, name_index = IdxEmployeesText.all(:conditions => {:employee_id => person.id}, :order => 'key asc')
|
82
70
|
assert_equal 'high', high_index.key
|
83
71
|
assert_equal 'gender:M age:34 name:Juan', high_index.value
|
84
72
|
assert_equal 'name_es', name_index.key
|
85
73
|
assert_equal 'Juan', name_index.value
|
86
74
|
end
|
87
75
|
|
88
|
-
should 'execute index Proc to build
|
76
|
+
should 'execute index Proc to build values' do
|
89
77
|
person = Person.create('name' => 'Juan', 'lang' => 'es', 'gender' => 'M', 'age' => 34)
|
90
|
-
int_index =
|
78
|
+
int_index = IdxEmployeesInteger.first(:conditions => {:employee_id => person.id, :key => 'age'})
|
91
79
|
assert_equal 44, int_index.value
|
92
80
|
end
|
81
|
+
|
82
|
+
should 'use index_group setting to build values' do
|
83
|
+
person = Person.create('name' => 'Juan', 'lang' => 'es', 'gender' => 'M', 'age' => 34)
|
84
|
+
int_index = IdxEmployeesInteger.first(:conditions => {:employee_id => person.id, :key => 'gender'})
|
85
|
+
assert_equal "M"[0], int_index.value
|
86
|
+
end
|
93
87
|
|
94
88
|
should 'remove blank values built from proc execution' do
|
95
89
|
person = Person.create('name' => 'Juan', 'lang' => 'es', 'gender' => 'M', 'age' => 34)
|
96
|
-
assert_difference('
|
90
|
+
assert_difference('IdxEmployeesInteger.count', -1) do
|
97
91
|
person.update_attributes('age' => 0)
|
98
92
|
end
|
99
93
|
end
|
@@ -105,13 +99,13 @@ class IndexComplexTest < ActiveSupport::TestCase
|
|
105
99
|
end
|
106
100
|
|
107
101
|
should 'update index entries' do
|
108
|
-
high_index, name_index =
|
109
|
-
assert_difference('
|
102
|
+
high_index, name_index = IdxEmployeesText.all(:conditions => {:employee_id => @person.id}, :order => 'key asc')
|
103
|
+
assert_difference('IdxEmployeesText.count', 0) do
|
110
104
|
@person.update_attributes('name' => 'Xavier')
|
111
105
|
end
|
112
106
|
|
113
|
-
high_index =
|
114
|
-
name_index =
|
107
|
+
high_index = IdxEmployeesText.find(high_index.id) # reload (make sure the record has been updated, not recreated)
|
108
|
+
name_index = IdxEmployeesText.find(name_index.id) # reload (make sure the record has been updated, not recreated)
|
115
109
|
|
116
110
|
assert_equal 'high', high_index.key
|
117
111
|
assert_equal 'gender:M age:34 name:Xavier', high_index.value
|
@@ -121,15 +115,15 @@ class IndexComplexTest < ActiveSupport::TestCase
|
|
121
115
|
|
122
116
|
context 'with key alterations' do
|
123
117
|
should 'remove and create new keys' do
|
124
|
-
high_index, name_index =
|
125
|
-
assert_difference('
|
118
|
+
high_index, name_index = IdxEmployeesText.all(:conditions => {:employee_id => @person.id}, :order => 'key asc')
|
119
|
+
assert_difference('IdxEmployeesText.count', 0) do
|
126
120
|
@person.update_attributes('lang' => 'en', 'name' => 'John')
|
127
121
|
end
|
128
122
|
|
129
|
-
assert
|
130
|
-
assert_nil
|
123
|
+
assert IdxEmployeesText.find(high_index.id)
|
124
|
+
assert_nil IdxEmployeesText.find_by_id(name_index.id)
|
131
125
|
|
132
|
-
high_index, name_index =
|
126
|
+
high_index, name_index = IdxEmployeesText.all(:conditions => {:employee_id => @person.id}, :order => 'key asc')
|
133
127
|
|
134
128
|
assert_equal 'high', high_index.key
|
135
129
|
assert_equal 'gender:M age:34 name:John', high_index.value
|
@@ -142,8 +136,8 @@ class IndexComplexTest < ActiveSupport::TestCase
|
|
142
136
|
context 'on record destruction' do
|
143
137
|
should 'remove index entries' do
|
144
138
|
person = Person.create('name' => 'Juan', 'lang' => 'es', 'gender' => 'M', 'age' => 34)
|
145
|
-
assert_difference('
|
146
|
-
assert_difference('
|
139
|
+
assert_difference('IdxEmployeesText.count', -2) do
|
140
|
+
assert_difference('IdxEmployeesInteger.count', -2) do
|
147
141
|
person.destroy
|
148
142
|
end
|
149
143
|
end
|
@@ -2,18 +2,6 @@ require 'test_helper'
|
|
2
2
|
require 'fixtures'
|
3
3
|
|
4
4
|
class IndexForeignTest < ActiveSupport::TestCase
|
5
|
-
class IndexedStringEmp < ActiveRecord::Base
|
6
|
-
set_table_name :i_string_employees
|
7
|
-
end
|
8
|
-
|
9
|
-
class IndexedIntegerEmp < ActiveRecord::Base
|
10
|
-
set_table_name :i_integer_employees
|
11
|
-
end
|
12
|
-
|
13
|
-
class IndexedTextEmp < ActiveRecord::Base
|
14
|
-
set_table_name :i_text_employees
|
15
|
-
end
|
16
|
-
|
17
5
|
class Version < ActiveRecord::Base
|
18
6
|
belongs_to :contact, :class_name => 'IndexForeignTest::Contact',
|
19
7
|
:foreign_key => 'employee_id'
|
@@ -74,14 +62,14 @@ class IndexForeignTest < ActiveSupport::TestCase
|
|
74
62
|
|
75
63
|
context 'on record creation' do
|
76
64
|
should 'create index entries' do
|
77
|
-
assert_difference('
|
65
|
+
assert_difference('IdxEmployeesString.count', 2) do
|
78
66
|
Contact.create('name' => 'Juan', 'lang' => 'es', 'gender' => 'M', 'age' => 34)
|
79
67
|
end
|
80
68
|
end
|
81
69
|
|
82
70
|
should 'store key and value pairs linked to the model' do
|
83
71
|
person = Contact.create('name' => 'Juan', 'lang' => 'es', 'gender' => 'M', 'age' => 34)
|
84
|
-
high_index, name_index =
|
72
|
+
high_index, name_index = IdxEmployeesString.all(:conditions => {:version_id => person.version.id}, :order => 'key asc')
|
85
73
|
assert_equal 'high', high_index.key
|
86
74
|
assert_equal 'gender:M age:34 name:Juan', high_index.value
|
87
75
|
assert_equal 'name_es', name_index.key
|
@@ -90,7 +78,7 @@ class IndexForeignTest < ActiveSupport::TestCase
|
|
90
78
|
|
91
79
|
should 'store key and value pairs linked to the foreign model' do
|
92
80
|
person = Contact.create('name' => 'Juan', 'lang' => 'es', 'gender' => 'M', 'age' => 34)
|
93
|
-
high_index, name_index =
|
81
|
+
high_index, name_index = IdxEmployeesString.all(:conditions => {:employee_id => person.id}, :order => 'key asc')
|
94
82
|
assert_equal 'high', high_index.key
|
95
83
|
assert_equal 'gender:M age:34 name:Juan', high_index.value
|
96
84
|
assert_equal 'name_es', name_index.key
|
@@ -104,13 +92,13 @@ class IndexForeignTest < ActiveSupport::TestCase
|
|
104
92
|
end
|
105
93
|
|
106
94
|
should 'update index entries' do
|
107
|
-
high_index, name_index =
|
108
|
-
assert_difference('
|
95
|
+
high_index, name_index = IdxEmployeesString.all(:conditions => {:employee_id => @person.id}, :order => 'key asc')
|
96
|
+
assert_difference('IdxEmployeesString.count', 0) do
|
109
97
|
@person.update_attributes('name' => 'Xavier')
|
110
98
|
end
|
111
99
|
|
112
|
-
high_index =
|
113
|
-
name_index =
|
100
|
+
high_index = IdxEmployeesString.find(high_index.id) # reload (make sure the record has been updated, not recreated)
|
101
|
+
name_index = IdxEmployeesString.find(name_index.id) # reload (make sure the record has been updated, not recreated)
|
114
102
|
|
115
103
|
assert_equal 'high', high_index.key
|
116
104
|
assert_equal 'gender:M age:34 name:Xavier', high_index.value
|
@@ -120,15 +108,15 @@ class IndexForeignTest < ActiveSupport::TestCase
|
|
120
108
|
|
121
109
|
context 'with key alterations' do
|
122
110
|
should 'remove and create new keys' do
|
123
|
-
high_index, name_index =
|
124
|
-
assert_difference('
|
111
|
+
high_index, name_index = IdxEmployeesString.all(:conditions => {:employee_id => @person.id}, :order => 'key asc')
|
112
|
+
assert_difference('IdxEmployeesString.count', 0) do
|
125
113
|
@person.update_attributes('lang' => 'en', 'name' => 'John')
|
126
114
|
end
|
127
115
|
|
128
|
-
assert
|
129
|
-
assert_nil
|
116
|
+
assert IdxEmployeesString.find(high_index.id)
|
117
|
+
assert_nil IdxEmployeesString.find_by_id(name_index.id)
|
130
118
|
|
131
|
-
high_index, name_index =
|
119
|
+
high_index, name_index = IdxEmployeesString.all(:conditions => {:employee_id => @person.id}, :order => 'key asc')
|
132
120
|
|
133
121
|
assert_equal 'high', high_index.key
|
134
122
|
assert_equal 'gender:M age:34 name:John', high_index.value
|
@@ -141,13 +129,13 @@ class IndexForeignTest < ActiveSupport::TestCase
|
|
141
129
|
context 'on record update with a new version' do
|
142
130
|
should 'create new index entries' do
|
143
131
|
@person = Contact.create('name' => 'Juan', 'lang' => 'es', 'gender' => 'M', 'age' => 34)
|
144
|
-
high_index1, name_index1 =
|
132
|
+
high_index1, name_index1 = IdxEmployeesString.all(:conditions => {:version_id => @person.version.id}, :order => 'key asc')
|
145
133
|
@person.new_version!
|
146
|
-
assert_difference('
|
134
|
+
assert_difference('IdxEmployeesString.count', 2) do
|
147
135
|
@person.update_attributes('name' => 'John', 'lang' => 'en')
|
148
136
|
end
|
149
137
|
|
150
|
-
high_index, name_index =
|
138
|
+
high_index, name_index = IdxEmployeesString.all(:conditions => {:version_id => @person.version.id}, :order => 'key asc')
|
151
139
|
assert_not_equal high_index1.id, high_index.id
|
152
140
|
assert_not_equal name_index1.id, name_index.id
|
153
141
|
|
@@ -174,10 +162,10 @@ class IndexForeignTest < ActiveSupport::TestCase
|
|
174
162
|
end
|
175
163
|
|
176
164
|
should 'create index entries to sort multilingual values' do
|
177
|
-
people_fr = Contact.find(:all, :joins => "INNER JOIN
|
165
|
+
people_fr = Contact.find(:all, :joins => "INNER JOIN idx_employees_strings AS ise ON ise.employee_id = employees.id AND ise.key = 'name_fr'",
|
178
166
|
:order => "ise.value asc")
|
179
167
|
|
180
|
-
people_en = Contact.find(:all, :joins => "INNER JOIN
|
168
|
+
people_en = Contact.find(:all, :joins => "INNER JOIN idx_employees_strings AS ise ON ise.employee_id = employees.id AND ise.key = 'name_en'",
|
181
169
|
:order => "ise.value asc")
|
182
170
|
|
183
171
|
assert_equal [@jean.id, @jim.id], people_fr.map {|r| r.id}
|
@@ -201,10 +189,10 @@ class IndexForeignTest < ActiveSupport::TestCase
|
|
201
189
|
end
|
202
190
|
|
203
191
|
should 'create index entries to sort multilingual values' do
|
204
|
-
people_fr = Contact.find(:all, :joins => "INNER JOIN
|
192
|
+
people_fr = Contact.find(:all, :joins => "INNER JOIN idx_employees_strings AS ise ON ise.employee_id = employees.id AND ise.key = 'name_fr'",
|
205
193
|
:order => "ise.value asc")
|
206
194
|
|
207
|
-
people_en = Contact.find(:all, :joins => "INNER JOIN
|
195
|
+
people_en = Contact.find(:all, :joins => "INNER JOIN idx_employees_strings AS ise ON ise.employee_id = employees.id AND ise.key = 'name_en'",
|
208
196
|
:order => "ise.value asc")
|
209
197
|
|
210
198
|
# This is what we would like to have (once we have found an SQL trick to get the record in 'en')
|
@@ -221,8 +209,8 @@ class IndexForeignTest < ActiveSupport::TestCase
|
|
221
209
|
context 'on record destruction' do
|
222
210
|
should 'remove index entries' do
|
223
211
|
person = Contact.create('name' => 'Juan', 'lang' => 'es', 'gender' => 'M', 'age' => 34)
|
224
|
-
assert_difference('
|
225
|
-
assert_difference('
|
212
|
+
assert_difference('IdxEmployeesString.count', -2) do
|
213
|
+
assert_difference('IdxEmployeesInteger.count', -1) do
|
226
214
|
person.destroy
|
227
215
|
end
|
228
216
|
end
|
@@ -2,13 +2,6 @@ require 'test_helper'
|
|
2
2
|
require 'fixtures'
|
3
3
|
|
4
4
|
class IndexSimpleTest < ActiveSupport::TestCase
|
5
|
-
class IndexedStringEmp < ActiveRecord::Base
|
6
|
-
set_table_name :i_special_employees
|
7
|
-
end
|
8
|
-
|
9
|
-
class IndexedIntegerEmp < ActiveRecord::Base
|
10
|
-
set_table_name :i_integer_employees
|
11
|
-
end
|
12
5
|
|
13
6
|
# Simple index definition class
|
14
7
|
class Dog < ActiveRecord::Base
|
@@ -59,7 +52,7 @@ class IndexSimpleTest < ActiveSupport::TestCase
|
|
59
52
|
|
60
53
|
context 'on record creation' do
|
61
54
|
should 'create index entries' do
|
62
|
-
assert_difference('
|
55
|
+
assert_difference('IdxEmployeesSpecial.count', 1) do
|
63
56
|
Dog.create('name' => 'Pavlov')
|
64
57
|
end
|
65
58
|
end
|
@@ -70,17 +63,24 @@ class IndexSimpleTest < ActiveSupport::TestCase
|
|
70
63
|
end
|
71
64
|
|
72
65
|
should 'not create index entries for blank values' do
|
73
|
-
assert_difference('
|
66
|
+
assert_difference('IdxEmployeesInteger.count', 0) do
|
74
67
|
Dog.create('name' => 'Pavlov')
|
75
68
|
end
|
76
69
|
end
|
77
70
|
|
78
|
-
should '
|
71
|
+
should 'special indices linked to the model' do
|
79
72
|
dog = Dog.create('name' => 'Pavlov')
|
80
|
-
index_string =
|
73
|
+
index_string = IdxEmployeesSpecial.first(:conditions => {:employee_id => dog.id})
|
81
74
|
assert_equal 'Pavlov', index_string.value
|
82
75
|
assert_equal 'name', index_string.key
|
83
76
|
end
|
77
|
+
|
78
|
+
should 'store a key and value pair linked to the model' do
|
79
|
+
dog = Dog.create('age' => 15)
|
80
|
+
index_integer = IdxEmployeesInteger.first(:conditions => {:employee_id => dog.id})
|
81
|
+
assert_equal 15, index_integer.value
|
82
|
+
assert_equal 'age', index_integer.key
|
83
|
+
end
|
84
84
|
end
|
85
85
|
|
86
86
|
context 'on record update' do
|
@@ -89,37 +89,37 @@ class IndexSimpleTest < ActiveSupport::TestCase
|
|
89
89
|
end
|
90
90
|
|
91
91
|
should 'update index entries' do
|
92
|
-
index_string =
|
93
|
-
assert_difference('
|
92
|
+
index_string = IdxEmployeesSpecial.first(:conditions => {:employee_id => @dog.id})
|
93
|
+
assert_difference('IdxEmployeesSpecial.count', 0) do
|
94
94
|
@dog.update_attributes('name' => 'Médor')
|
95
95
|
end
|
96
96
|
|
97
|
-
index_string =
|
97
|
+
index_string = IdxEmployeesSpecial.find(index_string.id)
|
98
98
|
assert_equal 'Médor', index_string.value
|
99
99
|
assert_equal 'name', index_string.key
|
100
100
|
end
|
101
101
|
|
102
102
|
should 'not create index entries for blank values' do
|
103
|
-
assert_difference('
|
103
|
+
assert_difference('IdxEmployeesInteger.count', 0) do
|
104
104
|
@dog.update_attributes('name' => 'Médor')
|
105
105
|
end
|
106
106
|
end
|
107
107
|
|
108
108
|
should 'remove blank values' do
|
109
|
-
assert_difference('
|
109
|
+
assert_difference('IdxEmployeesSpecial.count', -1) do
|
110
110
|
@dog.update_attributes('name' => '')
|
111
111
|
end
|
112
112
|
end
|
113
113
|
|
114
114
|
should 'create new entries for new keys' do
|
115
|
-
assert_difference('
|
115
|
+
assert_difference('IdxEmployeesInteger.count', 1) do
|
116
116
|
@dog.update_attributes('age' => 7)
|
117
117
|
end
|
118
118
|
end
|
119
119
|
|
120
120
|
should 'store a key and value pair linked to the model' do
|
121
121
|
@dog.update_attributes('age' => 7)
|
122
|
-
index_int =
|
122
|
+
index_int = IdxEmployeesInteger.first(:conditions => {:employee_id => @dog.id})
|
123
123
|
assert_equal 7, index_int.value
|
124
124
|
assert_equal 'age', index_int.key
|
125
125
|
end
|
@@ -130,13 +130,13 @@ class IndexSimpleTest < ActiveSupport::TestCase
|
|
130
130
|
end
|
131
131
|
|
132
132
|
should 'not alter indices' do
|
133
|
-
assert_difference('
|
133
|
+
assert_difference('IdxEmployeesInteger.count', 0) do
|
134
134
|
assert_raises(Exception) do
|
135
135
|
@dog.update_attributes('name' => 'raise')
|
136
136
|
end
|
137
137
|
end
|
138
138
|
|
139
|
-
index_string =
|
139
|
+
index_string = IdxEmployeesSpecial.first(:conditions => {:employee_id => @dog.id})
|
140
140
|
assert_equal 'Pavlov', index_string.value
|
141
141
|
assert_equal 'name', index_string.key
|
142
142
|
end
|
@@ -147,8 +147,8 @@ class IndexSimpleTest < ActiveSupport::TestCase
|
|
147
147
|
context 'on record destruction' do
|
148
148
|
should 'remove index entries' do
|
149
149
|
dog = Dog.create('name' => 'Pavlov', 'age' => 7)
|
150
|
-
assert_difference('
|
151
|
-
assert_difference('
|
150
|
+
assert_difference('IdxEmployeesSpecial.count', -1) do
|
151
|
+
assert_difference('IdxEmployeesInteger.count', -1) do
|
152
152
|
dog.destroy
|
153
153
|
end
|
154
154
|
end
|