Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 9e2948b

Browse files
committed
Add test showing how to open a readonly database
1 parent 58af46e commit 9e2948b

File tree

3 files changed

+185
-1
lines changed

3 files changed

+185
-1
lines changed
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
using System;
2+
using ServiceStack.DataAnnotations;
3+
4+
namespace ServiceStack.OrmLite.SqliteTests
5+
{
6+
public class Category
7+
{
8+
public int Id { get; set; }
9+
public string CategoryName { get; set; }
10+
public string Description { get; set; }
11+
}
12+
13+
public class Customer
14+
{
15+
public string Id { get; set; }
16+
public string CompanyName { get; set; }
17+
public string ContactName { get; set; }
18+
public string ContactTitle { get; set; }
19+
public string Address { get; set; }
20+
public string City { get; set; }
21+
public string Region { get; set; }
22+
public string PostalCode { get; set; }
23+
public string Country { get; set; }
24+
public string Phone { get; set; }
25+
public string Fax { get; set; }
26+
}
27+
28+
public class Employee
29+
{
30+
public int Id { get; set; }
31+
public string LastName { get; set; }
32+
public string FirstName { get; set; }
33+
public string Title { get; set; }
34+
public string TitleOfCourtesy { get; set; }
35+
public DateTime? BirthDate { get; set; }
36+
public DateTime? HireDate { get; set; }
37+
public string Address { get; set; }
38+
public string City { get; set; }
39+
public string Region { get; set; }
40+
public string PostalCode { get; set; }
41+
public string Country { get; set; }
42+
public string HomePhone { get; set; }
43+
public string Extension { get; set; }
44+
public byte[] Photo { get; set; }
45+
46+
[StringLength(StringLengthAttribute.MaxText)]
47+
public string Notes { get; set; }
48+
49+
public int? ReportsTo { get; set; }
50+
public string PhotoPath { get; set; }
51+
}
52+
53+
public class EmployeeTerritory
54+
{
55+
public string Id => $"{EmployeeId}/{TerritoryId}";
56+
public int EmployeeId { get; set; }
57+
public string TerritoryId { get; set; }
58+
}
59+
60+
public class Order
61+
{
62+
public int Id { get; set; }
63+
public string CustomerId { get; set; }
64+
public int EmployeeId { get; set; }
65+
public DateTime? OrderDate { get; set; }
66+
public DateTime? RequiredDate { get; set; }
67+
public DateTime? ShippedDate { get; set; }
68+
public int? ShipVia { get; set; }
69+
public decimal Freight { get; set; }
70+
public string ShipName { get; set; }
71+
public string ShipAddress { get; set; }
72+
public string ShipCity { get; set; }
73+
public string ShipRegion { get; set; }
74+
public string ShipPostalCode { get; set; }
75+
public string ShipCountry { get; set; }
76+
}
77+
78+
public class OrderDetail
79+
{
80+
public string Id => $"{OrderId}/{ProductId}";
81+
public int OrderId { get; set; }
82+
public int ProductId { get; set; }
83+
public decimal UnitPrice { get; set; }
84+
public short Quantity { get; set; }
85+
public double Discount { get; set; }
86+
}
87+
88+
public class Product
89+
{
90+
public int Id { get; set; }
91+
public string ProductName { get; set; }
92+
public int SupplierId { get; set; }
93+
public int CategoryId { get; set; }
94+
public string QuantityPerUnit { get; set; }
95+
public decimal UnitPrice { get; set; }
96+
public short UnitsInStock { get; set; }
97+
public short UnitsOnOrder { get; set; }
98+
public short ReorderLevel { get; set; }
99+
public bool Discontinued { get; set; }
100+
}
101+
102+
public class Region
103+
{
104+
public int Id { get; set; }
105+
public string RegionDescription { get; set; }
106+
}
107+
108+
public class Shipper
109+
{
110+
public int Id { get; set; }
111+
public string CompanyName { get; set; }
112+
public string Phone { get; set; }
113+
}
114+
115+
public class Supplier
116+
{
117+
public int Id { get; set; }
118+
public string CompanyName { get; set; }
119+
public string ContactName { get; set; }
120+
public string ContactTitle { get; set; }
121+
public string Address { get; set; }
122+
public string City { get; set; }
123+
public string Region { get; set; }
124+
public string PostalCode { get; set; }
125+
public string Country { get; set; }
126+
public string Phone { get; set; }
127+
public string Fax { get; set; }
128+
public string HomePage { get; set; }
129+
}
130+
131+
public class Territory
132+
{
133+
public string Id { get; set; }
134+
public string TerritoryDescription { get; set; }
135+
public int RegionId { get; set; }
136+
}
137+
}

tests/ServiceStack.OrmLite.SqliteTests/ServiceStack.OrmLite.SqliteTests.csproj

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@
2626
<ItemGroup>
2727
<PackageReference Include="NUnit" Version="3.6.1" />
2828
</ItemGroup>
29-
29+
3030
<PropertyGroup Condition=" '$(TargetFramework)' == 'net45' ">
3131
<DefineConstants>$(DefineConstants);NET45</DefineConstants>
3232
</PropertyGroup>
3333

3434
<ItemGroup Condition=" '$(TargetFramework)' == 'net45' ">
35+
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.105.2" />
3536
<Reference Include="System.Configuration" />
3637
<Reference Include="System.Threading" />
3738
<Reference Include="System.Threading.Tasks" />
@@ -66,6 +67,7 @@
6667
</ItemGroup>
6768

6869
<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp2.0' ">
70+
<PackageReference Include="Microsoft.Data.Sqlite" Version="1.1.1" />
6971
<PackageReference Include="Microsoft.Extensions.Primitives" Version="1.1.1" />
7072
<PackageReference Include="System.Runtime" Version="4.*" />
7173
<PackageReference Include="System.Runtime.Serialization.Xml" Version="4.*" />
@@ -80,4 +82,8 @@
8082
<Reference Include="..\..\lib\netstandard1.6\ServiceStack.dll" />
8183
</ItemGroup>
8284

85+
<ItemGroup>
86+
<Folder Include="App_Data\" />
87+
</ItemGroup>
88+
8389
</Project>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
using NUnit.Framework;
3+
using ServiceStack.OrmLite.Tests;
4+
using ServiceStack.Text;
5+
6+
namespace ServiceStack.OrmLite.SqliteTests
7+
{
8+
public class DisableWrites
9+
{
10+
public int Id { get; set; }
11+
}
12+
13+
class SqliteReadOnlyTests : OrmLiteTestBase
14+
{
15+
[Test]
16+
public void Can_open_readonly_connection_to_file_database()
17+
{
18+
var dbPath = "~/App_Data/northwind.sqlite".MapProjectPlatformPath();
19+
var connectionString = "Data Source=" + dbPath + ";Read Only=true";
20+
connectionString.Print();
21+
var dbFactory = new OrmLiteConnectionFactory(connectionString, SqliteDialect.Provider);
22+
23+
using (var db = dbFactory.OpenDbConnection())
24+
{
25+
var count = db.Count<Customer>();
26+
Assert.That(count, Is.GreaterThan(1));
27+
28+
try
29+
{
30+
db.DropAndCreateTable<DisableWrites>();
31+
Assert.Fail("should thow");
32+
}
33+
catch (Exception ex)
34+
{
35+
Assert.That(ex.Message, Does.Contain("attempt to write a readonly database"));
36+
}
37+
}
38+
}
39+
40+
}
41+
}

0 commit comments

Comments
 (0)