From f665c3978e874f918125bac6f3d3ef4d441a8a1b Mon Sep 17 00:00:00 2001 From: Steve Cresswell <229672+cressie176@users.noreply.github.com> Date: Fri, 12 Jul 2024 11:39:03 +0100 Subject: [PATCH] Use effective date not change set id when aggregating --- lib/partials/add-entity.hbs | 8 +++++++- test/dsl.test.js | 17 +++++++++++++++++ test/dsl/snippets/ADD_CHANGE_SET_4.yaml | 22 ++++++++++++++++++++++ test/dsl/snippets/ADD_CHANGE_SET_5.yaml | 16 ++++++++++++++++ 4 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 test/dsl/snippets/ADD_CHANGE_SET_4.yaml create mode 100644 test/dsl/snippets/ADD_CHANGE_SET_5.yaml diff --git a/lib/partials/add-entity.hbs b/lib/partials/add-entity.hbs index 0940b07..9192bee 100644 --- a/lib/partials/add-entity.hbs +++ b/lib/partials/add-entity.hbs @@ -31,19 +31,25 @@ CREATE FUNCTION {{identifier (aggregate name version)}}(p_change_set_id INTEGER) AS $$ BEGIN RETURN QUERY - WITH aggregated AS ( + WITH specified_change_set AS ( + SELECT * FROM fby_change_set cs + WHERE cs.id = p_change_set_id + ), aggregated AS ( SELECT DISTINCT ON ({{#identified_by}}{{identifier name}}{{#unless @last}},{{/unless}}{{/identified_by}}) f.action AS fby_action, {{#fields}}x.{{identifier name}}{{#unless @last}}, {{/unless}}{{/fields}} FROM + specified_change_set scs, fby_data_frame f INNER JOIN fby_entity e ON e.id = f.entity_id + INNER JOIN fby_change_set cs ON cs.id = f.change_set_id INNER JOIN {{identifier (table name version)}} x ON x.fby_frame_id = f.id WHERE e.name = {{literal name}} AND e.version = {{literal version}} AND f.change_set_id <= p_change_set_id + AND cs.effective <= scs.effective ORDER BY {{#identified_by}}x.{{identifier name}} ASC, {{/identified_by}} diff --git a/test/dsl.test.js b/test/dsl.test.js index d1572ea..e95e700 100644 --- a/test/dsl.test.js +++ b/test/dsl.test.js @@ -1683,6 +1683,8 @@ describe('DSL', () => { const ADD_CHANGE_SET_2 = loadYaml('ADD_CHANGE_SET_2'); const ADD_CHANGE_SET_3 = loadYaml('ADD_CHANGE_SET_3'); + const ADD_CHANGE_SET_4 = loadYaml('ADD_CHANGE_SET_4'); + const ADD_CHANGE_SET_5 = loadYaml('ADD_CHANGE_SET_5'); it('should aggregate data frames up to the specified change set', async (t) => { await filby.applyYaml(t.name, ADD_ENTITY, ADD_PROJECTION, ADD_CHANGE_SET_1, ADD_CHANGE_SET_2, ADD_CHANGE_SET_3); @@ -1705,6 +1707,21 @@ describe('DSL', () => { }); }); + it('should ignore lower change sets with a later effective date', async (t) => { + await filby.applyYaml(t.name, ADD_ENTITY, ADD_PROJECTION, ADD_CHANGE_SET_1, ADD_CHANGE_SET_2, ADD_CHANGE_SET_3, ADD_CHANGE_SET_4, ADD_CHANGE_SET_5); + + const projection = await filby.getProjection('VAT Rates', 1); + const changeLog = await filby.getChangeLog(projection); + + await filby.withTransaction(async (tx) => { + const { rows: aggregate1 } = await tx.query('SELECT * FROM get_vat_rate_v1_aggregate($1) ORDER BY rate DESC', [changeLog[4].id]); + eq(aggregate1.length, 3); + deq(aggregate1[0], { type: 'standard', rate: 0.16 }); + deq(aggregate1[1], { type: 'reduced', rate: 0.11 }); + deq(aggregate1[2], { type: 'zero', rate: 0 }); + }); + }); + it('should exclude aggregates where the most recent frame was a delete', async (t) => { await filby.applyYaml( t.name, diff --git a/test/dsl/snippets/ADD_CHANGE_SET_4.yaml b/test/dsl/snippets/ADD_CHANGE_SET_4.yaml new file mode 100644 index 0000000..df2dc3b --- /dev/null +++ b/test/dsl/snippets/ADD_CHANGE_SET_4.yaml @@ -0,0 +1,22 @@ +- operation: ADD_CHANGE_SET + description: 2025 VAT Rates Fix + effective: 2025-04-05T00:00:00.000Z + frames: + - entity: VAT Rate + version: 1 + action: POST + data: + - type: standard + rate: 0.25 + - entity: VAT Rate + version: 1 + action: POST + data: + - type: reduced + rate: 0.15 + - entity: VAT Rate + version: 1 + action: POST + data: + - type: zero + rate: 0.05 diff --git a/test/dsl/snippets/ADD_CHANGE_SET_5.yaml b/test/dsl/snippets/ADD_CHANGE_SET_5.yaml new file mode 100644 index 0000000..742bcc3 --- /dev/null +++ b/test/dsl/snippets/ADD_CHANGE_SET_5.yaml @@ -0,0 +1,16 @@ +- operation: ADD_CHANGE_SET + description: 2022 VAT Rates Fix + effective: 2022-04-05T00:00:00.000Z + frames: + - entity: VAT Rate + version: 1 + action: POST + data: + - type: standard + rate: 0.16 + - entity: VAT Rate + version: 1 + action: POST + data: + - type: reduced + rate: 0.11