Skip to content

Commit 4a989ce

Browse files
authored
Merge pull request #528 from alphagov/cm-854-simplify-use-of-schema-in-tests
CM-854: Simplify use of schema in tests
2 parents fa1faff + 4e80273 commit 4a989ce

27 files changed

Lines changed: 80 additions & 127 deletions

app/models/edition.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ class Edition < ApplicationRecord
1616
has_one :fact_check_outcome, dependent: :destroy
1717
has_many :domain_events
1818

19+
delegate :schema, to: :document
20+
1921
scope :current_versions, lambda {
2022
published
2123
}

app/models/edition/validates_details.rb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@ module Edition::ValidatesDetails
66
included do
77
validates_with DetailsValidator
88

9-
# Only used in tests, so we can easily add a schema to an edition, without
10-
# having to resort to mocks, which are difficult to setup/clean between tests
11-
attr_writer :schema
12-
139
def self.human_attribute_name(attr, options = {})
1410
if attr.starts_with?(DETAILS_PREFIX)
1511
key = attr.to_s.delete_prefix(DETAILS_PREFIX)
@@ -20,10 +16,6 @@ def self.human_attribute_name(attr, options = {})
2016
end
2117
end
2218

23-
def schema
24-
@schema ||= Schema.find_by_block_type(block_type)
25-
end
26-
2719
# When an error is raised about a field within the details hash
2820
# we have to prefix it. This overrides the default `read_attribute_for_validation`
2921
# method, and reads it from the details hash if the attribute name

app/validators/details_validator.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ class DetailsValidator < ActiveModel::Validator
55

66
def validate(edition)
77
@edition = edition
8+
9+
return false if block_type_blank?
10+
811
errors = validate_with_schema(edition)
912
errors.each do |e|
1013
if e["type"] == "required"
@@ -103,6 +106,10 @@ def translate_error(type, attribute, **args)
103106

104107
private
105108

109+
def block_type_blank?
110+
@edition.errors.attribute_names.include?(:"document.block_type")
111+
end
112+
106113
def generate_prefix(error)
107114
keys = error["data_pointer"].split("/")[1..]
108115
if error_is_for_embedded_object?(error)

engines/fact_check/spec/components/embedded_block_diff_component_spec.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@
1010

1111
let(:schema) { double(:schema) }
1212

13-
let(:document) { build(:document, schema:) }
13+
let(:document) { build(:document) }
1414

1515
let(:items) { CombinedEditionDetails.new(published_details: items_published, new_details: items_new).content }
1616

17+
before do
18+
allow(document).to receive(:schema).and_return(schema)
19+
end
20+
1721
describe "when there is no data to render" do
1822
before do
1923
render_inline(described_class.new(items:, object_type:, object_title:, document:))

engines/fact_check/spec/components/nested_blocks_with_summary_diff_component_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@
2727
let(:object_title) { "address-1" }
2828

2929
let(:schema) { build(:schema, body: SchemaHelpers::MINIMAL_CONTACT_SCHEMA_BODY) }
30-
let(:document) { create(:document, :contact, schema:) }
30+
let(:document) { create(:document, :contact) }
3131
let(:pub_edition) { build(:edition, :contact, :published, document:, details: published_details) }
3232
let(:edition) { build(:edition, :contact, :awaiting_factcheck, document:, details: new_details) }
3333
let(:block) { build(:content_block, edition:) }
3434
let(:component) { described_class.new(items:, object_type:, object_title:, block:) }
3535

3636
before do
37+
allow(document).to receive(:schema).and_return(schema)
3738
allow(document).to receive(:latest_published_edition).and_return(pub_edition)
3839
render_inline(component)
3940
end

engines/fact_check/spec/components/ungrouped_subschema_diff_component_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
} })
99
end
1010
let(:schema) { build(:schema) }
11-
let(:document) { build(:document, schema:) }
11+
let(:document) { build(:document) }
1212
let(:display_fields) { %w[amount] }
1313
let(:edition) do
1414
build(:edition,
@@ -30,6 +30,7 @@
3030
let(:component) { described_class.new(block:, subschema:) }
3131

3232
before do
33+
allow(document).to receive(:schema).and_return(schema)
3334
allow(schema).to receive(:subschema).with("content_block_block_type").and_return(subschema)
3435
allow(subschema).to receive(:block_display_fields).and_return(display_fields)
3536
end

engines/fact_check/spec/factories/content_block.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
transient do
66
schema { build(:schema) }
7-
document { build(:document, schema:) }
7+
document { build(:document) }
88
id { "123" }
99
end
1010

features/step_definitions/block_creation_steps.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def create_published_tax_edition
134134
:edition,
135135
:tax,
136136
document: tax_document,
137-
details: { description: "Some text" },
137+
details: { tax_type: "Tax", description: "Some text" },
138138
creator: @user,
139139
lead_organisation_id: organisation_id,
140140
title: "My tax",

spec/components/edition/show/embedded_object/subschema_item_component_spec.rb

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
RSpec.describe Edition::Show::EmbeddedObject::SubschemaItemComponent, type: :component do
2-
let(:schema) { double("schema: time period") }
3-
let(:subschema) { double("subschema: date_range") }
4-
52
let(:details) do
63
{
74
"date_range" => {
@@ -13,40 +10,32 @@
1310
}
1411
end
1512

16-
let(:edition) { build(:edition, :pension, details: details) }
17-
let(:object_type) { "date_range" }
18-
let(:schema_name) { "date_range" }
13+
let(:edition_with_sole_date_range_object) { build(:edition, :time_period, details: details) }
1914

2015
let(:component) do
2116
described_class.new(
22-
edition:,
23-
object_type:,
24-
schema_name:,
17+
edition: edition_with_sole_date_range_object,
18+
object_type: "date_range",
19+
schema_name: "date_range",
2520
)
2621
end
2722

2823
let(:metadata_response) { "METADATA" }
2924
let(:block_response) { "BLOCKS" }
3025

3126
before do
32-
allow(edition.document).to receive(:schema).and_return(schema)
33-
allow(schema).to receive(:subschema).with(object_type).and_return(subschema)
34-
allow(subschema).to receive(:id).and_return(object_type)
35-
allow(subschema).to receive(:block_display_fields).and_return(%w[start end])
36-
allow(subschema).to receive(:field_ordering_rule).with("start").and_return(1)
37-
allow(subschema).to receive(:field_ordering_rule).with("end").and_return(2)
38-
allow(subschema).to receive(:field_ordering_rule).with("title").and_return(3)
39-
4027
expect(component).to receive(:render).with(metadata_response).and_return(metadata_response)
4128
allow(component).to receive(:render).with(block_response).and_return(block_response)
4229
end
4330

4431
it "renders non-blank fields apart from 'block_display_fields' with the MetadataComponent" do
32+
expected_subschema = edition_with_sole_date_range_object.schema.subschema("date_range")
33+
4534
allow(Edition::Show::EmbeddedObjects::BlocksComponent).to receive(:new).and_return(block_response)
4635

4736
expect(Edition::Show::EmbeddedObjects::MetadataComponent).to receive(:new).with(
4837
items: { "other_field" => "Other value" },
49-
schema: subschema,
38+
schema: expected_subschema,
5039
).and_return(metadata_response)
5140

5241
render_inline component
@@ -63,7 +52,7 @@
6352
object_type: "date_range",
6453
object_title: nil,
6554
schema_name: "date_range",
66-
edition: edition,
55+
edition: edition_with_sole_date_range_object,
6756
).and_return(block_response)
6857

6958
render_inline component

spec/components/shared/embedded_object/summary_card_component_spec.rb

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
RSpec.describe Shared::EmbeddedObject::SummaryCardComponent, type: :component do
22
include Rails.application.routes.url_helpers
33

4-
context "when the embedded object has nested fields" do
4+
context "when the embedded object has nested fields, as within TimePeriod#date_range" do
55
let(:details) do
66
{
77
"date_range" => {
@@ -11,42 +11,12 @@
1111
}
1212
end
1313

14-
let(:subschema_id) { "date_range" }
15-
let(:parent_schema) { double("parent_schema", id: "content_block_time_period", block_type: "time_period") }
16-
let(:body) do
17-
{
18-
"type" => "object",
19-
"properties" => {
20-
"start" => {
21-
"type" => "object",
22-
"properties" => {
23-
"date" => { "type" => "string" },
24-
"time" => { "type" => "string" },
25-
},
26-
},
27-
"end" => {
28-
"type" => "object",
29-
"properties" => {
30-
"date" => { "type" => "string" },
31-
"time" => { "type" => "string" },
32-
},
33-
},
34-
},
35-
}
36-
end
37-
38-
let(:subschema) { Schema::EmbeddedSchema.new(subschema_id, body, parent_schema) }
39-
let(:schema) { double(:schema, block_type: "schema") }
40-
41-
let(:document) { build(:document, :pension, schema:) }
42-
let(:edition) { build_stubbed(:edition, :pension, details:, document:) }
14+
let(:document) { build(:document, :time_period) }
15+
let(:edition) { build_stubbed(:edition, :time_period, details:, document:) }
4316

4417
let(:component) { described_class.new(edition:, object_type: "date_range") }
4518

4619
before do
47-
allow(subschema).to receive(:block_display_fields).and_return(%w[start end])
48-
allow(schema).to receive(:subschema).and_return(subschema)
49-
5020
%w[
5121
date_range/start/date
5222
date_range/start/time

0 commit comments

Comments
 (0)