Added ability to easily run the test/sample data

master
Mark Moffat 2019-06-08 17:20:01 +09:30
parent bc4a5ee71b
commit f37e521768
4 changed files with 56 additions and 80 deletions

View File

@ -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)

View File

@ -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;
}

51
lib/testdata.js Normal file
View File

@ -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;
}

View File

@ -6,6 +6,7 @@
"scripts": {
"start": "node app.js",
"deploy": "gulp deploy",
"testdata": "node lib/testdata.js",
"test": "ava test/**/*.js --verbose"
},
"dependencies": {