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

Commit 8b08398

Browse files
committed
Add Customer UseCase to async tests
1 parent e8654d1 commit 8b08398

File tree

5 files changed

+231
-9
lines changed

5 files changed

+231
-9
lines changed

tests/ServiceStack.OrmLiteV45.Tests/LoadReferencesTests.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Threading.Tasks;
55
using NUnit.Framework;
66
using ServiceStack.DataAnnotations;
7+
using ServiceStack.OrmLite.Tests.UseCase;
78
using ServiceStack.Text;
89

910
namespace ServiceStack.OrmLite.Tests
@@ -60,7 +61,7 @@ public class LoadReferencesTests
6061
public new void TestFixtureSetUp()
6162
{
6263
db = base.OpenDbConnection();
63-
DropTables(db); //Has conflicting 'Order' table
64+
CustomerOrdersUseCase.DropTables(db); //Has conflicting 'Order' table
6465

6566
db.DropAndCreateTable<Order>();
6667
db.DropAndCreateTable<Customer>();
@@ -82,12 +83,6 @@ public void TestFixtureTearDown()
8283
db.Dispose();
8384
}
8485

85-
public static void DropTables(IDbConnection db)
86-
{
87-
db.DropTable<Order>();
88-
db.DropTable<Customer>();
89-
}
90-
9186
public static Customer GetCustomerWithOrders(string id = "1")
9287
{
9388
var customer = new Customer

tests/ServiceStack.OrmLiteV45.Tests/OrmLiteTestBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ protected void CreateNewDatabase()
8383
ConnectionString = GetFileConnectionString();
8484
}
8585

86-
public Dialect Dialect = Dialect.MySql;
86+
public Dialect Dialect = Dialect.SqlServer;
8787
protected OrmLiteConnectionFactory DbFactory;
8888

8989
OrmLiteConnectionFactory Init(string connStr, IOrmLiteDialectProvider dialectProvider)

tests/ServiceStack.OrmLiteV45.Tests/ServiceStack.OrmLiteV45.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
</Compile>
8181
<Compile Include="SqlExpressionTests.cs" />
8282
<Compile Include="TestHelpers.cs" />
83+
<Compile Include="UseCase\CustomerOrdersUseCase.cs" />
8384
</ItemGroup>
8485
<ItemGroup>
8586
<ProjectReference Include="..\..\src\ServiceStack.OrmLite.MySqlV45\ServiceStack.OrmLite.MySqlV45.csproj">
@@ -100,6 +101,7 @@
100101
</ProjectReference>
101102
</ItemGroup>
102103
<ItemGroup>
104+
<None Include="App.config" />
103105
<None Include="packages.config" />
104106
</ItemGroup>
105107
<ItemGroup>

tests/ServiceStack.OrmLiteV45.Tests/SqlExpressionTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Threading.Tasks;
22
using NUnit.Framework;
3+
using ServiceStack.OrmLite.Tests.UseCase;
34
using ServiceStack.Text;
45

56
namespace ServiceStack.OrmLite.Tests
@@ -12,7 +13,7 @@ public async Task Can_select_limit_on_Table_with_References_Async()
1213
{
1314
using (var db = OpenDbConnection())
1415
{
15-
LoadReferencesTests.DropTables(db); //Has conflicting 'Order' table
16+
CustomerOrdersUseCase.DropTables(db); //Has conflicting 'Order' table
1617
db.DropAndCreateTable<Order>();
1718
db.DropAndCreateTable<Customer>();
1819
db.DropAndCreateTable<CustomerAddress>();
Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Data;
4+
using System.Linq;
5+
using NUnit.Framework;
6+
using ServiceStack.DataAnnotations;
7+
using ServiceStack.Logging;
8+
9+
namespace ServiceStack.OrmLite.Tests.UseCase
10+
{
11+
public enum PhoneType
12+
{
13+
Home,
14+
Work,
15+
Mobile,
16+
}
17+
18+
public enum AddressType
19+
{
20+
Home,
21+
Work,
22+
Other,
23+
}
24+
25+
public class Address
26+
{
27+
public string Line1 { get; set; }
28+
public string Line2 { get; set; }
29+
public string ZipCode { get; set; }
30+
public string State { get; set; }
31+
public string City { get; set; }
32+
public string Country { get; set; }
33+
}
34+
35+
public class Customer
36+
{
37+
public Customer()
38+
{
39+
this.PhoneNumbers = new Dictionary<PhoneType, string>();
40+
this.Addresses = new Dictionary<AddressType, Address>();
41+
}
42+
43+
[AutoIncrement] // Creates Auto primary key
44+
public int Id { get; set; }
45+
46+
public string FirstName { get; set; }
47+
48+
public string LastName { get; set; }
49+
50+
[Index(Unique = true)] // Creates Unique Index
51+
public string Email { get; set; }
52+
53+
public Dictionary<PhoneType, string> PhoneNumbers { get; set; }
54+
55+
public Dictionary<AddressType, Address> Addresses { get; set; }
56+
57+
public DateTime CreatedAt { get; set; }
58+
}
59+
60+
public class Order
61+
{
62+
[AutoIncrement]
63+
public int Id { get; set; }
64+
65+
[References(typeof(Customer))] //Creates Foreign Key
66+
public int CustomerId { get; set; }
67+
68+
[References(typeof(Employee))] //Creates Foreign Key
69+
public int EmployeeId { get; set; }
70+
71+
public Address ShippingAddress { get; set; } //Blobbed (no Address table)
72+
73+
public DateTime? OrderDate { get; set; }
74+
75+
public DateTime? RequiredDate { get; set; }
76+
77+
public DateTime? ShippedDate { get; set; }
78+
79+
public int? ShipVia { get; set; }
80+
81+
public decimal Freight { get; set; }
82+
83+
public decimal Total { get; set; }
84+
}
85+
86+
public class OrderDetail
87+
{
88+
[AutoIncrement]
89+
public int Id { get; set; }
90+
91+
[References(typeof(Order))] //Creates Foreign Key
92+
public int OrderId { get; set; }
93+
94+
public int ProductId { get; set; }
95+
96+
public decimal UnitPrice { get; set; }
97+
98+
public short Quantity { get; set; }
99+
100+
public decimal Discount { get; set; }
101+
}
102+
103+
public class Employee
104+
{
105+
public int Id { get; set; }
106+
107+
public string Name { get; set; }
108+
}
109+
110+
public class Product
111+
{
112+
public int Id { get; set; }
113+
114+
public string Name { get; set; }
115+
116+
public decimal UnitPrice { get; set; }
117+
}
118+
119+
/// <summary>
120+
/// Stand-alone class, No other configs, nothing but POCOs.
121+
/// </summary>
122+
[TestFixture]
123+
public class CustomerOrdersUseCase : OrmLiteTestBase
124+
{
125+
[Test]
126+
public void Can_run_Customer_Orders_UseCase()
127+
{
128+
LogManager.LogFactory = new ConsoleLogFactory();
129+
130+
using (IDbConnection db = OpenDbConnection())
131+
{
132+
//Re-Create all table schemas:
133+
RecreateTables(db);
134+
135+
db.Insert(new Employee { Id = 1, Name = "Employee 1" });
136+
db.Insert(new Employee { Id = 2, Name = "Employee 2" });
137+
var product1 = new Product { Id = 1, Name = "Product 1", UnitPrice = 10 };
138+
var product2 = new Product { Id = 2, Name = "Product 2", UnitPrice = 20 };
139+
db.Save(product1, product2);
140+
141+
var customer = new Customer {
142+
FirstName = "Orm",
143+
LastName = "Lite",
144+
Email = "[email protected]",
145+
PhoneNumbers =
146+
{
147+
{ PhoneType.Home, "555-1234" },
148+
{ PhoneType.Work, "1-800-1234" },
149+
{ PhoneType.Mobile, "818-123-4567" },
150+
},
151+
Addresses =
152+
{
153+
{ AddressType.Work, new Address { Line1 = "1 Street", Country = "US", State = "NY", City = "New York", ZipCode = "10101" } },
154+
},
155+
CreatedAt = DateTime.UtcNow,
156+
};
157+
158+
var customerId = db.Insert(customer, selectIdentity: true); //Get Auto Inserted Id
159+
customer = db.Single<Customer>(new { customer.Email }); //Query
160+
Assert.That(customer.Id, Is.EqualTo(customerId));
161+
162+
//Direct access to System.Data.Transactions:
163+
using (IDbTransaction trans = db.OpenTransaction(IsolationLevel.ReadCommitted))
164+
{
165+
var order = new Order {
166+
CustomerId = customer.Id,
167+
EmployeeId = 1,
168+
OrderDate = DateTime.UtcNow,
169+
Freight = 10.50m,
170+
ShippingAddress = new Address { Line1 = "3 Street", Country = "US", State = "NY", City = "New York", ZipCode = "12121" },
171+
};
172+
db.Save(order); //Inserts 1st time
173+
174+
//order.Id populated on Save().
175+
176+
var orderDetails = new[] {
177+
new OrderDetail {
178+
OrderId = order.Id,
179+
ProductId = product1.Id,
180+
Quantity = 2,
181+
UnitPrice = product1.UnitPrice,
182+
},
183+
new OrderDetail {
184+
OrderId = order.Id,
185+
ProductId = product2.Id,
186+
Quantity = 2,
187+
UnitPrice = product2.UnitPrice,
188+
Discount = .15m,
189+
}
190+
};
191+
192+
db.Save(orderDetails);
193+
194+
order.Total = orderDetails.Sum(x => x.UnitPrice * x.Quantity * x.Discount) + order.Freight;
195+
196+
db.Save(order); //Updates 2nd Time
197+
198+
trans.Commit();
199+
}
200+
}
201+
}
202+
203+
public static void RecreateTables(IDbConnection db)
204+
{
205+
DropTables(db);
206+
207+
db.CreateTable<Employee>();
208+
db.CreateTable<Product>();
209+
db.CreateTable<Customer>();
210+
db.CreateTable<Order>();
211+
db.CreateTable<OrderDetail>();
212+
}
213+
214+
public static void DropTables(IDbConnection db)
215+
{
216+
db.DropTable<OrderDetail>();
217+
db.DropTable<Order>();
218+
db.DropTable<Customer>();
219+
db.DropTable<Product>();
220+
db.DropTable<Employee>();
221+
}
222+
}
223+
224+
}

0 commit comments

Comments
 (0)