-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow storing TimeSpans as strings (default to ticks)
- Loading branch information
Showing
4 changed files
with
131 additions
and
12 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
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
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
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,85 @@ | ||
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)); | ||
TestTimeSpan (db); | ||
} | ||
|
||
[Test] | ||
public void AsStrings () | ||
{ | ||
var db = new TestDb (TimeSpanAsTicks (false)); | ||
TestTimeSpan (db); | ||
} | ||
|
||
[Test] | ||
public void AsyncAsTicks () | ||
{ | ||
var db = new SQLiteAsyncConnection (TimeSpanAsTicks (true)); | ||
TestAsyncTimeSpan (db); | ||
} | ||
|
||
[Test] | ||
public void AsyncAsStrings () | ||
{ | ||
var db = new SQLiteAsyncConnection (TimeSpanAsTicks (false)); | ||
TestAsyncTimeSpan (db); | ||
} | ||
|
||
SQLiteConnectionString TimeSpanAsTicks (bool asTicks = true) => new SQLiteConnectionString (TestPath.GetTempFileName (), SQLiteOpenFlags.Create | SQLiteOpenFlags.ReadWrite, true, storeTimeSpanAsTicks: asTicks); | ||
|
||
void TestAsyncTimeSpan (SQLiteAsyncConnection db) | ||
{ | ||
db.CreateTableAsync<TestObj> ().Wait (); | ||
|
||
TestObj o, o2; | ||
|
||
o = new TestObj { | ||
Duration = new TimeSpan (42, 12, 33, 20, 501), | ||
}; | ||
db.InsertAsync (o).Wait (); | ||
o2 = db.GetAsync<TestObj> (o.Id).Result; | ||
Assert.AreEqual (o.Duration, o2.Duration); | ||
} | ||
|
||
void TestTimeSpan (TestDb db) | ||
{ | ||
db.CreateTable<TestObj> (); | ||
|
||
TestObj o, o2; | ||
|
||
o = new TestObj { | ||
Duration = new TimeSpan (42, 12, 33, 20, 501), | ||
}; | ||
db.Insert (o); | ||
o2 = db.Get<TestObj> (o.Id); | ||
Assert.AreEqual (o.Duration, o2.Duration); | ||
} | ||
} | ||
} |
cf41ec3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like it defaults to strings, not to ticks. Or am I missing something?
This change broke my code as I was saving timespans as ticks, now the code tries to parse them as strings.
I am creating the database connection like this:
var _db = new SQLiteConnection(databasePath, SQLiteOpenFlags.Create | SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.FullMutex);