Permalink
Browse files

Added new specs for stock adjustment model.

  • Loading branch information...
1 parent 15fe0f8 commit 0aba3f37b60a19a6828043062885d8624c38903c @Jellyfishboy committed Dec 8, 2016
Showing with 39 additions and 6 deletions.
  1. +1 −0 Gemfile
  2. +3 −1 Gemfile.lock
  3. +3 −3 app/models/stock_adjustment.rb
  4. +32 −2 spec/models/stock_adjustment_spec.rb
View
@@ -31,6 +31,7 @@ group :test do
gem 'email_spec'
gem 'fuubar'
gem 'test_after_commit'
+ gem 'timecop'
end
group :development, :test do
View
@@ -578,6 +578,7 @@ GEM
thor (0.19.1)
thread_safe (0.3.5)
tilt (1.4.1)
+ timecop (0.8.1)
trollop (2.1.2)
turbolinks (2.5.3)
coffee-rails
@@ -671,11 +672,12 @@ DEPENDENCIES
sprockets (= 2.11.0)
test_after_commit
thin
+ timecop
turbolinks
tzinfo-data
uglifier (>= 1.0.3)
unf
whenever (>= 0.8.4)
BUNDLED WITH
- 1.11.2
+ 1.13.6
@@ -27,7 +27,7 @@ class StockAdjustment < ActiveRecord::Base
validates :description, :adjustment, :adjusted_at, presence: true
validate :adjustment_value
- before_save :stock_adjustment, unless: :duplicate
+ before_save :adjust_sku_stock, unless: :duplicate
after_create :send_stock_notifications, unless: :duplicate
before_validation :set_current_time_as_adjusted, unless: :duplicate
@@ -37,7 +37,7 @@ class StockAdjustment < ActiveRecord::Base
# Modify the sku stock with the associated stock level adjustment value
#
- def stock_adjustment
+ def adjust_sku_stock
if Store.positive?(self.adjustment)
self.stock_total = self.sku.stock + self.adjustment
self.sku.update_column(:stock, self.sku.stock + self.adjustment)
@@ -66,6 +66,6 @@ def send_stock_notifications
# Sets the current records adjusted_at attribute to the current time and date
#
def set_current_time_as_adjusted
- self.adjusted_at = Time.now
+ self.adjusted_at = Time.current
end
end
@@ -25,8 +25,8 @@
describe "Validating the stock value before an adjustment value is applied" do
let!(:sku) { create(:sku, stock: 10) }
- it "should call stock_adjustment_adjustment method before a save" do
- StockAdjustment._save_callbacks.select { |cb| cb.kind.eql?(:before) }.map(&:raw_filter).include?(:stock_adjustment).should == true
+ it "should call adjust_sku_stock method before a save" do
+ StockAdjustment._save_callbacks.select { |cb| cb.kind.eql?(:before) }.map(&:raw_filter).include?(:adjust_sku_stock).should == true
end
context "if the adjustment value results in a negative stock value" do
@@ -67,7 +67,37 @@
expect(stock_adjustment.adjustment).to eq 3
end
end
+ end
+
+ describe 'When setting the adjusted_at datetime value' do
+
+ context "if the record is not a duplicate" do
+ let(:sku) { create(:sku, active: true) }
+ let(:stock_adjustment) { build(:stock_adjustment, sku: sku) }
+ it "should not set the adjusted_at attribute to current time" do
+ Timecop.freeze(Time.current) do
+ expect{
+ stock_adjustment.save
+ }.to change{
+ stock_adjustment.adjusted_at
+ }.from(nil).to(Time.current)
+ end
+ end
+ end
+
+ context "if the record is a duplicate" do
+ let(:sku) { create(:sku, active: true) }
+ let(:stock_adjustment) { build(:stock_adjustment, sku: sku, duplicate: true, adjusted_at: 1.day.ago) }
+
+ it "should not change the adjusted_at attribute" do
+ expect{
+ stock_adjustment.save
+ }.to_not change{
+ stock_adjustment.adjusted_at
+ }
+ end
+ end
end
end

0 comments on commit 0aba3f3

Please sign in to comment.