-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Zad 5 #133
base: master
Are you sure you want to change the base?
Zad 5 #133
Changes from all commits
a670e26
4d69f16
3c49750
e4873a9
cf08735
e1da444
076f301
a06c64e
71f0a62
6f163ca
ff5e65d
68a0951
f3dbf4e
20e0725
508a8d3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,3 +25,5 @@ | |
|
||
# Ignore master key for decrypting credentials and more. | ||
/config/master.key | ||
|
||
.DS_Store |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
--require spec_helper | ||
--format documentation |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,5 @@ Style/Documentation: | |
|
||
Metrics/LineLength: | ||
Max: 100 | ||
|
||
require: rubocop-rspec |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
class BuildingsController < ApplicationController | ||
def index; end | ||
def index | ||
render json: Building.all | ||
end | ||
|
||
def show; end | ||
def show | ||
render json: building, include: [:warriors] | ||
end | ||
|
||
private | ||
|
||
def building | ||
@building ||= Building.find(params[:id]) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
class BuildingSerializer < ActiveModel::Serializer | ||
attributes :name | ||
attributes :name, :type, :granary, :siege_ability | ||
|
||
has_many :warriors | ||
|
||
def siege_ability | ||
Reports::SiegeReport.new(building: object).call | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,10 +3,29 @@ | |
module Reports | ||
class SiegeReport | ||
def initialize(building:) | ||
@building = building | ||
end | ||
|
||
def call | ||
raise NotImprementedYet | ||
calc_siege_ability | ||
end | ||
|
||
private | ||
|
||
attr_reader :building | ||
|
||
def calc_siege_ability | ||
return 0 if @building.warriors.count.zero? | ||
|
||
day_req = 10 | ||
@building.warriors.each do |warrior| | ||
day_req += if warrior.type == 'Warriors::Hussar' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ładniej by to chyba wyglądało, jeśli Warrior miałby zaimplementowaną metodę/atrybut np. supply_burn_rate, który zwraca 1 jeśli warrior nie ma konia i 2 jeśli go ma, wtedy nie trzeba by myśleć nad takimi ifami w tym serwisie :) |
||
2 | ||
else | ||
1 | ||
end | ||
end | ||
@building.granary / day_req | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
FactoryBot.define do | ||
factory :building do | ||
name { 'Black Gate' } | ||
type { 'Buildings::Walls' } | ||
granary { 0 } | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
FactoryBot.define do | ||
factory :clan do | ||
name { 'Stary Oboz' } | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
FactoryBot.define do | ||
factory :warrior, class: 'Warrior' do | ||
association(:building) | ||
association(:clan) | ||
name { 'Lee' } | ||
|
||
factory :hussar, parent: :warrior, class: 'Warriors::Hussar' do | ||
end | ||
|
||
factory :samurai, parent: :warrior, class: 'Warriors::Samurai' do | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# frozen_string_literal: true | ||
|
||
# This file is copied to spec/ when you run 'rails generate rspec:install' | ||
require 'spec_helper' | ||
ENV['RAILS_ENV'] ||= 'test' | ||
require File.expand_path('../config/environment', __dir__) | ||
# Prevent database truncation if the environment is production | ||
abort('The Rails environment is running in production mode!') if Rails.env.production? | ||
require 'rspec/rails' | ||
# Add additional requires below this line. Rails is not loaded until this point! | ||
|
||
# Requires supporting ruby files with custom matchers and macros, etc, in | ||
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are | ||
# run as spec files by default. This means that files in spec/support that end | ||
# in _spec.rb will both be required and run as specs, causing the specs to be | ||
# run twice. It is recommended that you do not name files matching this glob to | ||
# end with _spec.rb. You can configure this pattern with the --pattern | ||
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`. | ||
# | ||
# The following line is provided for convenience purposes. It has the downside | ||
# of increasing the boot-up time by auto-requiring all files in the support | ||
# directory. Alternatively, in the individual `*_spec.rb` files, manually | ||
# require only the support files necessary. | ||
# | ||
Dir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f } | ||
|
||
# Checks for pending migrations and applies them before tests are run. | ||
# If you are not using ActiveRecord, you can remove these lines. | ||
begin | ||
ActiveRecord::Migration.maintain_test_schema! | ||
rescue ActiveRecord::PendingMigrationError => e | ||
puts e.to_s.strip | ||
exit 1 | ||
end | ||
RSpec.configure do |config| | ||
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures | ||
config.fixture_path = "#{::Rails.root}/spec/fixtures" | ||
|
||
# If you're not using ActiveRecord, or you'd prefer not to run each of your | ||
# examples within a transaction, remove the following line or assign false | ||
# instead of true. | ||
config.use_transactional_fixtures = true | ||
|
||
# RSpec Rails can automatically mix in different behaviours to your tests | ||
# based on their file location, for example enabling you to call `get` and | ||
# `post` in specs under `spec/controllers`. | ||
# | ||
# You can disable this behaviour by removing the line below, and instead | ||
# explicitly tag your specs with their type, e.g.: | ||
# | ||
# RSpec.describe UsersController, :type => :controller do | ||
# # ... | ||
# end | ||
# | ||
# The different available types are documented in the features, such as in | ||
# https://relishapp.com/rspec/rspec-rails/docs | ||
config.infer_spec_type_from_file_location! | ||
|
||
# Filter lines from Rails gems in backtraces. | ||
config.filter_rails_from_backtrace! | ||
# arbitrary gems may also be filtered via: | ||
# config.filter_gems_from_backtrace("gem name") | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe 'Buildings API', type: :request do | ||
describe 'GET /buildings' do | ||
before { create_list(:building, 2) } | ||
|
||
it 'responds with 200' do | ||
get '/buildings' | ||
expect(response).to have_http_status(200) | ||
end | ||
|
||
it 'includes correct number of records' do | ||
get '/buildings' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ten get też mógłby się znaleźć w before |
||
response_json = JSON.parse(response.body) | ||
expect(response_json['data'].size).to eq(2) | ||
end | ||
end | ||
|
||
describe 'GET /buildings/:id' do | ||
let(:building) do | ||
create(:building, | ||
name: building_name, | ||
granary: building_granary) | ||
end | ||
let(:building_name) { 'Black Gate' } | ||
let(:building_granary) { 100 } | ||
|
||
it 'includes correct name' do | ||
get "/buildings/#{building.id}" | ||
response_json = JSON.parse(response.body) | ||
expect(response_json.dig('data', 'attributes', 'name')).to eq(building_name) | ||
end | ||
|
||
it 'includes correct granary amount' do | ||
get "/buildings/#{building.id}" | ||
response_json = JSON.parse(response.body) | ||
expect(response_json.dig('data', 'attributes', 'granary')).to eq(building_granary) | ||
end | ||
|
||
it 'responds with 200' do | ||
get "/buildings/#{building.id}" | ||
expect(response).to have_http_status(200) | ||
end | ||
|
||
context 'when destroy building' do | ||
before { building.destroy } | ||
|
||
it 'responds with 404' do | ||
get "/buildings/#{building.id}" | ||
expect(response).to have_http_status(404) | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
describe Reports::SiegeReport do | ||
subject(:siege_report) { described_class.new(building: building) } | ||
|
||
let(:building) do | ||
create(:building, | ||
name: building_name, | ||
granary: building_granary) | ||
end | ||
let(:building_name) { 'Black Gate' } | ||
|
||
describe '#call' do | ||
context 'without granary' do | ||
let(:building_granary) { 0 } | ||
|
||
it 'returns 0' do | ||
expect(siege_report.call).to eq(0) | ||
end | ||
end | ||
|
||
context 'without warriors' do | ||
let(:building_granary) { 11 } | ||
|
||
it 'returns 0' do | ||
expect(siege_report.call).to eq(0) | ||
end | ||
end | ||
|
||
context 'with one infantry' do | ||
let(:warrior) { build(:samurai) } | ||
let(:building_granary) { 11 } | ||
|
||
it 'returns 1' do | ||
warrior.building = building | ||
warrior.save | ||
expect(siege_report.call).to eq(1) | ||
end | ||
end | ||
|
||
context 'with one cavalry' do | ||
let(:warrior) { build(:hussar) } | ||
let(:building_granary) { 12 } | ||
|
||
it 'returns 1' do | ||
warrior.building = building | ||
warrior.save | ||
expect(siege_report.call).to eq(1) | ||
end | ||
end | ||
|
||
context 'with one infantry and one cavalry' do | ||
let(:warrior1) { build(:samurai, name: 'Lee') } | ||
let(:warrior2) { build(:hussar) } | ||
let(:building_granary) { 12 } | ||
|
||
it 'returns 1' do | ||
warrior1.building = building | ||
warrior1.save | ||
warrior2.building = building | ||
warrior2.save | ||
expect(siege_report.call).to eq(1) | ||
end | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To sprawdzenie spokojnie by się mogło znaleźć gdzieś wyżej, żeby nie obciążać logiki serwisu :) Serwis najlepiej jeśli ma jedną odpowiedzialność, a Twój serwis poza liczeniem ile dni przetrwa dany budynek, stwierdza upadłość budynku jeśli nie ma w nim wojowników :)