Commit 71ec12da authored by Mikaël Salson's avatar Mikaël Salson

model.js: Make difference in dates work with undefined timestamps

Following 42d7ed86 and #2101 we ensure that such a problem won't appear again
by checking in dateDiffMinMax whether the dates can be substracted or not.
When dates can't be substracted the result will be NaN, which can be tested
and an exception raised.

Tests are done to ensure the correct behaviour.
parent 42d7ed86
......@@ -1433,6 +1433,10 @@ changeCloneNotation: function(cloneNotationType) {
for (var i = 1; i < this.samples.order.length; i++) {
t = this.samples.timestamp[this.samples.order[i]]
delta = this.dateDiffInDays(previous_t, t)
if (isNaN(delta)) {
throw TypeError("Are "+previous_t+" and "+t+" really dates?")
}
if (delta > delta_max)
delta_max = delta
......
......@@ -93,6 +93,14 @@ test("model : time control", function() {
equal(m.getStrTime(0, "sampling_date"), "2014-10-20", "get sampling date")
equal(m.getStrTime(0, "name"), "Diag", "get time original name")
equal(m.dateDiffInDays("2014-10-05", "2014-10-10"), "+5", "datediffindays")
ok(isNaN(m.dateDiffInDays("2014-10-05", "toto")), "datediffindays with a string")
deepEqual(m.dateDiffMinMax(), {'min': 5 , 'max': 30}, "dateDiffMinMax (min = " + m.dateDiffMinMax()['min']+", max = " + m.dateDiffMinMax()['max']+")")
var second_timestamp = m.samples.timestamp[1]
m.samples.timestamp[1] = 'toto'
deepEqual(m.dateDiffMinMax(), {'min': -1, 'max': -1}, 'dateDiffMinMax with undefined timestamp')
m.samples.timestamp[1] = second_timestamp
equal(m.getStrTime(1, "delta_date"), "+5", "get day since diag")
});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment