0

I'm manually testing some Node.js endpoints that access a MongoDB. And I'm using run-rs to setup replica sets in development mode (https://www.npmjs.com/package/run-rs)

My process is the following:

  1. $ run-rs -v 4.0.0 -s
  2. rs:PRIMARY> use my_db
  3. rs:PRIMARY> load("create-db.js")

Then in a separate terminal window, npm start to run my Node.js app and connect to my_db. In create-db.js I have the setup of many collections. Something like:

db.categorie.insert({"categories":["all","cat","dog","cow","mus"]});

var users = [{
    "_id" : ObjectId("5b7b99477943f109fba8b128"),
    "admin" : false,
    "log" : false,
    "city" : "",
    "state" : "",
    "country" : "",
    "active" : true,
    "forgot" : false,
    "email" : "some+user@gmail.com",
    "hash" : "88409d4e8754af084ccb8554934e979bcd7c6a7f2532915b225a47b97de9b80a70c2c025e5610d9cdc15afebdd4efe26f9df586ee3b57763d196c96fb5dff96a1e3cfa80acf0bd232acb5b8eb7a2e9cf06a48dfd9b8cfd16ebd6fc33a1d7c5a6f66fb1c1efa45c570acb16e08c1867c8a58de40d4471433aa025de46cc21d07f52b68e0494f24f41efc2234c97070a943fb149640ae9e434de0df0154e0893718d3177c9bde61169cac2ee3bf265ffa99ca8338602d5be8c3e18aa7df99ba3cb38ca00bc17e85d680f92c6e3f84bb0cd63c062931b5e90793e9774c1f7c58a4312562e24752c366d5459ff9214b57309ac8e16357fee109cc1d397d48d1f1a6438d19226ffb0c42a175a0043a8a6405f85290657b1ce75504585123b0af9c1de01e3edc2d0538ab2246900e3a811c12ece1b2495e2d532a4a01f5e5ebc1ebe25853147071cfc394f9161d643b518327c5be777d5dabb701485a919533d972a046c4b86af6c3edf427ea73ac8d7a5cf19cf236ec9849984f4c1029a12c7ee0bc655125b9558eba223e6768a36c4bbbefdcda6f218561446bbc86efb41c4ca5755e89d3a4a9f3e7f480dea158e15687d6b1838356cc9ce37b86827283bb4a596681a4f691c5ab812769b753e7ece29d4dd81c90e98e3b8cf0261d88ecfc37c8d32db7efea99a84d2689d577cd20130b4c7f93493bd7c88d2a388c1de4d0d4c41d1",
    "salt" : "9b3a7884e00077a125e7cbdc9dddc09236804cee65gb31467910755aeb2759d0",
    "__v" : 0
},
{
...
},
{
...
}];
db.users.insertMany(users);

...and so on, with many other collections.

The database setup is huge and I want to keep things organised. The thing is that I'm running run-rs manually, and I'm also loading the script from mongo shell. I don't know how to to this from Mocha - or even if it's best practice or not.

João Otero
  • 948
  • 1
  • 15
  • 30

2 Answers2

0

If you would be doing this every time before testing a suite of test cases then you should use the before hook where you could set the db / all the collections etc. that are needed for the tests. Then you do your tests and in the after hook you cleanup.

So you would run your manual scripts I am assuming after the run-rs all in the before hook ... then do your tests.

Here is a link to MongoDB hooks

Akrion
  • 18,117
  • 1
  • 34
  • 54
  • yes, but how do I call run-rs and mongo shell methods from inside the hooks? – João Otero Aug 30 '18 at 05:17
  • there's just this part missing (can you help?): https://stackoverflow.com/questions/52125137/mongodb-node-driver-how-to-run-the-load-command-from-node/52125312#52125312 – João Otero Sep 01 '18 at 06:13
0

If you want to run the commands you can always use exec() and make this as setup process before test. if you want to always run it inside hook, make it as function and run.

exec('dir', (err, stdout, stderr) => {
  if (err) {
    console.error(`exec error: ${err}`);
    return;
  }

  console.log(`Number of files ${stdout}`);
});
PJAutomator
  • 344
  • 3
  • 12
  • that might do the trick... and I think once the db is up I can run db.runCommand("load('create-db.js')") to execute my script – João Otero Aug 30 '18 at 19:04
  • Let me know if it works and please accept the answer if it did – PJAutomator Aug 30 '18 at 19:07
  • There's one step still: once run-rs has ran, and before calling db.runCommand, how to run "use db_name" from Mocha (as I would do from mongo shell) ? – João Otero Aug 30 '18 at 19:09
  • If i have to run bunch of set up command, i would create a batch file or similar and run the batch file itself. – PJAutomator Aug 30 '18 at 19:14
  • that's exactly what I'm struggling with.. I'm trying to call the load function from the mongo driver, but I'm not being able to... I even made another question just for this: https://stackoverflow.com/questions/52125137/mongodb-node-driver-how-to-run-the-load-command-from-node/52125312#52125312 – João Otero Sep 01 '18 at 06:11