-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from contentstack/test/CS-44748-import
test: Variant entries import unit test cases
- Loading branch information
Showing
16 changed files
with
355 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
200 changes: 200 additions & 0 deletions
200
packages/contentstack-variants/test/unit/import/variant-entries.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,200 @@ | ||
import { join } from 'path'; | ||
import { expect } from '@oclif/test'; | ||
import cloneDeep from 'lodash/cloneDeep'; | ||
import { fancy } from '@contentstack/cli-dev-dependencies'; | ||
|
||
import importConf from '../mock/import-config.json'; | ||
import ContentType from '../mock/contents/content_types/CT-1.json'; | ||
import { Import, ImportConfig, VariantHttpClient } from '../../../src'; | ||
import variantEntryData from '../mock/contents/mapper/entries/data-for-variant-entry.json'; | ||
import variantEntries from '../mock/contents/entries/CT-1/en-us/variants/E-1/9b0da6xd7et72y-6gv7he23.json'; | ||
|
||
describe('Variant Entries Import', () => { | ||
let config: ImportConfig; | ||
|
||
const test = fancy.stdout({ print: process.env.PRINT === 'true' || false }); | ||
|
||
beforeEach(() => { | ||
config = cloneDeep(importConf) as unknown as ImportConfig; | ||
}); | ||
|
||
describe('import method', () => { | ||
test | ||
.stub(Import.VariantEntries.prototype, 'importVariantEntries', async () => {}) | ||
.spy(Import.VariantEntries.prototype, 'importVariantEntries') | ||
.it('should call import variant entry method (API call)', async ({ spy }) => { | ||
let entryVariantInstace = new Import.VariantEntries(config); | ||
await entryVariantInstace.import(); | ||
|
||
expect(spy.importVariantEntries.called).to.be.true; | ||
expect(spy.importVariantEntries.calledWith(variantEntryData[0])).to.be.true; | ||
}); | ||
|
||
test | ||
.stub(Import.VariantEntries.prototype, 'importVariantEntries', async () => {}) | ||
.spy(Import.VariantEntries.prototype, 'importVariantEntries') | ||
.it('should return with entry not found message', async (ctx) => { | ||
config.backupDir = './'; | ||
let entryVariantInstace = new Import.VariantEntries(config); | ||
await entryVariantInstace.import(); | ||
|
||
expect(ctx.stdout).to.be.includes(entryVariantInstace.messages.IMPORT_ENTRY_NOT_FOUND); | ||
}); | ||
|
||
test | ||
.stub(Import.VariantEntries.prototype, 'importVariantEntries', async () => {}) | ||
.spy(Import.VariantEntries.prototype, 'importVariantEntries') | ||
.it('should return with variant UID mapper file not found message', async (ctx) => { | ||
config.modules.personalization.dirName = 'wrong-dir'; | ||
let entryVariantInstace = new Import.VariantEntries(config); | ||
await entryVariantInstace.import(); | ||
|
||
expect(ctx.stdout).to.be.includes(entryVariantInstace.messages.EMPTY_VARIANT_UID_DATA); | ||
}); | ||
|
||
test | ||
.stub(Import.VariantEntries.prototype, 'importVariantEntries', async () => {}) | ||
.spy(Import.VariantEntries.prototype, 'importVariantEntries') | ||
.it('should return with entry not found message if empty content found on file', async (ctx) => { | ||
let entryVariantInstace = new Import.VariantEntries(config); | ||
entryVariantInstace.entriesMapperPath = join(entryVariantInstace.entriesMapperPath, 'empty-data'); | ||
await entryVariantInstace.import(); | ||
|
||
expect(ctx.stdout).to.be.includes(entryVariantInstace.messages.IMPORT_ENTRY_NOT_FOUND); | ||
}); | ||
|
||
test | ||
.stub(Import.VariantEntries.prototype, 'importVariantEntries', async () => {}) | ||
.spy(Import.VariantEntries.prototype, 'importVariantEntries') | ||
.it('should check taxonomies folder existence', async (ctx) => { | ||
config.modules.taxonomies.dirName = 'wrong-dir'; | ||
let entryVariantInstace = new Import.VariantEntries(config); | ||
await entryVariantInstace.import(); | ||
|
||
expect(entryVariantInstace.taxonomies).to.contain({}); | ||
}); | ||
}); | ||
|
||
describe('importVariantEntries method', () => { | ||
test | ||
.stub(Import.VariantEntries.prototype, 'handleCuncurrency', async () => {}) | ||
.spy(Import.VariantEntries.prototype, 'handleCuncurrency') | ||
.it('should call handle Cuncurrency method to manage import batch', async ({ spy }) => { | ||
let entryVariantInstace = new Import.VariantEntries(config); | ||
await entryVariantInstace.importVariantEntries(variantEntryData[0]); | ||
|
||
expect(spy.handleCuncurrency.called).to.be.true; | ||
expect(spy.handleCuncurrency.calledWith(ContentType, variantEntries, variantEntryData[0])).to.be.true; | ||
}); | ||
|
||
test | ||
.stub(Import.VariantEntries.prototype, 'handleCuncurrency', async () => { | ||
throw new Error('Dummy error'); | ||
}) | ||
.spy(Import.VariantEntries.prototype, 'handleCuncurrency') | ||
.it('should catch and log errors on catch block', async (ctx) => { | ||
let entryVariantInstace = new Import.VariantEntries(config); | ||
await entryVariantInstace.importVariantEntries(variantEntryData[0]); | ||
|
||
expect(ctx.stdout).to.be.includes('Dummy error'); | ||
}); | ||
}); | ||
|
||
describe('handleCuncurrency method', () => { | ||
test | ||
.stub(VariantHttpClient.prototype, 'createVariantEntry', async () => {}) | ||
.stub(Import.VariantEntries.prototype, 'handleVariantEntryRelationalData', () => variantEntries[0]) | ||
.spy(VariantHttpClient.prototype, 'createVariantEntry') | ||
.spy(Import.VariantEntries.prototype, 'handleVariantEntryRelationalData') | ||
.it('should call handle Cuncurrency method to manage import batch', async ({ spy }) => { | ||
const variantEntry = variantEntries[0]; | ||
const { content_type, entry_uid, locale } = variantEntryData[0]; | ||
let entryVariantInstace = new Import.VariantEntries(config); | ||
entryVariantInstace.variantIdList = { 'VARIANT-ID-1': 'VARIANT-ID-2' }; | ||
await entryVariantInstace.handleCuncurrency(ContentType, variantEntries, variantEntryData[0]); | ||
|
||
expect(spy.createVariantEntry.called).to.be.true; | ||
expect(spy.handleVariantEntryRelationalData.called).to.be.true; | ||
expect(spy.handleVariantEntryRelationalData.calledWith(ContentType, variantEntry)).to.be.true; | ||
expect( | ||
spy.createVariantEntry.calledWith(variantEntry, { | ||
locale, | ||
entry_uid, | ||
variant_id: 'VARIANT-ID-2', | ||
content_type_uid: content_type, | ||
}), | ||
).to.be.true; | ||
}); | ||
|
||
test | ||
.stub(VariantHttpClient.prototype, 'createVariantEntry', async () => {}) | ||
.stub(Import.VariantEntries.prototype, 'handleVariantEntryRelationalData', () => variantEntries[0]) | ||
.spy(VariantHttpClient.prototype, 'createVariantEntry') | ||
.spy(Import.VariantEntries.prototype, 'handleVariantEntryRelationalData') | ||
.it('should return without any execution if empty batch found', async (ctx) => { | ||
let entryVariantInstace = new Import.VariantEntries(config); | ||
const result = await entryVariantInstace.handleCuncurrency(ContentType, [], variantEntryData[0]); | ||
|
||
expect(result).to.be.undefined; | ||
}); | ||
|
||
test | ||
.stub(VariantHttpClient.prototype, 'createVariantEntry', async () => {}) | ||
.stub(Import.VariantEntries.prototype, 'handleVariantEntryRelationalData', () => variantEntries[0]) | ||
.spy(VariantHttpClient.prototype, 'createVariantEntry') | ||
.spy(Import.VariantEntries.prototype, 'handleVariantEntryRelationalData') | ||
.it('should log error message if variant UID not found on the mapper file', async (ctx) => { | ||
let entryVariantInstace = new Import.VariantEntries(config); | ||
entryVariantInstace.config.modules.variantEntry.apiConcurrency = null as any; // NOTE Missing apiConcurrency value in config | ||
entryVariantInstace.variantIdList = { 'VARIANT-ID-2': 'VARIANT-ID-NEW-2' }; | ||
await entryVariantInstace.handleCuncurrency(ContentType, variantEntries, variantEntryData[0]); | ||
|
||
expect(ctx.stdout).to.be.includes(entryVariantInstace.messages.VARIANT_ID_NOT_FOUND); | ||
}); | ||
}); | ||
|
||
describe('handleVariantEntryRelationalData method', () => { | ||
test.it('should call handle Cuncurrency method to manage import batch', async () => { | ||
// NOTE passing helper methods along with config | ||
let conf = Object.assign(config, { | ||
helpers: { | ||
lookUpTerms: () => {}, | ||
lookupExtension: () => {}, | ||
lookupAssets: (entry: any) => entry, | ||
lookupEntries: (entry: any) => entry, | ||
restoreJsonRteEntryRefs: (entry: any) => entry, | ||
}, | ||
}); | ||
const variantEntry = variantEntries[0]; | ||
let entryVariantInstace = new Import.VariantEntries(conf); | ||
const entry = await entryVariantInstace.handleVariantEntryRelationalData(ContentType, variantEntry); | ||
|
||
expect(entry).to.contain(variantEntry); | ||
}); | ||
|
||
test.it('should skip calling lookupExtension if not available in helper', async () => { | ||
// NOTE passing helper methods along with config | ||
let conf = Object.assign(config, { | ||
helpers: { | ||
lookUpTerms: () => {}, | ||
lookupAssets: (entry: any) => entry, | ||
lookupEntries: (entry: any) => entry, | ||
restoreJsonRteEntryRefs: (entry: any) => entry, | ||
}, | ||
}); | ||
const variantEntry = variantEntries[0]; | ||
let entryVariantInstace = new Import.VariantEntries(conf); | ||
const entry = await entryVariantInstace.handleVariantEntryRelationalData(ContentType, variantEntry); | ||
|
||
expect(entry).to.contain(variantEntry); | ||
}); | ||
|
||
test.it('will skip calling lookup function if helper is not present in config', async () => { | ||
const variantEntry = variantEntries[0]; | ||
let entryVariantInstace = new Import.VariantEntries(config); | ||
const entry = await entryVariantInstace.handleVariantEntryRelationalData(ContentType, variantEntry); | ||
|
||
expect(entry).to.contain(variantEntry); | ||
}); | ||
}); | ||
}); |
7 changes: 7 additions & 0 deletions
7
packages/contentstack-variants/test/unit/mock/contents/content_types/CT-1.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"uid": "CT-1", | ||
"title": "CT-1", | ||
"description": "test CT", | ||
"schema": [], | ||
"options": {} | ||
} |
12 changes: 12 additions & 0 deletions
12
...ants/test/unit/mock/contents/entries/CT-1/en-us/variants/E-1/9b0da6xd7et72y-6gv7he23.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[{ | ||
"uid": "E-1", | ||
"locale": "en-us", | ||
"title": "Variant 1", | ||
"variant_id": "VARIANT-ID-1", | ||
"_version": 1, | ||
"_variant": { | ||
"uid": "UID-1", | ||
"_change_set": [], | ||
"_base_entry_version": 1 | ||
} | ||
}] |
3 changes: 3 additions & 0 deletions
3
.../contentstack-variants/test/unit/mock/contents/entries/CT-1/en-us/variants/E-1/index.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"1": "9b0da6xd7et72y-6gv7he23.json" | ||
} |
12 changes: 12 additions & 0 deletions
12
...ants/test/unit/mock/contents/entries/CT-1/en-us/variants/E-2/9b0da6xd7et72y-6gv7he23.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[{ | ||
"uid": "E-1", | ||
"locale": "en-us", | ||
"title": "Variant 1", | ||
"variant_id": "VARIANT-ID-1", | ||
"_version": 1, | ||
"_variant": { | ||
"uid": "UID-1", | ||
"_change_set": [], | ||
"_base_entry_version": 1 | ||
} | ||
}] |
3 changes: 3 additions & 0 deletions
3
.../contentstack-variants/test/unit/mock/contents/entries/CT-1/en-us/variants/E-2/index.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"1": "9b0da6xd7et72y-6gv7he23.json" | ||
} |
6 changes: 6 additions & 0 deletions
6
packages/contentstack-variants/test/unit/mock/contents/mapper/assets/uid-mapping.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"O-A-UID-1": "N-A-UID-1", | ||
"O-A-UID-2": "N-A-UID-2", | ||
"O-A-UID-3": "N-A-UID-3", | ||
"O-A-UID-4": "N-A-UID-4" | ||
} |
6 changes: 6 additions & 0 deletions
6
packages/contentstack-variants/test/unit/mock/contents/mapper/assets/url-mapping.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"o-ass-url-1.com": "n-ass-url-1.com", | ||
"o-ass-url-2.com": "n-ass-url-2.com", | ||
"o-ass-url-3.com": "n-ass-url-3.com", | ||
"o-ass-url-4.com": "n-ass-url-4.com" | ||
} |
6 changes: 6 additions & 0 deletions
6
.../contentstack-variants/test/unit/mock/contents/mapper/entries/data-for-variant-entry.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[ | ||
{ "content_type": "CT-1", "locale": "en-us", "entry_uid": "E-1" }, | ||
{ "content_type": "CT-2", "locale": "en-us", "entry_uid": "E-2" }, | ||
{ "content_type": "CT-3", "locale": "en-us", "entry_uid": "E-3" }, | ||
{ "content_type": "CT-4", "locale": "en-us", "entry_uid": "E-4" } | ||
] |
1 change: 1 addition & 0 deletions
1
...ck-variants/test/unit/mock/contents/mapper/entries/empty-data/data-for-variant-entry.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[] |
6 changes: 6 additions & 0 deletions
6
packages/contentstack-variants/test/unit/mock/contents/mapper/entries/uid-mapping.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"E-UID-1": "E-UID-1", | ||
"E-UID-2": "E-UID-2", | ||
"E-UID-3": "E-UID-3", | ||
"E-UID-4": "E-UID-4" | ||
} |
3 changes: 3 additions & 0 deletions
3
...es/contentstack-variants/test/unit/mock/contents/mapper/marketplace_apps/uid-mapping.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extension_uid": {} | ||
} |
5 changes: 5 additions & 0 deletions
5
...ants/test/unit/mock/contents/mapper/personalization/experiences/variants-uid-mapping.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"OLD-UID-1": "NEW-UID-1", | ||
"OLD-UID-2": "NEW-UID-2", | ||
"OLD-UID-3": "NEW-UID-3" | ||
} |
1 change: 1 addition & 0 deletions
1
packages/contentstack-variants/test/unit/mock/contents/mapper/taxonomies/terms/success.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
Oops, something went wrong.