Skip to content
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import i18next from 'i18next';

import { DATASET_ALLOW, DATASET_DENY } from '@app/ingestV2/source/builder/RecipeForm/bigqueryBeta';
import { FieldType } from '@app/ingestV2/source/builder/RecipeForm/common';
import { RECIPE_FIELDS } from '@app/ingestV2/source/builder/RecipeForm/constants';
import {
ODCS_AWS_ACCESS_KEY_ID,
ODCS_AWS_REGION,
ODCS_AWS_SECRET_ACCESS_KEY,
ODCS_BIND_TO_PHYSICAL_DATASET,
ODCS_DATA_CONTRACT_URN,
ODCS_DATA_CONTRACT_URN_ENTITY,
ODCS_DATA_CONTRACT_URN_ID,
ODCS_DATA_CONTRACT_URN_PREFIX,
ODCS_GCS_HMAC_KEY_ID,
ODCS_GCS_HMAC_KEY_SECRET,
ODCS_GIT_INFO_BRANCH,
Expand All @@ -24,6 +31,20 @@ import {
} from '@app/ingestV2/source/builder/RecipeForm/odcs';
import { ODCS } from '@app/ingestV2/source/conf/odcs/odcs';

const NAMESPACE = 'ingestion.sourceBuilder';

// Translations load before the module is imported, so changing one afterwards is
// what proves a label is read lazily from i18n rather than copied at import.
function withTranslation(key: string, value: string, run: () => void) {
const original = i18next.t(`${NAMESPACE}:${key}`);
i18next.addResourceBundle('en', NAMESPACE, { [key]: value }, true, true);
try {
run();
} finally {
i18next.addResourceBundle('en', NAMESPACE, { [key]: original }, true, true);
}
}

describe('ODCS recipe form fields', () => {
it('registers ODCS in RECIPE_FIELDS with path first and the source-location selector in the connection section', () => {
const spec = RECIPE_FIELDS[ODCS];
Expand Down Expand Up @@ -186,4 +207,76 @@ describe('ODCS recipe form fields', () => {
ODCS_LOCATION_GIT,
);
});

it('reads labels, option names and validator messages from the translations when used', async () => {
withTranslation('odcs.bindToPhysicalDataset.label', 'Translated bind', () => {
expect(ODCS_BIND_TO_PHYSICAL_DATASET.label).toBe('Translated bind');
});
withTranslation('odcs.sourceLocation.git', 'Translated git', () => {
expect((ODCS_SOURCE_LOCATION.options ?? []).map((o) => o.label)).toContain('Translated git');
});
const rule = (ODCS_BIND_TO_PHYSICAL_DATASET.rules ?? [])[0] as any;
let result: Promise<void> = Promise.resolve();
withTranslation('odcs.validation.bindNeedsIdUrns', 'Translated error', () => {
result = rule({ getFieldValue: () => undefined }).validator(null, true);
});
await expect(result).rejects.toThrow('Translated error');
});

it('offers the contract urn mode, prefix and physical binding as advanced options', () => {
const { advancedFields } = RECIPE_FIELDS[ODCS];
[ODCS_DATA_CONTRACT_URN, ODCS_DATA_CONTRACT_URN_PREFIX, ODCS_BIND_TO_PHYSICAL_DATASET].forEach((field) =>
expect(advancedFields).toContain(field),
);
expect(ODCS_DATA_CONTRACT_URN.fieldPath).toBe('source.config.data_contract_urn');
expect(ODCS_DATA_CONTRACT_URN_PREFIX.fieldPath).toBe('source.config.data_contract_urn_prefix');
expect(ODCS_BIND_TO_PHYSICAL_DATASET.fieldPath).toBe('source.config.bind_to_physical_dataset');
});

it('reads the contract urn mode in either case and defaults to the dataset-keyed urn', () => {
const read = (config: object) => ODCS_DATA_CONTRACT_URN.getValueFromRecipeOverride?.({ source: { config } });
expect(read({})).toBe(ODCS_DATA_CONTRACT_URN_ENTITY);
expect(read({ data_contract_urn: 'id' })).toBe(ODCS_DATA_CONTRACT_URN_ID);
expect((ODCS_DATA_CONTRACT_URN.options ?? []).map((o) => o.value)).toEqual([
ODCS_DATA_CONTRACT_URN_ENTITY,
ODCS_DATA_CONTRACT_URN_ID,
]);
});

it('drops the hidden urn prefix when the mode leaves id-keyed contracts', () => {
const recipe = {
source: { config: { data_contract_urn: 'ID', data_contract_urn_prefix: 'my-org/contracts' } },
};
const toEntity = ODCS_DATA_CONTRACT_URN.setValueOnRecipeOverride?.(recipe, ODCS_DATA_CONTRACT_URN_ENTITY);
expect(toEntity.source.config.data_contract_urn).toBe(ODCS_DATA_CONTRACT_URN_ENTITY);
expect(toEntity.source.config.data_contract_urn_prefix).toBeUndefined();

const stayId = ODCS_DATA_CONTRACT_URN.setValueOnRecipeOverride?.(recipe, ODCS_DATA_CONTRACT_URN_ID);
expect(stayId.source.config.data_contract_urn_prefix).toBe('my-org/contracts');
});

it('shows the urn prefix only for id-keyed contracts', () => {
const hidden = (mode: string) => ODCS_DATA_CONTRACT_URN_PREFIX.dynamicHidden?.({ data_contract_urn: mode });
expect(hidden(ODCS_DATA_CONTRACT_URN_ID)).toBe(false);
expect(hidden(ODCS_DATA_CONTRACT_URN_ENTITY)).toBe(true);
});

it('rejects physical binding without id-keyed contracts or with the logical parent link off', async () => {
const validate = (value: boolean, values: Record<string, unknown>) => {
const rule = (ODCS_BIND_TO_PHYSICAL_DATASET.rules ?? [])[0] as any;
return rule({ getFieldValue: (name: string) => values[name] }).validator(null, value);
};
await expect(validate(true, { data_contract_urn: ODCS_DATA_CONTRACT_URN_ID })).resolves.toBeUndefined();
await expect(validate(false, { data_contract_urn: ODCS_DATA_CONTRACT_URN_ENTITY })).resolves.toBeUndefined();
await expect(validate(true, { data_contract_urn: ODCS_DATA_CONTRACT_URN_ENTITY })).rejects.toThrow();
await expect(
validate(true, { data_contract_urn: ODCS_DATA_CONTRACT_URN_ID, emit_logical_parent: false }),
).rejects.toThrow();
});

it('names the field and location in the localized required-field message', async () => {
const rule = (ODCS_AWS_REGION.rules ?? [])[0] as any;
const check = rule({ getFieldValue: () => ODCS_LOCATION_S3 }).validator(null, '');
await expect(check).rejects.toThrow('AWS Region is required for an Amazon S3 source');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,9 @@ import {
ODCS_AWS_ACCESS_KEY_ID,
ODCS_AWS_REGION,
ODCS_AWS_SECRET_ACCESS_KEY,
ODCS_BIND_TO_PHYSICAL_DATASET,
ODCS_DATA_CONTRACT_URN,
ODCS_DATA_CONTRACT_URN_PREFIX,
Comment thread
acrylJonny marked this conversation as resolved.
Resolved
ODCS_EMIT_ASSERTIONS,
ODCS_EMIT_LOGICAL_PARENT,
ODCS_EMIT_SCHEMA_ASSERTION,
Expand Down Expand Up @@ -704,6 +707,9 @@ export const RECIPE_FIELDS: RecipeFields = {
ODCS_EMIT_ASSERTIONS,
ODCS_EMIT_SCHEMA_ASSERTION,
ODCS_EMIT_LOGICAL_PARENT,
ODCS_DATA_CONTRACT_URN,
ODCS_DATA_CONTRACT_URN_PREFIX,
ODCS_BIND_TO_PHYSICAL_DATASET,
ODCS_STRICT_VALIDATION,
STATEFUL_INGESTION_ENABLED,
],
Expand Down
Loading
Loading