Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: Cope with nuls in tags #16

Merged
merged 3 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
35 changes: 31 additions & 4 deletions src/domain/services/import.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jest.mock('mongodb', () => {
collection: () => ({
find: () => ({
next: async () => {
return index++ <= products.length ? products[index - 1] : null;
return index++ <= mockedProducts.length ? mockedProducts[index - 1] : null;
},
close: jest.fn(),
}),
Expand All @@ -44,6 +44,12 @@ jest.mock('mongodb', () => {
};
});

let mockedProducts = [];
function mockMongoDB(productList) {
index = 0;
mockedProducts = productList;
}

describe('importFromMongo', () => {
it('should import a new product update existing products and delete missing products', async () => {
await createTestingModule([DomainModule], async (app) => {
Expand Down Expand Up @@ -71,7 +77,7 @@ describe('importFromMongo', () => {
await em.flush();

// WHEN:Doing a full import from MongoDB
index = 0;
mockMongoDB(products);
await importService.importFromMongo();

// THEN: New product is addeded, updated product is updated and other product is unchanged
Expand Down Expand Up @@ -113,13 +119,34 @@ describe('importFromMongo', () => {
await em.flush();
const importService = app.get(ImportService);

// WHEN:Doing an incremental import from MongoDB
index = 0;
// WHEN: Doing an incremental import from MongoDB
mockMongoDB(products);
await importService.importFromMongo('');

// THEN: Loaded tags is not updated
const loadedTags = await app.get(TagService).getLoadedTags();
expect(loadedTags).not.toContain('ingredients_tags');
});
});

it('should cope with nul charactes', async () => {
await createTestingModule([DomainModule], async (app) => {
// WHEN: Impoting data containing nul characters
mockMongoDB([{
// This one will be new
code: productIdNew,
last_modified_t: 1692032161,
ingredients_tags: ["test \u0000 test2 \u0000 end"],
}]);
await app.get(ImportService).importFromMongo();

// THEN: Product should be loaded with nuls stripped
const ingredientsNew = await app.get(EntityManager).find(ProductIngredientsTag, {
product: { code: productIdNew},
});

expect(ingredientsNew).toHaveLength(1);
expect(ingredientsNew[0].value).toBe("test test2 end");
});
});
});
15 changes: 13 additions & 2 deletions src/domain/services/import.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export class ImportService {
}

/** Populate a Product record from MongoDB document */
nulRegex = /\0/g;
async fixupProduct(
update: boolean,
updateId: string,
Expand All @@ -113,7 +114,17 @@ export class ImportService {
const product = await this.findOrNewProduct(update, data);
const dataToStore = {};
for (const key of this.tags) {
dataToStore[key] = data[key];
const tagData = data[key] as string[];
if (tagData) {
// Strip out any nul characters
for (const [index, value] of tagData.entries()) {
if (value.includes('\u0000')) {
this.logger.warn(`Product: ${data.code}. Nuls stripped from ${key} value: ${value}`);
tagData[index] = value.replace(this.nulRegex, '');
}
}
dataToStore[key] = tagData;
}
}
product.data = dataToStore;
product.name = data.product_name;
Expand All @@ -123,7 +134,7 @@ export class ImportService {
product.obsolete = obsolete;
const lastModified = new Date(data.last_modified_t * 1000);
if (isNaN(+lastModified)) {
this.logger.log(`Invalid last_modified_t: ${data.last_modified_t}.`);
this.logger.warn(`Product: ${data.code}. Invalid last_modified_t: ${data.last_modified_t}.`);
} else {
product.lastModified = lastModified;
}
Expand Down
Loading