-
Notifications
You must be signed in to change notification settings - Fork 2k
Conversation
@mleanos I'd prefer this be an option in the env config. |
@codydaig I originally thought the same thing. However, we'd have to explicitly set the option to be specifically for when the tests run. For example: seedDb: {
logResultsDuringTests: process.env.logResultsDuringTests || false
} Maybe using options at the seed config, and env config would work.. that way someone could set the option in the env config if they want it applied everywhere. Or they could leave the env config as the default of The key thing is that we need to provide a way to mute the console messages when the test suite is running vs when the db is being seeded. I think I still prefer having the options parameter. But I'm not opposed to having a "global" env config as well. |
Another thing to keep in mind, is that the Seed Db may, eventually, be used in other parts of the application. Hence, why we have the |
@@ -4,6 +4,11 @@ var mongoose = require('mongoose'), | |||
chalk = require('chalk'), | |||
crypto = require('crypto'); | |||
|
|||
// set default seed options | |||
var seedOptions = { | |||
logResults: true // log the results of the seeding to the console |
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.
@codydaig What if we set the default here, from the env config? While keeping the seedOptions object.
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.
That's a lot better, yes. It means we are not inventing another configuration option but instead re-using and existing one.
@mleanos will this logging option support also logging the data (the seed user/pass that was created) into the app log file? |
@lirantal We could have an option for that as well. Or have logging options that may include a list of where to log the results.. The key point for me is to be able to control the logging, or any other options that we may introduce, on any given seed operation.. Meaning, that when someone calls |
Also, I think having default seed options coming from the env configs would a good idea. WDYT? |
70ee8b8
to
5176c2c
Compare
Made some modifications, based on suggestions from @codydaig & @lirantal The seed options are now defaulting from env configs. Also, changed the seedDB env config setting to an object, with an Also, fixed an issue with how the env config was reading the seed setting from the processes env variable; see commit message for more details. |
Coming from a ruby/C# Scene the whole seeding thing seems to get a lot bigger than it has to be. @lirantal & @codydaig what do you think about creating mocks that can be used in the tests for example for a user? In that case you'd test the mocks using your model-tests, would have only two places (model&mock) to reconfigure when changing something and you'd have the seeding right away. That would obviate the need to tests the seeding. Talked about that with @mleanos earlier, but we were disconnected while talking. |
} | ||
|
||
module.exports.start = function start() { | ||
module.exports.start = function start(options) { | ||
if (options && options.hasOwnProperty('logResults')) seedOptions.logResults = options.logResults; |
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.
How about using lodash's .has method? it's a shorthand way of doing it and we can use it consistently across the project?
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.
Please use {} for if statements.
@mleanos how about we move the seeded user details outside of the seed file and into the env variables as well as config options? (@bastianwegge is that what you meant by mock users?) |
This is what I meant: Imagine a MockFile like this
You would require this in your tests that would create something like this:
This way you would have tested your mongoose-Schema and your mock. Then you could just go ahead and create something like a seeding-service like the following
Any questions? |
@lirantal I will address your comments today. I already have a branch that moves the User details out of the seed configuration and puts it into the env configs. I'll add it to this PR. @bastianwegge What you're suggesting is outside the scope of what this PR is intended for. I think you make some valid points, and I'd like to discuss it further; and get everyone else involved. This would be a big change in most of the server side tests, so it's probably best for a separate PR. Thanks for the idea; I think we can incorporate something like your example. |
@lirantal @codydaig I addressed your comments, and added some tests to the core server config test suite.
@ilanbiala I actually prefer the way the code reads using the inline if statements, in this case. Otherwise, we would have something like this if (options) {
seedOptions.logResults = options.logResults || seedOptions.logResults;
seedOptions.seedUser = options.seedUser || seedOptions.seedUser;
seedOptions.seedAdmin = options.seedAdmin || options.seedAdmin;
} I think what I have now, is much cleaner, and we only touch the seedOptions when the options are provided. I can change this based on your suggestion, if it goes against our coding styles. I'm just not aware that we have a desired style in cases like this. Something that occurred to me while writing the tests, was that we can only test against the test env seed settings; even though we're manually changing the process.env.NODE_ENV. This probably isn't a problem, but I wanted to point it out. One last thing, is the possibility of the seedDB being turned on for the test environment. Should we allow this? Or should we set the |
BTW, I'm surprised the coverage only went up .02%. I had such high hopes for an increase here :( Was aspiring for something like @lirantal +8% the other day. haha I'm just glad the build succeeded :) |
@lirantal Was there a specific reason this is here? I don't see a usage. I forgot to mention it before |
8fb1ded
to
aca1af3
Compare
Yeah I see it's not being used, can you fix it? |
@mleanos why do we have both |
@lirantal Yes. I can remove that unused var. The If we don't test against the default settings, then we can only test the seed using the passed in user details, using the options param. What you're saying is that we can't guarantee that the env config will contain the seed user details? It would be nice to test the seeding using the default settings from the env config though. How about we test for the existence of the user details in the env config, and if present then we can proceed to test using those settings? Otherwise, we just allow the test to pass since we don't have the user details to test against. |
Got it. I'm ok with the tests as is. |
@ilanbiala How do you feel about this? https://github.com/meanjs/mean/pull/957/files#diff-7dfe67f86c4c0773b98c0422a0cb606aR105 See my comment above about it #957 (comment) |
@codydaig @ilanbiala @lirantal How do the seedDB env config settings look to you? I'm wondering if I need to add something like Also, would it be appropriate to add an exported function to the seed configuration, to retrieve the options? I'm thinking that it would be better to rely on the seed configuration to interact with the seedDB options from other parts of the application. For instance, the server config test suite; rather than retrieving the options from the application config like here https://github.com/meanjs/mean/pull/957/files#diff-27fe56fc024d6e9d46e8c361ad7879e4R45 The idea I'm going with here is to add some additional sanitization to the options. Perhaps, to gracefully handle when the seedDB options aren't present in the env config. I'm trying to get the seedDB feature to be more extensible & reusable; but also not too opinionated. Any thoughts? |
@mleanos no, that isn't okay in my opinion, and honestly having one strict style is better. Also, not having brackets leads to confusion and other issues that you may be aware of. |
@ilanbiala Are you specifically saying that you prefer something like this? if (_.has(options, 'logResults')) { seedOptions.logResults = options.logResults };
if (_.has(options, 'seedUser')) { seedOptions.seedUser = options.seedUser };
if (_.has(options, 'seedAdmin')) { seedOptions.seedAdmin = options.seedAdmin }; Or do you mean you'd rather have it broken up into multiple lines? if (_.has(options, 'logResults')) {
seedOptions.logResults = options.logResults
};
if (_.has(options, 'seedUser')) {
seedOptions.seedUser = options.seedUser
};
if (_.has(options, 'seedAdmin')) {
seedOptions.seedAdmin = options.seedAdmin
}; If the latter, I'd be concerned with readability. Although, we could put this logic in a separate function like |
The latter, and put it in whatever way makes sense. But the bracket requirement should be strict. |
function removeUser (user) { | ||
return new Promise(function (resolve, reject) { | ||
var User = mongoose.model('User'); | ||
User.find({username: user.username}).remove(function (err) { | ||
if (err) { | ||
reject(new Error('Database Seeding:\t\t\tFailed to remove local ' + user.username)); | ||
reject(new Error('Failed to remove local ' + user.username)); | ||
} | ||
resolve(); |
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.
Resolve with something to affirm, or maybe the removed user.
Added an options object to the database seed configuration. Currently, the only option implemented is `logResults`; set using the seedDB env config options (default to "true"). Modified the definition of the env config for seedDB. It's now an object, with options. Setting the logResults option is set to `false` in the core configuration server test suite. Also, fixed an issue with how env configs were reading the seedDB setting from the env variables. Previously, the config was getting set by looking for merely the existence of the env variable (MONGO_SEED). However, if this setting existed but was set to "false", the seedDB would be turned on. Added the SeedDB user details to the env config, and seedDB options. Added tests to the core server config test suite should have seedDB configuration set for "regular" user should have seedDB configuration set for admin user should seed admin, and "regular" user accounts when NODE_ENV is set to "test" when they already exist should ONLY seed admin user account when NODE_ENV is set to "production" with custom admin should seed admin, and "regular" user accounts when NODE_ENV is set to "test" with custom options should NOT seed admin user account if it already exists when NODE_ENV is set to "production" should NOT seed "regular" user account if missing email when NODE_ENV set to "test" Added support for environment variables to seedDB env configs; currently only supporting username & email. Refactored how the SeedDB rejects were being handled
Changes to formatting and indentation.
894966f
to
75cf745
Compare
@ilanbiala I addressed your line comments, with the exception of a couple. The As for the issue of the non-async tests using the |
@mleanos there are quite a few resolves that are just in a .then, which I'm not really understanding why they are there. Can you explain that? If tests aren't async it's unnecessary, and it also indicates whether the tested function is synchronous or asynchronous, so let's make sure we aren't abusing it. |
@ilanbiala Those resolves are to satisfy the current promise. You'll notice that each usage is at the end of the chain; we need to resolve each Promise, at each level. I'll go ahead and make those tests synchronous. |
Removed the done() callback method from the config tests that aren't truly asynchronous.
@ilanbiala Updated the tests. If we need to address the resolves, in regards to your concerns, it will require a bigger refactor of the SeedDB feature that we should do in a separate PR. |
@ilanbiala @lirantal Does this look ready to you both? |
@mleanos if we can't address the resolves in this PR, then let's open one right after because I don't think that is very intuitive. Otherwise, LGTM. |
I would rather finalize this PR, and then have a discussion on how we want to proceed with our strategy of using Promises & Closures. @lirantal Is that fine with you? I'll be on Gitter for a couple more hours, if you'd like to discuss. |
Sure. |
Added an options object to the database seed configuration. Currently, the only option implemented is
logResults
; set to 'true' by default.Setting the logResults option to
false
in the core configuration server test suite.The initial reason for this option is to disable the console output of the seed db events, while running the test coverage. However, this can be useful in other cases. For instance, if the user doesn't want the passwords displayed in the console where they may be comprimised.