-
Notifications
You must be signed in to change notification settings - Fork 3
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
Test validator 37 #3
Conversation
Can you include a video of this in action? |
Sure. I will do that tomorrow. |
why is it called test validator 37? |
Off to a good start! Can you add a couple of unit tests? We don't have any yet, so you get to choose what we'll use. Perhaps testing-library? I've heard great things about it.... Message me before you settle on a final choice. Actually, you know what, can you just create a separate ticket for that and we'll deal with it in the next PR? We want to avoid scope creep. |
beforeCreateMany(event) { | ||
console.log("**** beforeCreateMany hook is activated! ****"); | ||
const eventData = event.params.data; | ||
runMetaTest(eventData); | ||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you remove the many
ones?
beforeUpdate(event) { | ||
console.log("**** beforeUpdate hook is activated! ****"); | ||
const eventData = event.params.data; | ||
runMetaTest(eventData); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
runMetaTests
with an S
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, the name should be more specific. If you're planning on them throwing an error inside, then you can say something like validateInternalTests
. Validate typically implies something like that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it. I'm not 100% sure how I will implement it yet. I will change the name for the next PR once I decide? For now I can leave a comment about improving the name later.
Usually at the end, I try to rename the vars to make things easier to read/understand
select: ["id", "internalTest"], | ||
where: { | ||
id: { | ||
$gte: getCurrentTestID(eventData), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what does $gte stand for? I am too lazy to look it up
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is "greater than or equal to" for filtering db.query in strapi. used with "where" keyword
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$gte has been replaced with an array, to select the exact IDs needed
let testIDs = []; | ||
eventData.tests.forEach((test) => { | ||
testIDs.push(test.id); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use map instead it's more elegant and easier to read
eventData.tests.forEach((test) => { | ||
testIDs.push(test.id); | ||
}); | ||
const currentTestID = Math.min(...testIDs); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or actually use reduce that's even better
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then the entire function can be done in one line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
although up to you, reduce will be fancy but maybe slightly less readable. Fancy can be fun though as long as it's not too tricky
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed it to forEach to map. I'm going to leave it that way for now. I might have to tweak things before the feature is complete.
async function getMetaTests(eventData) { | ||
// Original Finding Internal Test | ||
const MetaTests = await strapi.db.query("challenge.meta-test").findMany({ | ||
select: ["id", "caseCode", "expectedResult"], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you change expectedResult
to passes
and make it a boolean? That is easier to configure
const tests = await getInternalTests(eventData); | ||
const metaTests = await getMetaTests(eventData); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you use Promise.all here
return internalTests; | ||
} | ||
|
||
function getCurrentTestID(eventData) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really understand this and getMetaTestId
. Why are you getting the minimum value? I can't intuitively guess why
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, it's a bad practice to have it receive the entire eventData. first of all, eventData is structurally different between beforeCreate
and beforeUpdate
, so you're running the risk of someone working on one of the lifecycles breaking the other. Second, it makes it harder to test this in unit tests because then you need to mock the entire massive object. In this case, just pass in the tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really understand this and
getMetaTestId
. Why are you getting the minimum value? I can't intuitively guess why
I will add a comment to make it more understandable. I ran into an problem where multiple tests/metatests were being returned out of order.
I needed a function Math.min(...test.IDs) to sort them by lowest ID --> highest
problem IDs (30, 29, 31)
after function (29, 30, 31)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, it's a bad practice to have it receive the entire eventData. first of all, eventData is structurally different between
beforeCreate
andbeforeUpdate
, so you're running the risk of someone working on one of the lifecycles breaking the other. Second, it makes it harder to test this in unit tests because then you need to mock the entire massive object. In this case, just pass in the tests
Okay I see what you mean here.
I didn't realize the event object would be changing. How do you know this?
How long have you been working with strapi?
Learning Strapi feels almost a little like learning react but for node...maybe this is why they call it a framework...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's all patterns. You'll recognize many similarities with other frameworks. Strapi isn't very unique or anything
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"I didn't realize the event object would be changing. How do you know this?" event objects are usually not standardized I think. Like even DOM events i would expect to change. In typescript you have a whoel bunch of different types for DOM events
37 is the jira task number |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Getting closer!
@@ -0,0 +1,81 @@ | |||
module.exports = { | |||
beforeCreate(event) { | |||
console.log("**** beforeCreate hook is activated! ****"); |
There was a problem hiding this comment.
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, | ||
]; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.findMany({ | ||
select: ["id", "internalTest"], | ||
where: { | ||
id: getCurrentTestIDs(eventTests), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getCurrentINTERNALTestIds
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
let currentTestIDs = []; | ||
eventTests.map((test) => { | ||
currentTestIDs.push(test.id); | ||
}); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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);
}
There was a problem hiding this comment.
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]
There was a problem hiding this comment.
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!
} | ||
|
||
async function getMetaTests(eventMetaTests) { | ||
const MetaTests = await strapi.db.query("challenge.meta-test").findMany({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be camel case
let metaIDs = []; | ||
eventMetaTests.map((metaTest) => { | ||
metaIDs.push(metaTest.id); | ||
}); |
There was a problem hiding this comment.
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
In this PR, the metaTest feature: