Open clevertechru opened 10 years ago
My recommendation would be to use SQLite for tests (as it is a drop in replacement for MySQL from orm's perspective) as it will allow you to run your tests on an 'in-memory' database rather than requiring an actual database server to be present.
If that doesn't fulfil your requirements then you would need to write a database interface layer for orm which allowed you to emulate the way the MySQL database would behave under different circumstances. If you're headed this way then I'd suggest looking at the lib/DDL
and lib/DML
folders for examples.
describe("#Emulating hit", function() {
before(function(done) {
mongoose.connect(mongoConn);
db = orm.connect(mysqlConn);
db.on("connect", function (err) {
if (err) {
console.log("ORM error: ", err);
return;
}
// connected!
db.load(path.resolve("./factories/models/mysql/models.js"), function (err) {
if (err) {
console.log("ORM error: ", err);
return;
}
// loaded!
// ????????????????
});
});
});
after(function(done) {
//some code
});
it("TEST1 Should write to mongo through mongoose", function(done){
//it works prefect!!!
var query = StatsPlatforms.find().sort({_id:-1}).limit(1); //get latest record
// console.log(query.model.collection);
query.exec(function (err, model) {
if (err) return handleError(err);
console.log(model);
var row = model[0];
console.log('date: %s cityId: %s countryId: %s deviceId: %s hits: %s osId: %s platformId: %s.',
row.date, row.cityId, row.countryId, row.deviceId, row.hits, row.platformId) // Space Ghost is a talk show host.
row.platformId.should.eql(2);
row.hits.should.eql(1);
row.hits.should.not.eql(0);
})
done();
});
it("TEST2 Should write and read to mysql through orm", function (done){
// It don't work ((( but prefect work in before section (((
Sync(function(){
var StatsCities = db.models.stats_cities;
var StatsCountries = db.models.stats_countries;
// console.log('Cities model: ' + Cities);
// console.log(Cities);
// console.log('Countries model: ');
// console.log(Countries);
StatsCities.create([
{
site_id : Math.floor(Math.random() * 90000) + 10000,
city_id : Math.floor(Math.random() * 90000) + 10000,
stat_week : 1
}
], function (err, items) {
if (err) {
console.log("ORM error: ", err);
}
// console.log(items);
});
StatsCities.find().limit(3).run(function(err, rows){
if (err) {
console.log("ORM error: ", err);
}
var row = rows[0];
console.log(row.site_id, row.city_id, row.stat_week);
console.log(JSON.stringify(rows));
});
StatsCities.find().remove().run(function(err){
if (err) {
console.log("ORM remove stats_cities error: ", err);
}
})
},function(err){
console.log("db sync err: ", err);
});
done();
})
});
Test1 for mongo through mongoose is works perfect Please help me with Test2 for mysql through orm ))
It looks to me like you're neglecting the fact that ORM runs asynchronously, starting operations before the prerequisite operations have completed. Try the following code for test 2 and see if it works.
it("TEST2 Should write and read to mysql through orm", function (done){
// It don't work ((( but prefect work in before section (((
Sync(function(){
var StatsCities = db.models.stats_cities;
var StatsCountries = db.models.stats_countries;
// console.log('Cities model: ' + Cities);
// console.log(Cities);
// console.log('Countries model: ');
// console.log(Countries);
StatsCities.create([
{
site_id : Math.floor(Math.random() * 90000) + 10000,
city_id : Math.floor(Math.random() * 90000) + 10000,
stat_week : 1
}
], function (err, items) {
if (err) {
console.log("ORM error: ", err);
return done(err);
}
// console.log(items);
StatsCities.find().limit(3).run(function(err, rows){
if (err) {
console.log("ORM error: ", err);
return done(err);
}
var row = rows[0];
console.log(row.site_id, row.city_id, row.stat_week);
console.log(JSON.stringify(rows));
StatsCities.find().remove().run(function(err){
if (err) {
console.log("ORM remove stats_cities error: ", err);
return done(err);
}
done();
})
});
});
}, function(err){
console.log("db sync err: ", err);
done(err);
});
});
#Emulating hit
◦ TEST2 Should write and read to mysql through orm: db sync err: undefined
✓ TEST2 Should write and read to mysql through orm
1 passing (1s)
but nothing to console log (((
Perhaps we should make a mock driver. That way, when your application is in "test" mode, it changes the protocol from whatever it was to "mock".
It would accept and respond to requests, it can be implemented as an object with objects of lists and searching those lists. Nothing fancy, just a simple backend, but one that we support entirely. That way you can do your unit testing with a very small subset of data.
How do you mock MySQL (with node-orm2) in Node.js?
http://stackoverflow.com/questions/18626823/how-do-you-mock-mysql-with-node-orm2-in-node-js-express