Sfoglia il codice sorgente

refactor getmultiple; tests

tags/v0.1.0
Robin Thoni 8 anni fa
parent
commit
3eb95ee1d9

+ 22
- 13
Luticate2.Utils/DataAccess/LuEfCrudDataAccess.cs Vedi File

@@ -25,7 +25,7 @@ namespace Luticate2.Utils.DataAccess
25 25
 
26 26
         protected abstract TDboRead GetDboFromModel(TModel model);
27 27
 
28
-        public Func<TDboRead, T> GetIdFunc<T>()
28
+        protected Func<TDboRead, T> GetIdFunc<T>()
29 29
         {
30 30
             var param = Expression.Parameter(typeof(TDboRead), "x");
31 31
             var exp = Expression.Property(param, "Id");
@@ -129,14 +129,18 @@ namespace Luticate2.Utils.DataAccess
129 129
         }
130 130
 
131 131
 
132
-        public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple<TKey>(Func<IQueryable<TModel>> orderBy,
132
+        public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(Func<DbSet<TModel>, IQueryable<TModel>> orderBy,
133 133
             Expression<Func<TModel, bool>> predicate, int page = 0, int perPage = int.MaxValue,
134
-            params Expression<Func<TModel, TKey>>[] otherOrderBy)
134
+            params Func<DbSet<TModel>, IQueryable<TModel>>[] otherOrderBy)
135 135
         {
136 136
             return Execute(() =>
137 137
             {
138 138
                 var count = Table.Count(predicate);
139
-                var ordered = orderBy();
139
+                var ordered = orderBy(Table);
140
+                foreach (var func in otherOrderBy)
141
+                {
142
+                    ordered = func(Table);
143
+                }
140 144
                 var data = ordered.Where(predicate).Skip(page * perPage).Take(perPage).Select(GetDboFromModel).ToList();
141 145
                 var result = new LuPaginatedDbo<TDboRead>
142 146
                 {
@@ -147,22 +151,27 @@ namespace Luticate2.Utils.DataAccess
147 151
             });
148 152
         }
149 153
 
150
-        public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple<TKey>(Expression<Func<TModel, TKey>> orderBy,
151
-            Expression<Func<TModel, bool>> predicate, int page = 0, int perPage = int.MaxValue)
154
+        public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(Expression<Func<TModel, object>> orderBy,
155
+            Expression<Func<TModel, bool>> predicate, int page = 0, int perPage = int.MaxValue,
156
+            params Expression<Func<TModel, object>>[] otherOrderBy)
152 157
         {
153
-            return GetMultiple<TKey>(() => Table.OrderBy(orderBy), predicate, page, perPage);
158
+            return GetMultiple(table => table.OrderBy(orderBy), predicate, page, perPage,
159
+                otherOrderBy.Select<Expression<Func<TModel, object>>, Func<DbSet<TModel>,
160
+                    IQueryable<TModel>>>(expression => (t => t.OrderBy(expression))).ToArray());
154 161
         }
155 162
 
156
-        public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple<TKey>(Expression<Func<TModel, TKey>> orderBy,
157
-            int page = 0, int perPage = int.MaxValue, params Expression<Func<TModel, TKey>>[] otherOrderBy)
163
+        public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(Func<DbSet<TModel>, IQueryable<TModel>> orderBy,
164
+            int page = 0, int perPage = int.MaxValue, params Func<DbSet<TModel>, IQueryable<TModel>>[] otherOrderBy)
158 165
         {
159
-            return GetMultiple(() => Table.OrderBy(orderBy), x => true, page, perPage, otherOrderBy);
166
+            return GetMultiple(orderBy, x => true, page, perPage, otherOrderBy);
160 167
         }
161 168
 
162
-        public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple<TKey>(Expression<Func<TModel, TKey>> orderBy,
163
-            int page = 0, int perPage = int.MaxValue)
169
+        public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(Expression<Func<TModel, object>> orderBy,
170
+            int page = 0, int perPage = int.MaxValue, params Expression<Func<TModel, object>>[] otherOrderBy)
164 171
         {
165
-            return GetMultiple(orderBy, x => true, page, perPage);
172
+            return GetMultiple(table => table.OrderBy(orderBy), x => true, page, perPage,
173
+                otherOrderBy.Select<Expression<Func<TModel, object>>, Func<DbSet<TModel>,
174
+                    IQueryable<TModel>>>(expression => (t => t.OrderBy(expression))).ToArray());
166 175
         }
167 176
 
168 177
 

+ 209
- 0
Test/Utils/EfCrubDataAccess/LuEfCreateDataAccessTest.cs Vedi File

@@ -1,7 +1,9 @@
1 1
 using System;
2 2
 using System.Collections.Generic;
3
+using System.Diagnostics;
3 4
 using System.Linq;
4 5
 using Luticate2.Utils.Dbo;
6
+using Microsoft.AspNetCore.Mvc.Controllers;
5 7
 using Test.Utils.DataAccess;
6 8
 using Test.Utils.Dbo.PkBigSerial;
7 9
 using Test.Utils.Dbo.PkGuid;
@@ -115,6 +117,30 @@ namespace Test.Utils.EfCrubDataAccess
115 117
             });
116 118
         }
117 119
 
120
+        [Fact]
121
+        public void TestAddMultiple4()
122
+        {
123
+            Tests.TestRealDb(context =>
124
+            {
125
+                var dbos = new List<PkBigSerialAddDbo>
126
+                {
127
+                    new PkBigSerialAddDbo
128
+                    {
129
+                        SomeInt = 42,
130
+                        SomeText = "42"
131
+                    },
132
+                    new PkBigSerialAddDbo
133
+                    {
134
+                        SomeInt = 21,
135
+                        SomeText = "42"
136
+                    }
137
+                };
138
+                var service = new LuUtilsPkBigSerialDataAccess(context);
139
+                var res = service.Add(dbos, enumerable => enumerable.Select(dbo => dbo.CreatedAt));
140
+                Assert.Equal(LuStatus.DbError, res.Status);
141
+            });
142
+        }
143
+
118 144
 
119 145
         [Fact]
120 146
         public void TestAddSingle1()
@@ -523,5 +549,188 @@ namespace Test.Utils.EfCrubDataAccess
523 549
                 Assert.Equal(LuStatus.NotFound, get.Status);
524 550
             });
525 551
         }
552
+
553
+        [Fact]
554
+        public void TestGetMultiple1()
555
+        {
556
+            Tests.TestRealDb(context =>
557
+            {
558
+                var dbos = new List<PkGuidAddDbo>
559
+                {
560
+                    new PkGuidAddDbo
561
+                    {
562
+                        SomeInt = 42,
563
+                        SomeText = "442"
564
+                    },
565
+                    new PkGuidAddDbo
566
+                    {
567
+                        SomeInt = 42,
568
+                        SomeText = "42"
569
+                    },
570
+                    new PkGuidAddDbo
571
+                    {
572
+                        SomeInt = 24,
573
+                        SomeText = "24"
574
+                    }
575
+                };
576
+                var service = new LuUtilsPkGuidDataAccess(context);
577
+                var res = service.AddGuid(dbos);
578
+                Assert.Equal(LuStatus.Success, res.Status);
579
+
580
+                var get = service.GetMultiple(table => table.OrderBy(guid => guid.some_int),
581
+                    guid => guid.some_int == 42, 0, int.MaxValue, set => set.OrderBy(guid => guid.some_text));
582
+
583
+                Assert.Equal(LuStatus.Success, get.Status);
584
+                Assert.Equal(2, get.Data.Count);
585
+                Assert.Equal(2, get.Data.Data.Count);
586
+
587
+                var dbo = get.Data.Data[0];
588
+                Assert.Equal(42, dbo.SomeInt);
589
+                Assert.Equal("42", dbo.SomeText);
590
+
591
+                dbo = get.Data.Data[1];
592
+                Assert.Equal(42, dbo.SomeInt);
593
+                Assert.Equal("442", dbo.SomeText);
594
+            });
595
+        }
596
+
597
+        [Fact]
598
+        public void TestGetMultiple2()
599
+        {
600
+            Tests.TestRealDb(context =>
601
+            {
602
+                var dbos = new List<PkGuidAddDbo>
603
+                {
604
+                    new PkGuidAddDbo
605
+                    {
606
+                        SomeInt = 42,
607
+                        SomeText = "442"
608
+                    },
609
+                    new PkGuidAddDbo
610
+                    {
611
+                        SomeInt = 42,
612
+                        SomeText = "42"
613
+                    },
614
+                    new PkGuidAddDbo
615
+                    {
616
+                        SomeInt = 24,
617
+                        SomeText = "24"
618
+                    }
619
+                };
620
+                var service = new LuUtilsPkGuidDataAccess(context);
621
+                var res = service.AddGuid(dbos);
622
+                Assert.Equal(LuStatus.Success, res.Status);
623
+
624
+                var get = service.GetMultiple(guid => guid.some_int, guid => guid.some_int == 42, 0, int.MaxValue,
625
+                    guid => guid.some_text);
626
+
627
+                Assert.Equal(LuStatus.Success, get.Status);
628
+                Assert.Equal(2, get.Data.Count);
629
+                Assert.Equal(2, get.Data.Data.Count);
630
+
631
+                var dbo = get.Data.Data[0];
632
+                Assert.Equal(42, dbo.SomeInt);
633
+                Assert.Equal("42", dbo.SomeText);
634
+
635
+                dbo = get.Data.Data[1];
636
+                Assert.Equal(42, dbo.SomeInt);
637
+                Assert.Equal("442", dbo.SomeText);
638
+            });
639
+        }
640
+
641
+        [Fact]
642
+        public void TestGetMultiple3()
643
+        {
644
+            Tests.TestRealDb(context =>
645
+            {
646
+                var dbos = new List<PkGuidAddDbo>
647
+                {
648
+                    new PkGuidAddDbo
649
+                    {
650
+                        SomeInt = 42,
651
+                        SomeText = "442"
652
+                    },
653
+                    new PkGuidAddDbo
654
+                    {
655
+                        SomeInt = 42,
656
+                        SomeText = "42"
657
+                    },
658
+                    new PkGuidAddDbo
659
+                    {
660
+                        SomeInt = 24,
661
+                        SomeText = "24"
662
+                    }
663
+                };
664
+                var service = new LuUtilsPkGuidDataAccess(context);
665
+                var res = service.AddGuid(dbos);
666
+                Assert.Equal(LuStatus.Success, res.Status);
667
+
668
+                var get = service.GetMultiple(table => table.OrderBy(guid => guid.some_int), 0, int.MaxValue,
669
+                    set => set.OrderBy(guid => guid.some_text));
670
+
671
+                Assert.Equal(LuStatus.Success, get.Status);
672
+                Assert.Equal(3, get.Data.Count);
673
+                Assert.Equal(3, get.Data.Data.Count);
674
+
675
+                var dbo = get.Data.Data[0];
676
+                Assert.Equal(24, dbo.SomeInt);
677
+                Assert.Equal("24", dbo.SomeText);
678
+
679
+                dbo = get.Data.Data[1];
680
+                Assert.Equal(42, dbo.SomeInt);
681
+                Assert.Equal("42", dbo.SomeText);
682
+
683
+                dbo = get.Data.Data[2];
684
+                Assert.Equal(42, dbo.SomeInt);
685
+                Assert.Equal("442", dbo.SomeText);
686
+            });
687
+        }
688
+
689
+        [Fact]
690
+        public void TestGetMultiple4()
691
+        {
692
+            Tests.TestRealDb(context =>
693
+            {
694
+                var dbos = new List<PkGuidAddDbo>
695
+                {
696
+                    new PkGuidAddDbo
697
+                    {
698
+                        SomeInt = 42,
699
+                        SomeText = "442"
700
+                    },
701
+                    new PkGuidAddDbo
702
+                    {
703
+                        SomeInt = 42,
704
+                        SomeText = "42"
705
+                    },
706
+                    new PkGuidAddDbo
707
+                    {
708
+                        SomeInt = 24,
709
+                        SomeText = "24"
710
+                    }
711
+                };
712
+                var service = new LuUtilsPkGuidDataAccess(context);
713
+                var res = service.AddGuid(dbos);
714
+                Assert.Equal(LuStatus.Success, res.Status);
715
+
716
+                var get = service.GetMultiple(guid => guid.some_int, 0, int.MaxValue, guid => guid.some_text);
717
+
718
+                Assert.Equal(LuStatus.Success, get.Status);
719
+                Assert.Equal(3, get.Data.Count);
720
+                Assert.Equal(3, get.Data.Data.Count);
721
+
722
+                var dbo = get.Data.Data[0];
723
+                Assert.Equal(24, dbo.SomeInt);
724
+                Assert.Equal("24", dbo.SomeText);
725
+
726
+                dbo = get.Data.Data[1];
727
+                Assert.Equal(42, dbo.SomeInt);
728
+                Assert.Equal("42", dbo.SomeText);
729
+
730
+                dbo = get.Data.Data[2];
731
+                Assert.Equal(42, dbo.SomeInt);
732
+                Assert.Equal("442", dbo.SomeText);
733
+            });
734
+        }
526 735
     }
527 736
 }

Loading…
Annulla
Salva