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

Conversation

andypants888
Copy link
Contributor

In this PR, the metaTest feature:

  • makes db calls to getInternalTest and getMetaTest
  • console.logs(the internal testing codes)
  • only returns false
  • does NOT evaluate metaTest YET.

@oriooctopus
Copy link
Owner

Can you include a video of this in action?

@andypants888
Copy link
Contributor Author

Can you include a video of this in action?

Sure. I will do that tomorrow.

@oriooctopus
Copy link
Owner

why is it called test validator 37?

@oriooctopus
Copy link
Owner

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.

Comment on lines 8 to 12
beforeCreateMany(event) {
console.log("**** beforeCreateMany hook is activated! ****");
const eventData = event.params.data;
runMetaTest(eventData);
},
Copy link
Owner

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);
Copy link
Owner

Choose a reason for hiding this comment

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

runMetaTests with an S

Copy link
Owner

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

Copy link
Contributor Author

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),
Copy link
Owner

@oriooctopus oriooctopus Feb 22, 2022

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

Copy link
Contributor Author

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

Copy link
Contributor Author

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

Comment on lines 60 to 63
let testIDs = [];
eventData.tests.forEach((test) => {
testIDs.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.

use map instead it's more elegant and easier to read

eventData.tests.forEach((test) => {
testIDs.push(test.id);
});
const currentTestID = Math.min(...testIDs);
Copy link
Owner

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

Copy link
Owner

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

Copy link
Owner

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

Copy link
Contributor Author

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"],
Copy link
Owner

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

Comment on lines 28 to 29
const tests = await getInternalTests(eventData);
const metaTests = await getMetaTests(eventData);
Copy link
Owner

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) {
Copy link
Owner

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

Copy link
Owner

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

Copy link
Contributor Author

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)

Copy link
Contributor Author

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

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...

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 all patterns. You'll recognize many similarities with other frameworks. Strapi isn't very unique or anything

Copy link
Owner

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

@andypants888
Copy link
Contributor Author

why is it called test validator 37?

37 is the jira task number
Originally we called the feature: "a test-validator", renamed to "metaTest"

Copy link
Owner

@oriooctopus oriooctopus left a 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! ****");
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

Comment on lines 4 to 7
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?

.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

Comment on lines 54 to 57
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!

}

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

Comment on lines 75 to 78
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

@oriooctopus oriooctopus merged commit d312b00 into oriooctopus:master Mar 1, 2022
@andypants888 andypants888 deleted the test-validator-37 branch March 1, 2022 19:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants