Skip to content

Commit 4fa0ec2

Browse files
chrislofloehopper
andcommitted
WIP: add tests for logging
Co-authored-by: James Mead <james.mead@gofreerange.com>
1 parent 61cc2b9 commit 4fa0ec2

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

spec/models/immunisation_import_spec.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@
5151
create(:gias_school, urn: "100000")
5252
end
5353

54+
around do |example|
55+
appender = SemanticLogger.add_appender(io: semantic_logger_io, formatter: :json)
56+
example.run
57+
ensure
58+
SemanticLogger.remove_appender(appender)
59+
end
60+
61+
let(:semantic_logger_io) { StringIO.new }
5462
let(:programmes) { [Programme.flu] }
5563
let(:team) do
5664
if type == "national_reporting"
@@ -77,6 +85,27 @@
7785

7886
it_behaves_like "a CSVImportable model"
7987

88+
describe ".create" do
89+
it "a tagged message containing the started_at time of the import" do
90+
import = immunisation_import
91+
92+
SemanticLogger.flush
93+
semantic_logger_io.rewind
94+
95+
log_lines = semantic_logger_io.read.split("\n")
96+
tagged_lines = log_lines.map {|l| JSON.parse(l) }.select do |j|
97+
j['named_tags'] == { 'import_class' => 'ImmunisationImport', 'import_id' => import.id }
98+
end
99+
100+
expect(tagged_lines.size).to eq(1)
101+
expect(tagged_lines.first).to include('message' => 'started')
102+
103+
payload = tagged_lines.first['payload']
104+
started_at = Time.zone.parse(payload['started_at'])
105+
expect(started_at.to_i).to eq(import.created_at.to_i)
106+
end
107+
end
108+
80109
describe "validations" do
81110
subject { unsaved_import }
82111

0 commit comments

Comments
 (0)