From f37e5217686ef39e2981bab1ec9e819f6ee300a1 Mon Sep 17 00:00:00 2001 From: Mark Moffat Date: Sat, 8 Jun 2019 17:20:01 +0930 Subject: [PATCH] Added ability to easily run the test/sample data --- README.md | 4 +++ lib/common.js | 80 ------------------------------------------------- lib/testdata.js | 51 +++++++++++++++++++++++++++++++ package.json | 1 + 4 files changed, 56 insertions(+), 80 deletions(-) create mode 100644 lib/testdata.js diff --git a/README.md b/README.md index c9460b3..cec9524 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,10 @@ The easiest way to get up and running is using Docker. Once the Docker CLI is in > Note: When deploying to Heroku you will need to configure your external MongoDB either on your own server or a hosted service on mLab, AWS etc. +### Sample/Test data + +Sometimes you might want some default sample/test data. To create this, run `npm run testdata`. Remember to only run this initially or anytime you want to reset the data as this function deletes ALL existing data. + ## Admin Visit: [http://127.0.0.1:1111/admin](http://127.0.0.1:1111/admin) diff --git a/lib/common.js b/lib/common.js index 130d0a6..ce23378 100755 --- a/lib/common.js +++ b/lib/common.js @@ -646,83 +646,3 @@ exports.runIndexing = (app) => { process.exit(2); }); }; - -exports.dropTestData = (db) => { - Promise.all([ - db.products.drop(), - db.users.drop(), - db.customers.drop() - ]) - .then((err) => { - return Promise.resolve(); - }) - .catch((err) => { - console.log('Error dropping test data', err); - }); -}; - -exports.sampleData = (app) => { - const db = app.db; - - db.products.count() - .then((products) => { - if(products !== 0){ - return Promise.resolve(); - } - - console.log('Inserting sample data'); - const testData = fs.readFileSync('./bin/testdata.json', 'utf-8'); - const jsonData = JSON.parse(testData); - - // Add sample data - return Promise.all([ - db.products.insertMany(fixProductDates(jsonData.products)), - db.menu.insertOne(jsonData.menu) - ]); - }); -}; - -exports.testData = async (app) => { - const db = app.db; - const testData = fs.readFileSync('./bin/testdata.json', 'utf-8'); - const jsonData = JSON.parse(testData); - - // TODO: A bit ugly, needs fixing - return new Promise((resolve, reject) => { - Promise.all([ - db.users.remove({}, {}), - db.customers.remove({}, {}), - db.products.remove({}, {}), - db.menu.remove({}, {}) - ]) - .then(() => { - Promise.all([ - db.users.insertMany(jsonData.users), - db.customers.insertMany(jsonData.customers), - db.products.insertMany(fixProductDates(jsonData.products)), - db.menu.insertOne(jsonData.menu) - ]) - .then(() => { - resolve(); - }) - .catch((err) => { - console.log('Error inserting test data', err); - reject(err); - }); - }) - .catch((err) => { - console.log('Error removing existing test data', err); - reject(err); - }); - }); -}; - -// Adds current date to product added date when smashing into DB -function fixProductDates(products){ - let index = 0; - products.forEach((product) => { - products[index].productAddedDate = new Date(); - index++; - }); - return products; -} diff --git a/lib/testdata.js b/lib/testdata.js new file mode 100644 index 0000000..d9020a3 --- /dev/null +++ b/lib/testdata.js @@ -0,0 +1,51 @@ +const common = require('./common'); +const { initDb } = require('./db'); +const fs = require('fs'); +const path = require('path'); + +console.log('__dirname', path.join(__dirname, '..', 'bin', 'testdata.json')); + +const testData = fs.readFileSync(path.join(__dirname, '..', 'bin', 'testdata.json'), 'utf-8'); +const jsonData = JSON.parse(testData); + +// get config +let config = common.getConfig(); + +initDb(config.databaseConnectionString, (err, db) => { + Promise.all([ + db.users.remove({}, {}), + db.customers.remove({}, {}), + db.products.remove({}, {}), + db.menu.remove({}, {}) + ]) + .then(() => { + Promise.all([ + db.users.insertMany(jsonData.users), + db.customers.insertMany(jsonData.customers), + db.products.insertMany(fixProductDates(jsonData.products)), + db.menu.insertOne(jsonData.menu) + ]) + .then(() => { + console.log('Test data complete'); + process.exit(); + }) + .catch((err) => { + console.log('Error inserting test data', err); + reject(err); + }); + }) + .catch((err) => { + console.log('Error removing existing test data', err); + reject(err); + }); +}); + +// Adds current date to product added date when smashing into DB +function fixProductDates(products){ + let index = 0; + products.forEach((product) => { + products[index].productAddedDate = new Date(); + index++; + }); + return products; +} diff --git a/package.json b/package.json index 35f5d2e..42492f2 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,7 @@ "scripts": { "start": "node app.js", "deploy": "gulp deploy", + "testdata": "node lib/testdata.js", "test": "ava test/**/*.js --verbose" }, "dependencies": {