Skip to content

Commit

Permalink
Use effective date not change set id when aggregating
Browse files Browse the repository at this point in the history
  • Loading branch information
cressie176 committed Jul 12, 2024
1 parent 109ff06 commit f665c39
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/partials/add-entity.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down
17 changes: 17 additions & 0 deletions test/dsl.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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,
Expand Down
22 changes: 22 additions & 0 deletions test/dsl/snippets/ADD_CHANGE_SET_4.yaml
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions test/dsl/snippets/ADD_CHANGE_SET_5.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit f665c39

Please sign in to comment.