forked from praeclarum/sqlite-net
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathTimeSpanTest.cs
More file actions
95 lines (79 loc) · 2.5 KB
/
TimeSpanTest.cs
File metadata and controls
95 lines (79 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using System;
using System.Threading.Tasks;
#if NETFX_CORE
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
using SetUp = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestInitializeAttribute;
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
#else
using NUnit.Framework;
#endif
namespace SQLite.Tests
{
[TestFixture]
public class TimeSpanTest
{
class TestObj
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string Name { get; set; }
public TimeSpan Duration { get; set; }
}
[Test]
public void AsTicks ()
{
var db = new TestDb (TimeSpanAsTicks (true));
var span = new TimeSpan (42, 12, 33, 20, 501);
TestTimeSpan (db, span, span.Ticks.ToString ());
}
[Test]
public void AsStrings ()
{
var db = new TestDb (TimeSpanAsTicks (false));
var span = new TimeSpan (42, 12, 33, 20, 501);
TestTimeSpan (db, span, span.ToString ());
}
[Test]
public void AsyncAsTicks ()
{
var db = new SQLiteAsyncConnection (TimeSpanAsTicks (true));
var span = new TimeSpan (42, 12, 33, 20, 501);
TestAsyncTimeSpan (db, span, span.Ticks.ToString ());
}
[Test]
public void AsyncAsStrings ()
{
var db = new SQLiteAsyncConnection (TimeSpanAsTicks (false));
var span = new TimeSpan (42, 12, 33, 20, 501);
TestAsyncTimeSpan (db, span, span.ToString ());
}
SQLiteConnectionString TimeSpanAsTicks (bool asTicks = true) => new SQLiteConnectionString (TestPath.GetTempFileName (), SQLiteOpenFlags.Create | SQLiteOpenFlags.ReadWrite, true, storeTimeSpanAsTicks: asTicks);
void TestAsyncTimeSpan (SQLiteAsyncConnection db, TimeSpan duration, string expected)
{
db.CreateTableAsync<TestObj> ().Wait ();
TestObj o, o2;
o = new TestObj {
Duration = duration,
};
db.InsertAsync (o).Wait ();
o2 = db.GetAsync<TestObj> (o.Id).Result;
Assert.AreEqual (o.Duration, o2.Duration);
var stored = db.ExecuteScalarAsync<string> ("SELECT Duration FROM TestObj;").Result;
Assert.AreEqual (expected, stored);
}
void TestTimeSpan (TestDb db, TimeSpan duration, string expected)
{
db.CreateTable<TestObj> ();
TestObj o, o2;
o = new TestObj {
Duration = duration,
};
db.Insert (o);
o2 = db.Get<TestObj> (o.Id);
Assert.AreEqual (o.Duration, o2.Duration);
var stored = db.ExecuteScalar<string> ("SELECT Duration FROM TestObj;");
Assert.AreEqual (expected, stored);
}
}
}