forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnitTestExample.cs
More file actions
210 lines (172 loc) · 6.31 KB
/
UnitTestExample.cs
File metadata and controls
210 lines (172 loc) · 6.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Copyright (c) Service Stack LLC. All Rights Reserved.
// License: https://raw.github.com/ServiceStack/ServiceStack/master/license.txt
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Funq;
using NUnit.Framework;
using ServiceStack.Data;
using ServiceStack.OrmLite;
using ServiceStack.Testing;
using ServiceStack.Text;
using ServiceStack.Web;
namespace ServiceStack.WebHost.Endpoints.Tests
{
// Request DTOs
public class FindRockstars
{
public int? Aged { get; set; }
public bool? Alive { get; set; }
}
public class GetStatus
{
public string LastName { get; set; }
}
// Types
public class Rockstar
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int? Age { get; set; }
}
public class RockstarStatus
{
public int Age { get; set; }
public bool Alive { get; set; }
}
// Implementation
public class SimpleService : Service
{
public IRockstarRepository RockstarRepository { get; set; }
public List<Rockstar> Get(FindRockstars request)
{
return request.Aged.HasValue
? Db.Select<Rockstar>(q => q.Age == request.Aged.Value)
: Db.Select<Rockstar>();
}
public RockstarStatus Get(GetStatus request)
{
var rockstar = RockstarRepository.GetByLastName(request.LastName);
if (rockstar == null)
throw HttpError.NotFound("'{0}' is not a Rockstar".Fmt(request.LastName));
var status = new RockstarStatus
{
Alive = RockstarRepository.IsAlive(request.LastName)
}.PopulateWith(rockstar); //Populates with matching fields
return status;
}
}
//Custom Repository
public interface IRockstarRepository
{
Rockstar GetByLastName(string lastName);
bool IsAlive(string lastName);
}
public class RockstarRepository : RepositoryBase, IRockstarRepository
{
public Rockstar GetByLastName(string lastName)
{
return Db.Single<Rockstar>(q => q.LastName == lastName);
}
readonly HashSet<string> fallenLegends = new HashSet<string> {
"Hendrix", "Hendrix", "Cobain", "Presley", "Jackson"
};
public bool IsAlive(string lastName)
{
return !fallenLegends.Contains(lastName);
}
}
//Use base class to keep common boilerplate
public class RepositoryBase : IDisposable
{
public IDbConnectionFactory DbFactory { get; set; }
IDbConnection db;
protected IDbConnection Db
{
get { return db ?? (db = DbFactory.Open()); }
}
public void Dispose()
{
if (db != null)
db.Dispose();
}
}
[TestFixture]
public class UnitTestExample
{
public static List<Rockstar> SeedData = new[] {
new Rockstar { Id = 1, FirstName = "Jimi", LastName = "Hendrix", Age = 27 },
new Rockstar { Id = 2, FirstName = "Jim", LastName = "Morrison", Age = 27 },
new Rockstar { Id = 3, FirstName = "Kurt", LastName = "Cobain", Age = 27 },
new Rockstar { Id = 4, FirstName = "Elvis", LastName = "Presley", Age = 42 },
new Rockstar { Id = 5, FirstName = "David", LastName = "Grohl", Age = 44 },
new Rockstar { Id = 6, FirstName = "Eddie", LastName = "Vedder", Age = 48 },
new Rockstar { Id = 7, FirstName = "Michael", LastName = "Jackson", Age = 27 },
}.ToList();
private ServiceStackHost appHost;
[TestFixtureSetUp]
public void TestFixtureSetUp()
{
appHost = new BasicAppHost().Init();
var container = appHost.Container;
container.Register<IDbConnectionFactory>(
new OrmLiteConnectionFactory(":memory:", SqliteDialect.Provider));
container.RegisterAutoWiredAs<RockstarRepository, IRockstarRepository>();
container.RegisterAutoWired<SimpleService>();
using (var db = container.Resolve<IDbConnectionFactory>().Open())
{
db.DropAndCreateTable<Rockstar>();
db.InsertAll(SeedData);
}
}
[TestFixtureTearDown]
public void TestFixtureTearDown()
{
appHost.Dispose();
}
[Test]
public void Using_in_memory_database()
{
var service = appHost.Container.Resolve<SimpleService>(); //Resolve auto-wired service
var rockstars = service.Get(new FindRockstars { Aged = 27 });
rockstars.PrintDump(); //Print results to screen
Assert.That(rockstars.Count, Is.EqualTo(SeedData.Count(x => x.Age == 27)));
var status = service.Get(new GetStatus { LastName = "Vedder" });
Assert.That(status.Age, Is.EqualTo(48));
Assert.That(status.Alive, Is.True);
status = service.Get(new GetStatus { LastName = "Hendrix" });
Assert.That(status.Age, Is.EqualTo(27));
Assert.That(status.Alive, Is.False);
Assert.Throws<HttpError>(() =>
service.Get(new GetStatus { LastName = "Unknown" }));
}
public class RockstarRepositoryMock : IRockstarRepository
{
public Rockstar GetByLastName(string lastName)
{
return lastName == "Vedder"
? new Rockstar { Id = 6, FirstName = "Eddie", LastName = "Vedder", Age = 48 }
: null;
}
public bool IsAlive(string lastName)
{
return lastName == "Grohl" || lastName == "Vedder";
}
}
[Test]
public void Using_manual_dependency_injection()
{
var service = new SimpleService
{
RockstarRepository = new RockstarRepositoryMock()
};
var status = service.Get(new GetStatus { LastName = "Vedder" });
Assert.That(status.Age, Is.EqualTo(48));
Assert.That(status.Alive, Is.True);
Assert.Throws<HttpError>(() =>
service.Get(new GetStatus { LastName = "Hendrix" }));
}
}
}