@davoodmehraban89 / Factor-easy-2 Mirror · read-only / commits / 4231b1e
Fork 0

سامانه صدور فاکتور و حسابداری

Mirrored from https://github.com/davoodmehraban89/Factor-easy.git

Synced Dec 4, 08:00 UTC

test(phase1a): assert 1000 posted journal events and 1000 net-zero compensating reversals

davoodmehraban89 committed Oct 16, 2026 at 13:41 UTC 4231b1e12dd9126b707fb1cbafb1b1c31bf9eaf5
1 file changed +88
supabase/tests/phase1a_property_1000.sql new
+88
@@ -0,0 +1,88 @@
1 +-- 1000 deterministic synthetic double-entry events + 1000 inverse reversal events.
2 +-- Full Supabase CLI only, NEVER Production. All test rows ROLLBACK after assertions.
3 +\set ON_ERROR_STOP on
4 +BEGIN;
5 +INSERT INTO auth.users(id,instance_id,aud,role,email,email_confirmed_at,raw_user_meta_data)
6 +VALUES('33333333-3333-4333-8333-333333333333','00000000-0000-0000-0000-000000000000',
7 + 'authenticated','authenticated','phase1a-property@example.invalid',now(),'{}'::jsonb);
8 +-- Draft vouchers; finalization from trusted local superuser exercises deferred guard.
9 +WITH actor AS (
10 + SELECT organization_id FROM public.organization_members
11 + WHERE user_id='33333333-3333-4333-8333-333333333333' LIMIT 1
12 +)
13 +INSERT INTO public.records(organization_id,owner_id,collection,id,data)
14 +SELECT actor.organization_id,'33333333-3333-4333-8333-333333333333'::uuid,
15 + 'journalVouchers','PROP'||lpad(g::text,6,'0'),
16 + jsonb_build_object('companyId','PROP_COMP','fiscalYearId','PROP_FY',
17 + 'status','draft','number',g::text,'date','1405/07/24')
18 +FROM actor CROSS JOIN generate_series(1,1000) g;
19 +WITH actor AS (
20 + SELECT organization_id FROM public.organization_members
21 + WHERE user_id='33333333-3333-4333-8333-333333333333' LIMIT 1
22 +)
23 +INSERT INTO public.records(organization_id,owner_id,collection,id,data)
24 +SELECT actor.organization_id,'33333333-3333-4333-8333-333333333333'::uuid,
25 + 'journalLines','PROPL'||lpad(g::text,6,'0')||s.side::text,
26 + jsonb_build_object('voucherId','PROP'||lpad(g::text,6,'0'),
27 + 'companyId','PROP_COMP','accountId',case when s.side=1 then 'A' else 'B' end,
28 + 'debit',case when s.side=1 then 1+abs(hashtext(g::text)::bigint)%9999999 else 0 end,
29 + 'credit',case when s.side=2 then 1+abs(hashtext(g::text)::bigint)%9999999 else 0 end)
30 +FROM actor CROSS JOIN generate_series(1,1000) g CROSS JOIN (values(1),(2)) s(side);
31 +UPDATE public.records
32 +SET data=jsonb_set(data,'{status}','"posted"')
33 +WHERE collection='journalVouchers' AND id LIKE 'PROP%' AND owner_id='33333333-3333-4333-8333-333333333333';
34 +-- Compensating posted reversal transactions. Old originals remain immutable.
35 +WITH actor AS (
36 + SELECT organization_id FROM public.organization_members
37 + WHERE user_id='33333333-3333-4333-8333-333333333333' LIMIT 1
38 +)
39 +INSERT INTO public.records(organization_id,owner_id,collection,id,data)
40 +SELECT actor.organization_id,'33333333-3333-4333-8333-333333333333'::uuid,
41 + 'journalVouchers','REVP'||lpad(g::text,6,'0'),
42 + jsonb_build_object('companyId','PROP_COMP','fiscalYearId','PROP_FY',
43 + 'status','draft','voucherType','reversal','sourceType','reversal',
44 + 'sourceId','PROP'||lpad(g::text,6,'0'),'number','R'||g::text,'date','1405/07/24')
45 +FROM actor CROSS JOIN generate_series(1,1000) g;
46 +WITH actor AS (
47 + SELECT organization_id FROM public.organization_members
48 + WHERE user_id='33333333-3333-4333-8333-333333333333' LIMIT 1
49 +)
50 +INSERT INTO public.records(organization_id,owner_id,collection,id,data)
51 +SELECT actor.organization_id,'33333333-3333-4333-8333-333333333333'::uuid,
52 + 'journalLines','REVL'||lpad(g::text,6,'0')||s.side::text,
53 + jsonb_build_object('voucherId','REVP'||lpad(g::text,6,'0'),
54 + 'companyId','PROP_COMP','accountId',case when s.side=1 then 'B' else 'A' end,
55 + 'debit',case when s.side=1 then 1+abs(hashtext(g::text)::bigint)%9999999 else 0 end,
56 + 'credit',case when s.side=2 then 1+abs(hashtext(g::text)::bigint)%9999999 else 0 end)
57 +FROM actor CROSS JOIN generate_series(1,1000) g CROSS JOIN (values(1),(2)) s(side);
58 +UPDATE public.records
59 +SET data=jsonb_set(data,'{status}','"posted"')
60 +WHERE collection='journalVouchers' AND id LIKE 'REVP%' AND owner_id='33333333-3333-4333-8333-333333333333';
61 +-- Force all DEFERRED constraints to run before assertions/rollback.
62 +SET CONSTRAINTS ALL IMMEDIATE;
63 +DO $assert$
64 +DECLARE vouchers integer; lines integer; unmatched integer; nonzero_accounts integer;
65 +BEGIN
66 + SELECT count(*) INTO vouchers FROM public.records
67 + WHERE collection='journalVouchers' AND owner_id='33333333-3333-4333-8333-333333333333' AND data->>'status'='posted';
68 + SELECT count(*) INTO lines FROM public.records
69 + WHERE collection='journalLines' AND owner_id='33333333-3333-4333-8333-333333333333';
70 + WITH sums AS (
71 + SELECT data->>'accountId' account, sum((data->>'debit')::numeric-(data->>'credit')::numeric) net
72 + FROM public.records WHERE collection='journalLines'
73 + AND owner_id='33333333-3333-4333-8333-333333333333' GROUP BY 1
74 + ) SELECT count(*) INTO nonzero_accounts FROM sums WHERE net<>0;
75 + WITH line_totals AS (
76 + SELECT data->>'voucherId' vid,count(*) line_count,
77 + sum((data->>'debit')::numeric) debit,sum((data->>'credit')::numeric) credit
78 + FROM public.records WHERE collection='journalLines'
79 + AND owner_id='33333333-3333-4333-8333-333333333333' GROUP BY 1
80 + ) SELECT count(*) INTO unmatched FROM line_totals WHERE debit<>credit OR line_count<>2;
81 + IF vouchers<>2000 OR lines<>4000 OR unmatched<>0 OR nonzero_accounts<>0 THEN
82 + RAISE EXCEPTION 'Property failure: vouchers %, lines %, invalid %, account net nonzero %',
83 + vouchers,lines,unmatched,nonzero_accounts;
84 + END IF;
85 + RAISE NOTICE 'FINORA_1A_PROPERTY_PASS 1000 randomized postings, 1000 compensating reversals, 2000 posted vouchers, 4000 lines, account net zero';
86 +END
87 +$assert$;
88 +ROLLBACK;