Skip to content

Commit

Permalink
feat: function to mock the time inside a transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
pbastia committed Dec 9, 2020
1 parent 4a8beb4 commit c711fb6
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 25 deletions.
15 changes: 15 additions & 0 deletions mocks_schema/deploy/set_mocked_time_in_transaction.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-- Deploy mocks:set_mocked_time_in_transaction to pg
-- requires: mock_now_method

BEGIN;

create or replace function mocks.set_mocked_time_in_transaction(mocked_timestamp timestamptz)
returns void as
$function$

set search_path=mocks,"$user",public;
set_config('mocks.mocked_timestamp', mocked_timestamp, true);

$function$ language plpgsql volatile;

COMMIT;
7 changes: 7 additions & 0 deletions mocks_schema/revert/set_mocked_time_in_transaction.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Revert mocks:set_mocked_time_in_transaction from pg

BEGIN;

drop function mocks.set_mocked_time_in_transaction;

COMMIT;
1 change: 1 addition & 0 deletions mocks_schema/sqitch.plan
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@

schema_mocks 2020-10-28T17:42:06Z Pierre Bastianelli <pierre.bastianelli@gov.bc.ca> # Creating schema for mocked functions
mock_now_method [schema_mocks] 2020-10-28T17:59:38Z Pierre Bastianelli <pierre.bastianelli@gov.bc.ca> # Mock method to override the default now() behaviour
set_mocked_time_in_transaction [mock_now_method] 2020-12-04T16:51:59Z Pierre Bastianelli <pierre.bastianelli@gov.bc.ca> # Function to set the proper configuration to use the mocked now() method. Only impacts the current transaction.
7 changes: 7 additions & 0 deletions mocks_schema/verify/set_mocked_time_in_transaction.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Verify mocks:set_mocked_time_in_transaction on pg

BEGIN;

select pg_get_functiondef('mocks.set_mocked_time_in_transaction(timestamptz)'::regprocedure);

ROLLBACK;
17 changes: 4 additions & 13 deletions schema/data/dev/application-2018.sql
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
begin;

-- Set the current reporting year to 2018
create or replace function ggircs_portal.current_timestamp() returns timestamptz as
$$
select application_open_time
from ggircs_portal.reporting_year
where reporting_year = 2018
$$ language sql;
-- Set the current reporting year to 2018, application open
select mocks.set_mocked_time_in_transaction('2019-04-01 14:49:54.191757-07'::timestamptz)

alter table ggircs_portal.application disable trigger _send_draft_application_email;
alter table ggircs_portal.application_revision_status disable trigger _status_change_email;
Expand Down Expand Up @@ -44,12 +39,8 @@ $$ language sql;
alter table ggircs_portal.application enable trigger _send_draft_application_email;
alter table ggircs_portal.certification_url enable trigger _certification_request_email;
alter table ggircs_portal.certification_url enable trigger _signed_by_certifier_email;

-- Set the current reporting year to 2019
create or replace function ggircs_portal.current_timestamp() returns timestamptz as
$$
select application_open_time
from ggircs_portal.reporting_year
where reporting_year = 2019
$$ language sql;
select mocks.set_mocked_time_in_transaction('2020-07-03 00:00:00.000000-07'::timestamptz)

commit;
12 changes: 0 additions & 12 deletions schema/data/dev/mock_current_timestamp.sql

This file was deleted.

0 comments on commit c711fb6

Please sign in to comment.