-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Issue #228] consumers to add model hooks
- Loading branch information
Russ Nicoletti
committed
Jan 25, 2017
1 parent
adf2fd8
commit 638ea7b
Showing
4 changed files
with
156 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import app from './server'; | ||
import db from '../src/models/db'; | ||
import { | ||
datastreams, | ||
DatastreamsEntity, | ||
entities, | ||
featuresOfInterest, | ||
FeaturesOfInterestEntity, | ||
historicalLocations, | ||
HistoricalLocationsEntity, | ||
locations, | ||
LocationsEntity, | ||
observations, | ||
ObservationsEntity, | ||
observedProperties, | ||
ObservedPropertiesEntity, | ||
sensors, | ||
SensorsEntity, | ||
things, | ||
ThingsEntity | ||
} from './constants'; | ||
import supertest from 'supertest'; | ||
|
||
const server = supertest.agent(app.listen(8890)); | ||
|
||
db().then(models => { | ||
describe('db hooks', () => { | ||
beforeEach(done => { | ||
models.sequelize.transaction(transaction => { | ||
return Promise.all(Object.keys(entities).map(name => { | ||
return models[name].destroy({ transaction, where: {} }); | ||
})); | ||
}).then(() => done()); | ||
}); | ||
|
||
[ | ||
[ datastreams, DatastreamsEntity ], | ||
[ featuresOfInterest, FeaturesOfInterestEntity ], | ||
[ historicalLocations, HistoricalLocationsEntity ], | ||
[ locations, LocationsEntity ], | ||
[ observations, ObservationsEntity ], | ||
[ observedProperties, ObservedPropertiesEntity ], | ||
[ sensors, SensorsEntity ], | ||
[ things, ThingsEntity ] | ||
].forEach(testObj => { | ||
it(testObj[0] + ' db \"create\" hooks', done => { | ||
models[testObj[0]].create(testObj[1]).then(instance => { | ||
const expectedMap = new Map(); | ||
[ | ||
'beforeValidate', | ||
'afterValidate', | ||
'beforeCreate', | ||
'afterCreate' | ||
].map(expectedObj => { | ||
expectedMap.set(expectedObj, expectedObj); | ||
}); | ||
const instanceProperties = Object.entries(instance.properties); | ||
const propIndex = (instance.$modelOptions.name.plural === 'Things') ? 3 : 0; | ||
for (var i = 0; i < instanceProperties.length; i++) { | ||
const expectedHook = expectedMap.get(instanceProperties[i + propIndex][0]); | ||
const instanceHook = instanceProperties[i + propIndex][1]; | ||
expectedHook.should.be.equal(instanceHook); | ||
} | ||
}); | ||
done(); | ||
}) | ||
}); | ||
}); | ||
}); | ||
|