|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Data.SqlClient; |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | +using NUnit.Framework; |
| 7 | +using ServiceStack.DataAnnotations; |
| 8 | +using ServiceStack.OrmLite.SqlServer; |
| 9 | + |
| 10 | +namespace ServiceStack.OrmLite.SqlServerTests |
| 11 | +{ |
| 12 | + public class Datetime2Tests : SqlServerTests.OrmLiteTestBase |
| 13 | + { |
| 14 | + [Test] |
| 15 | + public void datetime_tests__can_use_datetime2() |
| 16 | + { |
| 17 | + var dbFactory = new OrmLiteConnectionFactory(base.ConnectionString, SqlServerOrmLiteDialectProvider.Instance); |
| 18 | + |
| 19 | + //change to datetime2 - check for higher range and precision |
| 20 | + (SqlServerOrmLiteDialectProvider.Instance as SqlServerOrmLiteDialectProvider).UseDatetime2(true); |
| 21 | + |
| 22 | + using(var conn = dbFactory.OpenDbConnection()) { |
| 23 | + var test_object_ValidForDatetime2 = Table_for_datetime2_tests.get_test_object_ValidForDatetime2(); |
| 24 | + var test_object_ValidForNormalDatetime = Table_for_datetime2_tests.get_test_object_ValidForNormalDatetime(); |
| 25 | + |
| 26 | + conn.CreateTable<Table_for_datetime2_tests>(true); |
| 27 | + |
| 28 | + //normal insert |
| 29 | + conn.Insert(test_object_ValidForDatetime2); |
| 30 | + var insertedId = (int)conn.GetLastInsertId(); |
| 31 | + |
| 32 | + //read back, and verify precision |
| 33 | + var fromDb = conn.GetById<Table_for_datetime2_tests>(insertedId); |
| 34 | + Assert.AreEqual(test_object_ValidForDatetime2.ToVerifyPrecision, fromDb.ToVerifyPrecision); |
| 35 | + |
| 36 | + //update |
| 37 | + fromDb.ToVerifyPrecision = test_object_ValidForDatetime2.ToVerifyPrecision.Value.AddYears(1); |
| 38 | + conn.UpdateParam(fromDb); |
| 39 | + var fromDb2 = conn.GetById<Table_for_datetime2_tests>(insertedId); |
| 40 | + Assert.AreEqual(test_object_ValidForDatetime2.ToVerifyPrecision.Value.AddYears(1), fromDb2.ToVerifyPrecision); |
| 41 | + |
| 42 | + |
| 43 | + //check InsertParam |
| 44 | + conn.InsertParam(test_object_ValidForDatetime2); |
| 45 | + } |
| 46 | + } |
| 47 | + [Test] |
| 48 | + public void datetime_tests__check_default_behaviour() |
| 49 | + { |
| 50 | + var dbFactory = new OrmLiteConnectionFactory(base.ConnectionString, SqlServerOrmLiteDialectProvider.Instance); |
| 51 | + |
| 52 | + //default behaviour: normal datetime can't hold DateTime values of year 1. |
| 53 | + (SqlServerOrmLiteDialectProvider.Instance as SqlServerOrmLiteDialectProvider).UseDatetime2(false); |
| 54 | + |
| 55 | + using(var conn = dbFactory.OpenDbConnection()) { |
| 56 | + var test_object_ValidForDatetime2 = Table_for_datetime2_tests.get_test_object_ValidForDatetime2(); |
| 57 | + var test_object_ValidForNormalDatetime = Table_for_datetime2_tests.get_test_object_ValidForNormalDatetime(); |
| 58 | + |
| 59 | + conn.CreateTable<Table_for_datetime2_tests>(true); |
| 60 | + |
| 61 | + //normal insert |
| 62 | + conn.Insert(test_object_ValidForNormalDatetime); |
| 63 | + var insertedId = conn.GetLastInsertId(); |
| 64 | + |
| 65 | + //insert works, but can't regular datetime's precision is not great enough. |
| 66 | + var fromDb = conn.GetById<Table_for_datetime2_tests>(insertedId); |
| 67 | + Assert.AreNotEqual(test_object_ValidForNormalDatetime.ToVerifyPrecision, fromDb.ToVerifyPrecision); |
| 68 | + |
| 69 | + var thrown = Assert.Throws<SqlException>(() => { |
| 70 | + conn.Insert(test_object_ValidForDatetime2); |
| 71 | + }); |
| 72 | + Assert.That(thrown.Message.Contains("The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.")); |
| 73 | + |
| 74 | + |
| 75 | + //check InsertParam |
| 76 | + conn.InsertParam(test_object_ValidForNormalDatetime); |
| 77 | + //InsertParam fails differently: |
| 78 | + var insertParamException = Assert.Throws<System.Data.SqlTypes.SqlTypeException>(() => { |
| 79 | + conn.InsertParam(test_object_ValidForDatetime2); |
| 80 | + }); |
| 81 | + Assert.That(insertParamException.Message.Contains("SqlDateTime overflow")); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private class Table_for_datetime2_tests |
| 86 | + { |
| 87 | + [AutoIncrement] |
| 88 | + public int Id { get; set; } |
| 89 | + public DateTime SomeDateTime { get; set; } |
| 90 | + public DateTime? ToVerifyPrecision { get; set; } |
| 91 | + public DateTime? NullableDateTimeLeaveItNull { get; set; } |
| 92 | + |
| 93 | + /// <summary> |
| 94 | + /// to check datetime(2)'s precision. A regular 'datetime' is not precise enough |
| 95 | + /// </summary> |
| 96 | + public static readonly DateTime regular_datetime_field_cant_hold_this_exact_moment = new DateTime(2013, 3, 17, 21, 29, 1, 678); |
| 97 | + |
| 98 | + public static Table_for_datetime2_tests get_test_object_ValidForDatetime2() { return new Table_for_datetime2_tests { SomeDateTime = new DateTime(1, 1, 1), ToVerifyPrecision = regular_datetime_field_cant_hold_this_exact_moment }; } |
| 99 | + |
| 100 | + public static Table_for_datetime2_tests get_test_object_ValidForNormalDatetime() { return new Table_for_datetime2_tests { SomeDateTime = new DateTime(2001, 1, 1), ToVerifyPrecision = regular_datetime_field_cant_hold_this_exact_moment }; } |
| 101 | + |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments