Skip to content

Commit

Permalink
#38: added a test for query fixKeys
Browse files Browse the repository at this point in the history
  • Loading branch information
phjulia committed Jul 5, 2023
1 parent a91ca12 commit 70c5f0f
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 9 deletions.
23 changes: 17 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,7 @@ class Mcdev {
Util.logger.error('fixKeys expects a single type to be deployed');
return null;
}
if (type.definition.keyField === this.definition.idField) {
if (MetadataTypeDefinitions[type].keyField === MetadataTypeDefinitions[type].idField) {
Util.logger.error(`Key cannot be updated for this type`);
return null;
}
Expand Down Expand Up @@ -926,11 +926,13 @@ class Mcdev {
* @param {string} bu name of BU
* @param {TYPE.SupportedMetadataTypes[]} [selectedTypesArr] limit execution to given metadata type
* @param {string[]} keyArr customerkey of the metadata
* @returns {Promise.<Object.<string,TYPE.MultiMetadataTypeMap>>} deployed metadata per BU (first key: bu name, second key: metadata type)
* @returns {Promise.<boolean>} true if all successfully, false if not
*/
static async _fixKeysBU(cred, bu, type, keyArr) {
const properties = await config.getProperties();
let counter_failed = 0;
let counter_success = 0;
let numItemsToUpdate = 0;
let deployed;
const toDeploy = [];
const buObject = await Cli.getCredentialObject(
properties,
Expand All @@ -956,11 +958,20 @@ class Mcdev {
const retrieved = await retriever.retrieve(type, keyArr, null, false);
Object.keys(retrieved).forEach((key) => {
for (const item of Object.values(retrieved[key][0])) {
if (item.name.length > MetadataTypeDefinitions[type].maxKeyLength) {
Util.logger.warn(
`Name of the item ${item.key} is too long for a key. Consider renaming your item. Key will be equal first ${MetadataTypeDefinitions[type].maxKeyLength} characters of the name`
);
item.name = item.name.slice(0, MetadataTypeDefinitions[type].maxKeyLength);
}
numItemsToUpdate = Object.values(retrieved[key][0]).length;
if (item != null) {
if (item.name != item.key) {
if (item.name != item.key && !MetadataTypeDefinitions[type].keyIsFixed) {
toDeploy.push(item.key);
counter_success++;
Util.logger.info(`Updating key for query ${item.key}`);
} else {
numItemsToUpdate--;
Util.logger.info(
` ☇ skipping query ${item.key} - key does not need to be updated`
);
Expand All @@ -972,12 +983,12 @@ class Mcdev {
});
Util.OPTIONS.changeKeyField = MetadataTypeDefinitions[type].nameField;
properties.directories.deploy = properties.directories.retrieve;
await Deployer._deployBU(cred, bu, properties, type, toDeploy, true);
deployed = await Deployer._deployBU(cred, bu, properties, type, toDeploy, true);
this._retrieveBU(cred, bu, MetadataTypeDefinitions[type].dependencies);
} catch (ex) {
Util.logger.errorStack(ex, 'mcdev.fixKeys failed');
}
return counter_failed === 0 ? true : false;
return numItemsToUpdate === Object.values(deployed).length ? true : false;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "updateMePlease",
"key": "testNew_query1",
"description": "created on deploy",
"targetKey": "testExisting_dataExtension",
"createdDate": "2022-04-26T15:21:16.453",
"modifiedDate": "2022-04-26T16:04:15.88",
"targetUpdateTypeName": "Overwrite",
"isFrozen": false,
"r__folder_Path": "Query"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SELECT
SubscriberKey as testField
FROM
_Subscribers
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"queryDefinitionId": "b28a1c2f-6656-478a-be51-4f12be4c9088",
"name": "updateMePlease",
"key": "updateMePlease",
"description": "bla bla",
"queryText": "SELECT\n SubscriberKey as testField\nFROM\n _Subscribers\nWHERE\n country IN ('test')\n",
"targetName": "testExisting_dataExtension",
"targetKey": "testExisting_dataExtension-WRONG",
"targetId": "21711373-72c1-ec11-b83b-48df37d1deb7",
"targetDescription": "",
"createdDate": "2022-04-26T15:21:16.453",
"modifiedDate": "2022-04-26T16:02:44.01",
"targetUpdateTypeId": 0,
"targetUpdateTypeName": "Overwrite",
"categoryId": 999,
"isFrozen": false
}
17 changes: 17 additions & 0 deletions test/resources/9999999/automation/v1/queries/get-response.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,23 @@
"targetUpdateTypeName": "Overwrite",
"categoryId": 999,
"isFrozen": false
},
{
"queryDefinitionId": "b28a1c2f-6656-478a-be51-4f12be4c9088",
"name": "updateMePlease",
"key": "testNew_query1",
"description": "bla bla",
"queryText": "SELECT\n SubscriberKey as testField\nFROM\n _Subscribers\nWHERE\n country IN ('test')\n",
"targetName": "testExisting_dataExtension",
"targetKey": "testExisting_dataExtension-WRONG",
"targetId": "21711373-72c1-ec11-b83b-48df37d1deb7",
"targetDescription": "",
"createdDate": "2022-04-26T15:21:16.453",
"modifiedDate": "2022-04-26T16:02:44.01",
"targetUpdateTypeId": 0,
"targetUpdateTypeName": "Overwrite",
"categoryId": 999,
"isFrozen": false
}
]
}
11 changes: 11 additions & 0 deletions test/resources/9999999/query/post2-expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "updateMePlease",
"key": "testNew_query",
"description": "created on deploy",
"targetKey": "testExisting_dataExtension",
"createdDate": "2022-04-26T15:21:16.453",
"modifiedDate": "2022-04-26T16:04:15.88",
"targetUpdateTypeName": "Overwrite",
"isFrozen": false,
"r__folder_Path": "Query"
}
4 changes: 4 additions & 0 deletions test/resources/9999999/query/post2-expected.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SELECT
SubscriberKey AS testField
FROM
_Subscribers
54 changes: 51 additions & 3 deletions test/type.query.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('type: query', () => {
const result = cache.getCache();
assert.equal(
result.query ? Object.keys(result.query).length : 0,
2,
3,
'only two queries expected'
);
// normal test
Expand Down Expand Up @@ -87,14 +87,18 @@ describe('type: query', () => {
});
it('Should create & upsert a query', async () => {
// WHEN
await handler.deploy('testInstance/testBU', ['query']);
await handler.deploy(
'testInstance/testBU',
['query'],
['testNew_query', 'testExisting_query']
);
// THEN
assert.equal(process.exitCode, false, 'deploy should not have thrown an error');
// get results from cache
const result = cache.getCache();
assert.equal(
result.query ? Object.keys(result.query).length : 0,
3,
4,
'three queries expected'
);
// confirm created item
Expand Down Expand Up @@ -125,6 +129,50 @@ describe('type: query', () => {
});
it('Should change the key during update with --changeKeyValue');
});
describe('FixKeys ================', () => {
beforeEach(() => {
testUtils.mockSetup(true);
});
it('Should create & upsert a query', async () => {
// WHEN
await handler.deploy('testInstance/testBU', ['query']);
// THEN
assert.equal(process.exitCode, false, 'deploy should not have thrown an error');
// get results from cache
const result = cache.getCache();
assert.equal(
result.query ? Object.keys(result.query).length : 0,
4,
'three queries expected'
);
// confirm created item
// assert.deepEqual(
// await testUtils.getActualJson('testNew_query', 'query'),
// await testUtils.getExpectedJson('9999999', 'query', 'post'),
// 'returned metadata was not equal expected for insert query'
// );
// expect(file(testUtils.getActualFile('testNew_query', 'query', 'sql'))).to.equal(
// file(testUtils.getExpectedFile('9999999', 'query', 'post', 'sql'))
// );
// // confirm updated item
// assert.deepEqual(
// await testUtils.getActualJson('testExisting_query', 'query'),
// await testUtils.getExpectedJson('9999999', 'query', 'patch'),
// 'returned metadata was not equal expected for insert query'
// );
// expect(file(testUtils.getActualFile('testExisting_query', 'query', 'sql'))).to.equal(
// file(testUtils.getExpectedFile('9999999', 'query', 'patch', 'sql'))
// );
// // check number of API calls
// assert.equal(
// testUtils.getAPIHistoryLength(),
// 8,
// 'Unexpected number of requests made. Run testUtils.logAPIHistoryDebug() to see the requests'
// );
return;
});
it('Should change the key during update with --changeKeyValue');
});
describe('Templating ================', () => {
it('Should create a query template via retrieveAsTemplate and build it', async () => {
// GIVEN there is a template
Expand Down

0 comments on commit 70c5f0f

Please sign in to comment.