From b7bde8e868d0eb93df887064935a9e66e376fe67 Mon Sep 17 00:00:00 2001 From: Ben Burkert Date: Wed, 28 May 2008 18:01:25 -0500 Subject: [PATCH] Added specs for item & line item. --- app/models/item.rb | 4 +++- app/models/item/line_item.rb | 5 +++-- app/models/product.rb | 4 ++-- app/models/upload/image.rb | 1 - spec/models/item/line_item_spec.rb | 17 +++++++++++++++++ spec/models/item_spec.rb | 16 ++++++++++++++++ spec/spec_fixtures.rb | 16 ++++++++++++++++ 7 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 spec/models/item/line_item_spec.rb create mode 100644 spec/models/item_spec.rb diff --git a/app/models/item.rb b/app/models/item.rb index 600d22c..b3c4bbf 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -6,5 +6,7 @@ class Item property :name, String property :product_id, Integer # foreign-key - has 1, :product + belongs_to :product + + validates_present :product end \ No newline at end of file diff --git a/app/models/item/line_item.rb b/app/models/item/line_item.rb index 5644663..fd010ff 100644 --- a/app/models/item/line_item.rb +++ b/app/models/item/line_item.rb @@ -1,5 +1,6 @@ class LineItem < Item + property :unit_price, BigDecimal + property :quantity, Integer - property :unit_price, BigDecimal, :default => 0.0, :nullable => false - property :quantity, Integer, :default => 0, :nullable => false + validates_present :unit_price, :quantity end \ No newline at end of file diff --git a/app/models/product.rb b/app/models/product.rb index 100ff8b..62a35fb 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -13,11 +13,11 @@ class Product property :size_height, Float, :default => 0.0, :nullable => false property :size_depth, Float, :default => 0.0, :nullable => false property :weight, Float, :default => 0.0, :nullable => false - property :is_discontinued, Boolean, :default => false + property :is_discontinued, Boolean, :default => false, :nullable => false has n, :items has n, :variations has n, :product_images - validates_present :name, :code + alias_method :discontinued?, :is_discontinued end \ No newline at end of file diff --git a/app/models/upload/image.rb b/app/models/upload/image.rb index ec13ceb..3a9bd95 100644 --- a/app/models/upload/image.rb +++ b/app/models/upload/image.rb @@ -1,5 +1,4 @@ class Image < Upload - IMAGE_EXTENSIONS = ['gif', 'jpg', 'jpeg', 'png', 'bmp'] property :width, Integer, :default => 0, :nullable => false diff --git a/spec/models/item/line_item_spec.rb b/spec/models/item/line_item_spec.rb new file mode 100644 index 0000000..6d2818e --- /dev/null +++ b/spec/models/item/line_item_spec.rb @@ -0,0 +1,17 @@ +require File.join( File.dirname(__FILE__), "..", "..", "spec_helper" ) + +describe LineItem do + before(:each) do + DataMapper.auto_migrate! + end + + it "should require a unit_price & quantity" do + li = LineItem.create(LineItem.gen_attrs.except(:unit_price)) + li.should_not be_valid + li.errors.should include(:unit_price) + + li = LineItem.create(LineItem.gen_attrs.except(:quantity)) + li.should_not be_valid + li.errors.should include(:quantity) + end +end \ No newline at end of file diff --git a/spec/models/item_spec.rb b/spec/models/item_spec.rb new file mode 100644 index 0000000..31232e4 --- /dev/null +++ b/spec/models/item_spec.rb @@ -0,0 +1,16 @@ +require File.join( File.dirname(__FILE__), "..", "spec_helper" ) + +describe Item do + before(:each) do + DataMapper.auto_migrate! + end + + describe "associations" do + it "should belong to a product" do + product = Product.gen + item = Item.create(Item.gen_attrs.merge(:product => product)) + item.product.should == product + item.should be_valid + end + end +end \ No newline at end of file diff --git a/spec/spec_fixtures.rb b/spec/spec_fixtures.rb index d0d1d1b..1569e73 100644 --- a/spec/spec_fixtures.rb +++ b/spec/spec_fixtures.rb @@ -24,11 +24,27 @@ Customer.fixture {{ :username => Random.word(:max => 50, :unique => true) }} +Item.fixture {{ + +}} + +LineItem.fixture {{ + :quantity => rand(10), + :unit_price => rand * rand(1_000) +}} + Order.fixture {{ :order_number => rand(100_000), :tax => rand * rand(1_000) }} +Product.fixture {{ + :name => Random.word, + :code => "%010d" % rand(1_000_000), + :date_available => Time.now, + :is_discontinued => Random.boolean +}} + State.fixture {{ :country => Country.gen, :name => name = Random.word, -- 2.11.4.GIT