Skip to content

Commit 231b723

Browse files
committed
Add factories; refactor specs to services
1 parent e11605b commit 231b723

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+335
-126
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414
- Fixes [issues/5](https://github.com/patch-technology/patch-ruby/issues/5). Adds method `.openapi_nullable` which returns a set with all nullable properties. This method is referenced in `#to_hash` which returns a hash representation of the resource.
1515
- Removes non-deterministic integration spec that is creating an Estimate with a specific project-id. Since we do not control the test data (yet) and make queries against our Test projects, we cannot be sure that we will have offsets available for a project ID. Instead we can rely on Patch core specs to cover this.
1616
- Fixes a spec that was failing due to a Patch core change.
17+
- Add spec factories and refactor generated method specs to shared examples.
1718

1819
## [1.2.4] - 2020-10-14
1920

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ group :development, :test do
66
gem 'rake', '~> 13.0.1'
77
gem 'pry-byebug'
88
gem 'rubocop'
9+
gem 'factory_bot'
910
end

Gemfile.lock

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,27 @@ PATH
88
GEM
99
remote: https://rubygems.org/
1010
specs:
11+
activesupport (6.1.1)
12+
concurrent-ruby (~> 1.0, >= 1.0.2)
13+
i18n (>= 1.6, < 2)
14+
minitest (>= 5.1)
15+
tzinfo (~> 2.0)
16+
zeitwerk (~> 2.3)
1117
ast (2.4.1)
1218
byebug (11.1.3)
1319
coderay (1.1.3)
20+
concurrent-ruby (1.1.7)
1421
diff-lcs (1.4.3)
1522
ethon (0.12.0)
1623
ffi (>= 1.3.0)
24+
factory_bot (6.1.0)
25+
activesupport (>= 5.0.0)
1726
ffi (1.13.1)
27+
i18n (1.8.7)
28+
concurrent-ruby (~> 1.0)
1829
json (2.4.1)
1930
method_source (1.0.0)
31+
minitest (5.14.3)
2032
parallel (1.19.2)
2133
parser (2.7.1.4)
2234
ast (~> 2.4.1)
@@ -57,12 +69,16 @@ GEM
5769
ruby-progressbar (1.10.1)
5870
typhoeus (1.4.0)
5971
ethon (>= 0.9.0)
72+
tzinfo (2.0.4)
73+
concurrent-ruby (~> 1.0)
6074
unicode-display_width (1.7.0)
75+
zeitwerk (2.4.2)
6176

6277
PLATFORMS
6378
ruby
6479

6580
DEPENDENCIES
81+
factory_bot
6682
patch_ruby!
6783
pry-byebug
6884
rake (~> 13.0.1)

spec/factories/allocations.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FactoryBot.define do
2+
factory :allocation, class: Patch::Allocation do
3+
sequence(:id) { |n| n }
4+
production { false }
5+
mass_g { 100 }
6+
end
7+
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FactoryBot.define do
2+
factory :create_mass_estimate_request, class: Patch::CreateMassEstimateRequest do
3+
sequence(:project_id) { |n| n }
4+
mass_g { 100 }
5+
end
6+
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FactoryBot.define do
2+
factory :create_order_request, class: Patch::CreateOrderRequest do
3+
sequence(:project_id) { |n| n }
4+
mass_g { 100 }
5+
total_price_cents_usd { 100 }
6+
metadata { {} }
7+
end
8+
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
FactoryBot.define do
2+
factory :create_preference_request, class: Patch::CreatePreferenceRequest do
3+
sequence(:project_id) { |n| n }
4+
end
5+
end

spec/factories/error_responses.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FactoryBot.define do
2+
factory :error_response, class: Patch::ErrorResponse do
3+
success { true }
4+
error { {} }
5+
data { {} }
6+
end
7+
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FactoryBot.define do
2+
factory :estimate_list_response, class: Patch::EstimateListResponse do
3+
success { true }
4+
error { {} }
5+
data { {} }
6+
meta { {} }
7+
end
8+
end

spec/factories/estimate_responses.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FactoryBot.define do
2+
factory :estimate_response, class: Patch::EstimateResponse do
3+
success { true }
4+
error { {} }
5+
data { {} }
6+
end
7+
end

spec/factories/estimates.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FactoryBot.define do
2+
factory :estimate, class: Patch::Estimate do
3+
sequence(:id) { |n| n }
4+
association :order
5+
production { false }
6+
type { "mass" }
7+
end
8+
end

spec/factories/meta_index_objects.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FactoryBot.define do
2+
factory :meta_index_object, class: Patch::MetaIndexObject do
3+
sequence(:next_page) { |n| n + 1 }
4+
sequence(:prev_page) { |n| n - 1 }
5+
end
6+
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FactoryBot.define do
2+
factory :order_list_response, class: Patch::OrderListResponse do
3+
success { true }
4+
error { {} }
5+
data { {} }
6+
meta { {} }
7+
end
8+
end

spec/factories/order_responses.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FactoryBot.define do
2+
factory :order_response, class: Patch::OrderResponse do
3+
success { true }
4+
error { {} }
5+
data { {} }
6+
end
7+
end

spec/factories/orders.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
FactoryBot.define do
2+
factory :order, class: Patch::Order do
3+
sequence(:id) { |n| n }
4+
mass_g { 1_000 }
5+
production { false }
6+
state { "draft" }
7+
allocation_state { "pending" }
8+
price_cents_usd { 500 }
9+
patch_fee_cents_usd { 5 }
10+
metadata { {} }
11+
end
12+
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FactoryBot.define do
2+
factory :preference_list_response, class: Patch::PreferenceListResponse do
3+
success { true }
4+
error { {} }
5+
data { {} }
6+
meta { {} }
7+
end
8+
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FactoryBot.define do
2+
factory :preference_response, class: Patch::PreferenceResponse do
3+
success { true }
4+
error { {} }
5+
data { {} }
6+
end
7+
end

spec/factories/preferences.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FactoryBot.define do
2+
factory :preference, class: Patch::Preference do
3+
sequence(:id) { |n| n }
4+
association :project
5+
allocation_percentage { 1 }
6+
end
7+
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FactoryBot.define do
2+
factory :project_list_response, class: Patch::ProjectListResponse do
3+
success { true }
4+
error { {} }
5+
data { {} }
6+
meta { {} }
7+
end
8+
end

spec/factories/project_responses.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FactoryBot.define do
2+
factory :project_response, class: Patch::ProjectResponse do
3+
success { true }
4+
error { {} }
5+
data { {} }
6+
end
7+
end

spec/factories/projects.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
FactoryBot.define do
2+
factory :project, class: Patch::Project do
3+
sequence(:id) { |n| n }
4+
production { false }
5+
name { "New Project" }
6+
description { "New Descirption" }
7+
type { "biomass" }
8+
country { "DK" }
9+
developer { "Danish Developer" }
10+
photos { [] }
11+
average_price_per_tonne_cents_usd { 120 }
12+
remaining_mass_g { 1_000 }
13+
standard { 'european_biochar_certificate' }
14+
end
15+
end

spec/models/allocation_spec.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,19 @@
2020
describe 'Allocation' do
2121
before do
2222
# run before each test
23-
@instance = Patch::Allocation.new
23+
@instance = build(:allocation)
2424
end
2525

2626
after do
2727
# run after each test
2828
end
2929

30+
it_behaves_like "a generated class" do
31+
let(:instance) { @instance }
32+
let(:instance_hash) { { id: @instance.id, mass_g: @instance.mass_g, production: @instance.production } }
33+
let(:nullable_properties) { Set.new }
34+
end
35+
3036
describe 'test an instance of Allocation' do
3137
it 'should create an instance of Allocation' do
3238
expect(@instance).to be_instance_of(Patch::Allocation)
@@ -50,10 +56,4 @@
5056
# assertion here. ref: https://www.relishapp.com/rspec/rspec-expectations/docs/built-in-matchers
5157
end
5258
end
53-
54-
describe '.openapi_nullable' do
55-
it 'returns a set with nullable properties' do
56-
expect(Patch::Allocation.openapi_nullable).to be_empty
57-
end
58-
end
5959
end

spec/models/create_mass_estimate_request_spec.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,19 @@
2020
describe 'CreateMassEstimateRequest' do
2121
before do
2222
# run before each test
23-
@instance = Patch::CreateMassEstimateRequest.new
23+
@instance = build(:create_mass_estimate_request)
2424
end
2525

2626
after do
2727
# run after each test
2828
end
2929

30+
it_behaves_like "a generated class" do
31+
let(:instance) { @instance }
32+
let(:instance_hash) { { project_id: @instance.project_id, mass_g: @instance.mass_g } }
33+
let(:nullable_properties) { Set.new }
34+
end
35+
3036
describe 'test an instance of CreateMassEstimateRequest' do
3137
it 'should create an instance of CreateMassEstimateRequest' do
3238
expect(@instance).to be_instance_of(Patch::CreateMassEstimateRequest)
@@ -37,10 +43,4 @@
3743
# assertion here. ref: https://www.relishapp.com/rspec/rspec-expectations/docs/built-in-matchers
3844
end
3945
end
40-
41-
describe '.openapi_nullable' do
42-
it 'returns a set with nullable properties' do
43-
expect(Patch::CreateMassEstimateRequest.openapi_nullable).to be_empty
44-
end
45-
end
4646
end

spec/models/create_order_request_spec.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,19 @@
2020
describe 'CreateOrderRequest' do
2121
before do
2222
# run before each test
23-
@instance = Patch::CreateOrderRequest.new
23+
@instance = build(:create_order_request)
2424
end
2525

2626
after do
2727
# run after each test
2828
end
2929

30+
it_behaves_like "a generated class" do
31+
let(:instance) { @instance }
32+
let(:instance_hash) { { project_id: @instance.project_id, mass_g: @instance.mass_g, total_price_cents_usd: @instance.total_price_cents_usd, metadata: @instance.metadata } }
33+
let(:nullable_properties) { Set.new }
34+
end
35+
3036
describe 'test an instance of CreateOrderRequest' do
3137
it 'should create an instance of CreateOrderRequest' do
3238
expect(@instance).to be_instance_of(Patch::CreateOrderRequest)
@@ -37,10 +43,4 @@
3743
# assertion here. ref: https://www.relishapp.com/rspec/rspec-expectations/docs/built-in-matchers
3844
end
3945
end
40-
41-
describe '.openapi_nullable' do
42-
it 'returns a set with nullable properties' do
43-
expect(Patch::CreateOrderRequest.openapi_nullable).to be_empty
44-
end
45-
end
4646
end

spec/models/create_preference_request_spec.rb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,28 @@
2020
describe 'CreatePreferenceRequest' do
2121
before do
2222
# run before each test
23-
@instance = Patch::CreatePreferenceRequest.new
23+
@instance = build(:create_preference_request)
2424
end
2525

2626
after do
2727
# run after each test
2828
end
2929

30+
it_behaves_like "a generated class" do
31+
let(:instance) { @instance }
32+
let(:instance_hash) { { project_id: @instance.project_id } }
33+
let(:nullable_properties) { Set.new }
34+
end
35+
3036
describe 'test an instance of CreatePreferenceRequest' do
3137
it 'should create an instance of CreatePreferenceRequest' do
3238
expect(@instance).to be_instance_of(Patch::CreatePreferenceRequest)
3339
end
3440
end
41+
3542
describe 'test attribute "project_id"' do
3643
it 'should work' do
3744
# assertion here. ref: https://www.relishapp.com/rspec/rspec-expectations/docs/built-in-matchers
3845
end
3946
end
40-
41-
describe '.openapi_nullable' do
42-
it 'returns a set with nullable properties' do
43-
expect(Patch::CreatePreferenceRequest.openapi_nullable).to be_empty
44-
end
45-
end
4647
end

spec/models/error_response_spec.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,19 @@
2020
describe 'ErrorResponse' do
2121
before do
2222
# run before each test
23-
@instance = Patch::ErrorResponse.new
23+
@instance = build(:error_response)
2424
end
2525

2626
after do
2727
# run after each test
2828
end
2929

30+
it_behaves_like "a generated class" do
31+
let(:instance) { @instance }
32+
let(:instance_hash) { { success: @instance.success, error: @instance.error, data: @instance.data } }
33+
let(:nullable_properties) { Set.new(["data"]) }
34+
end
35+
3036
describe 'test an instance of ErrorResponse' do
3137
it 'should create an instance of ErrorResponse' do
3238
expect(@instance).to be_instance_of(Patch::ErrorResponse)
@@ -49,10 +55,4 @@
4955
# assertion here. ref: https://www.relishapp.com/rspec/rspec-expectations/docs/built-in-matchers
5056
end
5157
end
52-
53-
describe '.openapi_nullable' do
54-
it 'returns a set with nullable properties' do
55-
expect(Patch::ErrorResponse.openapi_nullable).to contain_exactly("data")
56-
end
57-
end
5858
end

0 commit comments

Comments
 (0)