From 883e637545a59706685bd4f2cc19c2bad15b3703 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Mon, 26 Jun 2023 17:08:23 +1000 Subject: [PATCH] Create deterministic test data for stable API docs Otherwise the auto-generated Swagger file changes all the time without code changes. --- .../spec/requests/enterprises_spec.rb | 21 +++++++---- .../spec/requests/persons_spec.rb | 4 +- .../spec/requests/supplied_products_spec.rb | 16 ++++++-- swagger/dfc-v1.7/swagger.yaml | 37 ++++++++----------- 4 files changed, 43 insertions(+), 35 deletions(-) diff --git a/engines/dfc_provider/spec/requests/enterprises_spec.rb b/engines/dfc_provider/spec/requests/enterprises_spec.rb index ed440b3ecc..db3f6709f5 100644 --- a/engines/dfc_provider/spec/requests/enterprises_spec.rb +++ b/engines/dfc_provider/spec/requests/enterprises_spec.rb @@ -5,8 +5,15 @@ require DfcProvider::Engine.root.join("spec/spec_helper") describe "Enterprises", type: :request, swagger_doc: "dfc-v1.7/swagger.yaml", rswag_autodoc: true do let!(:user) { create(:oidc_user) } - let!(:enterprise) { create(:distributor_enterprise, owner: user) } - let!(:product) { create(:simple_product, supplier: enterprise ) } + let!(:enterprise) { create(:distributor_enterprise, id: 10_000, owner: user) } + let!(:product) { + create( + :base_product, + supplier: enterprise, name: "Apple", description: "Round", + variants: [variant], + ) + } + let(:variant) { build(:base_variant, id: 10_001, unit_value: 1, sku: "APP") } before { login_as user } @@ -20,9 +27,9 @@ describe "Enterprises", type: :request, swagger_doc: "dfc-v1.7/swagger.yaml", rs let(:id) { "default" } run_test! do - expect(response.body).to include(product.name) - expect(response.body).to include(product.variants.first.sku) - expect(response.body).to include("offers/#{product.variants.first.id}") + expect(response.body).to include("Apple") + expect(response.body).to include("APP") + expect(response.body).to include("offers/10001") end end @@ -30,7 +37,7 @@ describe "Enterprises", type: :request, swagger_doc: "dfc-v1.7/swagger.yaml", rs let(:id) { enterprise.id } run_test! do - expect(response.body).to include(product.name) + expect(response.body).to include "Apple" end end end @@ -40,7 +47,7 @@ describe "Enterprises", type: :request, swagger_doc: "dfc-v1.7/swagger.yaml", rs let(:other_enterprise) { create(:distributor_enterprise) } run_test! do - expect(response.body).to_not include(product.name) + expect(response.body).to_not include "Apple" end end end diff --git a/engines/dfc_provider/spec/requests/persons_spec.rb b/engines/dfc_provider/spec/requests/persons_spec.rb index ba0bd9549b..db311f2843 100644 --- a/engines/dfc_provider/spec/requests/persons_spec.rb +++ b/engines/dfc_provider/spec/requests/persons_spec.rb @@ -4,7 +4,7 @@ require "swagger_helper" require DfcProvider::Engine.root.join("spec/spec_helper") describe "Persons", type: :request, swagger_doc: "dfc-v1.7/swagger.yaml", rswag_autodoc: true do - let(:user) { create(:oidc_user) } + let(:user) { create(:oidc_user, id: 10_000) } let(:other_user) { create(:oidc_user) } before { login_as user } @@ -19,7 +19,7 @@ describe "Persons", type: :request, swagger_doc: "dfc-v1.7/swagger.yaml", rswag_ run_test! do expect(response.body).to include "dfc-b:Person" - expect(response.body).to include "persons/#{user.id}" + expect(response.body).to include "persons/10000" end end diff --git a/engines/dfc_provider/spec/requests/supplied_products_spec.rb b/engines/dfc_provider/spec/requests/supplied_products_spec.rb index f409a890da..192d3e72c5 100644 --- a/engines/dfc_provider/spec/requests/supplied_products_spec.rb +++ b/engines/dfc_provider/spec/requests/supplied_products_spec.rb @@ -6,9 +6,15 @@ require DfcProvider::Engine.root.join("spec/spec_helper") describe "SuppliedProducts", type: :request, swagger_doc: "dfc-v1.7/swagger.yaml", rswag_autodoc: true do let!(:user) { create(:oidc_user) } - let!(:enterprise) { create(:distributor_enterprise, owner: user) } - let!(:product) { create(:simple_product, supplier: enterprise ) } - let!(:variant) { product.variants.first } + let!(:enterprise) { create(:distributor_enterprise, id: 10_000, owner: user) } + let!(:product) { + create( + :base_product, + supplier: enterprise, name: "Pesto", description: "Basil Pesto", + variants: [variant], + ) + } + let(:variant) { build(:base_variant, id: 10_001, unit_value: 1) } before { login_as user } @@ -53,10 +59,12 @@ describe "SuppliedProducts", type: :request, swagger_doc: "dfc-v1.7/swagger.yaml end it "creates a variant" do |example| + existing = Spree::Variant.pluck(:id) + expect { submit_request(example.metadata) } .to change { enterprise.supplied_products.count }.by(1) - variant = Spree::Variant.last + variant = Spree::Variant.where.not(id: existing).first expect(variant.name).to eq "Apple" expect(variant.unit_value).to eq 3 end diff --git a/swagger/dfc-v1.7/swagger.yaml b/swagger/dfc-v1.7/swagger.yaml index e4d276942d..17886e6542 100644 --- a/swagger/dfc-v1.7/swagger.yaml +++ b/swagger/dfc-v1.7/swagger.yaml @@ -44,21 +44,17 @@ paths: value: "@context": http://static.datafoodconsortium.org/ontologies/context.json "@graph": - - "@id": http://test.host/api/dfc-v1.7/enterprises/473 + - "@id": http://test.host/api/dfc-v1.7/enterprises/10000 "@type": dfc-b:Enterprise dfc-b:hasName: '' dfc-b:hasDescription: '' dfc-b:VATnumber: '' - dfc-b:supplies: http://test.host/api/dfc-v1.7/enterprises/473/supplied_products/298 - dfc-b:manages: http://test.host/api/dfc-v1.7/enterprises/473/catalog_items/298 - - "@id": http://test.host/api/dfc-v1.7/enterprises/473/supplied_products/298 + dfc-b:supplies: http://test.host/api/dfc-v1.7/enterprises/10000/supplied_products/10001 + dfc-b:manages: http://test.host/api/dfc-v1.7/enterprises/10000/catalog_items/10001 + - "@id": http://test.host/api/dfc-v1.7/enterprises/10000/supplied_products/10001 "@type": dfc-b:SuppliedProduct - dfc-b:name: 'Product #2 - 8569' - dfc-b:description: |- - Voluptatem sequi hic expedita reprehenderit ad veniam recusandae explicabo. Occaecati eum cumque exercitationem culpa dolor. Velit accusamus blanditiis quia laboriosam fuga corrupti maiores ut. - Consectetur illo necessitatibus porro eveniet. Similique est error labore inventore exercitationem. Similique nulla iusto tenetur reiciendis. - Quis laborum saepe illo magnam dolorem distinctio sed eligendi. Suscipit et iure vitae facilis nam quia placeat. Vero ea accusantium corrupti possimus error ad. Repellendus tenetur atque minima architecto. Optio cum magni quisquam aliquid rerum. - Laborum quae tempora iure accusamus sed blanditiis enim. Debitis earum omnis voluptate accusantium deserunt nisi nemo ad. Ea nihil architecto eius odit illo aut. Totam corporis error illum voluptatem. Repellendus eius adipisci eaque amet porro occaecati. + dfc-b:name: Apple + dfc-b:description: Round dfc-b:hasType: dfc-pt:non-local-vegetable dfc-b:hasQuantity: "@type": dfc-b:QuantitativeValue @@ -68,12 +64,12 @@ paths: dfc-b:lifetime: '' dfc-b:usageOrStorageCondition: '' dfc-b:totalTheoreticalStock: 0.0 - - "@id": http://test.host/api/dfc-v1.7/enterprises/473/catalog_items/298 + - "@id": http://test.host/api/dfc-v1.7/enterprises/10000/catalog_items/10001 "@type": dfc-b:CatalogItem - dfc-b:references: http://test.host/api/dfc-v1.7/enterprises/473/supplied_products/298 - dfc-b:sku: '' - dfc-b:stockLimitation: 5 - dfc-b:offeredThrough: http://test.host/api/dfc-v1.7/enterprises/473/offers/298 + dfc-b:references: http://test.host/api/dfc-v1.7/enterprises/10000/supplied_products/10001 + dfc-b:sku: APP + dfc-b:stockLimitation: 0 + dfc-b:offeredThrough: http://test.host/api/dfc-v1.7/enterprises/10000/offers/10001 '404': description: not found "/api/dfc-v1.7/persons/{id}": @@ -94,7 +90,7 @@ paths: test_example: value: "@context": http://static.datafoodconsortium.org/ontologies/context.json - "@id": http://test.host/api/dfc-v1.7/persons/910 + "@id": http://test.host/api/dfc-v1.7/persons/10000 "@type": dfc-b:Person '404': description: not found @@ -155,13 +151,10 @@ paths: test_example: value: "@context": http://static.datafoodconsortium.org/ontologies/context.json - "@id": http://test.host/api/dfc-v1.7/enterprises/478/supplied_products/303 + "@id": http://test.host/api/dfc-v1.7/enterprises/10000/supplied_products/10001 "@type": dfc-b:SuppliedProduct - dfc-b:name: 'Product #6 - 8953' - dfc-b:description: Magni nemo enim saepe harum corporis nobis - facilis. Nostrum facilis molestiae at quaerat exercitationem - itaque veritatis. Incidunt eaque asperiores assumenda eveniet - doloribus qui voluptatibus quidem. + dfc-b:name: Pesto + dfc-b:description: Basil Pesto dfc-b:hasType: dfc-pt:non-local-vegetable dfc-b:hasQuantity: "@type": dfc-b:QuantitativeValue