From ff3f3ccc7e31867de427ea7e460b04523c074534 Mon Sep 17 00:00:00 2001 From: lukaszreszke Date: Sat, 21 Sep 2024 09:43:21 +0200 Subject: [PATCH] Customers tests green --- .../app/controllers/customers_controller.rb | 2 +- .../app/controllers/products_controller.rb | 2 +- rails_application/app/models/customer.rb | 2 ++ .../app/views/customers/index.html.erb | 2 +- .../test/integration/customers_test.rb | 20 +++++++++------- rails_application/test/test_helper.rb | 23 +++++++++++-------- 6 files changed, 31 insertions(+), 20 deletions(-) diff --git a/rails_application/app/controllers/customers_controller.rb b/rails_application/app/controllers/customers_controller.rb index 9fc7b97d..62aa4c01 100644 --- a/rails_application/app/controllers/customers_controller.rb +++ b/rails_application/app/controllers/customers_controller.rb @@ -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" diff --git a/rails_application/app/controllers/products_controller.rb b/rails_application/app/controllers/products_controller.rb index e9ba45fa..d5da1a97 100644 --- a/rails_application/app/controllers/products_controller.rb +++ b/rails_application/app/controllers/products_controller.rb @@ -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 diff --git a/rails_application/app/models/customer.rb b/rails_application/app/models/customer.rb index 81804801..a32d354c 100644 --- a/rails_application/app/models/customer.rb +++ b/rails_application/app/models/customer.rb @@ -10,3 +10,5 @@ def promote_to_vip update!(vip: true) end end + +class AlreadyVip < StandardError; end diff --git a/rails_application/app/views/customers/index.html.erb b/rails_application/app/views/customers/index.html.erb index ae69bb94..59d1cd3a 100644 --- a/rails_application/app/views/customers/index.html.erb +++ b/rails_application/app/views/customers/index.html.erb @@ -20,7 +20,7 @@ <% @customers.each do |customer| %> - <%= link_to customer.email, customer_path(customer), class: "text-blue-500 hover:underline" %> + <%= link_to (customer.first_name + customer.last_name), customer_path(customer), class: "text-blue-500 hover:underline" %> <%- if customer.vip %> Already a VIP diff --git a/rails_application/test/integration/customers_test.rb b/rails_application/test/integration/customers_test.rb index e6c18701..d99f36ae 100644 --- a/rails_application/test/integration/customers_test.rb +++ b/rails_application/test/integration/customers_test.rb @@ -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") @@ -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 diff --git a/rails_application/test/test_helper.rb b/rails_application/test/test_helper.rb index 7afc620a..1c343329 100644 --- a/rails_application/test/test_helper.rb +++ b/rails_application/test/test_helper.rb @@ -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 = "user@example.com") + 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) @@ -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 @@ -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