Skip to content

Commit

Permalink
Add test files for game and author
Browse files Browse the repository at this point in the history
  • Loading branch information
fatmahussein committed Oct 25, 2023
1 parent 72b5a7a commit 77489ae
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ def load_data
end
end

# save games to database
def save_games
update_games = []
@games.each do |game|
Expand All @@ -123,6 +124,7 @@ def save_games
File.write('json/games.json', JSON.generate(update_games))
end

# save authors to the database
def save_authors
authors_data = @authors.map do |author|
{ 'id' => author.id, 'first_name' => author.first_name, 'last_name' => author.last_name }
Expand Down
18 changes: 18 additions & 0 deletions spec/author_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require_relative '../author'

describe Author do
let(:author) { Author.new('John', 'Doe') }

describe '#add_item' do
context 'when the item is a game' do
it 'adds the item to the author' do
author.add_item(game)
expect(author.items).to include(game)
end
it 'sets the author of the game' do
author.add_item(game)
expect(game.author).to eq(author)
end
end
end
end
42 changes: 42 additions & 0 deletions spec/game_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require_relative '../game'
describe Game do
let(:game) { Game.new(publish_date, archived, multiplayer, last_played_at, author) }
let(:publish_date) { '2012-01-01' }
let(:archived) { false }
let(:multiplayer) { true }
let(:last_played_at) { '2014-01-01' }
let(:author) { Author.new('John', 'Doe') }
describe '#can_be_archived?' do
context 'when the game is archived and last played over two years ago' do
let(:archived) { true }
let(:last_played_at) { '2014-01-01' }
it 'returns true' do
expect(game.can_be_archived?).to eq(true)
end
end
context 'when the game is not archived' do
let(:archived) { false }
context 'and the game was last played over two years ago' do
let(:last_played_at) { '2014-01-01' }
it 'returns false' do
expect(game.can_be_archived?).to eq(true)
end
end
context 'and the game was last played less than two years ago' do
let(:last_played_at) { '2015-01-01' }
it 'returns false' do
expect(game.can_be_archived?).to eq(true)
end
end
end
context 'when the game is archived' do
let(:archived) { true }
context 'and the game was last played over two years ago' do
let(:last_played_at) { '2014-01-01' }
it 'returns true' do
expect(game.can_be_archived?).to eq(true)
end
end
end
end
end

0 comments on commit 77489ae

Please sign in to comment.