Skip to content

Commit

Permalink
Customers tests green
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszreszke committed Sep 21, 2024
1 parent 0845c39 commit ff3f3cc
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 20 deletions.
2 changes: 1 addition & 1 deletion rails_application/app/controllers/customers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def create

def update
promote_to_vip(params[:id])
rescue Crm::Customer::AlreadyVip
rescue AlreadyVip
redirect_to customers_path, notice: "Customer was marked as vip"
else
redirect_to customers_path, notice: "Customer was promoted to VIP"
Expand Down
2 changes: 1 addition & 1 deletion rails_application/app/controllers/products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ def add_future_price
private

def product_params
params.require(:product).permit(:name, :price, :vat_rate).to_h.symbolize_keys.slice(:price, :vat_rate, :name)
params.require(:product).permit(:name, :price, :vat_rate, :sku).to_h.symbolize_keys.slice(:price, :vat_rate, :name, :sku)
end
end
2 changes: 2 additions & 0 deletions rails_application/app/models/customer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ def promote_to_vip
update!(vip: true)
end
end

class AlreadyVip < StandardError; end
2 changes: 1 addition & 1 deletion rails_application/app/views/customers/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<tbody>
<% @customers.each do |customer| %>
<tr class="border-t">
<td class="py-2"><%= link_to customer.email, customer_path(customer), class: "text-blue-500 hover:underline" %></td>
<td class="py-2"><%= link_to (customer.first_name + customer.last_name), customer_path(customer), class: "text-blue-500 hover:underline" %></td>
<td class="py-2 text-center">
<%- if customer.vip %>
Already a VIP
Expand Down
20 changes: 12 additions & 8 deletions rails_application/test/integration/customers_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@ def test_paid_orders_summary
assert_customer_summary("Customer Shop", "$0.00")
assert_customer_summary("BigCorp Ltd", "$0.00")

order_and_pay(customer_id, SecureRandom.uuid, product_1_id, product_2_id)
order = new_order

order_and_pay(customer_id, new_order.id, product_1_id, product_2_id)
visit_customers_index

assert_customer_summary("Customer Shop", "$7.00")
assert_customer_summary("BigCorp Ltd", "$0.00")

order_and_pay(customer_id, SecureRandom.uuid, product_1_id)
another_order = new_order

order_and_pay(customer_id, another_order.id, product_1_id)
visit_customers_index

assert_customer_summary("Customer Shop", "$11.00")
Expand All @@ -41,16 +45,16 @@ def test_customer_details
customer_id = register_customer("Customer Shop")
product_id = register_product("Fearless Refactoring", 4, 10)

order_uid = SecureRandom.uuid
order_id = new_order.id

order_and_pay(customer_id, order_uid, product_id)
order_and_pay(customer_id, order_id, product_id)
visit_customer_page(customer_id)

order = ClientOrders::Order.find_by(order_uid: order_uid)
order = Order.find(order_id)

assert_select "h1", "Customer Page"
assert_customer_details "Customer Shop", "No"
assert_customer_orders_table order.number, "Paid", "$4.00", "$4.00"
assert_select("h1", "Customer Page")
assert_customer_details("Customer Shop", "No")
assert_customer_orders_table(order.number, "Paid", "$4.00", "$4.00")
end

private
Expand Down
23 changes: 14 additions & 9 deletions rails_application/test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,16 @@ def before_teardown
result
end

def register_customer(name)
customer_id = SecureRandom.uuid
post "/customers", params: { customer_id: customer_id, name: name }
customer_id
def register_customer(name, email = "[email protected]")
post "/customers", params: { customer: { first_name: name, last_name: "", email: } }
Customer.find_by(email:).id
end

def register_product(name, price, vat_rate)
product_id = SecureRandom.uuid
post "/products", params: { product_id: product_id, name: name, price: price, vat_rate: vat_rate }
product_id
def register_product(name, price, vat_rate, sku = SecureRandom.uuid, stock_level: 10)
post "/products", params: { product: { name: name, price: price, vat_rate: vat_rate, sku: } }
product = Product.find_by(sku:)
post "/products/#{product.id}/supplies", params: { product_id: product.id, quantity: stock_level }
product.id
end

def supply_product(product_id, quantity)
Expand Down Expand Up @@ -111,7 +111,7 @@ def submit_order(customer_id, order_id)
"order_id" => order_id,
"customer_id" => customer_id,
"commit" => "Submit order"
}
}
end

def visit_customers_index
Expand All @@ -134,6 +134,11 @@ def add_product_to_basket(order_id, product_id)
post "/orders/#{order_id}/add_item?product_id=#{product_id}"
end

def new_order
get "/orders/new"
Order.last
end

def run_command(command)
Rails.configuration.command_bus.call(command)
end
Expand Down

0 comments on commit ff3f3cc

Please sign in to comment.