Skip to content
Browse files

Finish the tests

  • Loading branch information...
1 parent 23ae24b commit 85ab84d3f6c8d0b2019af1c2970096414e542b5b Mike Fulcher committed
Showing with 56 additions and 45 deletions.
  1. +15 −2 test/fixtures/sample_relation.rb
  2. +0 −43 test/modest_model_test.rb
  3. +41 −0 test/modest_relation_test.rb
View
17 test/fixtures/sample_relation.rb
@@ -1,8 +1,21 @@
class SampleRelation < ModestModel::Base
- include ModestModel::Relation
attributes :name, :email
-
has_one :address
has_many :orders
belongs_to :owner
+end
+
+class Address < ModestModel::Base
+ attributes :name
+ belongs_to :sample_relation
+end
+
+class Order < ModestModel::Base
+ attributes :name
+ belongs_to :sample_relation
+end
+
+class Owner < ModestModel::Base
+ attributes :name
+ has_one :sample_relation
end
View
43 test/modest_model_test.rb
@@ -1,6 +1,5 @@
require 'test_helper'
require 'fixtures/sample_model'
-require 'fixtures/sample_relation'
class ModestModelTest < ActiveSupport::TestCase
test "validates absence of nickname" do
@@ -47,46 +46,4 @@ class ModestModelTest < ActiveSupport::TestCase
sample.email = "user@example.com"
assert_equal "user@example.com", sample.email
end
-
- test 'sample relation should have_one :address' do
- sample = SampleRelation.new
- sample.address = "Dummy Address"
- assert_equal "Dummy Address", sample.address
- end
-
- test 'sample relation should have_many :orders' do
- sample = SampleRelation.new
- assert_equal 0, sample.orders.size
- sample.orders << "Dummy Order 1"
- sample.orders << "Dummy Order 2"
- assert_equal 2, sample.orders.size
- end
-
- test 'sample relation should belong_to :owner' do
- sample = SampleRelation.new
- sample.owner = "Dummy Owner"
- assert_equal "Dummy Owner", sample.owner
- end
-
- test 'sample relation should collect all associations' do
- sample = SampleRelation.new
- assert_equal 3, sample.class._associations.size
- end
-
- test 'sample relation attributes should exclude the associations' do
- sample = SampleRelation.new
- sample.address = "Dummy Address"
- sample.orders << "Dummy Order 1"
- sample.orders << "Dummy Order 2"
- sample.owner = "Dummy Owner"
- sample.name = "Dummy Name"
- sample.email = "dummy@email.com"
- assert_equal 2, sample.attributes.size
- end
-
- # test 'sample relation should only allow an Address to be set as the has_one :address' do
- # sample = SampleRelation.new
- # sample.address = "Dummy Address" # raise error? see AR
- # sample.address = SampleAddress.new
- # end
end
View
41 test/modest_relation_test.rb
@@ -0,0 +1,41 @@
+require 'test_helper'
+require 'fixtures/sample_relation'
+
+class ModestModelTest < ActiveSupport::TestCase
+ test 'sample relation should have_one :address' do
+ sample = SampleRelation.new
+ sample.address = Address.new
+ assert_equal Address, sample.address.class
+ end
+
+ test 'sample relation should have_many :orders' do
+ sample = SampleRelation.new
+ assert_equal 0, sample.orders.size
+ sample.orders << Order.new
+ sample.orders << Order.new
+ assert_equal 2, sample.orders.size
+ end
+
+ test 'sample relation should belong_to :owner' do
+ sample = SampleRelation.new
+ sample.owner = Owner.new
+ assert_equal Owner, sample.owner.class
+ end
+
+ test 'sample relation attributes should exclude the associations' do
+ sample = SampleRelation.new
+ sample.address = Address.new
+ sample.orders << Order.new
+ sample.orders << Order.new
+ sample.owner = Owner.new
+ sample.name = "Dummy Name"
+ sample.email = "dummy@email.com"
+ assert_equal 2, sample.attributes.size
+ end
+
+ test 'sample relation should only allow an Address to be set as the has_one :address' do
+ sample = SampleRelation.new
+ assert_raise(ModestModel::AssociationTypeMismatch) { sample.address = "INVALID" }
+ assert_nothing_raised { sample.address = Address.new }
+ end
+end

0 comments on commit 85ab84d

Please sign in to comment.
Something went wrong with that request. Please try again.