-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
moved test cases all under one test file, remove unnecessary test cas…
…es, unify setup procedures
- Loading branch information
Showing
3 changed files
with
250 additions
and
381 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,250 @@ | ||
// Copyright IBM Corp. 2013,2016. All Rights Reserved. | ||
// Node module: loopback-connector-mysql | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
'use strict'; | ||
require('./init.js'); | ||
var assert = require('assert'); | ||
|
||
var db, DateModel; | ||
|
||
describe('MySQL DATE, DATETTIME, TIMESTAMP types on server with local TZ', function() { | ||
var date; | ||
var dateOnly = new Date(2015,10,25); //2015-12-25 | ||
var timezone = getTimeZone(); | ||
|
||
before(function (done) { | ||
prepareModel('local', true, done); | ||
}); | ||
|
||
it('should set local timezone in mysql ' + timezone, function(done) { | ||
query("SET @@session.time_zone = '" + timezone + "';", function(err) { | ||
assert.ok(!err); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should create a model instance with dates', function(done) { | ||
date = new Date(); | ||
date.setMilliseconds(0); | ||
DateModel.create({ | ||
datetimeField: date, | ||
timestampField: date, | ||
dateField: dateOnly | ||
}, function(err, obj) { | ||
assert.ok(!err); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should get model instance', function(done) { | ||
DateModel.findOne({ | ||
where: { | ||
id: 1 | ||
} | ||
}, function(err, found) { | ||
assert.ok(!err); | ||
assert.equal(found.datetimeField.toISOString(), date.toISOString()); | ||
assert.equal(found.timestampField.toISOString(), date.toISOString()); | ||
assert.equal(found.dateField.toISOString(), dateOnly.toISOString()); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('timestampField shoud equal DEFAULT CURRENT_TIMESTAMP field', function(done) { | ||
DateModel.findOne({ | ||
where: { | ||
id: 1 | ||
} | ||
}, function(err, found) { | ||
assert.ok(!err); | ||
assert.equal(found.timestampField.toISOString(), found.timestampDefaultField.toISOString()); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should find model instance by datetime field', function(done) { | ||
DateModel.findOne({ | ||
where: { | ||
datetimeField: date | ||
} | ||
}, function(err, found) { | ||
assert.ok(!err); | ||
assert.ok(found); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should find model instance by timestamp field', function(done) { | ||
DateModel.findOne({ | ||
where: { | ||
timestampField: date | ||
} | ||
}, function(err, found) { | ||
assert.ok(!err); | ||
assert.ok(found); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should find model instance by date field', function(done) { | ||
DateModel.findOne({ | ||
where: { | ||
dateField: dateOnly | ||
} | ||
}, function(err, found) { | ||
assert.ok(!err); | ||
assert.ok(found); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should disconnect when done', function(done) { | ||
db.disconnect(); | ||
done(); | ||
}); | ||
}); | ||
|
||
describe('MySQL DATE, DATETTIME, TIMESTAMP types on server with non local TZ (+05:30)', function() { | ||
var timezone = '+05:30'; | ||
var date; | ||
var dateOnly = new Date(2016,11,22); //2015-12-25 | ||
|
||
before(function(done) { | ||
prepareModel(timezone, true, done); | ||
}); | ||
|
||
it('should set session timezone to ' + timezone, function(done) { | ||
query("SET @@session.time_zone = '" + timezone + "'", function(err) { | ||
assert.ok(!err); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should create a model instance with dates with TZ: +05:30', function(done) { | ||
//set date to current timestamp to comapre with mysql CURRENT_TIMESTAMP from the server | ||
date = new Date(); | ||
date.setMilliseconds(0); | ||
DateModel.create({ | ||
datetimeField: date, | ||
timestampField: date, | ||
timestampDefaultField: null, | ||
dateField: dateOnly | ||
}, function(err, found) { | ||
assert.ok(!err); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should get model instance', function(done) { | ||
DateModel.findOne({ | ||
where: { | ||
id: 1 | ||
} | ||
}, function(err, found) { | ||
assert.ok(!err); | ||
assert.equal(found.datetimeField.toISOString(), date.toISOString()); | ||
assert.equal(found.timestampField.toISOString(), date.toISOString()); | ||
assert.equal(found.dateField.toISOString(), dateOnly.toISOString()); | ||
done(); | ||
}); | ||
}); | ||
it('timestampField shoud equal DEFAULT CURRENT_TIMESTAMP field', function(done) { | ||
DateModel.findOne({ | ||
where: { | ||
id: 1 | ||
} | ||
}, function(err, found) { | ||
assert.ok(!err); | ||
assert.equal(found.timestampField.toISOString(), found.timestampDefaultField.toISOString()); | ||
done(); | ||
}); | ||
}); | ||
it('should find model instance by datetime field', function(done) { | ||
DateModel.findOne({ | ||
where: { | ||
datetimeField: date | ||
} | ||
}, function(err, found) { | ||
assert.ok(!err); | ||
assert.ok(found); | ||
assert.equal(found.id, 1); | ||
done(); | ||
}); | ||
}); | ||
it('should find model instance by timestamp field', function(done) { | ||
DateModel.findOne({ | ||
where: { | ||
timestampField: date | ||
} | ||
}, function(err, found) { | ||
assert.ok(!err); | ||
assert.ok(found); | ||
assert.equal(found.id, 1); | ||
done(); | ||
}); | ||
}); | ||
it('should find model instance by date field', function(done) { | ||
DateModel.findOne({ | ||
where: { | ||
dateField: dateOnly | ||
} | ||
}, function(err, found) { | ||
assert.ok(!err); | ||
assert.ok(found); | ||
assert.equal(found.id, 1); | ||
done(); | ||
}); | ||
}); | ||
it('set timezone to UTC +00:00', function(done) { | ||
query("SET @@session.time_zone = '+00:00'", function(err) { | ||
assert.ok(!err); | ||
done(); | ||
}); | ||
}); | ||
it('now datetime and timestamp field should be different in 330 minutes 5:30 - 0:00', function(done) { | ||
DateModel.findOne({ | ||
where: { | ||
id: 1 | ||
} | ||
}, function(err, found) { | ||
assert.ok(!err); | ||
var diff = (found.datetimeField.getTime() - found.timestampField.getTime()) / 60000; | ||
assert.equal(diff, 330); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should disconnect when done', function(done) { | ||
db.disconnect(); | ||
done(); | ||
});}); | ||
|
||
var prepareModel = function(tz, migrate, done) { | ||
db = getSchema({timezone:tz}); | ||
DateModel = db.define('DateModel', { | ||
id: {type:Number, id:1, generated: true}, | ||
datetimeField: {type: Date, dataType: 'datetime', null: false}, | ||
timestampField: {type: Date, dataType: 'timestamp', null: false}, | ||
timestampDefaultField: {type: Date, dataType: 'timestamp', null: false}, | ||
dateField: {type: Date, dataType: 'date', null: false}, | ||
}); | ||
if (migrate) { | ||
db.automigrate('DateModel', function() { | ||
//SET DEFAULT CURRENT_TIMESTAMP for timestampDefaultField | ||
query("ALTER TABLE `DateModel` CHANGE COLUMN `timestampDefaultField` `timestampDefaultField` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP;", done); | ||
}); | ||
} else { | ||
done(); | ||
} | ||
} | ||
|
||
var query = function(sql, cb) { | ||
db.adapter.execute(sql, cb); | ||
}; | ||
|
||
function getTimeZone() { | ||
var offset = new Date().getTimezoneOffset(), o = Math.abs(offset); | ||
return (offset < 0 ? "+" : "-") + ("00" + Math.floor(o / 60)).slice(-2) + ":" + ("00" + (o % 60)).slice(-2); | ||
} |
Oops, something went wrong.