|
6 | 6 | using ServiceStack; |
7 | 7 | using ServiceStack.Data; |
8 | 8 | using ServiceStack.OrmLite; |
9 | | -using ServiceStack.Testing; |
10 | 9 | using ServiceStack.Text; |
11 | | -using StackApis.ServiceInterface; |
12 | 10 | using StackApis.ServiceModel; |
13 | 11 | using StackApis.ServiceModel.Types; |
14 | 12 |
|
15 | 13 | namespace StackApis.Tests |
16 | 14 | { |
17 | 15 | [TestFixture] |
18 | | - public class UnitTests |
| 16 | + public class StackOverflowTasks |
19 | 17 | { |
20 | | - private readonly ServiceStackHost appHost; |
| 18 | + private IDbConnectionFactory dbFactory; |
21 | 19 |
|
22 | | - public UnitTests() |
| 20 | + [OneTimeSetUp] |
| 21 | + public void OneTimeSetUp() |
23 | 22 | { |
24 | | - appHost = new BasicAppHost(typeof(MyServices).Assembly) |
25 | | - { |
26 | | - ConfigureContainer = container => |
27 | | - { |
28 | | - //Add your IoC dependencies here |
29 | | - container.Register<IDbConnectionFactory>( |
30 | | - new OrmLiteConnectionFactory( |
31 | | - "~/../../../StackApis/App_Data/db.sqlite".MapServerPath(), |
32 | | - SqliteDialect.Provider)); |
33 | | - |
34 | | - using (var db = container.Resolve<IDbConnectionFactory>().OpenDbConnection()) |
35 | | - { |
36 | | - db.DropAndCreateTable<Question>(); |
37 | | - db.DropAndCreateTable<Answer>(); |
38 | | - db.DropAndCreateTable<QuestionTag>(); |
39 | | - } |
40 | | - |
41 | | - SeedStackOverflowData(container.Resolve<IDbConnectionFactory>()); |
42 | | - } |
43 | | - } |
44 | | - .Init(); |
45 | | - } |
46 | | - |
47 | | - [TestFixtureTearDown] |
48 | | - public void TestFixtureTearDown() |
49 | | - { |
50 | | - appHost.Dispose(); |
| 23 | + dbFactory = new OrmLiteConnectionFactory( |
| 24 | + "~/../../../StackApis/App_Data/db.sqlite".MapServerPath(), SqliteDialect.Provider); |
51 | 25 | } |
52 | 26 |
|
53 | 27 | [Test] |
54 | | - public void TestImport() |
55 | | - { |
56 | | - var dbConnectionFactory = appHost.TryResolve<IDbConnectionFactory>(); |
57 | | - using (var db = dbConnectionFactory.OpenDbConnection()) |
58 | | - { |
59 | | - var numberOfQuestions = db.Count<Question>(); |
60 | | - var numberOfAnswers = db.Count<Answer>(); |
61 | | - Assert.That(numberOfQuestions > 0); |
62 | | - Assert.That(numberOfAnswers > 0); |
63 | | - } |
64 | | - } |
65 | | - |
66 | | - private void SeedStackOverflowData(IDbConnectionFactory dbConnectionFactory) |
| 28 | + public void Import_from_StackOverflow() |
67 | 29 | { |
68 | 30 | var client = new JsonServiceClient(); |
69 | | - int numberOfPages = 80; |
| 31 | + int numberOfPages = 10; |
70 | 32 | int pageSize = 100; |
71 | 33 | var dbQuestions = new List<Question>(); |
72 | 34 | var dbAnswers = new List<Answer>(); |
@@ -76,9 +38,8 @@ private void SeedStackOverflowData(IDbConnectionFactory dbConnectionFactory) |
76 | 38 | { |
77 | 39 | //Throttle queries |
78 | 40 | Thread.Sleep(100); |
79 | | - var questionsResponse = |
80 | | - client.Get("https://api.stackexchange.com/2.2/questions?page={0}&pagesize={1}&site={2}&tagged=servicestack" |
81 | | - .Fmt(i, pageSize, "stackoverflow")); |
| 41 | + var questionsResponse = client.Get("https://api.stackexchange.com/2.2/questions?page={0}&pagesize={1}&site={2}&tagged=servicestack" |
| 42 | + .Fmt(i, pageSize, "stackoverflow")); |
82 | 43 |
|
83 | 44 | QuestionsResponse qResponse; |
84 | 45 | using (new ConfigScope()) |
@@ -107,6 +68,7 @@ private void SeedStackOverflowData(IDbConnectionFactory dbConnectionFactory) |
107 | 68 | catch (Exception ex) |
108 | 69 | { |
109 | 70 | //ignore |
| 71 | + ex.Message.Print(); |
110 | 72 | } |
111 | 73 |
|
112 | 74 | //Filter duplicates |
@@ -115,26 +77,43 @@ private void SeedStackOverflowData(IDbConnectionFactory dbConnectionFactory) |
115 | 77 | var questionTags = dbQuestions.SelectMany(q => |
116 | 78 | q.Tags.Select(t => new QuestionTag { QuestionId = q.QuestionId, Tag = t })); |
117 | 79 |
|
118 | | - using (var db = dbConnectionFactory.OpenDbConnection()) |
| 80 | + using (var db = dbFactory.OpenDbConnection()) |
119 | 81 | { |
| 82 | + db.DropAndCreateTable<Question>(); |
| 83 | + db.DropAndCreateTable<Answer>(); |
| 84 | + db.DropAndCreateTable<QuestionTag>(); |
| 85 | + |
120 | 86 | db.InsertAll(dbQuestions); |
121 | 87 | db.InsertAll(dbAnswers); |
122 | 88 | db.InsertAll(questionTags); |
123 | 89 | } |
124 | 90 | } |
| 91 | + |
| 92 | + [Test] |
| 93 | + public void Test_Import() |
| 94 | + { |
| 95 | + using (var db = dbFactory.OpenDbConnection()) |
| 96 | + { |
| 97 | + var numberOfQuestions = db.Count<Question>(); |
| 98 | + var numberOfAnswers = db.Count<Answer>(); |
| 99 | + Assert.That(numberOfQuestions > 0); |
| 100 | + Assert.That(numberOfAnswers > 0); |
| 101 | + } |
| 102 | + } |
125 | 103 | } |
126 | 104 |
|
127 | 105 | public class ConfigScope : IDisposable |
128 | 106 | { |
129 | 107 | private readonly WriteComplexTypeDelegate holdQsStrategy; |
130 | | - private readonly JsConfigScope jsConfigScope; |
| 108 | + private readonly JsConfigScope scope; |
131 | 109 |
|
132 | 110 | public ConfigScope() |
133 | 111 | { |
134 | | - jsConfigScope = JsConfig.With(dateHandler: DateHandler.UnixTime, |
135 | | - propertyConvention: PropertyConvention.Lenient, |
136 | | - emitLowercaseUnderscoreNames: true, |
137 | | - emitCamelCaseNames: false); |
| 112 | + scope = JsConfig.With( |
| 113 | + dateHandler: DateHandler.UnixTime, |
| 114 | + propertyConvention: PropertyConvention.Lenient, |
| 115 | + emitLowercaseUnderscoreNames: true, |
| 116 | + emitCamelCaseNames: false); |
138 | 117 |
|
139 | 118 | holdQsStrategy = QueryStringSerializer.ComplexTypeStrategy; |
140 | 119 | QueryStringSerializer.ComplexTypeStrategy = QueryStringStrategy.FormUrlEncoded; |
@@ -143,7 +122,8 @@ public ConfigScope() |
143 | 122 | public void Dispose() |
144 | 123 | { |
145 | 124 | QueryStringSerializer.ComplexTypeStrategy = holdQsStrategy; |
146 | | - jsConfigScope.Dispose(); |
| 125 | + scope.Dispose(); |
147 | 126 | } |
148 | 127 | } |
| 128 | + |
149 | 129 | } |