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

Commit 176b42a

Browse files
committed
Add Soft Delete tests
1 parent 1cf470c commit 176b42a

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Data;
4+
using NUnit.Framework;
5+
using ServiceStack.DataAnnotations;
6+
using ServiceStack.OrmLite.Tests.UseCase;
7+
8+
namespace ServiceStack.OrmLite.Tests
9+
{
10+
public class Vendor : ISoftDelete
11+
{
12+
public Guid Id { get; set; }
13+
public string Name { get; set; }
14+
public bool IsDeleted { get; set; }
15+
16+
[Reference]
17+
public List<Product> Products { get; set; }
18+
}
19+
20+
public class Product : ISoftDelete
21+
{
22+
public Guid Id { get; set; }
23+
public string Name { get; set; }
24+
public bool IsDeleted { get; set; }
25+
26+
[ForeignKey(typeof(Vendor))]
27+
public Guid VendorId { get; set; }
28+
}
29+
30+
public class SoftDeleteTests : OrmLiteTestBase
31+
{
32+
private static void InitData(IDbConnection db)
33+
{
34+
db.DropTable<Product>();
35+
db.DropTable<Vendor>();
36+
db.CreateTable<Vendor>();
37+
db.CreateTable<Product>();
38+
39+
db.Save(new Vendor
40+
{
41+
Id = Guid.NewGuid(),
42+
Name = "Active Vendor",
43+
Products = new List<Product>
44+
{
45+
new Product {Id = Guid.NewGuid(), Name = "Active Product"},
46+
new Product {Id = Guid.NewGuid(), Name = "Retired Product", IsDeleted = true},
47+
}
48+
}, references:true);
49+
50+
db.Save(new Vendor
51+
{
52+
Id = Guid.NewGuid(),
53+
Name = "Retired Vendor",
54+
IsDeleted = true,
55+
Products = new List<Product>
56+
{
57+
new Product {Id = Guid.NewGuid(), Name = "Active Product"},
58+
new Product {Id = Guid.NewGuid(), Name = "Retired Product", IsDeleted = true},
59+
}
60+
}, references: true);
61+
}
62+
63+
[Test]
64+
public void Can_filter_deleted_products_reference_data()
65+
{
66+
using (var db = OpenDbConnection())
67+
{
68+
InitData(db);
69+
70+
var vendors = db.LoadSelect<Vendor>(x => !x.IsDeleted);
71+
72+
Assert.That(vendors.Count, Is.EqualTo(1));
73+
Assert.That(vendors[0].Name, Is.EqualTo("Active Vendor"));
74+
Assert.That(vendors[0].Products.Count, Is.EqualTo(2));
75+
}
76+
}
77+
78+
[Test]
79+
public void Can_get_active_products_using_merge()
80+
{
81+
using (var db = OpenDbConnection())
82+
{
83+
InitData(db);
84+
85+
var vendors = db.Select<Vendor>(x => !x.IsDeleted);
86+
var products = db.Select(db.From<Product>().Join<Vendor>()
87+
.Where(p => !p.IsDeleted)
88+
.And<Vendor>(v => !v.IsDeleted));
89+
90+
var merged = vendors.Merge(products);
91+
92+
Assert.That(merged.Count, Is.EqualTo(1));
93+
Assert.That(merged[0].Name, Is.EqualTo("Active Vendor"));
94+
Assert.That(merged[0].Products.Count, Is.EqualTo(1));
95+
Assert.That(merged[0].Products[0].Name, Is.EqualTo("Active Product"));
96+
}
97+
}
98+
99+
[Test]
100+
public void Can_get_active_products_using_SoftDelete_SqlExpression()
101+
{
102+
OrmLiteConfig.SqlExpressionSelectFilter = q =>
103+
{
104+
if (q.ModelDef.ModelType.HasInterface(typeof(ISoftDelete)))
105+
{
106+
q.Where<ISoftDelete>(x => !x.IsDeleted);
107+
}
108+
};
109+
110+
using (var db = OpenDbConnection())
111+
{
112+
InitData(db);
113+
114+
var vendors = db.LoadSelect<Vendor>();
115+
116+
Assert.That(vendors.Count, Is.EqualTo(1));
117+
Assert.That(vendors[0].Name, Is.EqualTo("Active Vendor"));
118+
}
119+
120+
OrmLiteConfig.SqlExpressionSelectFilter = null;
121+
}
122+
}
123+
}

0 commit comments

Comments
 (0)