diff --git a/spec/domain/etl/ga/views_and_navigation_processor_spec.rb b/spec/domain/etl/ga/views_and_navigation_processor_spec.rb index 831a3c9da..c7c8331b3 100644 --- a/spec/domain/etl/ga/views_and_navigation_processor_spec.rb +++ b/spec/domain/etl/ga/views_and_navigation_processor_spec.rb @@ -15,9 +15,7 @@ before { allow(Etl::GA::ViewsAndNavigationService).to receive(:find_in_batches).and_yield(ga_response) } it "update the facts with the GA metrics" do - fact1 = create :metric, - edition: edition1, - date: "2018-02-20" + fact1 = create :metric, edition: edition1, date: "2018-02-20" fact2 = create :metric, edition: edition2, date: "2018-02-20" described_class.process(date:) @@ -68,6 +66,58 @@ end end + context "When the GA path contains the gov.uk prefix" do + before { allow(Etl::GA::ViewsAndNavigationService).to receive(:find_in_batches).and_yield(ga_response_with_govuk_prefix) } + + context "when an event does not already exist with the same page_path" do + it "updates the fact with the GA metrics" do + fact1 = create :metric, edition: edition1, date: "2018-02-20" + + described_class.process(date:) + + expect(fact1.reload).to have_attributes(pviews: 1, upviews: 1) + end + end + + context "when an event already exists with the same page_path" do + before do + create(:ga_event, :with_views, date: "2018-02-20", page_path: "/path1") + end + + it "updates the fact with the aggregated GA metrics" do + fact1 = create :metric, edition: edition1, date: "2018-02-20" + + described_class.process(date:) + + expect(fact1.reload).to have_attributes(pviews: 11, upviews: 6) + end + + it "does not update metrics for other days" do + fact1 = create :metric, edition: edition1, date: "2018-02-20", pviews: 20, upviews: 10 + + day_before = date - 1 + described_class.process(date: day_before) + + expect(fact1.reload).to have_attributes(pviews: 20, upviews: 10) + end + + it "does not update metrics for other items" do + edition = create :edition, base_path: "/non-matching-path", date: "2018-02-20" + fact = create :metric, edition:, date: "2018-02-20", pviews: 99, upviews: 90 + + described_class.process(date:) + + expect(fact.reload).to have_attributes(pviews: 99, upviews: 90) + end + + it "deletes events after updating facts metrics" do + described_class.process(date:) + + expect(Events::GA.count).to eq(0) + end + end + end + it_behaves_like "traps and logs errors in process", Etl::GA::ViewsAndNavigationService, :find_in_batches private