Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/db/src/competition-sql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ function readScript(dbPath: string, path: string): void {
const FIXTURE = `
INSERT INTO authorities (id, name, bulstat, type_group) VALUES
('auth:A', 'Институция А', '100000001', 'община'),
('auth:B', 'Институция Б', '100000002', 'община');
('auth:B', 'Институция Б', '100000019', 'община');
INSERT INTO bidders (id, name, bulstat, eik_normalized, eik_valid, kind) VALUES
('eik:X', 'Фирма Х', '200000001', '200000001', 1, 'company'),
('eik:Y', 'Фирма У', '200000002', '200000002', 1, 'company');
('eik:X', 'Фирма Х', '200000002', '200000002', 1, 'company'),
('eik:Y', 'Фирма У', '200000015', '200000015', 1, 'company');
INSERT INTO tenders (id, source_id, title, authority_id, cpv_code, procedure_type, status) VALUES
('t:A', 'UNP-A', 'Поръчка А', 'auth:A', '45', 'открита процедура', 'awarded'),
('t:B', 'UNP-B', 'Поръчка Б', 'auth:B', '45', 'открита процедура', 'awarded');
Expand Down
33 changes: 33 additions & 0 deletions packages/db/src/eik-checksum-consistency.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/// <reference types="node" />
import { readFileSync } from 'node:fs';
import { dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { describe, expect, it } from 'vitest';

const root = resolve(dirname(fileURLToPath(import.meta.url)), '../../..');

function checksumBlocks(path: string): string[] {
const sql = readFileSync(path, 'utf8');
const pattern =
/^[ \t]*CASE\r?\n(?=[ \t]+WHEN eik_clean IS NULL\b[^\r\n]*LENGTH\(eik_clean\) NOT IN \(9, 13\) THEN 0$)[\s\S]*?^[ \t]*END AS eik_valid$/gm;

return [...sql.matchAll(pattern)].map(([block]) => {
const lines = block.split(/\r?\n/);
const indentation = Math.min(
...lines.filter((line) => line.trim()).map((line) => line.match(/^[ \t]*/)?.[0].length ?? 0),
);
return lines.map((line) => line.slice(indentation)).join('\n');
});
}

describe('ЕИК checksum copies stay consistent', () => {
it('keeps every checksum CASE block textually identical', () => {
const normalizeBlocks = checksumBlocks(resolve(root, 'scripts/normalize-raw.sql'));
const refreshBlocks = checksumBlocks(resolve(root, 'scripts/refresh-slice.sql'));
const blocks = [...normalizeBlocks, ...refreshBlocks];

expect(normalizeBlocks).toHaveLength(3);
expect(refreshBlocks).toHaveLength(4);
expect(new Set(blocks)).toEqual(new Set([blocks[0]]));
});
});
10 changes: 5 additions & 5 deletions packages/db/src/queries/companies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import type { CompanyTotalsRow } from './rows';

const filteredRows: (CompanyTotalsRow & { sort_value: number })[] = [
{
bidder_id: 'eik:111111111',
bidder_id: 'eik:111111113',
name: 'Филтрирана фирма',
kind: 'company',
ownership_kind: null,
eik: '111111111',
eik: '111111113',
eik_valid: 1,
settlement: 'София',
won_eur: 1000,
Expand All @@ -25,11 +25,11 @@ const filteredRows: (CompanyTotalsRow & { sort_value: number })[] = [
const unfilteredRows: (CompanyTotalsRow & { sort_value: number })[] = [
...filteredRows,
{
bidder_id: 'eik:999999999',
bidder_id: 'eik:999999995',
name: 'Нефилтрирана фирма',
kind: 'company',
ownership_kind: null,
eik: '999999999',
eik: '999999995',
eik_valid: 1,
settlement: 'Пловдив',
won_eur: 900,
Expand Down Expand Up @@ -93,7 +93,7 @@ describe('streamCompaniesCsv', () => {
.map((line) => line.split(',')[0]);

expect(csvEiks).toEqual(list.items.map((item) => item.eik));
expect(csvEiks).toEqual(['111111111']);
expect(csvEiks).toEqual(['111111113']);
});

it('exports fewer rows for a sector filter than for the unfiltered corpus', async () => {
Expand Down
10 changes: 5 additions & 5 deletions packages/db/src/queries/contracts-filter-sql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ const FIXTURE = `
INSERT INTO authorities (id, name, bulstat, type_group) VALUES
('auth:100000001', 'Институция А', '100000001', 'община');
INSERT INTO bidders (id, name, bulstat, eik_normalized, eik_valid, kind) VALUES
('eik:200000001', 'Фирма Х', '200000001', '200000001', 1, 'company');
('eik:200000002', 'Фирма Х', '200000002', '200000002', 1, 'company');
INSERT INTO tenders (id, source_id, title, authority_id, cpv_code, procedure_type, status) VALUES
('t:A', 'UNP-A', 'Поръчка А', 'auth:100000001', '45000000', 'открита процедура', 'awarded');
INSERT INTO contracts (id, tender_id, bidder_id, amount, currency, signed_at, bids_received, value_flag, amount_eur) VALUES
('c:NULL', 't:A', 'eik:200000001', 100, 'EUR', '2024-01-01', NULL, 'ok', 100),
('c:ZERO', 't:A', 'eik:200000001', 200, 'EUR', '2024-01-02', 0, 'ok', 200),
('c:ONE', 't:A', 'eik:200000001', 300, 'EUR', '2024-01-03', 1, 'ok', 300),
('c:TWO', 't:A', 'eik:200000001', 400, 'EUR', '2024-01-04', 2, 'ok', 400);
('c:NULL', 't:A', 'eik:200000002', 100, 'EUR', '2024-01-01', NULL, 'ok', 100),
('c:ZERO', 't:A', 'eik:200000002', 200, 'EUR', '2024-01-02', 0, 'ok', 200),
('c:ONE', 't:A', 'eik:200000002', 300, 'EUR', '2024-01-03', 1, 'ok', 300),
('c:TWO', 't:A', 'eik:200000002', 400, 'EUR', '2024-01-04', 2, 'ok', 400);
`;

/** Minimal D1Database facade over node:sqlite — enough for the query layer's prepare/bind/all/first. */
Expand Down
4 changes: 2 additions & 2 deletions packages/db/src/queries/contracts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ const contractRow = {
unp: 'UNP-1',
cpv_code: '45000000',
eu_funded: 0,
authority_id: 'auth:123456789',
authority_id: 'auth:123456786',
authority_name: 'Authority',
bidder_id: 'eik:111111111',
bidder_id: 'eik:111111113',
bidder_name: 'Bidder',
bidder_kind: 'company' as const,
procedure_type: 'Открита процедура',
Expand Down
10 changes: 5 additions & 5 deletions packages/db/src/queries/details.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ const baseContractRow = {
tender_fx_rate: null as number | null,
start_date: null,
end_date: null,
authority_id: 'auth:123456789',
authority_id: 'auth:123456786',
authority_name: 'Authority',
authority_type_group: 'ministry',
authority_settlement: 'Sofia',
bidder_id: 'eik:111111111',
bidder_id: 'eik:111111113',
bidder_name: 'Bidder',
bidder_kind: 'company' as const,
bidder_eik: '111111111',
bidder_eik: '111111113',
bidder_settlement: 'Sofia',
};

Expand Down Expand Up @@ -102,7 +102,7 @@ describe('getContract', () => {
estimated_fx_rate: null,
bidder_name: 'Bidder',
bidder_kind: 'company',
bidder_id: 'eik:111111111',
bidder_id: 'eik:111111113',
},
{
lot_id: 'lot:UNP-1:2',
Expand Down Expand Up @@ -147,7 +147,7 @@ describe('getContract', () => {
estimated_fx_rate: 0.9,
bidder_name: 'Bidder',
bidder_kind: 'company',
bidder_id: 'eik:111111111',
bidder_id: 'eik:111111113',
},
{
lot_id: 'lot:UNP-1:2',
Expand Down
2 changes: 1 addition & 1 deletion packages/db/src/queries/identity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('company slug', () => {
expect(bidderIdFromSlug(slug)).toBe(id);
});
it('round-trips a 13-digit ЕИК', () => {
expect(bidderIdFromSlug(companySlug('eik:1234567890123'))).toBe('eik:1234567890123');
expect(bidderIdFromSlug(companySlug('eik:8316417910124'))).toBe('eik:8316417910124');
});
});

Expand Down
Loading