Reputation: 16352
When I run this code:
var data = "test"; var timestamp = DateTime.Now; var id = repo.InsertNewRecord(data, timestamp); var record = repo.GetRecord(id); Assert.AreEqual(data, record.Data); Assert.AreEqual(timestamp, record.TimeStamp);
... it fails on the second assertion.
The expected timestamp has a tick count of 635449135145624472, but the value I'm receiving from the database has a tick count of 635449135140000000. This is understandable because .NET DateTime values have an accuracy of 100ns while SQL Server DateTime only has a accuracy of 3.33ms. I don't really need to be the timestamps to be more accurate than that, but is there a standard way of testing the inputs and outputs for equality?
Obtaining a timestamp from the database is one way to do it:
var timestamp = Session.CreateSQLQuery("SELECT GetDate()").UniqueResult<DateTime>();
Upvotes: 0
Views: 304
Reputation: 82096
Don't test at tick level, you could use a particular format to test the dates e.g.
var dateFmt = "dd-MM-yyyy hh:mm:ss";
Assert.AreEqual(timestamp.ToString(dateFmt), record.TimeStamp.ToString(dateFmt));
Or alternatively, you could do that but just add some room for variance
Assert.True((timeStamp - record.TimeStamp).TotalMilliseconds < 1000);
Upvotes: 2