Skip to content
This repository has been archived by the owner on Feb 15, 2021. It is now read-only.

Commit

Permalink
Fix several issues with SQLite compatibility with NET46
Browse files Browse the repository at this point in the history
  • Loading branch information
geoperez committed Nov 20, 2017
1 parent f3581d8 commit ed55ef6
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 49 deletions.
4 changes: 4 additions & 0 deletions src/Unosquare.Labs.LiteLib/LiteDbContext.cs
Expand Up @@ -16,6 +16,8 @@
using Swan.Reflection;
#if MONO
using Mono.Data.Sqlite;
#elif NET46
using System.Data.SQLite;
#else
using Microsoft.Data.Sqlite;
#endif
Expand Down Expand Up @@ -58,6 +60,8 @@ protected LiteDbContext(string databaseFilePath, bool enabledLog = true)
var databaseExists = File.Exists(databaseFilePath);
#if MONO
Connection = new SqliteConnection($"URI=file:{databaseFilePath}");
#elif NET46
Connection = new SQLiteConnection($"URI=file:{databaseFilePath}");
#else
var builder = new SqliteConnectionStringBuilder
{
Expand Down
8 changes: 4 additions & 4 deletions src/Unosquare.Labs.LiteLib/Unosquare.Labs.LiteLib.csproj
Expand Up @@ -9,7 +9,7 @@
<PackageId>LiteLib</PackageId>
<DebugType>Full</DebugType>
<CodeAnalysisRuleSet>..\..\StyleCop.Analyzers.ruleset</CodeAnalysisRuleSet>
<Version>0.14.3</Version>
<Version>0.15.0</Version>
<Authors>Unosquare</Authors>
<Company>Unosquare</Company>
<Copyright>Copyright (c) 2016-2017 Unosquare</Copyright>
Expand All @@ -20,6 +20,7 @@
<PackageIconUrl>https://raw.githubusercontent.com/unosquare/litelib/master/litelib-logo.png</PackageIconUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>orm sqlite dapper</PackageTags>
<ContentSQLiteInteropFiles>true</ContentSQLiteInteropFiles>
</PropertyGroup>

<ItemGroup>
Expand All @@ -28,12 +29,11 @@
</PackageReference>
<PackageReference Include="Dapper" Version="1.50.2" />
<PackageReference Include="System.ComponentModel.Annotations" Version="4.4.0" />
<PackageReference Include="Unosquare.Swan" Version="0.17.1" />
<PackageReference Include="Unosquare.Swan" Version="0.18.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net46' ">
<PackageReference Include="Microsoft.Data.Sqlite" Version="1.0.1" />
<PackageReference Include="SQLite" Version="3.13.0" />
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.106" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
Expand Down
35 changes: 25 additions & 10 deletions test/Unosquare.Labs.LiteLib.Tests/DbContextAsyncFixture.cs
@@ -1,9 +1,13 @@
using Microsoft.Data.Sqlite;
using NUnit.Framework;
using NUnit.Framework;
using System.Linq;
using System.Threading.Tasks;
using Unosquare.Labs.LiteLib.Tests.Database;
using Unosquare.Labs.LiteLib.Tests.Helpers;
#if NET46
using System.Data.SQLite;
#else
using Microsoft.Data.Sqlite;
#endif

namespace Unosquare.Labs.LiteLib.Tests
{
Expand Down Expand Up @@ -42,7 +46,7 @@ public async Task SelectWithParametersAsync()

// Selecting Data By name
var selectingData =
await context.Orders.SelectAsync("CustomerName = @CustomerName", new { CustomerName = "Peter" });
await context.Orders.SelectAsync("CustomerName = @CustomerName", new {CustomerName = "Peter"});

foreach (var item in selectingData)
{
Expand All @@ -63,7 +67,7 @@ public async Task SelectDataAsync()

// Selecting Data By name
var selectingData =
await context.Orders.SelectAsync("CustomerName = @CustomerName", new { CustomerName = "Peter" });
await context.Orders.SelectAsync("CustomerName = @CustomerName", new {CustomerName = "Peter"});

foreach (var item in selectingData)
{
Expand Down Expand Up @@ -123,7 +127,7 @@ public async Task DeletingAsyncWithParams()
}

var deletedData =
await context.Orders.DeleteAsync("CustomerName = @CustomerName", new { CustomerName = "Peter" });
await context.Orders.DeleteAsync("CustomerName = @CustomerName", new {CustomerName = "Peter"});
Assert.AreEqual(deletedData, TestHelper.DataSource.Count(x => x.CustomerName == "Peter"));
}
}
Expand Down Expand Up @@ -163,8 +167,11 @@ public async Task InsertAsyncUsingUnique_ThrowsSqliteException()
{
await context.Orders.InsertAsync(item);
}

#if NET46
Assert.ThrowsAsync<SQLiteException>(async () =>
#else
Assert.ThrowsAsync<SqliteException>(async () =>
#endif
{
var newOrder = new Order
{
Expand All @@ -173,6 +180,7 @@ public async Task InsertAsyncUsingUnique_ThrowsSqliteException()
ShipperCity = "Atlanta",
UniqueId = "1"
};
await context.Orders.InsertAsync(newOrder);
});
}
Expand All @@ -183,7 +191,11 @@ public async Task InsertAsyncWithOutOfRangeString_ThrowsSqliteException()
{
using (var context = new TestDbContext(nameof(InsertAsyncWithOutOfRangeString_ThrowsSqliteException)))
{
#if NET46
Assert.ThrowsAsync<SQLiteException>(async () =>
#else
Assert.ThrowsAsync<SqliteException>(async () =>
#endif
{
await context.Orders.InsertAsync(new Order
{
Expand All @@ -209,15 +221,17 @@ public async Task AsyncUpdateData()
}

var list = await context.Orders.SelectAsync("CustomerName = @CustomerName",
new { CustomerName = "John" });
new {CustomerName = "John"});

foreach (var item in list)
{
item.ShipperCity = "Atlanta";
await context.Orders.UpdateAsync(item);
}

var updatedList =
await context.Orders.SelectAsync("ShipperCity = @ShipperCity", new { ShipperCity = "Atlanta" });
await context.Orders.SelectAsync("ShipperCity = @ShipperCity", new {ShipperCity = "Atlanta"});

foreach (var item in updatedList)
{
Assert.AreEqual("Atlanta", item.ShipperCity);
Expand Down Expand Up @@ -314,8 +328,9 @@ public async Task AsyncUpdateFromSetname()
item.ShipperCity = "Atlanta";
await context.UpdateAsync(item);
}

var updatedItems =
await context.Orders.SelectAsync("ShipperCity = @ShipperCity", new { ShipperCity = "Atlanta" });
await context.Orders.SelectAsync("ShipperCity = @ShipperCity", new {ShipperCity = "Atlanta"});
Assert.AreEqual(TestHelper.DataSource.Length, updatedItems.Count());
}
}
Expand All @@ -337,7 +352,7 @@ public async Task AsyncQueryData()
await
context.QueryAsync<Order>(
$"{context.Orders.SelectDefinition} WHERE CustomerName = @CustomerName",
new Order { CustomerName = "John" });
new Order {CustomerName = "John"});

foreach (var item in selectedData)
{
Expand Down
72 changes: 50 additions & 22 deletions test/Unosquare.Labs.LiteLib.Tests/DbContextFixture.cs
Expand Up @@ -5,7 +5,9 @@
using Unosquare.Labs.LiteLib.Tests.Database;
using Unosquare.Labs.LiteLib.Tests.Helpers;
#if MONO
using Mono.Data.Sqlite;
using Mono.Data.Sqlite;
#elif NET46
using System.Data.SQLite;
#else
using Microsoft.Data.Sqlite;
#endif
Expand Down Expand Up @@ -48,7 +50,7 @@ public void SelectDataWithParameters()
context.Orders.Insert(item);
}

var entities = context.Orders.Select("CustomerName = @CustomerName", new { CustomerName = "John" });
var entities = context.Orders.Select("CustomerName = @CustomerName", new {CustomerName = "John"});
foreach (var item in entities)
{
Assert.AreEqual("John", item.CustomerName);
Expand All @@ -65,14 +67,17 @@ public void SelectWithBadParameters_ThrowsException()
{
context.Orders.Insert(item);
}

#if NET46
Assert.Throws<SQLiteException>(() =>
#else
Assert.Throws<SqliteException>(() =>
#endif
{
var entities = context.Orders.Select("Customer = @CustomerName", new { CustomerName = "John" });
var entities = context.Orders.Select("Customer = @CustomerName", new {CustomerName = "John"});
});
}
}

[Test]
public void SelectingFirstOrDefault()
{
Expand Down Expand Up @@ -119,7 +124,8 @@ public void DeletingUsingParams()
context.Orders.Insert(item);
}

var deletedData = context.Orders.Delete("CustomerName = @CustomerName", new { CustomerName = "Peter" });
var deletedData =
context.Orders.Delete("CustomerName = @CustomerName", new {CustomerName = "Peter"});
Assert.AreEqual(deletedData, TestHelper.DataSource.Count(x => x.CustomerName == "Peter"));
}
}
Expand All @@ -134,8 +140,14 @@ public void DeletingUsingInvalidParams_TrhowsSqliteException()
context.Orders.Insert(item);
}

Assert.Throws<SqliteException>(() => {
var deletedData = context.Orders.Delete("Customer = @CustomerName", new { CustomerName = "Peter" });
#if NET46
Assert.Throws<SQLiteException>(() =>
#else
Assert.Throws<SqliteException>(() =>
#endif
{
var deletedData =
context.Orders.Delete("Customer = @CustomerName", new {CustomerName = "Peter"});
});
}
}
Expand Down Expand Up @@ -165,7 +177,11 @@ public void InsertUsingUnique_ThrowsSqliteException()
context.Orders.Insert(item);
}

Assert.Throws<SqliteException>(delegate
#if NET46
Assert.Throws<SQLiteException>(() =>
#else
Assert.Throws<SqliteException>(() =>
#endif
{
var newOrder = new Order
{
Expand All @@ -178,13 +194,17 @@ public void InsertUsingUnique_ThrowsSqliteException()
});
}
}

[Test]
public void InsertingWithOutOfRangeString_ThrowsSqliteException()
{
using (var context = new TestDbContext(nameof(InsertingWithOutOfRangeString_ThrowsSqliteException)))
{
Assert.Throws<SqliteException>(delegate
#if NET46
Assert.Throws<SQLiteException>(() =>
#else
Assert.Throws<SqliteException>(() =>
#endif
{
context.Orders.Insert(new Order
{
Expand Down Expand Up @@ -213,7 +233,7 @@ public void InsertingDataList()
[Test]
public void InsertingEmptyDataList_TrhowsArgumentException()
{
Assert.Throws(typeof(ArgumentNullException), () =>
Assert.Throws<ArgumentNullException>(() =>
{
using (var context = new TestDbContext(nameof(InsertingEmptyDataList_TrhowsArgumentException)))
{
Expand All @@ -235,14 +255,15 @@ public void UpdatingEntities()
context.Orders.Insert(item);
}

var list = context.Orders.Select("CustomerName = @CustomerName", new { CustomerName = "John" });
var list = context.Orders.Select("CustomerName = @CustomerName", new {CustomerName = "John"});
foreach (var item in list)
{
item.ShipperCity = "Atlanta";
context.Orders.Update(item);
}

var updatedList = context.Orders.Select("ShipperCity = @ShipperCity", new { ShipperCity = "Atlanta" });
var updatedList =
context.Orders.Select("ShipperCity = @ShipperCity", new {ShipperCity = "Atlanta"});
foreach (var item in updatedList)
{
Assert.AreEqual("Atlanta", item.ShipperCity);
Expand Down Expand Up @@ -298,7 +319,7 @@ public void SetEntity()
{
var names = context.GetSetNames();
Assert.IsNotNull(names);
Assert.AreEqual(names, new[] { nameof(context.Orders), nameof(context.Warehouses) });
Assert.AreEqual(names, new[] {nameof(context.Orders), nameof(context.Warehouses)});

var orders = context.Set<Order>();
var ordersByName = context.Set(typeof(Order));
Expand Down Expand Up @@ -330,7 +351,7 @@ public void SelectFromSetname()
[Test]
public void InvalidSetname_ThrowsArgumentOutOfRangeException()
{
Assert.Throws(typeof(System.ArgumentOutOfRangeException), () =>
Assert.Throws<ArgumentOutOfRangeException>(() =>
{
using (var context = new TestDbContext(nameof(InvalidSetname_ThrowsArgumentOutOfRangeException)))
{
Expand Down Expand Up @@ -389,12 +410,13 @@ public void UpdateFromSetname()
item.ShipperCity = "Atlanta";
context.Update(item);
}
var updatedItems = context.Orders.Select("ShipperCity = @ShipperCity", new { ShipperCity = "Atlanta" });
var updatedItems =
context.Orders.Select("ShipperCity = @ShipperCity", new {ShipperCity = "Atlanta"});
Assert.AreEqual(TestHelper.DataSource.Length, updatedItems.Count());
}
}
}

public class Qerytest : DbContextFixture
{
[Test]
Expand All @@ -414,7 +436,7 @@ public void SelectingData()

var selectedData =
context.Query<Order>($"{context.Orders.SelectDefinition} WHERE CustomerName = @CustomerName",
new Order { CustomerName = "Margarita" });
new Order {CustomerName = "Margarita"});

foreach (var item in selectedData)
{
Expand All @@ -438,10 +460,16 @@ public void UsingBadQuery_ThrowsSqliteException()
}
}

Assert.Throws<SqliteException>(() => {
#if NET46
Assert.Throws<SQLiteException>(() =>
#else
Assert.Throws<SqliteException>(() =>
#endif
{
var selectedData =
context.Query<Order>($"{context.Orders.UpdateDefinition} WHERE CustomerName = @CustomerName",
new Order { CustomerName = "Margarita" });
context.Query<Order>(
$"{context.Orders.UpdateDefinition} WHERE CustomerName = @CustomerName",
new Order {CustomerName = "Margarita"});
});
}
Expand Down
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp2.0;net46</TargetFrameworks>
<TargetFrameworks>net46;netcoreapp2.0</TargetFrameworks>
<AssemblyName>Unosquare.Labs.LiteLib.Tests</AssemblyName>
<PackageId>Unosquare.Labs.LiteLib.Tests</PackageId>
</PropertyGroup>
Expand All @@ -11,18 +11,9 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
<PackageReference Include="dotnet-test-nunit" Version="3.4.0-beta-1" />
<PackageReference Include="NUnit" Version="3.8.1" />
<PackageReference Include="NUnit3TestAdapter" Version="3.8.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net46' ">
<PackageReference Include="Microsoft.Data.Sqlite" Version="1.0.1" />
<PackageReference Include="SQLite" Version="3.13.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
<PackageReference Include="Microsoft.Data.Sqlite" Version="2.0.0" />
<PackageReference Include="NUnit" Version="3.9.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.9.0" />
</ItemGroup>
</Project>

0 comments on commit ed55ef6

Please sign in to comment.