Browse Source

refactor; tests; added cruddataaccess delete

tags/v0.1.0
Robin Thoni 8 years ago
parent
commit
10cc9526e8

+ 21
- 26
Luticate2.Auth/DataAccess/LuGroupsDataAccess.cs View File

@@ -4,38 +4,33 @@ using Luticate2.Utils.DataAccess;
4 4
 
5 5
 namespace Luticate2.Auth.DataAccess
6 6
 {
7
-    public class LuGroupsDataAccess : LuEfCrudDataAccess<lu_groups, LuGroupsAddDbo, LuGroupsDbo, LuGroupsAddDbo, LuDatabaseContext>
7
+    public class LuGroupsDataAccess //: LuEfCrudDataAccess<lu_groups, LuGroupsAddDbo, LuGroupsDbo, LuDatabaseContext>
8 8
     {
9
-        public LuGroupsDataAccess(LuDatabaseContext db)
10
-            : base(db, db.lu_groups)
11
-        {
12
-        }
9
+//        public LuGroupsDataAccess(LuDatabaseContext db)
10
+//            : base(db, db.lu_groups)
11
+//        {
12
+//        }
13 13
 
14 14
         public string Get()
15 15
         {
16 16
             return "groups";
17 17
         }
18 18
 
19
-        protected override lu_groups GetModelFromTCreate(LuGroupsAddDbo obj)
20
-        {
21
-            return new lu_groups
22
-            {
23
-                name = obj.Name
24
-            };
25
-        }
26
-
27
-        protected override lu_groups GetModelFromTUpdate(LuGroupsAddDbo obj)
28
-        {
29
-            return GetModelFromTCreate(obj);
30
-        }
31
-
32
-        protected override LuGroupsDbo GetDboFromModel(lu_groups model)
33
-        {
34
-            return new LuGroupsDbo
35
-            {
36
-                Id = model.id.ToString(),
37
-                Name = model.name
38
-            };
39
-        }
19
+//        protected override lu_groups GetModelFromTCreate(LuGroupsAddDbo obj)
20
+//        {
21
+//            return new lu_groups
22
+//            {
23
+//                name = obj.Name
24
+//            };
25
+//        }
26
+//
27
+//        protected override LuGroupsDbo GetDboFromModel(lu_groups model)
28
+//        {
29
+//            return new LuGroupsDbo
30
+//            {
31
+//                Id = model.id.ToString(),
32
+//                Name = model.name
33
+//            };
34
+//        }
40 35
     }
41 36
 }

+ 1
- 1
Luticate2.Auth/DataAccess/code-from-ds/DataSource.twig View File

@@ -32,7 +32,7 @@ namespace Luticate2.Auth.DataAccess
32 32
             {% for table in dataSource.getTables() %}{% if (table.hasAny()) %}
33 33
             modelBuilder.Entity<{{ table.getName() }}>()
34 34
                 .HasKey(c => new { {% for column in table.getPrimaryKeys() %}{% if (column.isSelected()) %}c.{{ column.getName() }}{% if not (loop.last) %}, {% endif %}{% endif %}{% endfor %} });
35
-            {% for column in table.getPrimaryKeys() %}{% if (column.isSelected()) and (column.hasDefaultValue()) %}
35
+            {% for column in table.getColumns() %}{% if (column.isSelected()) and (column.hasDefaultValue()) %}
36 36
             modelBuilder.Entity<{{ table.getName() }}>()
37 37
                 .Property(e => e.{{ column.getName() }})
38 38
                 .HasDefaultValueSql("{{ column.getDefaultValueEscaped() }}");

+ 210
- 23
Luticate2.Utils/DataAccess/LuEfCrudDataAccess.cs View File

@@ -21,10 +21,28 @@ namespace Luticate2.Utils.DataAccess
21 21
 
22 22
         protected abstract TModel GetModelFromTCreate(TDboCreate obj);
23 23
 
24
-        protected abstract TModel GetModelFromTUpdate(TDboUpdate obj);
24
+        protected abstract void EditModelFromTUpdate(TDboUpdate obj, TModel model);
25 25
 
26 26
         protected abstract TDboRead GetDboFromModel(TModel model);
27 27
 
28
+        public Func<TDboRead, T> GetIdFunc<T>()
29
+        {
30
+            var param = Expression.Parameter(typeof(TDboRead), "x");
31
+            var exp = Expression.Property(param, "Id");
32
+            var lambda = Expression.Lambda<Func<TDboRead, T>>(exp, param);
33
+            var func = lambda.Compile();
34
+            return func;
35
+        }
36
+
37
+        protected TModel GetModelFromTUpdate(TDboUpdate obj, TModel model)
38
+        {
39
+            EditModelFromTUpdate(obj, model);
40
+            return model;
41
+        }
42
+
43
+
44
+
45
+
28 46
         public LuResult<T> Add<T>(IEnumerable<TDboCreate> objs, Func<IEnumerable<TDboRead>, T> returnFunc)
29 47
         {
30 48
             return Execute(() =>
@@ -45,10 +63,7 @@ namespace Luticate2.Utils.DataAccess
45 63
 
46 64
         public LuResult<IEnumerable<string>> AddGuid(IEnumerable<TDboCreate> objs)
47 65
         {
48
-            var param = Expression.Parameter(typeof(TDboRead), "x");
49
-            var exp = Expression.Property(param, "Id");
50
-            var lambda = Expression.Lambda<Func<TDboRead, string>>(exp, param);
51
-            var func = lambda.Compile();
66
+            var func = GetIdFunc<string>();
52 67
             return Add(objs, list => list.Select(func));
53 68
         }
54 69
 
@@ -59,10 +74,7 @@ namespace Luticate2.Utils.DataAccess
59 74
 
60 75
         public LuResult<IEnumerable<long>> AddId(IEnumerable<TDboCreate> obj)
61 76
         {
62
-            var param = Expression.Parameter(typeof(TDboRead), "x");
63
-            var exp = Expression.Property(param, "Id");
64
-            var lambda = Expression.Lambda<Func<TDboRead, long>>(exp, param);
65
-            var func = lambda.Compile();
77
+            var func = GetIdFunc<long>();
66 78
             return Add(obj, list => list.Select(func));
67 79
         }
68 80
 
@@ -81,6 +93,9 @@ namespace Luticate2.Utils.DataAccess
81 93
             return AddDbo(new List<TDboCreate> {obj}).To(list => list.First());
82 94
         }
83 95
 
96
+
97
+
98
+
84 99
         public LuResult<TDboRead> GetSingle(Expression<Func<TModel, bool>> predicate)
85 100
         {
86 101
             return Execute(() =>
@@ -100,14 +115,9 @@ namespace Luticate2.Utils.DataAccess
100 115
             return GetSingle(GetExpression(keys));
101 116
         }
102 117
 
103
-        public LuResult<TDboRead> GetSingleById(Guid id)
104
-        {
105
-            return GetSingleByKeys(new KeyValuePair<string, object>("id", id));
106
-        }
107
-
108 118
         public LuResult<TDboRead> GetSingleById(string id)
109 119
         {
110
-            return GetSingleById(new Guid(id));
120
+            return GetSingleByKeys(new KeyValuePair<string, object>("id", new Guid(id)));
111 121
         }
112 122
 
113 123
         public LuResult<TDboRead> GetSingleById(long id)
@@ -115,6 +125,7 @@ namespace Luticate2.Utils.DataAccess
115 125
             return GetSingleByKeys(new KeyValuePair<string, object>("id", id));
116 126
         }
117 127
 
128
+
118 129
         public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple<TKey>(Func<IQueryable<TModel>> orderBy,
119 130
             Expression<Func<TModel, bool>> predicate, int page = 0, int perPage = int.MaxValue,
120 131
             params Expression<Func<TModel, TKey>>[] otherOrderBy)
@@ -139,14 +150,23 @@ namespace Luticate2.Utils.DataAccess
139 150
             return GetMultiple<TKey>(() => Table.OrderBy(orderBy), predicate, page, perPage);
140 151
         }
141 152
 
153
+        public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple<TKey>(Expression<Func<TModel, TKey>> orderBy,
154
+            int page = 0, int perPage = int.MaxValue, params Expression<Func<TModel, TKey>>[] otherOrderBy)
155
+        {
156
+            return GetMultiple(() => Table.OrderBy(orderBy), x => true, page, perPage, otherOrderBy);
157
+        }
158
+
142 159
         public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple<TKey>(Expression<Func<TModel, TKey>> orderBy,
143 160
             int page = 0, int perPage = int.MaxValue)
144 161
         {
145 162
             return GetMultiple(orderBy, x => true, page, perPage);
146 163
         }
147 164
 
165
+
166
+
167
+
148 168
         public LuResult<T> Edit<T>(Expression<Func<TModel, bool>> predicate, Action<TModel> update,
149
-            Func<List<TDboRead>, T> returnFunc)
169
+            Func<IEnumerable<TDboRead>, T> returnFunc)
150 170
         {
151 171
             return Execute(() =>
152 172
             {
@@ -164,25 +184,192 @@ namespace Luticate2.Utils.DataAccess
164 184
             });
165 185
         }
166 186
 
167
-        public LuResult<T> EditSingleByPkId<T, TId>(TId id, Action<TModel> update, Func<TDboRead, T> returnFunc)
187
+        public LuResult<IEnumerable<string>> EditGuid(Expression<Func<TModel, bool>> predicate, Action<TModel> update)
168 188
         {
169
-            return Edit(GetExpression(new KeyValuePair<string, object>("id", id)), update,
170
-                list => list.Count == 0 ? returnFunc(default(TDboRead)) : returnFunc(list[0]));
189
+            var func = GetIdFunc<string>();
190
+            return Edit(predicate, update, reads => reads.Select(func));
191
+        }
192
+
193
+        public LuResult<IEnumerable<long>> EditId(Expression<Func<TModel, bool>> predicate, Action<TModel> update)
194
+        {
195
+            var func = GetIdFunc<long>();
196
+            return Edit(predicate, update, reads => reads.Select(func));
171 197
         }
172 198
 
199
+        public LuResult<IEnumerable<TDboRead>> EditDbo(Expression<Func<TModel, bool>> predicate, Action<TModel> update)
200
+        {
201
+            return Edit(predicate, update, read => read);
202
+        }
203
+
204
+
173 205
         public LuResult<T> EditSingleById<T>(long id, Action<TModel> update, Func<TDboRead, T> returnFunc)
174 206
         {
175
-            return EditSingleByPkId(id, update, returnFunc);
207
+            return Edit(GetExpression(new KeyValuePair<string, object>("id", id)), update,
208
+                list => returnFunc(list.FirstOrDefault()));
209
+        }
210
+
211
+        public LuResult<string> EditSingleByIdGuid(long id, Action<TModel> update)
212
+        {
213
+            var func = GetIdFunc<string>();
214
+            return EditSingleById(id, update, func);
176 215
         }
177 216
 
178
-        public LuResult<T> EditSingleById<T>(Guid id, Action<TModel> update, Func<TDboRead, T> returnFunc)
217
+        public LuResult<long> EditSingleByIdId(long id, Action<TModel> update)
179 218
         {
180
-            return EditSingleByPkId(id, update, returnFunc);
219
+            var func = GetIdFunc<long>();
220
+            return EditSingleById(id, update, func);
181 221
         }
182 222
 
223
+        public LuResult<TDboRead> EditSingleByIdDbo(long id, Action<TModel> update)
224
+        {
225
+            return EditSingleById(id, update, read => read);
226
+        }
227
+
228
+
183 229
         public LuResult<T> EditSingleById<T>(string id, Action<TModel> update, Func<TDboRead, T> returnFunc)
184 230
         {
185
-            return EditSingleById(new Guid(id), update, returnFunc);
231
+            return Edit(GetExpression(new KeyValuePair<string, object>("id", new Guid(id))), update,
232
+                list => returnFunc(list.FirstOrDefault()));
233
+        }
234
+
235
+        public LuResult<string> EditSingleByIdGuid(string id, Action<TModel> update)
236
+        {
237
+            return EditSingleById(id, update, GetIdFunc<string>());
238
+        }
239
+
240
+        public LuResult<long> EditSingleByIdId(string id, Action<TModel> update)
241
+        {
242
+            return EditSingleById(id, update, GetIdFunc<long>());
243
+        }
244
+
245
+        public LuResult<TDboRead> EditSingleByIdDbo(string id, Action<TModel> update)
246
+        {
247
+            return EditSingleById(id, update, read => read);
248
+        }
249
+
250
+
251
+        public LuResult<T> EditSingleById<T>(long id, TDboUpdate update, Func<TDboRead, T> returnFunc)
252
+        {
253
+            return Edit(GetExpression(new KeyValuePair<string, object>("id", id)),
254
+                model => EditModelFromTUpdate(update, model), list => returnFunc(list.FirstOrDefault()));
255
+        }
256
+
257
+        public LuResult<string> EditSingleByIdGuid(long id, TDboUpdate update)
258
+        {
259
+            return EditSingleById(id, update, GetIdFunc<string>());
260
+        }
261
+
262
+        public LuResult<long> EditSingleByIdId(long id, TDboUpdate update)
263
+        {
264
+            return EditSingleById(id, update, GetIdFunc<long>());
265
+        }
266
+
267
+        public LuResult<TDboRead> EditSingleByIdDbo(long id, TDboUpdate update)
268
+        {
269
+            return EditSingleById(id, update, read => read);
270
+        }
271
+
272
+
273
+        public LuResult<T> EditSingleById<T>(string id, TDboUpdate update, Func<TDboRead, T> returnFunc)
274
+        {
275
+            return Edit(GetExpression(new KeyValuePair<string, object>("id", id)),
276
+                model => EditModelFromTUpdate(update, model), list => returnFunc(list.FirstOrDefault()));
277
+        }
278
+
279
+        public LuResult<string> EditSingleByIdGuid(string id, TDboUpdate update)
280
+        {
281
+            return EditSingleById(id, update, GetIdFunc<string>());
282
+        }
283
+
284
+        public LuResult<long> EditSingleByIdId(string id, TDboUpdate update)
285
+        {
286
+            return EditSingleById(id, update, GetIdFunc<long>());
287
+        }
288
+
289
+        public LuResult<TDboRead> EditSingleByIdDbo(string id, TDboUpdate update)
290
+        {
291
+            return EditSingleById(id, update, read => read);
292
+        }
293
+
294
+
295
+
296
+
297
+        public LuResult<T> Delete<T>(Expression<Func<TModel, bool>> predicate,
298
+            Func<IEnumerable<TDboRead>, T> returnFunc)
299
+        {
300
+            return Execute(() =>
301
+            {
302
+                var models = Table.Where(predicate).ToList();
303
+                Table.RemoveRange(models);
304
+                Db.SaveChanges();
305
+                var dbos = models.Select(GetDboFromModel);
306
+                return LuResult<T>.Ok(returnFunc(dbos));
307
+            });
308
+        }
309
+
310
+        public LuResult<IEnumerable<string>> DeleteGuid(Expression<Func<TModel, bool>> predicate)
311
+        {
312
+            var func = GetIdFunc<string>();
313
+            return Delete(predicate, reads => reads.Select(func));
314
+        }
315
+
316
+        public LuResult<IEnumerable<long>> DeleteId(Expression<Func<TModel, bool>> predicate)
317
+        {
318
+            var func = GetIdFunc<long>();
319
+            return Delete(predicate, reads => reads.Select(func));
320
+        }
321
+
322
+        public LuResult<IEnumerable<TDboRead>> DeleteDbo(Expression<Func<TModel, bool>> predicate)
323
+        {
324
+            return Delete(predicate, read => read);
325
+        }
326
+
327
+
328
+        public LuResult<T> DeleteSingleById<T>(string id, Func<TDboRead, T> returnFunc)
329
+        {
330
+            return Delete(GetExpression(new KeyValuePair<string, object>("id", new Guid(id))),
331
+                reads => returnFunc(reads.First()));
332
+        }
333
+
334
+        public LuResult<string> DeleteSingleByIdGuid(string id)
335
+        {
336
+            var func = GetIdFunc<string>();
337
+            return DeleteSingleById(id, func);
338
+        }
339
+
340
+        public LuResult<long> DeleteSingleByIdId(string id)
341
+        {
342
+            var func = GetIdFunc<long>();
343
+            return DeleteSingleById(id, func);
344
+        }
345
+
346
+        public LuResult<TDboRead> DeleteSingleByIdDbo(string id)
347
+        {
348
+            return DeleteSingleById(id, read => read);
349
+        }
350
+
351
+
352
+        public LuResult<T> DeleteSingleById<T>(long id, Func<TDboRead, T> returnFunc)
353
+        {
354
+            return Delete(GetExpression(new KeyValuePair<string, object>("id", id)),
355
+                reads => returnFunc(reads.First()));
356
+        }
357
+
358
+        public LuResult<string> DeleteSingleByGuid(long id)
359
+        {
360
+            var func = GetIdFunc<string>();
361
+            return DeleteSingleById(id, func);
362
+        }
363
+
364
+        public LuResult<long> DeleteSingleByIdId(long id)
365
+        {
366
+            var func = GetIdFunc<long>();
367
+            return DeleteSingleById(id, func);
368
+        }
369
+
370
+        public LuResult<TDboRead> DeleteSingleByIdDbo(long id)
371
+        {
372
+            return DeleteSingleById(id, read => read);
186 373
         }
187 374
     }
188 375
 }

+ 141
- 141
Test/Auth/Groups/LuGroupsDataAccessTest.cs View File

@@ -13,146 +13,146 @@ namespace Test.Auth.Groups
13 13
 {
14 14
     public class LuGroupsDataAccessTest
15 15
     {
16
-        [Fact]
17
-        public void TestMock()
18
-        {
19
-            var mockSet = new Mock<DbSet<lu_groups>>();
20
-
21
-            var mockContext = new Mock<LuDatabaseContext>();
22
-            mockContext.Setup(m => m.lu_groups).Returns(mockSet.Object);
23
-
24
-            var service = new LuGroupsDataAccess(mockContext.Object);
25
-            var res = service.AddDbo(new LuGroupsAddDbo {Name = "Test."});
26
-            Assert.Equal(LuStatus.Success, res.Status);
27
-            Assert.Equal(new Guid().ToString(), res.Data.Id);
28
-            Assert.Equal("Test.", res.Data.Name);
29
-
30
-            mockSet.Verify(m => m.AddRange(It.IsAny<IEnumerable<lu_groups>>()), Times.Once());
31
-            mockContext.Verify(m => m.SaveChanges(), Times.Once());
32
-        }
33
-
34
-        [Fact]
35
-        public void TestReal1()
36
-        {
37
-            Tests.TestRealDb(context =>
38
-            {
39
-                const string name = "Test.";
40
-                var service = new LuGroupsDataAccess(context);
41
-                var res = service.AddDbo(new LuGroupsAddDbo {Name = name});
42
-                Assert.Equal(LuStatus.Success, res.Status);
43
-                Assert.NotEqual(new Guid().ToString(), res.Data.Id);
44
-                Assert.Equal(name, res.Data.Name);
45
-
46
-                var get = service.GetSingleById(res.Data.Id);
47
-                Assert.Equal(LuStatus.Success, get.Status);
48
-                Assert.Equal(name, get.Data.Name);
49
-                Assert.Equal(res.Data.Id, get.Data.Id);
50
-            });
51
-        }
52
-
53
-        [Fact]
54
-        public void TestReal2()
55
-        {
56
-            Tests.TestRealDb(context =>
57
-            {
58
-                const string name = "Test.";
59
-                var service = new LuGroupsDataAccess(context);
60
-                var res = service.AddGuid(new LuGroupsAddDbo {Name = name});
61
-                Assert.Equal(LuStatus.Success, res.Status);
62
-                Assert.NotEqual(new Guid().ToString(), res.Data);
63
-
64
-                var get = service.GetSingleById(res.Data);
65
-                Assert.Equal(LuStatus.Success, get.Status);
66
-                Assert.Equal(name, get.Data.Name);
67
-            });
68
-        }
69
-
70
-        [Fact]
71
-        public void TestReal3()
72
-        {
73
-            Tests.TestRealDb(context =>
74
-            {
75
-                const string name = "Test.";
76
-                var service = new LuGroupsDataAccess(context);
77
-                var res = service.AddGuid(new LuGroupsAddDbo {Name = name});
78
-                Assert.Equal(LuStatus.Success, res.Status);
79
-                Assert.NotEqual(new Guid().ToString(), res.Data);
80
-
81
-                var get = service.GetSingleById(res.Data);
82
-                Assert.Equal(LuStatus.Success, get.Status);
83
-                Assert.Equal(name, get.Data.Name);
84
-            });
85
-        }
86
-
87
-        [Fact]
88
-        public void TestReal4()
89
-        {
90
-            Tests.TestRealDb(context =>
91
-            {
92
-                var names = new List<string>{"001-Test.", "010-Test.", "030-Test.", "020-Test.", "000-Test."};
93
-                var service = new LuGroupsDataAccess(context);
94
-                var res = service.AddGuid(names.Select(s => new LuGroupsAddDbo {Name = s}).ToList());
95
-                Assert.Equal(LuStatus.Success, res.Status);
96
-                foreach (var id in res.Data)
97
-                {
98
-                    Assert.NotEqual(new Guid().ToString(), id);
99
-                }
100
-
101
-                var get = service.GetMultiple(groups => groups.name, 0, 2);
102
-                Assert.Equal(LuStatus.Success, get.Status);
103
-                Assert.Equal(5, get.Data.Count);
104
-                Assert.Equal(2, get.Data.Data.Count);
105
-                Assert.Equal("000-Test.", get.Data.Data[0].Name);
106
-                Assert.Equal("001-Test.", get.Data.Data[1].Name);
107
-
108
-                get = service.GetMultiple(groups => groups.name, 2, 2);
109
-                Assert.Equal(LuStatus.Success, get.Status);
110
-                Assert.Equal(5, get.Data.Count);
111
-                Assert.Equal(1, get.Data.Data.Count);
112
-                Assert.Equal("030-Test.", get.Data.Data[0].Name);
113
-
114
-                get = service.GetMultiple(groups => groups.name, 3, 2);
115
-                Assert.Equal(LuStatus.Success, get.Status);
116
-                Assert.Equal(5, get.Data.Count);
117
-                Assert.Equal(0, get.Data.Data.Count);
118
-
119
-                get = service.GetMultiple(groups => groups.name, groups => groups.name.Contains("0-"), 1, 1);
120
-                Assert.Equal(LuStatus.Success, get.Status);
121
-                Assert.Equal(4, get.Data.Count);
122
-                Assert.Equal(1, get.Data.Data.Count);
123
-                Assert.Equal("010-Test.", get.Data.Data[0].Name);
124
-            });
125
-        }
126
-
127
-        [Fact]
128
-        public void TestReal5()
129
-        {
130
-            Tests.TestRealDb(context =>
131
-            {
132
-                string[] names = {"001-Test.", "010-Test.", "030-Test.", "020-Test.", "000-Test."};
133
-                var service = new LuGroupsDataAccess(context);
134
-                var ids = new List<string>();
135
-                foreach (var name in names)
136
-                {
137
-                    var res = service.AddGuid(new LuGroupsAddDbo {Name = name});
138
-                    Assert.Equal(LuStatus.Success, res.Status);
139
-                    Assert.NotEqual(new Guid().ToString(), res.Data);
140
-                    ids.Add(res.Data);
141
-                }
142
-
143
-                var edit = service.Edit(groups => groups.name.Contains("0-"),
144
-                    groups => groups.name = groups.name + "_Edited", dbos => dbos);
145
-                Assert.Equal(LuStatus.Success, edit.Status);
146
-                Assert.Equal(4, edit.Data.Count);
147
-
148
-                var edit2 = service.EditSingleById(ids[0], groups => groups.name = groups.name + "_Edited", dbo => dbo);
149
-                Assert.Equal(LuStatus.Success, edit2.Status);
150
-
151
-                var get = service.GetMultiple(groups => groups.name, groups => groups.name.EndsWith("_Edited"), 0, names.Length);
152
-                Assert.Equal(LuStatus.Success, get.Status);
153
-                Assert.Equal(names.Length, get.Data.Count);
154
-                Assert.Equal(names.Length, get.Data.Data.Count);
155
-            });
156
-        }
16
+//        [Fact]
17
+//        public void TestMock()
18
+//        {
19
+//            var mockSet = new Mock<DbSet<lu_groups>>();
20
+//
21
+//            var mockContext = new Mock<LuDatabaseContext>();
22
+//            mockContext.Setup(m => m.lu_groups).Returns(mockSet.Object);
23
+//
24
+//            var service = new LuGroupsDataAccess(mockContext.Object);
25
+//            var res = service.AddDbo(new LuGroupsAddDbo {Name = "Test."});
26
+//            Assert.Equal(LuStatus.Success, res.Status);
27
+//            Assert.Equal(new Guid().ToString(), res.Data.Id);
28
+//            Assert.Equal("Test.", res.Data.Name);
29
+//
30
+//            mockSet.Verify(m => m.AddRange(It.IsAny<IEnumerable<lu_groups>>()), Times.Once());
31
+//            mockContext.Verify(m => m.SaveChanges(), Times.Once());
32
+//        }
33
+//
34
+//        [Fact]
35
+//        public void TestReal1()
36
+//        {
37
+//            Tests.TestRealDb(context =>
38
+//            {
39
+//                const string name = "Test.";
40
+//                var service = new LuGroupsDataAccess(context);
41
+//                var res = service.AddDbo(new LuGroupsAddDbo {Name = name});
42
+//                Assert.Equal(LuStatus.Success, res.Status);
43
+//                Assert.NotEqual(new Guid().ToString(), res.Data.Id);
44
+//                Assert.Equal(name, res.Data.Name);
45
+//
46
+//                var get = service.GetSingleById(res.Data.Id);
47
+//                Assert.Equal(LuStatus.Success, get.Status);
48
+//                Assert.Equal(name, get.Data.Name);
49
+//                Assert.Equal(res.Data.Id, get.Data.Id);
50
+//            });
51
+//        }
52
+//
53
+//        [Fact]
54
+//        public void TestReal2()
55
+//        {
56
+//            Tests.TestRealDb(context =>
57
+//            {
58
+//                const string name = "Test.";
59
+//                var service = new LuGroupsDataAccess(context);
60
+//                var res = service.AddGuid(new LuGroupsAddDbo {Name = name});
61
+//                Assert.Equal(LuStatus.Success, res.Status);
62
+//                Assert.NotEqual(new Guid().ToString(), res.Data);
63
+//
64
+//                var get = service.GetSingleById(res.Data);
65
+//                Assert.Equal(LuStatus.Success, get.Status);
66
+//                Assert.Equal(name, get.Data.Name);
67
+//            });
68
+//        }
69
+//
70
+//        [Fact]
71
+//        public void TestReal3()
72
+//        {
73
+//            Tests.TestRealDb(context =>
74
+//            {
75
+//                const string name = "Test.";
76
+//                var service = new LuGroupsDataAccess(context);
77
+//                var res = service.AddGuid(new LuGroupsAddDbo {Name = name});
78
+//                Assert.Equal(LuStatus.Success, res.Status);
79
+//                Assert.NotEqual(new Guid().ToString(), res.Data);
80
+//
81
+//                var get = service.GetSingleById(res.Data);
82
+//                Assert.Equal(LuStatus.Success, get.Status);
83
+//                Assert.Equal(name, get.Data.Name);
84
+//            });
85
+//        }
86
+//
87
+//        [Fact]
88
+//        public void TestReal4()
89
+//        {
90
+//            Tests.TestRealDb(context =>
91
+//            {
92
+//                var names = new List<string>{"001-Test.", "010-Test.", "030-Test.", "020-Test.", "000-Test."};
93
+//                var service = new LuGroupsDataAccess(context);
94
+//                var res = service.AddGuid(names.Select(s => new LuGroupsAddDbo {Name = s}).ToList());
95
+//                Assert.Equal(LuStatus.Success, res.Status);
96
+//                foreach (var id in res.Data)
97
+//                {
98
+//                    Assert.NotEqual(new Guid().ToString(), id);
99
+//                }
100
+//
101
+//                var get = service.GetMultiple(groups => groups.name, 0, 2);
102
+//                Assert.Equal(LuStatus.Success, get.Status);
103
+//                Assert.Equal(5, get.Data.Count);
104
+//                Assert.Equal(2, get.Data.Data.Count);
105
+//                Assert.Equal("000-Test.", get.Data.Data[0].Name);
106
+//                Assert.Equal("001-Test.", get.Data.Data[1].Name);
107
+//
108
+//                get = service.GetMultiple(groups => groups.name, 2, 2);
109
+//                Assert.Equal(LuStatus.Success, get.Status);
110
+//                Assert.Equal(5, get.Data.Count);
111
+//                Assert.Equal(1, get.Data.Data.Count);
112
+//                Assert.Equal("030-Test.", get.Data.Data[0].Name);
113
+//
114
+//                get = service.GetMultiple(groups => groups.name, 3, 2);
115
+//                Assert.Equal(LuStatus.Success, get.Status);
116
+//                Assert.Equal(5, get.Data.Count);
117
+//                Assert.Equal(0, get.Data.Data.Count);
118
+//
119
+//                get = service.GetMultiple(groups => groups.name, groups => groups.name.Contains("0-"), 1, 1);
120
+//                Assert.Equal(LuStatus.Success, get.Status);
121
+//                Assert.Equal(4, get.Data.Count);
122
+//                Assert.Equal(1, get.Data.Data.Count);
123
+//                Assert.Equal("010-Test.", get.Data.Data[0].Name);
124
+//            });
125
+//        }
126
+//
127
+//        [Fact]
128
+//        public void TestReal5()
129
+//        {
130
+//            Tests.TestRealDb(context =>
131
+//            {
132
+//                string[] names = {"001-Test.", "010-Test.", "030-Test.", "020-Test.", "000-Test."};
133
+//                var service = new LuGroupsDataAccess(context);
134
+//                var ids = new List<string>();
135
+//                foreach (var name in names)
136
+//                {
137
+//                    var res = service.AddGuid(new LuGroupsAddDbo {Name = name});
138
+//                    Assert.Equal(LuStatus.Success, res.Status);
139
+//                    Assert.NotEqual(new Guid().ToString(), res.Data);
140
+//                    ids.Add(res.Data);
141
+//                }
142
+//
143
+//                var edit = service.Edit(groups => groups.name.Contains("0-"),
144
+//                    groups => groups.name = groups.name + "_Edited", dbos => dbos);
145
+//                Assert.Equal(LuStatus.Success, edit.Status);
146
+//                Assert.Equal(4, edit.Data.Count());
147
+//
148
+//                var edit2 = service.EditSingleById(ids[0], groups => groups.name = groups.name + "_Edited", dbo => dbo);
149
+//                Assert.Equal(LuStatus.Success, edit2.Status);
150
+//
151
+//                var get = service.GetMultiple(groups => groups.name, groups => groups.name.EndsWith("_Edited"), 0, names.Length);
152
+//                Assert.Equal(LuStatus.Success, get.Status);
153
+//                Assert.Equal(names.Length, get.Data.Count);
154
+//                Assert.Equal(names.Length, get.Data.Data.Count);
155
+//            });
156
+//        }
157 157
     }
158 158
 }

Test/Tests.cs → Test/Auth/Tests.cs View File

@@ -1,7 +1,7 @@
1 1
 using System;
2 2
 using Luticate2.Auth.DataAccess;
3 3
 
4
-namespace Test
4
+namespace Test.Auth
5 5
 {
6 6
     public class Tests
7 7
     {

+ 67
- 0
Test/Utils/DataAccess/LuUtilsDbContext.cs View File

@@ -0,0 +1,67 @@
1
+using Microsoft.EntityFrameworkCore;
2
+using Luticate2.Auth.DataAccess.Models;
3
+using Test.Utils.DataAccess.Models;
4
+
5
+namespace Test.Utils.DataAccess
6
+{
7
+    public partial class LuUtilsDbContext : DbContext
8
+    {
9
+        private readonly string _connectionString;
10
+
11
+        public LuUtilsDbContext()
12
+        {
13
+        }
14
+
15
+        public LuUtilsDbContext(string connectionString)
16
+        {
17
+            _connectionString = connectionString;
18
+        }
19
+
20
+        public LuUtilsDbContext(DbContextOptions options) :base(options)
21
+        {
22
+        }
23
+
24
+        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
25
+        {
26
+            if (_connectionString != null) {
27
+                optionsBuilder.UseNpgsql(_connectionString);
28
+            }
29
+        }
30
+
31
+        protected override void OnModelCreating(ModelBuilder modelBuilder)
32
+        {
33
+            
34
+            modelBuilder.Entity<pk_bigserial>()
35
+                .HasKey(c => new { c.id });
36
+            
37
+            modelBuilder.Entity<pk_bigserial>()
38
+                .Property(e => e.id)
39
+                .HasDefaultValueSql("nextval('pk_bigserial_id_seq'::regclass)");
40
+            
41
+            modelBuilder.Entity<pk_bigserial>()
42
+                .Property(e => e.created_at)
43
+                .HasDefaultValueSql("now()");
44
+            
45
+            
46
+            
47
+            modelBuilder.Entity<pk_guid>()
48
+                .HasKey(c => new { c.id });
49
+            
50
+            modelBuilder.Entity<pk_guid>()
51
+                .Property(e => e.id)
52
+                .HasDefaultValueSql("uuid_generate_v1()");
53
+            
54
+            modelBuilder.Entity<pk_guid>()
55
+                .Property(e => e.created_at)
56
+                .HasDefaultValueSql("now()");
57
+            
58
+            
59
+            
60
+        }
61
+        
62
+        public virtual DbSet<pk_bigserial> pk_bigserial { get; set; }
63
+        
64
+        public virtual DbSet<pk_guid> pk_guid { get; set; }
65
+        
66
+    }
67
+}

+ 35
- 0
Test/Utils/DataAccess/LuUtilsPkBigSerialDataAccess.cs View File

@@ -0,0 +1,35 @@
1
+using Luticate2.Utils.DataAccess;
2
+using Test.Utils.DataAccess.Models;
3
+using Test.Utils.Dbo.PkBigSerial;
4
+
5
+namespace Test.Utils.DataAccess
6
+{
7
+    public class LuUtilsPkBigSerialDataAccess : LuEfCrudDataAccess<pk_bigserial, PkBigSerialAddDbo, PkBigSerialDbo, PkBigSerialAddDbo, LuUtilsDbContext>
8
+    {
9
+        public LuUtilsPkBigSerialDataAccess(LuUtilsDbContext db) : base(db, db.pk_bigserial)
10
+        {
11
+        }
12
+
13
+        protected override pk_bigserial GetModelFromTCreate(PkBigSerialAddDbo obj)
14
+        {
15
+            return GetModelFromTUpdate(obj, new pk_bigserial());
16
+        }
17
+
18
+        protected override void EditModelFromTUpdate(PkBigSerialAddDbo obj, pk_bigserial model)
19
+        {
20
+            model.some_int = obj.SomeInt;
21
+            model.some_text = obj.SomeText;
22
+        }
23
+
24
+        protected override PkBigSerialDbo GetDboFromModel(pk_bigserial model)
25
+        {
26
+            return new PkBigSerialDbo
27
+            {
28
+                CreatedAt = model.created_at,
29
+                Id = model.id,
30
+                SomeInt = model.some_int,
31
+                SomeText = model.some_text
32
+            };
33
+        }
34
+    }
35
+}

+ 35
- 0
Test/Utils/DataAccess/LuUtilsPkGuidDataAccess.cs View File

@@ -0,0 +1,35 @@
1
+using Luticate2.Utils.DataAccess;
2
+using Test.Utils.DataAccess.Models;
3
+using Test.Utils.Dbo.PkGuid;
4
+
5
+namespace Test.Utils.DataAccess
6
+{
7
+    public class LuUtilsPkGuidDataAccess : LuEfCrudDataAccess<pk_guid, PkGuidAddDbo, PkGuidDbo, PkGuidAddDbo, LuUtilsDbContext>
8
+    {
9
+        public LuUtilsPkGuidDataAccess(LuUtilsDbContext db) : base(db, db.pk_guid)
10
+        {
11
+        }
12
+
13
+        protected override pk_guid GetModelFromTCreate(PkGuidAddDbo obj)
14
+        {
15
+            return GetModelFromTUpdate(obj, new pk_guid());
16
+        }
17
+
18
+        protected override void EditModelFromTUpdate(PkGuidAddDbo obj, pk_guid model)
19
+        {
20
+            model.some_int = obj.SomeInt;
21
+            model.some_text = obj.SomeText;
22
+        }
23
+
24
+        protected override PkGuidDbo GetDboFromModel(pk_guid model)
25
+        {
26
+            return new PkGuidDbo
27
+            {
28
+                CreatedAt = model.created_at,
29
+                Id = model.id.ToString(),
30
+                SomeInt = model.some_int,
31
+                SomeText = model.some_text
32
+            };
33
+        }
34
+    }
35
+}

+ 22
- 0
Test/Utils/DataAccess/Models/pk_bigserial.cs View File

@@ -0,0 +1,22 @@
1
+using System;
2
+using System.Collections.Generic;
3
+using System.ComponentModel.DataAnnotations;
4
+using System.ComponentModel.DataAnnotations.Schema;
5
+
6
+namespace Test.Utils.DataAccess.Models
7
+{
8
+    public partial class pk_bigserial
9
+    {
10
+        
11
+        public long id { get; set; }
12
+        
13
+        public string some_text { get; set; }
14
+        
15
+        public int? some_int { get; set; }
16
+        
17
+        public DateTime created_at { get; set; }
18
+        
19
+        
20
+        
21
+    }
22
+}

+ 22
- 0
Test/Utils/DataAccess/Models/pk_guid.cs View File

@@ -0,0 +1,22 @@
1
+using System;
2
+using System.Collections.Generic;
3
+using System.ComponentModel.DataAnnotations;
4
+using System.ComponentModel.DataAnnotations.Schema;
5
+
6
+namespace Test.Utils.DataAccess.Models
7
+{
8
+    public partial class pk_guid
9
+    {
10
+        
11
+        public Guid id { get; set; }
12
+        
13
+        public string some_text { get; set; }
14
+        
15
+        public int? some_int { get; set; }
16
+        
17
+        public DateTime created_at { get; set; }
18
+        
19
+        
20
+        
21
+    }
22
+}

+ 54
- 0
Test/Utils/DataAccess/code-from-ds/DataSource.twig View File

@@ -0,0 +1,54 @@
1
+using Microsoft.EntityFrameworkCore;
2
+using Luticate2.Auth.DataAccess.Models;
3
+using Test.Utils.DataAccess.Models;
4
+
5
+namespace Test.Utils.DataAccess
6
+{
7
+    public partial class LuUtilsDbContext : DbContext
8
+    {
9
+        private readonly string _connectionString;
10
+
11
+        public LuUtilsDbContext()
12
+        {
13
+        }
14
+
15
+        public LuUtilsDbContext(string connectionString)
16
+        {
17
+            _connectionString = connectionString;
18
+        }
19
+
20
+        public LuUtilsDbContext(DbContextOptions options) :base(options)
21
+        {
22
+        }
23
+
24
+        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
25
+        {
26
+            if (_connectionString != null) {
27
+                optionsBuilder.UseNpgsql(_connectionString);
28
+            }
29
+        }
30
+
31
+        protected override void OnModelCreating(ModelBuilder modelBuilder)
32
+        {
33
+            {% for table in dataSource.getTables() %}{% if (table.hasAny()) %}
34
+            modelBuilder.Entity<{{ table.getName() }}>()
35
+                .HasKey(c => new { {% for column in table.getPrimaryKeys() %}{% if (column.isSelected()) %}c.{{ column.getName() }}{% if not (loop.last) %}, {% endif %}{% endif %}{% endfor %} });
36
+            {% for column in table.getColumns() %}{% if (column.isSelected()) and (column.hasDefaultValue()) %}
37
+            modelBuilder.Entity<{{ table.getName() }}>()
38
+                .Property(e => e.{{ column.getName() }})
39
+                .HasDefaultValueSql("{{ column.getDefaultValueEscaped() }}");
40
+            {% endif %}{% endfor %}
41
+            {% for fk in table.getSourceForeignKeys() %}
42
+            modelBuilder.Entity<{{ fk.getSourceTable().getName() }}>()
43
+                .HasOne(e => e.{{ fk.getSourceForeignKeyName() }})
44
+                .WithMany(e => e.{{ fk.getTargetForeignKeyName() }})
45
+                .HasForeignKey("{% for column in fk.getSourceColumns() %}{{ column.getName }}{% if not (loop.last) %}, {% endif %}{% endfor %}")
46
+                .HasConstraintName("{{ fk.getName() }}");
47
+            {% endfor %}
48
+            {% endif %}{% endfor %}
49
+        }
50
+        {% for table in dataSource.getTables() %}{% if (table.hasAny()) %}
51
+        public virtual DbSet<{{ table.getName() }}> {{ table.getName() }} { get; set; }
52
+        {% endif %}{% endfor %}
53
+    }
54
+}

+ 20
- 0
Test/Utils/DataAccess/code-from-ds/Models.twig View File

@@ -0,0 +1,20 @@
1
+using System;
2
+using System.Collections.Generic;
3
+using System.ComponentModel.DataAnnotations;
4
+using System.ComponentModel.DataAnnotations.Schema;
5
+
6
+namespace Test.Utils.DataAccess.Models
7
+{
8
+    public partial class {{ table.getName() }}
9
+    {
10
+        {% for column in table.getColumns() %}{% if (column.isSelected()) %}
11
+        public {{ column.getType() }}{% if (not (column.isNotNull()) and (column.isTypeNotNull())) %}?{% endif %} {{ column.getName() }} { get; set; }
12
+        {% endif %}{% endfor %}
13
+        {% for fk in table.getSourceForeignKeys() %}{% if (fk.isSelected()) %}
14
+        public virtual {{ fk.getTargetTable().getName() }} {{ fk.getSourceForeignKeyName() }} { get; set; }
15
+        {% endif %}{% endfor %}
16
+        {% for fk in table.getTargetForeignKeys() %}{% if (fk.isSelected()) %}
17
+        public virtual ICollection<{{ fk.getSourceTable().getName() }}> {{ fk.getTargetForeignKeyName() }} { get; set; }
18
+        {% endif %}{% endfor %}
19
+    }
20
+}

+ 55
- 0
Test/Utils/DataAccess/code-from-ds/code-from-ds.json View File

@@ -0,0 +1,55 @@
1
+{
2
+    "castFileRelativePath": "../../Test/Utils/DataAccess/code-from-ds/types-cast.json",
3
+    "dataSourceRelativePath": "../../Test/Utils/DataAccess/LuUtilsDbContext.cs",
4
+    "modelsRelativePath": "../../Test/Utils/DataAccess/Models",
5
+    "selection": {
6
+        "tables": [
7
+            {
8
+                "columns": [
9
+                    {
10
+                        "column": "id",
11
+                        "selected": true
12
+                    },
13
+                    {
14
+                        "column": "some_text",
15
+                        "selected": true
16
+                    },
17
+                    {
18
+                        "column": "some_int",
19
+                        "selected": true
20
+                    },
21
+                    {
22
+                        "column": "created_at",
23
+                        "selected": true
24
+                    }
25
+                ],
26
+                "table": "pk_bigserial"
27
+            },
28
+            {
29
+                "columns": [
30
+                    {
31
+                        "column": "id",
32
+                        "selected": true
33
+                    },
34
+                    {
35
+                        "column": "some_text",
36
+                        "selected": true
37
+                    },
38
+                    {
39
+                        "column": "some_int",
40
+                        "selected": true
41
+                    },
42
+                    {
43
+                        "column": "created_at",
44
+                        "selected": true
45
+                    }
46
+                ],
47
+                "table": "pk_guid"
48
+            }
49
+        ],
50
+        "source": "luticate2_utils"
51
+    },
52
+    "dataSourceTemplateRelativePath": "../../Test/Utils/DataAccess/code-from-ds/DataSource.twig",
53
+    "filesExtension": "cs",
54
+    "modelsTemplateRelativePath": "../../Test/Utils/DataAccess/code-from-ds/Models.twig"
55
+}

+ 69
- 0
Test/Utils/DataAccess/code-from-ds/types-cast.json View File

@@ -0,0 +1,69 @@
1
+{
2
+    "arrayTemplate": "Array",
3
+    "non-nullable-types": [
4
+        "bool",
5
+        "char",
6
+        "short",
7
+        "int",
8
+        "long",
9
+        "float",
10
+        "double",
11
+        "decimal",
12
+        "NpgsqlTypes.NpgsqlPoint",
13
+        "NpgsqlTypes.NpgsqlLSeg",
14
+        "NpgsqlTypes.NpgsqlPath",
15
+        "NpgsqlTypes.NpgsqlPolygon",
16
+        "NpgsqlTypes.NpgsqlLine",
17
+        "NpgsqlTypes.NpgsqlCircle",
18
+        "NpgsqlTypes.NpgsqlBox",
19
+        "NpgsqlTypes.NpgsqlInet",
20
+        "Guid",
21
+        "DateTime",
22
+        "TimeSpan",
23
+        "DateTimeOffset"
24
+    ],
25
+    "types": {
26
+        "boolean": "bool",
27
+        "smallint": "short",
28
+        "integer": "int",
29
+        "bigint": "long",
30
+        "real": "float",
31
+        "double precision": "double",
32
+        "numeric": "decimal",
33
+        "money": "decimal",
34
+        "text": "string",
35
+        "varchar": "string",
36
+        "json": "string",
37
+        "jsonb": "string",
38
+        "xml": "string",
39
+        "point": "NpgsqlTypes.NpgsqlPoint",
40
+        "lseg": "NpgsqlTypes.NpgsqlLSeg",
41
+        "path": "NpgsqlTypes.NpgsqlPath",
42
+        "polygon": "NpgsqlTypes.NpgsqlPolygon",
43
+        "line": "NpgsqlTypes.NpgsqlLine",
44
+        "circle": "NpgsqlTypes.NpgsqlCircle",
45
+        "box": "NpgsqlTypes.NpgsqlBox",
46
+        "bit": {
47
+            "-1": "bool",
48
+            "1": "bool",
49
+            "*": "System.Collections.BitArray"
50
+        },
51
+        "bit varying": "System.Collections.BitArray",
52
+        "uuid": "Guid",
53
+        "cidr": "NpgsqlTypes.NpgsqlInet",
54
+        "inet": "System.Net.IPAddress",
55
+        "macaddr": "System.Net.NetworkInformation.PhysicalAddress",
56
+        "tsquery": "NpgsqlTypes.NpgsqlTsQuery",
57
+        "tsvector": "NpgsqlTypes.NpgsqlTsVector",
58
+        "date": "DateTime",
59
+        "interval": "TimeSpan",
60
+        "timestamp": "DateTime",
61
+        "timestamp with time zone": "DateTime",
62
+        "time": "TimeSpan",
63
+        "time with time zone": "DateTimeOffset",
64
+        "bytea": "byte[]",
65
+        "name": "string",
66
+        "\"char\"": "char",
67
+        "char": "char"
68
+    }
69
+}

+ 9
- 0
Test/Utils/Dbo/PkBigSerial/PkBigSerialAddDbo.cs View File

@@ -0,0 +1,9 @@
1
+namespace Test.Utils.Dbo.PkBigSerial
2
+{
3
+    public class PkBigSerialAddDbo
4
+    {
5
+        public string SomeText { get; set; }
6
+
7
+        public int? SomeInt { get; set; }
8
+    }
9
+}

+ 15
- 0
Test/Utils/Dbo/PkBigSerial/PkBigSerialDbo.cs View File

@@ -0,0 +1,15 @@
1
+using System;
2
+
3
+namespace Test.Utils.Dbo.PkBigSerial
4
+{
5
+    public class PkBigSerialDbo
6
+    {
7
+        public long Id { get; set; }
8
+
9
+        public string SomeText { get; set; }
10
+
11
+        public int? SomeInt { get; set; }
12
+
13
+        public DateTime CreatedAt { get; set; }
14
+    }
15
+}

+ 9
- 0
Test/Utils/Dbo/PkGuid/PkGuidAddDbo.cs View File

@@ -0,0 +1,9 @@
1
+namespace Test.Utils.Dbo.PkGuid
2
+{
3
+    public class PkGuidAddDbo
4
+    {
5
+        public string SomeText { get; set; }
6
+
7
+        public int? SomeInt { get; set; }
8
+    }
9
+}

+ 15
- 0
Test/Utils/Dbo/PkGuid/PkGuidDbo.cs View File

@@ -0,0 +1,15 @@
1
+using System;
2
+
3
+namespace Test.Utils.Dbo.PkGuid
4
+{
5
+    public class PkGuidDbo
6
+    {
7
+        public string Id { get; set; }
8
+
9
+        public string SomeText { get; set; }
10
+
11
+        public int? SomeInt { get; set; }
12
+
13
+        public DateTime CreatedAt { get; set; }
14
+    }
15
+}

+ 361
- 0
Test/Utils/EfCrubDataAccess/LuEfCreateDataAccessTest.cs View File

@@ -0,0 +1,361 @@
1
+using System;
2
+using System.Collections.Generic;
3
+using System.Linq;
4
+using Luticate2.Utils.Dbo;
5
+using Test.Utils.DataAccess;
6
+using Test.Utils.Dbo.PkBigSerial;
7
+using Test.Utils.Dbo.PkGuid;
8
+using Xunit;
9
+
10
+namespace Test.Utils.EfCrubDataAccess
11
+{
12
+    public class LuEfCreateDataAccessTest
13
+    {
14
+        [Fact]
15
+        public void TestAddMultiple1()
16
+        {
17
+            Tests.TestRealDb(context =>
18
+            {
19
+                var dbos = new List<PkBigSerialAddDbo>
20
+                {
21
+                    new PkBigSerialAddDbo
22
+                    {
23
+                        SomeInt = 42,
24
+                        SomeText = "42"
25
+                    },
26
+                    new PkBigSerialAddDbo
27
+                    {
28
+                        SomeInt = 21,
29
+                        SomeText = "24"
30
+                    }
31
+                };
32
+                var service = new LuUtilsPkBigSerialDataAccess(context);
33
+                var res = service.Add(dbos, enumerable => enumerable);
34
+                Assert.Equal(LuStatus.Success, res.Status);
35
+                foreach (var dbo in dbos.Zip(res.Data, (dbo, serialDbo) => new KeyValuePair<PkBigSerialAddDbo, PkBigSerialDbo>(dbo, serialDbo)))
36
+                {
37
+                    Assert.NotNull(dbo.Key);
38
+                    Assert.NotNull(dbo.Value);
39
+
40
+                    Assert.NotEqual(0, dbo.Value.Id);
41
+                    Assert.Equal(dbo.Key.SomeText, dbo.Value.SomeText);
42
+                    Assert.Equal(dbo.Key.SomeInt, dbo.Value.SomeInt);
43
+
44
+                    var get = service.GetSingleById(dbo.Value.Id);
45
+                    Assert.Equal(LuStatus.Success, get.Status);
46
+                    Assert.Equal(dbo.Key.SomeText, get.Data.SomeText);
47
+                    Assert.Equal(dbo.Key.SomeInt, get.Data.SomeInt);
48
+                    Assert.Equal(dbo.Value.Id, get.Data.Id);
49
+                }
50
+            });
51
+        }
52
+
53
+        [Fact]
54
+        public void TestAddMultiple2()
55
+        {
56
+            Tests.TestRealDb(context =>
57
+            {
58
+                var dbos = new List<PkBigSerialAddDbo>
59
+                {
60
+                    new PkBigSerialAddDbo
61
+                    {
62
+                        SomeInt = 42,
63
+                        SomeText = "42"
64
+                    },
65
+                    new PkBigSerialAddDbo
66
+                    {
67
+                        SomeInt = 21,
68
+                        SomeText = "24"
69
+                    }
70
+                };
71
+                var service = new LuUtilsPkBigSerialDataAccess(context);
72
+                var res = service.Add(dbos, enumerable => enumerable.Select(dbo => dbo.Id));
73
+                Assert.Equal(LuStatus.Success, res.Status);
74
+                foreach (var dbo in dbos.Zip(res.Data, (dbo, serialDbo) => new KeyValuePair<PkBigSerialAddDbo, long>(dbo, serialDbo)))
75
+                {
76
+                    Assert.NotNull(dbo.Key);
77
+                    Assert.NotNull(dbo.Value);
78
+
79
+                    Assert.NotEqual(0, dbo.Value);
80
+
81
+                    var get = service.GetSingleById(dbo.Value);
82
+                    Assert.Equal(LuStatus.Success, get.Status);
83
+                    Assert.Equal(dbo.Key.SomeText, get.Data.SomeText);
84
+                    Assert.Equal(dbo.Key.SomeInt, get.Data.SomeInt);
85
+                    Assert.Equal(dbo.Value, get.Data.Id);
86
+                }
87
+            });
88
+        }
89
+
90
+        [Fact]
91
+        public void TestAddMultiple3()
92
+        {
93
+            Tests.TestRealDb(context =>
94
+            {
95
+                var dbos = new List<PkBigSerialAddDbo>
96
+                {
97
+                    new PkBigSerialAddDbo
98
+                    {
99
+                        SomeInt = 42,
100
+                        SomeText = "42"
101
+                    },
102
+                    new PkBigSerialAddDbo
103
+                    {
104
+                        SomeInt = 21,
105
+                        SomeText = "24"
106
+                    }
107
+                };
108
+                var service = new LuUtilsPkBigSerialDataAccess(context);
109
+                var res = service.Add(dbos, enumerable => enumerable.Select(dbo => dbo.CreatedAt));
110
+                Assert.Equal(LuStatus.Success, res.Status);
111
+                foreach (var dbo in res.Data)
112
+                {
113
+                    Assert.NotEqual(default(DateTime), dbo);
114
+                }
115
+            });
116
+        }
117
+
118
+
119
+        [Fact]
120
+        public void TestAddSingle1()
121
+        {
122
+            Tests.TestRealDb(context =>
123
+            {
124
+                var service = new LuUtilsPkBigSerialDataAccess(context);
125
+                var res = service.Add(new PkBigSerialAddDbo
126
+                {
127
+                    SomeInt = 42,
128
+                    SomeText = "42"
129
+                }, dbo => dbo);
130
+                Assert.Equal(LuStatus.Success, res.Status);
131
+                Assert.NotEqual(0, res.Data.Id);
132
+                Assert.Equal("42", res.Data.SomeText);
133
+                Assert.Equal(42, res.Data.SomeInt);
134
+
135
+                var get = service.GetSingleById(res.Data.Id);
136
+                Assert.Equal(LuStatus.Success, get.Status);
137
+                Assert.Equal("42", get.Data.SomeText);
138
+                Assert.Equal(42, get.Data.SomeInt);
139
+                Assert.Equal(res.Data.Id, get.Data.Id);
140
+            });
141
+        }
142
+
143
+        [Fact]
144
+        public void TestAddSingle2()
145
+        {
146
+            Tests.TestRealDb(context =>
147
+            {
148
+                var service = new LuUtilsPkBigSerialDataAccess(context);
149
+                var res = service.Add(new PkBigSerialAddDbo
150
+                {
151
+                    SomeInt = 42,
152
+                    SomeText = "42"
153
+                }, dbo => dbo.Id);
154
+                Assert.Equal(LuStatus.Success, res.Status);
155
+                Assert.NotEqual(0, res.Data);
156
+
157
+                var get = service.GetSingleById(res.Data);
158
+                Assert.Equal(LuStatus.Success, get.Status);
159
+                Assert.Equal("42", get.Data.SomeText);
160
+                Assert.Equal(42, get.Data.SomeInt);
161
+                Assert.Equal(res.Data, get.Data.Id);
162
+            });
163
+        }
164
+
165
+        [Fact]
166
+        public void TestAddSingle3()
167
+        {
168
+            Tests.TestRealDb(context =>
169
+            {
170
+                var service = new LuUtilsPkBigSerialDataAccess(context);
171
+                var res = service.Add(new PkBigSerialAddDbo
172
+                {
173
+                    SomeInt = 42,
174
+                    SomeText = "42"
175
+                }, dbo => dbo.CreatedAt);
176
+                Assert.Equal(LuStatus.Success, res.Status);
177
+                Assert.NotEqual(default(DateTime), res.Data);
178
+            });
179
+        }
180
+
181
+        [Fact]
182
+        public void TestAddGuidMultiple1()
183
+        {
184
+            Tests.TestRealDb(context =>
185
+            {
186
+                var dbos = new List<PkGuidAddDbo>
187
+                {
188
+                    new PkGuidAddDbo
189
+                    {
190
+                        SomeInt = 42,
191
+                        SomeText = "42"
192
+                    },
193
+                    new PkGuidAddDbo
194
+                    {
195
+                        SomeInt = 21,
196
+                        SomeText = "24"
197
+                    }
198
+                };
199
+                var service = new LuUtilsPkGuidDataAccess(context);
200
+                var res = service.AddGuid(dbos);
201
+                Assert.Equal(LuStatus.Success, res.Status);
202
+                foreach (var dbo in dbos.Zip(res.Data, (dbo, serialDbo) => new KeyValuePair<PkGuidAddDbo, string>(dbo, serialDbo)))
203
+                {
204
+                    Assert.NotNull(dbo.Key);
205
+                    Assert.NotNull(dbo.Value);
206
+
207
+                    Assert.NotEqual(new Guid().ToString(), dbo.Value);
208
+
209
+                    var get = service.GetSingleById(dbo.Value);
210
+                    Assert.Equal(LuStatus.Success, get.Status);
211
+                    Assert.Equal(dbo.Key.SomeText, get.Data.SomeText);
212
+                    Assert.Equal(dbo.Key.SomeInt, get.Data.SomeInt);
213
+                    Assert.Equal(dbo.Value, get.Data.Id);
214
+                }
215
+            });
216
+        }
217
+
218
+
219
+        [Fact]
220
+        public void TestAddGuidSingle1()
221
+        {
222
+            Tests.TestRealDb(context =>
223
+            {
224
+                var service = new LuUtilsPkGuidDataAccess(context);
225
+                var res = service.AddGuid(new PkGuidAddDbo
226
+                {
227
+                    SomeInt = 42,
228
+                    SomeText = "42"
229
+                });
230
+                Assert.Equal(LuStatus.Success, res.Status);
231
+                Assert.NotEqual(new Guid().ToString(), res.Data);
232
+
233
+                var get = service.GetSingleById(res.Data);
234
+                Assert.Equal(LuStatus.Success, get.Status);
235
+                Assert.Equal("42", get.Data.SomeText);
236
+                Assert.Equal(42, get.Data.SomeInt);
237
+                Assert.Equal(res.Data, get.Data.Id);
238
+            });
239
+        }
240
+
241
+        [Fact]
242
+        public void TestAddIdMultiple1()
243
+        {
244
+            Tests.TestRealDb(context =>
245
+            {
246
+                var dbos = new List<PkBigSerialAddDbo>
247
+                {
248
+                    new PkBigSerialAddDbo
249
+                    {
250
+                        SomeInt = 42,
251
+                        SomeText = "42"
252
+                    },
253
+                    new PkBigSerialAddDbo
254
+                    {
255
+                        SomeInt = 21,
256
+                        SomeText = "24"
257
+                    }
258
+                };
259
+                var service = new LuUtilsPkBigSerialDataAccess(context);
260
+                var res = service.AddId(dbos);
261
+                Assert.Equal(LuStatus.Success, res.Status);
262
+                foreach (var dbo in dbos.Zip(res.Data, (dbo, serialDbo) => new KeyValuePair<PkBigSerialAddDbo, long>(dbo, serialDbo)))
263
+                {
264
+                    Assert.NotNull(dbo.Key);
265
+                    Assert.NotNull(dbo.Value);
266
+
267
+                    Assert.NotEqual(0, dbo.Value);
268
+
269
+                    var get = service.GetSingleById(dbo.Value);
270
+                    Assert.Equal(LuStatus.Success, get.Status);
271
+                    Assert.Equal(dbo.Key.SomeText, get.Data.SomeText);
272
+                    Assert.Equal(dbo.Key.SomeInt, get.Data.SomeInt);
273
+                    Assert.Equal(dbo.Value, get.Data.Id);
274
+                }
275
+            });
276
+        }
277
+
278
+
279
+        [Fact]
280
+        public void TestAddIdSingle1()
281
+        {
282
+            Tests.TestRealDb(context =>
283
+            {
284
+                var service = new LuUtilsPkBigSerialDataAccess(context);
285
+                var res = service.AddId(new PkBigSerialAddDbo
286
+                {
287
+                    SomeInt = 42,
288
+                    SomeText = "42"
289
+                });
290
+                Assert.Equal(LuStatus.Success, res.Status);
291
+                Assert.NotEqual(0, res.Data);
292
+
293
+                var get = service.GetSingleById(res.Data);
294
+                Assert.Equal(LuStatus.Success, get.Status);
295
+                Assert.Equal("42", get.Data.SomeText);
296
+                Assert.Equal(42, get.Data.SomeInt);
297
+                Assert.Equal(res.Data, get.Data.Id);
298
+            });
299
+        }
300
+
301
+        [Fact]
302
+        public void TestAddDboMultiple1()
303
+        {
304
+            Tests.TestRealDb(context =>
305
+            {
306
+                var dbos = new List<PkBigSerialAddDbo>
307
+                {
308
+                    new PkBigSerialAddDbo
309
+                    {
310
+                        SomeInt = 42,
311
+                        SomeText = "42"
312
+                    },
313
+                    new PkBigSerialAddDbo
314
+                    {
315
+                        SomeInt = 21,
316
+                        SomeText = "24"
317
+                    }
318
+                };
319
+                var service = new LuUtilsPkBigSerialDataAccess(context);
320
+                var res = service.AddDbo(dbos);
321
+                Assert.Equal(LuStatus.Success, res.Status);
322
+                foreach (var dbo in dbos.Zip(res.Data, (dbo, serialDbo) => new KeyValuePair<PkBigSerialAddDbo, PkBigSerialDbo>(dbo, serialDbo)))
323
+                {
324
+                    Assert.NotNull(dbo.Key);
325
+                    Assert.NotNull(dbo.Value);
326
+
327
+                    Assert.NotEqual(0, dbo.Value.Id);
328
+
329
+                    var get = service.GetSingleById(dbo.Value.Id);
330
+                    Assert.Equal(LuStatus.Success, get.Status);
331
+                    Assert.Equal(dbo.Key.SomeText, get.Data.SomeText);
332
+                    Assert.Equal(dbo.Key.SomeInt, get.Data.SomeInt);
333
+                    Assert.Equal(dbo.Value.Id, get.Data.Id);
334
+                }
335
+            });
336
+        }
337
+
338
+
339
+        [Fact]
340
+        public void TestAddDboSingle1()
341
+        {
342
+            Tests.TestRealDb(context =>
343
+            {
344
+                var service = new LuUtilsPkBigSerialDataAccess(context);
345
+                var res = service.AddDbo(new PkBigSerialAddDbo
346
+                {
347
+                    SomeInt = 42,
348
+                    SomeText = "42"
349
+                });
350
+                Assert.Equal(LuStatus.Success, res.Status);
351
+                Assert.NotEqual(0, res.Data.Id);
352
+
353
+                var get = service.GetSingleById(res.Data.Id);
354
+                Assert.Equal(LuStatus.Success, get.Status);
355
+                Assert.Equal("42", get.Data.SomeText);
356
+                Assert.Equal(42, get.Data.SomeInt);
357
+                Assert.Equal(res.Data.Id, get.Data.Id);
358
+            });
359
+        }
360
+    }
361
+}

+ 23
- 0
Test/Utils/Tests.cs View File

@@ -0,0 +1,23 @@
1
+using System;
2
+using Test.Utils.DataAccess;
3
+
4
+namespace Test.Utils
5
+{
6
+    public class Tests
7
+    {
8
+        public static LuUtilsDbContext GetDataBaseContext()
9
+        {
10
+            return
11
+                new LuUtilsDbContext(
12
+                    "User ID=dev;Password=dev;Host=localhost;Port=5432;Database=luticate2_utils;Pooling=true;");
13
+        }
14
+
15
+        public static void TestRealDb(Action<LuUtilsDbContext> func)
16
+        {
17
+            var dbContext = GetDataBaseContext();
18
+            dbContext.Database.BeginTransaction();
19
+            func(dbContext);
20
+            dbContext.Database.RollbackTransaction();
21
+        }
22
+    }
23
+}

+ 1
- 1
Test/project.json View File

@@ -4,7 +4,7 @@
4 4
     "debugType": "portable"
5 5
   },
6 6
   "dependencies": {
7
-    "dotnet-test-xunit": "1.0.0-rc2-192208-24",
7
+    "dotnet-test-xunit": "1.0.0-rc2-*",
8 8
     "Luticate2.Auth": "1.0.*",
9 9
     "Moq": "4.6.38-alpha",
10 10
     "System.Runtime.Serialization.Primitives": "4.1.1",

+ 6
- 6
Test/project.lock.json View File

@@ -37,7 +37,7 @@
37 37
           "lib/netstandard1.3/Castle.Core.dll": {}
38 38
         }
39 39
       },
40
-      "dotnet-test-xunit/1.0.0-rc2-build10015": {
40
+      "dotnet-test-xunit/1.0.0-rc2-build10025": {
41 41
         "type": "package",
42 42
         "dependencies": {
43 43
           "Microsoft.Extensions.Testing.Abstractions": "1.0.0-preview1-002702",
@@ -4000,12 +4000,12 @@
4000 4000
         "readme.txt"
4001 4001
       ]
4002 4002
     },
4003
-    "dotnet-test-xunit/1.0.0-rc2-build10015": {
4004
-      "sha512": "2traZWYeJiFzau+1j9HcnSZ3rQLDyIrqKyYKCTbPPmu6lsQAtaOG5q+fuKS9Vaxczmh0IcvZ2hdWEuYmtVb9zw==",
4003
+    "dotnet-test-xunit/1.0.0-rc2-build10025": {
4004
+      "sha512": "MhxfSjj6z/dpct/9zsssDAXKxWXhAx9s39080Qm+59k2vLJafUjUTzl4cs2rKHK7BYty2EyNxir68v7cJcaBEA==",
4005 4005
       "type": "package",
4006
-      "path": "dotnet-test-xunit/1.0.0-rc2-build10015",
4006
+      "path": "dotnet-test-xunit/1.0.0-rc2-build10025",
4007 4007
       "files": [
4008
-        "dotnet-test-xunit.1.0.0-rc2-build10015.nupkg.sha512",
4008
+        "dotnet-test-xunit.1.0.0-rc2-build10025.nupkg.sha512",
4009 4009
         "dotnet-test-xunit.nuspec",
4010 4010
         "lib/net451/dotnet-test-xunit.exe",
4011 4011
         "lib/netcoreapp1.0/dotnet-test-xunit.dll",
@@ -10436,7 +10436,7 @@
10436 10436
       "Luticate2.Auth >= 1.0.*",
10437 10437
       "Moq >= 4.6.38-alpha",
10438 10438
       "System.Runtime.Serialization.Primitives >= 4.1.1",
10439
-      "dotnet-test-xunit >= 1.0.0-rc2-192208-24",
10439
+      "dotnet-test-xunit >= 1.0.0-rc2-*",
10440 10440
       "xunit >= 2.1.0"
10441 10441
     ],
10442 10442
     ".NETCoreApp,Version=v1.0": [

Loading…
Cancel
Save