Skip to content

Commit

Permalink
feat: implement prepareSeedCriteria
Browse files Browse the repository at this point in the history
  • Loading branch information
lykmapipo committed Aug 16, 2019
1 parent 1eb254b commit 90b7a4a
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 2 deletions.
34 changes: 32 additions & 2 deletions src/priority.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@
* @public
*/
import _ from 'lodash';
import { randomColor, compact, mergeObjects } from '@lykmapipo/common';
import { idOf, randomColor, compact, mergeObjects } from '@lykmapipo/common';
import { createSchema, model, ObjectId } from '@lykmapipo/mongoose-common';
import { localize, localizedIndexesFor } from 'mongoose-locale-schema';
import {
localize,
localizedIndexesFor,
localizedKeysFor,
localizedValuesFor,
} from 'mongoose-locale-schema';
import actions from 'mongoose-rest-actions';
import exportable from '@lykmapipo/mongoose-exportable';
import { Jurisdiction } from '@codetanzania/majifix-jurisdiction';
Expand Down Expand Up @@ -307,6 +312,31 @@ PrioritySchema.statics.findDefault = done => {
return Priority.getOneOrDefault({}, done);
};

/**
* @name prepareSeedCriteria
* @function prepareSeedCriteria
* @description define seed data criteria
* @param {Object} seed priority to be seeded
* @returns {Object} packed criteria for seeding
*
* @author lally elias <lallyelias87@gmail.com>
* @since 1.5.0
* @version 0.1.0
* @static
*/
PrioritySchema.statics.prepareSeedCriteria = seed => {
const names = localizedKeysFor('name');

const copyOfSeed = seed;
copyOfSeed.name = localizedValuesFor(seed.name);

const criteria = idOf(copyOfSeed)
? _.pick(copyOfSeed, '_id')
: _.pick(copyOfSeed, 'jurisdiction', ...names);

return criteria;
};

/**
* @name getOneOrDefault
* @function getOneOrDefault
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/priorities.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[
{
"_id": "5d535a0a62b47901d3294ff8",
"name": { "en": "Low" },
"weight": 1,
"color": "#BACFFC",
"default": true
}
]
88 changes: 88 additions & 0 deletions test/integration/priority.seed.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import path from 'path';
import _ from 'lodash';
import { clear, expect } from '@lykmapipo/mongoose-test-helpers';
import { Priority } from '../../src';

describe('Priority Seed', () => {
const { SEEDS_PATH } = process.env;
let priority;

before(done => clear(done));

before(() => {
process.env.SEEDS_PATH = path.join(__dirname, '..', 'fixtures');
});

it('should be able to seed', done => {
Priority.seed((error, seeded) => {
expect(error).to.not.exist;
expect(seeded).to.exist;
expect(seeded).to.length.at.least(1);
priority = _.first(seeded);
done(error, seeded);
});
});

it('should not throw if seed exist', done => {
Priority.seed((error, seeded) => {
expect(error).to.not.exist;
expect(seeded).to.exist;
expect(seeded).to.length.at.least(1);
done(error, seeded);
});
});

it('should seed provided', done => {
const seed = Priority.fake().toObject();
Priority.seed(seed, (error, seeded) => {
expect(error).to.not.exist;
expect(seeded).to.exist;
expect(seeded).to.length.at.least(1);
done(error, seeded);
});
});

it('should seed provided', done => {
const seed = Priority.fake().toObject();
Priority.seed([seed], (error, seeded) => {
expect(error).to.not.exist;
expect(seeded).to.exist;
expect(seeded).to.length.at.least(1);
done(error, seeded);
});
});

it('should not throw if provided exist', done => {
const seed = priority.toObject();
Priority.seed(seed, (error, seeded) => {
expect(error).to.not.exist;
expect(seeded).to.exist;
expect(seeded).to.length.at.least(1);
done(error, seeded);
});
});

it('should be able to seed from environment', done => {
Priority.seed((error, seeded) => {
expect(error).to.not.exist;
expect(seeded).to.exist;
expect(seeded).to.length.at.least(1);
done(error, seeded);
});
});

it('should not throw if seed from environment exist', done => {
Priority.seed((error, seeded) => {
expect(error).to.not.exist;
expect(seeded).to.exist;
expect(seeded).to.length.at.least(1);
done(error, seeded);
});
});

after(done => clear(done));

after(() => {
process.env.SEEDS_PATH = SEEDS_PATH;
});
});

0 comments on commit 90b7a4a

Please sign in to comment.