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

Test validator 37 #3

Merged
merged 6 commits into from
Mar 1, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
81 changes: 81 additions & 0 deletions src/api/code-challenge/content-types/code-challenge/lifecycles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
module.exports = {
beforeCreate(event) {
console.log("**** beforeCreate hook is activated! ****");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The console.log can be used for testing but shouldn't be merged. Make sure to remove it when you finish. If anything we would do more specific logging , but this is too generic

const [eventTests, eventMetaTests] = [
event.params.data.tests,
event.params.data.MetaTest,
];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't make sense. Take a second look at it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, but I am following the conventions of the Strapi Components. I could rename it to metaTests, but then it would look weird
Screen Shot 2022-02-28 at 2 02 17 PM
on strapi. Which one do you think we should prefer?

runMetaTests(eventTests, eventMetaTests);
},

beforeUpdate(event) {
console.log("**** beforeUpdate hook is activated! ****");
const [eventTests, eventMetaTests] = [
event.params.data.tests,
event.params.data.MetaTest,
];
runMetaTests(eventTests, eventMetaTests);
},
};

// runMetaTestsNeed more specific name later, maybe use "validateInternalTests"
async function runMetaTests(eventTests, eventMetaTests) {
const [tests, metaTests] = await Promise.all([
getInternalTests(eventTests),
getMetaTests(eventMetaTests),
]);

tests.map((test) =>
console.log("final test.internalTest: ", test.internalTest)
);

metaTests.map((metaTest) =>
console.log("final metaTest caseCode: ", metaTest.caseCode)
);

return false;
}

async function getInternalTests(eventTests) {
const internalTests = await strapi.db
.query("challenge.code-challenge-test")
.findMany({
select: ["id", "internalTest"],
where: {
id: getCurrentTestIDs(eventTests),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getCurrentINTERNALTestIds

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I prefer Ids to IDs? I'm ok with either one but just not sure.

I read this SO response https://stackoverflow.com/questions/15526107/acronyms-in-camelcase/27172000

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's whatever. I think Ids is more common

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with the top answer, that's how I do it I guess

},
});

// console.log("internalTests: ", internalTests);
return internalTests;
}

function getCurrentTestIDs(eventTests) {
let currentTestIDs = [];
eventTests.map((test) => {
currentTestIDs.push(test.id);
});
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this defeats the point of map. You need to return inside of the map function. Take a look at map on MDN

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

function getCurrentTestIDs(eventTests) {
return eventTests.map(({id}) => id);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The eventTests array is an array of objects like
[{id: 10}, {id: 11}]
I am trying to create a new array of ids like this:
[10, 11]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the heads up. I didn't realize .map() could be so flexible!

// console.log("currentTestIDs: ", currentTestIDs);
return currentTestIDs;
}

async function getMetaTests(eventMetaTests) {
const MetaTests = await strapi.db.query("challenge.meta-test").findMany({
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be camel case

select: ["id", "caseCode", "passes"],
where: {
id: getCurrentMetaIDs(eventMetaTests),
},
});

// console.log("MetaTests: ", MetaTests);
return MetaTests;
}

function getCurrentMetaIDs(eventMetaTests) {
let metaIDs = [];
eventMetaTests.map((metaTest) => {
metaIDs.push(metaTest.id);
});
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as the above comment regarding map

// console.log("metaIDs: ", metaIDs);
return metaIDs;
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@
"pluginOptions": {},
"type": "boolean",
"default": false
},
"MetaTest": {
"type": "component",
"repeatable": true,
"component": "challenge.meta-test"
}
}
}
20 changes: 20 additions & 0 deletions src/components/challenge/meta-test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"collectionName": "components_challenge_meta_tests",
"info": {
"displayName": "MetaTest",
"icon": "balance-scale",
"description": ""
},
"options": {},
"attributes": {
"caseCode": {
"type": "richtext"
},
"label": {
"type": "string"
},
"passes": {
"type": "boolean"
}
}
}