Skip to content

Commit

Permalink
Merge pull request #2631 from alphagov/output-model-id-in-sync-checks
Browse files Browse the repository at this point in the history
Output whitehall model id for sync check failures
  • Loading branch information
tvararu committed Jun 27, 2016
2 parents 3d9d3d7 + 87909f4 commit 667df9e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
25 changes: 19 additions & 6 deletions lib/data_hygiene/publishing_api_sync_check.rb
Expand Up @@ -10,6 +10,9 @@
# pasc.add_expectation { |json, model| json.fetch("format") == "statistics_announcement" }
# pasc.perform
#
# Pass in a file path as the first argument to output CSV data of any failures to
# that file. For example: rails runner script/publishing-api-sync-checks/my-sync-checks.rb failures.csv
#
# The disadvantages of running against content-store are that you can't inspect
# things like rendering_app.
#
Expand Down Expand Up @@ -41,18 +44,19 @@ def to_row
class Failure
attr_reader :base_path, :failed_expectations

def initialize(base_path:, failed_expectations:, content_store:)
def initialize(record_id:, base_path:, failed_expectations:, content_store:)
@record_id = record_id
@base_path = base_path
@failed_expectations = failed_expectations
@content_store = content_store
end

def to_s
"Failed path: #{@base_path} in #{@content_store.titleize}, failed expectations: #{@failed_expectations.join(', ')}"
"Model id #{@record_id} failed path: #{@base_path} in #{@content_store.titleize}, failed expectations: #{@failed_expectations.join(', ')}"
end

def to_row
[@base_path, "failure", @content_store] + @failed_expectations
[@record_id, @base_path, "failure", @content_store] + @failed_expectations
end

def ==(other)
Expand All @@ -72,8 +76,10 @@ def initialize(scope)
@successes = []
@failures = []

if ARGV[0].present? && !Rails.env.test?
csv_file = File.open(File.expand_path(ARGV[0]), "w")
csv_file_path = ARGV[0]

if csv_file_path.present? && !Rails.env.test?
csv_file = File.open(File.expand_path(csv_file_path), "w")
@csv = CSV.new(csv_file)
else
@csv = NullCSV.new
Expand Down Expand Up @@ -169,6 +175,7 @@ def compare_content(response, whitehall_model, content_store)
expectation[:block].call(json, whitehall_model)
rescue => e
Failure.new(
record_id: whitehall_model.id,
base_path: base_path,
failed_expectations: ["error: #{e.message}"],
content_store: content_store
Expand All @@ -179,10 +186,16 @@ def compare_content(response, whitehall_model, content_store)
Success.new(base_path: base_path)
else
failed_expectation_descriptions = failed_expectations.map { |expectation| expectation[:description] }
Failure.new(base_path: base_path, failed_expectations: failed_expectation_descriptions, content_store: content_store)
Failure.new(
record_id: whitehall_model.id,
base_path: base_path,
failed_expectations: failed_expectation_descriptions,
content_store: content_store
)
end
else
Failure.new(
record_id: whitehall_model.id,
base_path: base_path,
failed_expectations: ["unreachable: #{response.status_message}"],
content_store: content_store
Expand Down
1 change: 1 addition & 0 deletions test/unit/data_hygiene/publishing_api_sync_check_test.rb
Expand Up @@ -262,6 +262,7 @@ def check_case_study

def check_failure(base_path:, failed_expectations:, content_store: "content-store")
DataHygiene::PublishingApiSyncCheck::Failure.new(
record_id: 1,
base_path: base_path,
failed_expectations: failed_expectations,
content_store: content_store
Expand Down

0 comments on commit 667df9e

Please sign in to comment.