clean_model 0.0.5 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,178 +1,178 @@
1
- require 'spec_helper'
2
-
3
- include BaseModels
4
-
5
- describe CleanModel::Base do
6
-
7
- context 'Basic attributes access' do
8
-
9
- subject { Person.new }
10
-
11
- it 'Respond to each defined attribute' do
12
- should respond_to 'first_name'
13
- should respond_to 'first_name='
14
- should respond_to 'last_name'
15
- should respond_to 'last_name='
16
- end
17
-
18
- it 'Keep value of attributes' do
19
- person = Person.new
20
-
21
- person.first_name.should be_nil
22
- person.first_name = 'John'
23
- person.first_name.should eq 'John'
24
-
25
- person.last_name.should be_nil
26
- person.last_name = 'Doe'
27
- person.last_name.should eq 'Doe'
28
- end
29
-
30
- it 'Can assign attributes via constructor hash' do
31
- person = Person.new first_name: 'John', last_name: 'Doe'
32
- person.first_name.should eq 'John'
33
- person.last_name.should eq 'Doe'
34
- end
35
-
36
- it 'Can assign attributes via constructor block' do
37
- person = Person.new do |p|
38
- p.first_name = 'John'
39
- p.last_name = 'Doe'
40
- end
41
- person.first_name.should eq 'John'
42
- person.last_name.should eq 'Doe'
43
- end
44
-
45
- it 'Get attributes hash' do
46
- person = Person.new first_name: 'John', last_name: 'Doe', nationality: :usa
47
- person.attributes.keys.should eq [:first_name, :last_name, :nationality, :age]
48
- person.attributes[:first_name].should eq 'John'
49
- person.attributes[:last_name].should eq 'Doe'
50
- person.attributes[:nationality].should eq :usa
51
- end
52
-
53
- end
54
-
55
- context 'Strong typed attributes restrictions' do
56
-
57
- it 'Type defined with a symbol' do
58
- engine = Engine.new
59
- engine.cylinders = 6
60
- expect { engine.cylinders = 6.5 }.to raise_error CleanModel::InvalidTypeAssignment
61
- end
62
-
63
- it 'Type defined with a string' do
64
- engine = Engine.new
65
- engine.valves = 16
66
- expect { engine.valves = 16.5 }.to raise_error CleanModel::InvalidTypeAssignment
67
- end
68
-
69
- it 'Type defined with a super class' do
70
- engine = Engine.new
71
- engine.power = 130
72
- expect { engine.power = '130hp' }.to raise_error CleanModel::InvalidTypeAssignment
73
- end
74
-
75
- end
76
-
77
- context 'Default values' do
78
-
79
- it 'Verify default value' do
80
- person = Person.new
81
- person.first_name.should be_nil
82
- person.last_name.should be_nil
83
- person.nationality.should eq :argentina
84
- person.age.should eq Time.now.year - 1979
85
- end
86
-
87
- end
88
-
89
- context 'Attribute type conversions' do
90
-
91
- it 'Transform to model when assign a hash' do
92
- car = Car.new do |c|
93
- c.engine = {power: 110, cylinders: 16, valves: 6}
94
- end
95
- car.engine.should be_a Engine
96
- car.engine.power.should eq 110
97
- car.engine.cylinders.should eq 16
98
- car.engine.valves.should eq 6
99
- end
100
-
101
- it 'Apply custom transformation' do
102
- car = Car.new do |c|
103
- c.comfort = 'bluetooth, gps, electric pack'
104
- end
105
- car.comfort.should be_a Array
106
- car.comfort.should eq ['bluetooth', 'gps', 'electric pack']
107
- end
108
-
109
- it 'Transform array elements when collection defined' do
110
- factory = Factory.new do |f|
111
- f.cars = [
112
- {brand: 'Honda', model: 'Civic'},
113
- {brand: 'Toyota', model: 'Corolla'}
114
- ]
115
- end
116
-
117
- factory.cars.should be_a Array
118
- factory.cars.should have(2).items
119
-
120
- factory.cars[0].should be_a Car
121
- factory.cars[0].brand.should eq 'Honda'
122
- factory.cars[0].model.should eq 'Civic'
123
-
124
- factory.cars[1].should be_a Car
125
- factory.cars[1].brand.should eq 'Toyota'
126
- factory.cars[1].model.should eq 'Corolla'
127
- end
128
-
129
- it 'Not transform array elements when contains expected class' do
130
- factory = Factory.new do |f|
131
- f.cars = [
132
- Car.new(brand: 'Honda', model: 'Civic'),
133
- Car.new(brand: 'Toyota', model: 'Corolla')
134
- ]
135
- end
136
-
137
- factory.cars.should be_a Array
138
- factory.cars.should have(2).items
139
-
140
- factory.cars[0].should be_a Car
141
- factory.cars[0].brand.should eq 'Honda'
142
- factory.cars[0].model.should eq 'Civic'
143
-
144
- factory.cars[1].should be_a Car
145
- factory.cars[1].brand.should eq 'Toyota'
146
- factory.cars[1].model.should eq 'Corolla'
147
- end
148
-
149
- end
150
-
151
- context 'Active model naming and translation' do
152
-
153
- it 'Get a model name' do
154
- Person.model_name.should eq 'BaseModels::Person'
155
- Person.model_name.human.should eq 'Person'
156
- end
157
-
158
- it 'Get a human attribute names' do
159
- Person.human_attribute_name(:first_name).should eq 'First name'
160
- Person.human_attribute_name(:last_name).should eq 'Last name'
161
- end
162
-
163
- end
164
-
165
- context 'Active model validations' do
166
-
167
- it 'Validates presence' do
168
- Person.new(first_name: 'John', last_name: 'Doe').should be_valid
169
-
170
- person = Person.new
171
- person.should_not be_valid
172
- person.errors[:first_name].should have(1).items
173
- person.errors[:last_name].should have(1).items
174
- end
175
-
176
- end
177
-
1
+ require 'spec_helper'
2
+
3
+ include BaseModels
4
+
5
+ describe CleanModel::Base do
6
+
7
+ context 'Basic attributes access' do
8
+
9
+ subject { Person.new }
10
+
11
+ it 'Respond to each defined attribute' do
12
+ should respond_to 'first_name'
13
+ should respond_to 'first_name='
14
+ should respond_to 'last_name'
15
+ should respond_to 'last_name='
16
+ end
17
+
18
+ it 'Keep value of attributes' do
19
+ person = Person.new
20
+
21
+ person.first_name.should be_nil
22
+ person.first_name = 'John'
23
+ person.first_name.should eq 'John'
24
+
25
+ person.last_name.should be_nil
26
+ person.last_name = 'Doe'
27
+ person.last_name.should eq 'Doe'
28
+ end
29
+
30
+ it 'Can assign attributes via constructor hash' do
31
+ person = Person.new first_name: 'John', last_name: 'Doe'
32
+ person.first_name.should eq 'John'
33
+ person.last_name.should eq 'Doe'
34
+ end
35
+
36
+ it 'Can assign attributes via constructor block' do
37
+ person = Person.new do |p|
38
+ p.first_name = 'John'
39
+ p.last_name = 'Doe'
40
+ end
41
+ person.first_name.should eq 'John'
42
+ person.last_name.should eq 'Doe'
43
+ end
44
+
45
+ it 'Get attributes hash' do
46
+ person = Person.new first_name: 'John', last_name: 'Doe', nationality: :usa
47
+ person.attributes.keys.should eq [:first_name, :last_name, :nationality, :age]
48
+ person.attributes[:first_name].should eq 'John'
49
+ person.attributes[:last_name].should eq 'Doe'
50
+ person.attributes[:nationality].should eq :usa
51
+ end
52
+
53
+ end
54
+
55
+ context 'Strong typed attributes restrictions' do
56
+
57
+ it 'Type defined with a symbol' do
58
+ engine = Engine.new
59
+ engine.cylinders = 6
60
+ expect { engine.cylinders = 6.5 }.to raise_error CleanModel::InvalidTypeAssignment
61
+ end
62
+
63
+ it 'Type defined with a string' do
64
+ engine = Engine.new
65
+ engine.valves = 16
66
+ expect { engine.valves = 16.5 }.to raise_error CleanModel::InvalidTypeAssignment
67
+ end
68
+
69
+ it 'Type defined with a super class' do
70
+ engine = Engine.new
71
+ engine.power = 130
72
+ expect { engine.power = '130hp' }.to raise_error CleanModel::InvalidTypeAssignment
73
+ end
74
+
75
+ end
76
+
77
+ context 'Default values' do
78
+
79
+ it 'Verify default value' do
80
+ person = Person.new
81
+ person.first_name.should be_nil
82
+ person.last_name.should be_nil
83
+ person.nationality.should eq :argentina
84
+ person.age.should eq Time.now.year - 1979
85
+ end
86
+
87
+ end
88
+
89
+ context 'Attribute type conversions' do
90
+
91
+ it 'Transform to model when assign a hash' do
92
+ car = Car.new do |c|
93
+ c.engine = {power: 110, cylinders: 16, valves: 6}
94
+ end
95
+ car.engine.should be_a Engine
96
+ car.engine.power.should eq 110
97
+ car.engine.cylinders.should eq 16
98
+ car.engine.valves.should eq 6
99
+ end
100
+
101
+ it 'Apply custom transformation' do
102
+ car = Car.new do |c|
103
+ c.comfort = 'bluetooth, gps, electric pack'
104
+ end
105
+ car.comfort.should be_a Array
106
+ car.comfort.should eq ['bluetooth', 'gps', 'electric pack']
107
+ end
108
+
109
+ it 'Transform array elements when collection defined' do
110
+ factory = Factory.new do |f|
111
+ f.cars = [
112
+ {brand: 'Honda', model: 'Civic'},
113
+ {brand: 'Toyota', model: 'Corolla'}
114
+ ]
115
+ end
116
+
117
+ factory.cars.should be_a Array
118
+ factory.cars.should have(2).items
119
+
120
+ factory.cars[0].should be_a Car
121
+ factory.cars[0].brand.should eq 'Honda'
122
+ factory.cars[0].model.should eq 'Civic'
123
+
124
+ factory.cars[1].should be_a Car
125
+ factory.cars[1].brand.should eq 'Toyota'
126
+ factory.cars[1].model.should eq 'Corolla'
127
+ end
128
+
129
+ it 'Not transform array elements when contains expected class' do
130
+ factory = Factory.new do |f|
131
+ f.cars = [
132
+ Car.new(brand: 'Honda', model: 'Civic'),
133
+ Car.new(brand: 'Toyota', model: 'Corolla')
134
+ ]
135
+ end
136
+
137
+ factory.cars.should be_a Array
138
+ factory.cars.should have(2).items
139
+
140
+ factory.cars[0].should be_a Car
141
+ factory.cars[0].brand.should eq 'Honda'
142
+ factory.cars[0].model.should eq 'Civic'
143
+
144
+ factory.cars[1].should be_a Car
145
+ factory.cars[1].brand.should eq 'Toyota'
146
+ factory.cars[1].model.should eq 'Corolla'
147
+ end
148
+
149
+ end
150
+
151
+ context 'Active model naming and translation' do
152
+
153
+ it 'Get a model name' do
154
+ Person.model_name.should eq 'BaseModels::Person'
155
+ Person.model_name.human.should eq 'Person'
156
+ end
157
+
158
+ it 'Get a human attribute names' do
159
+ Person.human_attribute_name(:first_name).should eq 'First name'
160
+ Person.human_attribute_name(:last_name).should eq 'Last name'
161
+ end
162
+
163
+ end
164
+
165
+ context 'Active model validations' do
166
+
167
+ it 'Validates presence' do
168
+ Person.new(first_name: 'John', last_name: 'Doe').should be_valid
169
+
170
+ person = Person.new
171
+ person.should_not be_valid
172
+ person.errors[:first_name].should have(1).items
173
+ person.errors[:last_name].should have(1).items
174
+ end
175
+
176
+ end
177
+
178
178
  end
@@ -1,112 +1,112 @@
1
- require 'spec_helper'
2
-
3
- include PersistentModels
4
-
5
- describe CleanModel::Persistent do
6
-
7
- context 'Respond to persistence methods' do
8
-
9
- subject { Post.new }
10
-
11
- it 'Class methods ' do
12
- Post.should respond_to :create
13
- Post.should respond_to :create!
14
- end
15
-
16
- it 'Instance methods' do
17
- should respond_to :id
18
- should respond_to :id=
19
- should respond_to :save
20
- should respond_to :save!
21
- should respond_to :update_attributes
22
- should respond_to :destroy
23
- should respond_to :new_record?
24
- should respond_to :persisted?
25
- end
26
-
27
- end
28
-
29
- context 'Undefined persistence methods' do
30
-
31
- it 'Can not create' do
32
- expect { Post.new.send :create }.to raise_error CleanModel::UndefinedPersistenceMethod
33
- end
34
-
35
- it 'Can not update' do
36
- expect { Post.new.send :update }.to raise_error CleanModel::UndefinedPersistenceMethod
37
- end
38
-
39
- it 'Can not destroy' do
40
- expect { Post.new.send :destroy }.to raise_error CleanModel::UndefinedPersistenceMethod
41
- end
42
-
43
- end
44
-
45
- context 'Defined persistence methods' do
46
-
47
- it 'Create with class method' do
48
- Post.any_instance.stub(:create).and_return(:true)
49
- Post.any_instance.should_receive :create
50
-
51
- Post.create(subject: 'Title', content: 'Some text').should be_a Post
52
- end
53
-
54
- it 'Create with instance method' do
55
- post = Post.new subject: 'Title', content: 'Some text'
56
-
57
- post.stub(:create) { true }
58
- post.should_receive :create
59
-
60
- post.save.should be_true
61
-
62
- post.stub(:id) { rand(1000) }
63
-
64
- post.should be_persisted
65
- post.should_not be_new_record
66
- end
67
-
68
- it 'Save persisted model' do
69
- post = Post.new id: 1, subject: 'Title', content: 'Some text'
70
- post.should be_persisted
71
- post.should_not be_new_record
72
-
73
- post.stub(:update) { true }
74
- post.should_receive :update
75
-
76
- post.save.should be_true
77
- end
78
-
79
- it 'Update attributes' do
80
- post = Post.new id: 1, subject: 'Title', content: 'Some text'
81
-
82
- post.stub(:update) { true }
83
- post.should_receive :update
84
-
85
- post.update_attributes(content: 'Other text').should be_true
86
- end
87
-
88
- it 'Destroy persisted model' do
89
- post = Post.new id: 1, subject: 'Title', content: 'Some text'
90
-
91
- post.stub(:delete) { true }
92
- post.should_receive :delete
93
-
94
- post.destroy.should be_true
95
- end
96
-
97
- end
98
-
99
- context 'Active model conversion' do
100
-
101
- let(:post) { Post.new id: 1 }
102
-
103
- it 'Respond to instance methods' do
104
- post.to_model.should eq post
105
- post.to_key.should eq [1]
106
- post.to_param.should eq '1'
107
- post.to_partial_path.should eq 'persistent_models/posts/post'
108
- end
109
-
110
- end
111
-
1
+ require 'spec_helper'
2
+
3
+ include PersistentModels
4
+
5
+ describe CleanModel::Persistent do
6
+
7
+ context 'Respond to persistence methods' do
8
+
9
+ subject { Post.new }
10
+
11
+ it 'Class methods ' do
12
+ Post.should respond_to :create
13
+ Post.should respond_to :create!
14
+ end
15
+
16
+ it 'Instance methods' do
17
+ should respond_to :id
18
+ should respond_to :id=
19
+ should respond_to :save
20
+ should respond_to :save!
21
+ should respond_to :update_attributes
22
+ should respond_to :destroy
23
+ should respond_to :new_record?
24
+ should respond_to :persisted?
25
+ end
26
+
27
+ end
28
+
29
+ context 'Undefined persistence methods' do
30
+
31
+ it 'Can not create' do
32
+ expect { Post.new.send :create }.to raise_error CleanModel::UndefinedPersistenceMethod
33
+ end
34
+
35
+ it 'Can not update' do
36
+ expect { Post.new.send :update }.to raise_error CleanModel::UndefinedPersistenceMethod
37
+ end
38
+
39
+ it 'Can not destroy' do
40
+ expect { Post.new.send :destroy }.to raise_error CleanModel::UndefinedPersistenceMethod
41
+ end
42
+
43
+ end
44
+
45
+ context 'Defined persistence methods' do
46
+
47
+ it 'Create with class method' do
48
+ Post.any_instance.stub(:create).and_return(:true)
49
+ Post.any_instance.should_receive :create
50
+
51
+ Post.create(subject: 'Title', content: 'Some text').should be_a Post
52
+ end
53
+
54
+ it 'Create with instance method' do
55
+ post = Post.new subject: 'Title', content: 'Some text'
56
+
57
+ post.stub(:create) { true }
58
+ post.should_receive :create
59
+
60
+ post.save.should be_true
61
+
62
+ post.stub(:id) { rand(1000) }
63
+
64
+ post.should be_persisted
65
+ post.should_not be_new_record
66
+ end
67
+
68
+ it 'Save persisted model' do
69
+ post = Post.new id: 1, subject: 'Title', content: 'Some text'
70
+ post.should be_persisted
71
+ post.should_not be_new_record
72
+
73
+ post.stub(:update) { true }
74
+ post.should_receive :update
75
+
76
+ post.save.should be_true
77
+ end
78
+
79
+ it 'Update attributes' do
80
+ post = Post.new id: 1, subject: 'Title', content: 'Some text'
81
+
82
+ post.stub(:update) { true }
83
+ post.should_receive :update
84
+
85
+ post.update_attributes(content: 'Other text').should be_true
86
+ end
87
+
88
+ it 'Destroy persisted model' do
89
+ post = Post.new id: 1, subject: 'Title', content: 'Some text'
90
+
91
+ post.stub(:delete) { true }
92
+ post.should_receive :delete
93
+
94
+ post.destroy.should be_true
95
+ end
96
+
97
+ end
98
+
99
+ context 'Active model conversion' do
100
+
101
+ let(:post) { Post.new id: 1 }
102
+
103
+ it 'Respond to instance methods' do
104
+ post.to_model.should eq post
105
+ post.to_key.should eq [1]
106
+ post.to_param.should eq '1'
107
+ post.to_partial_path.should eq 'persistent_models/posts/post'
108
+ end
109
+
110
+ end
111
+
112
112
  end