Last active
June 9, 2022 12:53
-
-
Save joboccara/c4d6cc4afb0364af50438a3113d30734 to your computer and use it in GitHub Desktop.
Software design training: tests for iteration on data model
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' | |
class ProductsControllerTest < ActionDispatch::IntegrationTest | |
test 'gets a book with its details' do | |
book = # Create book with title: 'Item 1', isbn: '1', purchase_price: 42, is_hot: true | |
get product_url(book[:id]) | |
res = response.parsed_body | |
assert_equal 'Item 1', res['title'] | |
assert_equal 'book', res['kind'] | |
assert_equal 42, res['purchase_price'] | |
assert_equal true, res['is_hot'] | |
assert_nil res['width'] | |
assert_nil res['duration'] | |
end | |
test 'gets an image with its details' do | |
image = # Create image with title: 'Item 2', width: 800, height: 600, source: 'unknown', format: 'jpg' | |
get product_url(image[:id]) | |
res = response.parsed_body | |
assert_equal 'Item 2', res['title'] | |
assert_equal 'image', res['kind'] | |
assert_equal 800, res['width'] | |
assert_equal 600, res['height'] | |
assert_equal 'unknown', res['source'] | |
assert_equal 'jpg', res['format'] | |
assert_nil res['duration'] | |
assert_nil res['is_hot'] | |
end | |
test 'gets a video with its details' do | |
video = # Create video with title: 'Item 3', duration: 120, quality: 'FullHD' | |
get product_url(video[:id]) | |
res = response.parsed_body | |
assert_equal 'Item 3', res['title'] | |
assert_equal 'video', res['kind'] | |
assert_equal 120, res['duration'] | |
assert_equal 'FullHD', res['quality'] | |
assert_nil res['is_hot'] | |
assert_nil res['width'] | |
end | |
test 'gets all products' do | |
# Create book with title: 'Item 1', isbn: '1', purchase_price: 42, is_hot: false | |
# Create image with title: 'Item 2', width: 800, height: 600, source: 'unknown', format: 'jpg' | |
# Create video with title: 'Item 3', duration: 120, quality: 'FullHD' | |
# Create video with title: 'Item 4', duration: 130, quality: 'FullHD' | |
get products_url | |
products_by_kind = response.parsed_body | |
assert_equal 1, products_by_kind['books'].size | |
books = products_by_kind['books'] | |
assert_equal 'Item 1', books[0]['title'] | |
assert_equal 42, books[0]['purchase_price'] | |
images = products_by_kind['images'] | |
assert_equal 1, images.size | |
assert_equal 'Item 2', images[0]['title'] | |
assert_equal 800, images[0]['width'] | |
videos = products_by_kind['videos'] | |
assert_equal 2, videos.size | |
assert_equal 'Item 3', videos[0]['title'] | |
assert_equal 120, videos[0]['duration'] | |
assert_equal 'Item 4', videos[1]['title'] | |
assert_equal 130, videos[1]['duration'] | |
end | |
test 'get the products of a month' do | |
book = # Create book with title: 'Title of Book', isbn: '1', purchase_price: 42, is_hot: false, created_at: '2019-01-01' | |
video = # Create video with title: 'Title of Video', duration: 60, quality: 'FullHD', created_at: '2019-02-01' | |
get '/products?month=february', as: :json | |
products_by_kind = response.parsed_body | |
assert_equal ['videos'], products_by_kind.keys | |
videos = products_by_kind['videos'] | |
assert_equal 'video', videos[0]['kind'] | |
assert_equal 'Title of Video', videos[0]['title'] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment