daily pastebin goal
70%
SHARE
TWEET

Untitled

a guest Jul 11th, 2018 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ## cart.rb
  2. class Cart < ActiveRecord::Base
  3.   include Shipping
  4.  
  5.   has_many :cart_items
  6.   has_many :skus, :through => :cart_items
  7.   has_one :order
  8.  
  9.   # validates_associated :skus
  10.   # validates_associated :cart_items
  11.  
  12.   def validate
  13.     logger.info "Validating cart..."
  14.     sold_out = find_out_of_stock_items
  15.     if sold_out.size > 0
  16.       errors.add_to_base("Cart contains sold out items.")
  17.     end
  18.  
  19.     sold_out.each {|i| errors.add i.sku.sku, "is sold out."}
  20.   end
  21.  
  22.   def allocate_inventory
  23.     logger.info "Allocating inventory..."
  24.     begin
  25.       CartItem.transaction do
  26.         cart_items.each do |i|
  27.           # new_stock_on_hand = i.sku.stock_on_hand >= i.quantity ? (i.sku.stock_on_hand - i.quantity) : 0
  28.           new_stock_on_hand = i.sku.stock_on_hand - i.quantity
  29.           logger.info "Updating stock item #{i.sku.sku} to #{new_stock_on_hand}"
  30.           i.sku.stock_on_hand = new_stock_on_hand
  31.           i.sku.save!
  32.         end
  33.       end
  34.     rescue ActiveRecord::StaleObjectError
  35.       retry
  36.     end
  37.   end
  38.    
  39. end
  40.  
  41. ## order_test.rb (order model calls cart#allocate_inventory)
  42. require 'test_helper'
  43.  
  44. class OrderTest < ActiveSupport::TestCase
  45.   self.use_transactional_fixtures = false
  46.  
  47.   fixtures :products
  48.  
  49.   def setup
  50.     @good_order = Order.new(order_hash)
  51.     @good_order.customer_ip = "10.1.1.1"
  52.    
  53.     @cart = Cart.create
  54.     @cart.add(skus(:burton_supermodel_151).id, 3)
  55.     @good_order.cart = @cart
  56.   end
  57.  
  58.   ...
  59.  
  60.   test "order save should fail if one cart item out of stock" do
  61.     @cart = Cart.create
  62.     @cart.add(skus(:lib_tech_skate_banana_149).id, 2)
  63.     @cart.add(skus(:burton_supermodel_151).id, 3)
  64.     assert_equal 2, @cart.cart_items.size
  65.     assert_equal 2, @cart.cart_items[0].quantity
  66.     assert_equal 10, @cart.cart_items[0].sku.stock_on_hand
  67.     assert_equal 3, @cart.cart_items[1].quantity
  68.    
  69.     @good_order.cart = @cart
  70.     @good_order.cart.save!
  71.     assert_equal skus(:burton_supermodel_151).id, @cart.cart_items[1].sku_id
  72.     assert_not_nil @good_order.cart.cart_items[1].sku
  73.     sku = @good_order.cart.cart_items[1].sku
  74.     sku.stock_on_hand = 2
  75.     sku.save!
  76.     assert @good_order.save
  77.     assert_equal 10, @cart.cart_items[0].sku.stock_on_hand
  78.     assert_equal 2, @cart.cart_items[1].sku.stock_on_hand
  79.   end
  80.      
  81.   ...
  82.  
  83. end
  84.  
  85. ## log output from running above test
  86.  
  87. Validating cart...
  88.   CartItem Load Including Associations (0.5ms)   SELECT "cart_items"."id" AS t0_r0, "cart_items"."cart_id" AS t0_r1, "cart_items"."price" AS t0_r2, "cart_items"."quantity" AS t0_r3, "cart_items"."created_at" AS t0_r4, "cart_items"."updated_at" AS t0_r5, "cart_items"."sku_id" AS t0_r6, "skus"."id" AS t1_r0, "skus"."product_id" AS t1_r1, "skus"."sku" AS t1_r2, "skus"."created_at" AS t1_r3, "skus"."updated_at" AS t1_r4, "skus"."size" AS t1_r5, "skus"."color" AS t1_r6, "skus"."stock_on_hand" AS t1_r7, "skus"."lock_version" AS t1_r8 FROM "cart_items" LEFT OUTER JOIN "skus" ON "skus".id = "cart_items".sku_id WHERE ("cart_items".cart_id = NULL AND (skus.stock_on_hand < 1))
  89.   Cart Create (0.5ms)   INSERT INTO "carts" ("updated_at", "purchased_at", "created_at") VALUES('2009-08-31 21:05:35', NULL, '2009-08-31 21:05:35')
  90.   CartItem Load (0.1ms)   SELECT * FROM "cart_items" WHERE ("cart_items".cart_id = NULL)
  91.   Sku Load (0.4ms)   SELECT * FROM "skus" WHERE ("skus"."id" = 20741745)
  92. Cart#add
  93.   CartItem Load (0.2ms)   SELECT * FROM "cart_items" WHERE ("cart_items"."sku_id" = 20741745) AND ("cart_items".cart_id = 3)
  94.   Sku Load (0.3ms)   SELECT * FROM "skus" WHERE ("skus"."id" = 20741745)
  95.   Product Load (0.4ms)   SELECT * FROM "products" WHERE ("products"."id" = 160147030)
  96.   Sku Load (0.5ms)   SELECT * FROM "skus" WHERE ("skus".product_id = 160147030)
  97.   CartItem Load (0.2ms)   SELECT * FROM "cart_items" WHERE ("cart_items".cart_id = 3 AND ("cart_items"."sku_id" IN (20741740,20741745)))
  98. 0 quantity for this product in the cart...
  99.   Sku Load (0.5ms)   SELECT * FROM "skus" WHERE ("skus"."id" = 20741745)
  100. Validating CartItem
  101.   CartItem Create (0.5ms)   INSERT INTO "cart_items" ("sku_id", "updated_at", "price", "quantity", "cart_id", "created_at") VALUES(20741745, '2009-08-31 21:05:35', 149.0, 3, 3, '2009-08-31 21:05:35')
  102. Validating cart...
  103.   CartItem Load Including Associations (0.3ms)   SELECT "cart_items"."id" AS t0_r0, "cart_items"."cart_id" AS t0_r1, "cart_items"."price" AS t0_r2, "cart_items"."quantity" AS t0_r3, "cart_items"."created_at" AS t0_r4, "cart_items"."updated_at" AS t0_r5, "cart_items"."sku_id" AS t0_r6, "skus"."id" AS t1_r0, "skus"."product_id" AS t1_r1, "skus"."sku" AS t1_r2, "skus"."created_at" AS t1_r3, "skus"."updated_at" AS t1_r4, "skus"."size" AS t1_r5, "skus"."color" AS t1_r6, "skus"."stock_on_hand" AS t1_r7, "skus"."lock_version" AS t1_r8 FROM "cart_items" LEFT OUTER JOIN "skus" ON "skus".id = "cart_items".sku_id WHERE ("cart_items".cart_id = NULL AND (skus.stock_on_hand < 1))
  104.   Cart Create (0.3ms)   INSERT INTO "carts" ("updated_at", "purchased_at", "created_at") VALUES('2009-08-31 21:05:35', NULL, '2009-08-31 21:05:35')
  105.   CartItem Load (0.1ms)   SELECT * FROM "cart_items" WHERE ("cart_items".cart_id = NULL)
  106.   Sku Load (0.8ms)   SELECT * FROM "skus" WHERE ("skus"."id" = 143629153)
  107. Cart#add
  108.   CartItem Load (0.3ms)   SELECT * FROM "cart_items" WHERE ("cart_items"."sku_id" = 143629153) AND ("cart_items".cart_id = 4)
  109.   Sku Load (0.5ms)   SELECT * FROM "skus" WHERE ("skus"."id" = 143629153)
  110.   Product Load (0.5ms)   SELECT * FROM "products" WHERE ("products"."id" = 873410035)
  111.   Sku Load (0.3ms)   SELECT * FROM "skus" WHERE ("skus".product_id = 873410035)
  112.   CartItem Load (0.2ms)   SELECT * FROM "cart_items" WHERE ("cart_items".cart_id = 4 AND ("cart_items"."sku_id" IN (143629153)))
  113. 0 quantity for this product in the cart...
  114.   Sku Load (0.3ms)   SELECT * FROM "skus" WHERE ("skus"."id" = 143629153)
  115. Validating CartItem
  116.   CartItem Create (0.5ms)   INSERT INTO "cart_items" ("sku_id", "updated_at", "price", "quantity", "cart_id", "created_at") VALUES(143629153, '2009-08-31 21:05:35', 200.0, 2, 4, '2009-08-31 21:05:35')
  117. Cart#add
  118.   CartItem Load (0.3ms)   SELECT * FROM "cart_items" WHERE ("cart_items"."sku_id" = 20741745) AND ("cart_items".cart_id = 4)
  119.   Sku Load (0.4ms)   SELECT * FROM "skus" WHERE ("skus"."id" = 20741745)
  120.   Product Load (0.5ms)   SELECT * FROM "products" WHERE ("products"."id" = 160147030)
  121.   Sku Load (0.5ms)   SELECT * FROM "skus" WHERE ("skus".product_id = 160147030)
  122.   CartItem Load (1.1ms)   SELECT * FROM "cart_items" WHERE ("cart_items".cart_id = 4 AND ("cart_items"."sku_id" IN (20741740,20741745)))
  123. 0 quantity for this product in the cart...
  124.   Sku Load (1.0ms)   SELECT * FROM "skus" WHERE ("skus"."id" = 20741745)
  125. Validating CartItem
  126.   CartItem Create (0.5ms)   INSERT INTO "cart_items" ("sku_id", "updated_at", "price", "quantity", "cart_id", "created_at") VALUES(20741745, '2009-08-31 21:05:35', 149.0, 3, 4, '2009-08-31 21:05:35')
  127. Validating cart...
  128.   CartItem Load Including Associations (0.3ms)   SELECT "cart_items"."id" AS t0_r0, "cart_items"."cart_id" AS t0_r1, "cart_items"."price" AS t0_r2, "cart_items"."quantity" AS t0_r3, "cart_items"."created_at" AS t0_r4, "cart_items"."updated_at" AS t0_r5, "cart_items"."sku_id" AS t0_r6, "skus"."id" AS t1_r0, "skus"."product_id" AS t1_r1, "skus"."sku" AS t1_r2, "skus"."created_at" AS t1_r3, "skus"."updated_at" AS t1_r4, "skus"."size" AS t1_r5, "skus"."color" AS t1_r6, "skus"."stock_on_hand" AS t1_r7, "skus"."lock_version" AS t1_r8 FROM "cart_items" LEFT OUTER JOIN "skus" ON "skus".id = "cart_items".sku_id WHERE ("cart_items".cart_id = 4 AND (skus.stock_on_hand < 1))
  129.   Sku Update with optimistic locking (0.6ms)    UPDATE "skus"
  130.  SET "stock_on_hand" = 2, "updated_at" = '2009-08-31 21:05:35', "lock_version" = 1
  131.  WHERE id = 20741745
  132.  AND "lock_version" = 0
  133.  
  134. Allocating inventory...
  135. Updating stock item lt-skb-149 to 8
  136.   Sku Update with optimistic locking (0.9ms)    UPDATE "skus"
  137.  SET "stock_on_hand" = 8, "updated_at" = '2009-08-31 21:05:35', "lock_version" = 1
  138.  WHERE id = 143629153
  139.  AND "lock_version" = 0
  140.  
  141. Updating stock item brt-sprm-151 to -1
  142. Invalid Record found, adding errors to order...             <----------------------- THAT'S THE EXCEPTION from i.sku.save! on line 30 of cart.rb
  143. Validating cart...
  144.   CartItem Load Including Associations (0.2ms)   SELECT "cart_items"."id" AS t0_r0, "cart_items"."cart_id" AS t0_r1, "cart_items"."price" AS t0_r2, "cart_items"."quantity" AS t0_r3, "cart_items"."created_at" AS t0_r4, "cart_items"."updated_at" AS t0_r5, "cart_items"."sku_id" AS t0_r6, "skus"."id" AS t1_r0, "skus"."product_id" AS t1_r1, "skus"."sku" AS t1_r2, "skus"."created_at" AS t1_r3, "skus"."updated_at" AS t1_r4, "skus"."size" AS t1_r5, "skus"."color" AS t1_r6, "skus"."stock_on_hand" AS t1_r7, "skus"."lock_version" AS t1_r8 FROM "cart_items" LEFT OUTER JOIN "skus" ON "skus".id = "cart_items".sku_id WHERE ("cart_items".cart_id = 4 AND (skus.stock_on_hand < 1))
  145.   Order Create (0.3ms)   INSERT INTO "orders" ("ship_to_street_address_1", "ship_to_postal_code", "bill_to_street_address_1", "status", "ship_to_last_name", "ship_to_street_address_2", "ship_to_city", "bill_to_street_address_2", "updated_at", "number", "ship_to_country", "bill_to_postal_code", "bill_to_country", "phone_number", "bill_to_city", "error_message", "tracking_number", "shipping_provider", "shipping_method", "bill_to_last_name", "customer_ip", "cart_id", "ship_to_first_name", "ship_to_state", "bill_to_state", "email", "bill_to_first_name", "created_at", "state") VALUES('1234 Any Street', '12345', '1234 Any Street', NULL, 'Doe', 'Apt #30', 'Anytown', 'Apt #30', '2009-08-31 21:05:35', 'GS5506874dfe', 'United States', '12345', 'United States', '4085551212', 'Anytown', NULL, NULL, NULL, NULL, 'Doe', '10.1.1.1', 4, 'John', 'OR', 'OR', 'prez@whitehouse.gov', 'John', '2009-08-31 21:05:35', 'failed')
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top