Skip to content

Commit

Permalink
validations
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszreszke committed Sep 23, 2024
1 parent a4e0334 commit 1629faa
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 56 deletions.
2 changes: 2 additions & 0 deletions rails_application/app/controllers/products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ def edit
def create
Product.create!(product_params)
redirect_to products_path, notice: "Product was successfully created"
rescue ActiveRecord::RecordInvalid => e
return render :new, status: :unprocessable_entity, locals: { errors: e.record.errors }
end

def update
Expand Down
11 changes: 11 additions & 0 deletions rails_application/app/models/product.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
# frozen_string_literal: true

class Product < ApplicationRecord
validates :name, presence: true
validates :price, numericality: { greater_than: 0 }
validate :validate_vat_rate
validates :sku, presence: true

default_scope { where(latest: true) }

before_create :set_stock_level

def set_stock_level
self.stock_level = 0
end

def validate_vat_rate
unless vat_rate.is_a?(Numeric)
errors.add(:vat_rate, "is not a number")
end
end
end
65 changes: 9 additions & 56 deletions rails_application/test/integration/products_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,15 @@ def setup

def test_happy_path
register_customer("Arkency")
product_id = SecureRandom.uuid

get "/products/new"
assert_select "h1", "New Product"
post "/products",
params: {
"authenticity_token" => "[FILTERED]",
"product_id" => product_id,
"name" => "product name",
:price => "20.01",
"vat_rate" => "10"
}
product_id = register_product("product name", 20.01, 10)
follow_redirect!

assert_equal "20.01",
number_to_currency(
Products::Product.find(product_id).price,
Product.find(product_id).price,
unit: ""
)

Expand All @@ -35,86 +27,47 @@ def test_happy_path
patch "/products/#{product_id}",
params: {
"authenticity_token" => "[FILTERED]",
"product_id" => product_id,
:price => "20.02"
product: { price: "20.02" }
}

assert_equal "20.02",
number_to_currency(
Products::Product.find(product_id).price,
Product.last.price,
unit: ""
)
end

def test_does_not_crash_when_setting_products_price_to_0
register_customer("Arkency")
product_id = SecureRandom.uuid

get "/products/new"
assert_select "h1", "New Product"
post "/products",
params: {
"authenticity_token" => "[FILTERED]",
"product_id" => product_id,
"name" => "product name",
"price" => "0",
"vat_rate" => "10"
}

post "/products", params: { product: { name: 'name', price: 0, vat_rate: 10, sku: 'test'} }

assert_response :unprocessable_entity
assert_select "span", "Price must be greater than 0"
end

def test_does_not_crash_when_vat_rate_is_absent
register_customer("Arkency")
product_id = SecureRandom.uuid

get "/products/new"
assert_select "h1", "New Product"
post "/products",
params: {
"authenticity_token" => "[FILTERED]",
"product_id" => product_id,
"name" => "product name",
"price" => "100",
}

assert_response :unprocessable_entity
assert_select "span", "Vat rate can't be blank"
end

def test_does_not_crash_when_vat_rate_is_not_a_number
register_customer("Arkency")
product_id = SecureRandom.uuid

get "/products/new"
assert_select "h1", "New Product"
post "/products",
params: {
"authenticity_token" => "[FILTERED]",
"product_id" => product_id,
"name" => "product name",
"price" => "100",
"vat_rate" =>"abc"
}
post "/products", params: { product: { name: 'name', price: 10, vat_rate: nil, sku: 'test' } }

assert_response :unprocessable_entity
assert_select "span", "Vat rate is not a number"
end

def test_does_not_crash_when_name_is_not_present
register_customer("Arkency")
product_id = SecureRandom.uuid

get "/products/new"
assert_select "h1", "New Product"
post "/products",
params: {
"authenticity_token" => "[FILTERED]",
"product_id" => product_id,
"price" => "100",
"vat_rate" =>"10"
}

post "/products", params: { product: { name: nil, price: 10, vat_rate: 23, sku: 'test' } }

assert_response :unprocessable_entity
assert_select "span", "Name can't be blank"
Expand Down

0 comments on commit 1629faa

Please sign in to comment.