Last active
June 9, 2022 09:11
-
-
Save joboccara/f319bd1c2c973390036bf8494e55820f to your computer and use it in GitHub Desktop.
Software design training: tests for iteration on prices
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "test_helper" | |
require "csv" | |
class ProductPricesControllerTest < ActionDispatch::IntegrationTest | |
teardown do | |
Timecop.return | |
end | |
test 'prices books at +25% margin' do | |
book1 = # Create book with title: 'Title of Book1', isbn: '1', purchase_price: 12, is_hot: false | |
assert_price_equal 15, get_product_price(book1[:id]) | |
book2 = # Create book with title: 'Title of Book2', isbn: '1', purchase_price: 16, is_hot: false | |
assert_price_equal 20, get_product_price(book2[:id]) | |
end | |
test 'checks the ISBN list to price books' do | |
begin | |
CSV.open(Rails.root.join('isbn_list.csv'), 'w') do |csv| | |
csv << ['ISBN', 'price'] | |
csv << ['9781603095136', '14.99'] | |
csv << ['9781603095099', '19.99'] | |
csv << ['9781603095143', '9.99'] | |
csv << ['9781603095051', '19.99'] | |
end | |
book = # Create book with title: 'Title of Book', isbn: '9781603095099', purchase_price: 12, is_hot: false | |
assert_price_equal 19.99, get_product_price(book[:id]) | |
book = # Create book with title: 'Title of Book', isbn: '1234567890', purchase_price: 16, is_hot: false | |
assert_price_equal 20, get_product_price(book[:id]) | |
ensure | |
File.delete(Rails.root.join('isbn_list.csv')) | |
end | |
end | |
test 'prices hot books at 9.99 during weekdays' do | |
book = # Create book with title: 'Title of Book', isbn: '9781603095099', purchase_price: 14, is_hot: true | |
Timecop.travel(Time.new(2022, 1, 3)) # Monday | |
assert_price_equal 9.99, get_product_price(book[:id]) | |
Timecop.travel(Time.new(2022, 1, 4)) # Tuesday | |
assert_price_equal 9.99, get_product_price(book[:id]) | |
Timecop.travel(Time.new(2022, 1, 5)) # Wednesday | |
assert_price_equal 9.99, get_product_price(book[:id]) | |
Timecop.travel(Time.new(2022, 1, 6)) # Thursday | |
assert_price_equal 9.99, get_product_price(book[:id]) | |
Timecop.travel(Time.new(2022, 1, 7)) # Friday | |
assert_price_equal 9.99, get_product_price(book[:id]) | |
end | |
test 'does not take hot books into account during weekends' do | |
begin | |
CSV.open(Rails.root.join('isbn_list.csv'), 'w') do |csv| | |
csv << ['ISBN', 'price'] | |
csv << ['9781603095136', '14.99'] | |
end | |
book = # Create book with title: 'Title of Book', isbn: '9781603095099', purchase_price: 14, is_hot: true | |
Timecop.travel(Time.new(2022, 1, 8)) # Saturday | |
assert_price_equal 9.99, get_product_price(book[:id]) | |
Timecop.travel(Time.new(2022, 1, 9)) # Sunday | |
assert_price_equal 9.99, get_product_price(book[:id]) | |
ensure | |
File.delete(Rails.root.join('isbn_list.csv')) | |
end | |
end | |
test 'prices images from NationalGeographic .02/9600px' do | |
image = # Create image with title: 'Title of Image', width: 800, height: 600, source: 'NationalGeographic', format: 'jpg' | |
assert_price_equal 1, get_product_price(image[:id]) | |
end | |
test 'prices images from Getty at 1 when below 1280x720' do | |
image = # Create image with title: 'Title of Image', width: 1280, height: 720, source: 'Getty', format: 'jpg' | |
assert_price_equal 1, get_product_price(image[:id]) | |
end | |
test 'prices images from Getty at 3 when below 1920x1080' do | |
image1 = # Create image with title: 'Title of Image1', width: 1281, height: 720, source: 'Getty', format: 'jpg' | |
assert_price_equal 3, get_product_price(image1[:id]) | |
image2 = # Create image with title: 'Title of Image2', width: 1920, height: 1080, source: 'Getty', format: 'jpg' | |
assert_price_equal 3, get_product_price(image2[:id]) | |
end | |
test 'prices images from Getty at 5 when above 1920x1080' do | |
image = # Create image with title: 'Title of Image', width: 1921, height: 1080, source: 'Getty', format: 'jpg' | |
assert_price_equal 5, get_product_price(image[:id]) | |
end | |
test 'prices images from Getty in raw format at 10' do | |
image1 = # Create image with title: 'Title of Image1', width: 800, height: 600, source: 'Getty', format: 'raw' | |
image2 = # Create image with title: 'Title of Image2', width: 1920, height: 1080, source: 'Getty', format: 'raw' | |
image3 = # Create image with title: 'Title of Image3', width: 1921, height: 1080, source: 'Getty', format: 'raw' | |
assert_price_equal 10, get_product_price(image1[:id]) | |
assert_price_equal 10, get_product_price(image2[:id]) | |
assert_price_equal 10, get_product_price(image3[:id]) | |
end | |
test 'prices other images at 7' do | |
image = # Create image with title: 'Title of Image', width: 800, height: 600, source: 'unknown', format: 'jpg' | |
assert_price_equal 7, get_product_price(image[:id]) | |
end | |
test 'prices 4k videos at 0.08/second' do | |
Timecop.travel Time.new(2022, 1, 1) + 6.hours | |
video = # Create video with title: 'Title of Video', duration: 150, quality: '4k' | |
assert_price_equal 12, get_product_price(video[:id]) | |
end | |
test 'prices FullHD videos at 3 per started minute' do | |
Timecop.travel Time.new(2022, 1, 1) + 6.hours | |
video60 = # Create video with title: 'Title of Video', duration: 59, quality: 'FullHD' | |
assert_price_equal 3, get_product_price(video60[:id]) | |
video61 = # Create video with title: 'Title of Video', duration: 60, quality: 'FullHD' | |
assert_price_equal 6, get_product_price(video61[:id]) | |
end | |
test 'prices SD videos at 1 per started minute' do | |
Timecop.travel Time.new(2022, 1, 1) + 6.hours | |
video60 = # Create video with title: 'Title of Video', duration: 59, quality: 'SD' | |
assert_price_equal 1, get_product_price(video60[:id]) | |
video61 = # Create video with title: 'Title of Video', duration: 60, quality: 'SD' | |
assert_price_equal 2, get_product_price(video61[:id]) | |
end | |
test 'prices SD videos longer than 10 minutes at 10' do | |
Timecop.travel Time.new(2022, 1, 1) + 6.hours | |
video = # Create video with title: 'Title of Video', duration: 10*60, quality: 'SD' | |
assert_price_equal 10, get_product_price(video[:id]) | |
end | |
test 'reduces video prices by -40% during the night' do | |
video = # Create video with title: 'Title of Video', duration: 150, quality: '4k' | |
Timecop.travel Time.new(2022, 1, 1) + 5.hours - 1.minute | |
assert_price_equal 7.2, get_product_price(video[:id]) | |
Timecop.travel Time.new(2022, 1, 1) + 5.hours + 1.minute | |
assert_price_equal 12, get_product_price(video[:id]) | |
Timecop.travel Time.new(2022, 1, 1) + 22.hours - 1.minute | |
assert_price_equal 12, get_product_price(video[:id]) | |
Timecop.travel Time.new(2022, 1, 1) + 22.hours + 1.minute | |
assert_price_equal 7.2, get_product_price(video[:id]) | |
end | |
test 'bumps price of any product by +5% if the title contains "premium"' do | |
book1 = # Create book with title: 'Title of Book', isbn: '1', purchase_price: 12, is_hot: false | |
assert_price_equal 15, get_product_price(book1[:id]) | |
book = # Create book with title: 'Title of premium Book', isbn: '1', purchase_price: 12, is_hot: false | |
assert_price_equal 15.75, get_product_price(book[:id]) | |
image = # Create image with title: 'Title of Image', width: 800, height: 600, source: 'unknown', format: 'jpg' | |
assert_price_equal 7, get_product_price(image[:id]) | |
image = # Create image with title: 'Premium title of Image', width: 800, height: 600, source: 'unknown', format: 'jpg' | |
assert_price_equal 7.35, get_product_price(image[:id]) | |
video = # Create video with title: 'Title of Video', duration: 150, quality: '4k' | |
assert_price_equal 12, get_product_price(video[:id]) | |
video = # Create video with title: 'Title of Video premium', duration: 150, quality: '4k' | |
assert_price_equal 12.6, get_product_price(video[:id]) | |
end | |
private | |
def get_product_price(id) | |
get product_price_url(id) | |
response.parsed_body.to_f | |
end | |
def assert_price_equal(expected, actual) | |
assert_in_delta expected, actual, 0.01 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment