Browse Source

fixed LuFilterDbo; tests; added filters in pkguid

tags/v0.3.0
Robin Thoni 8 years ago
parent
commit
84cce620dc

+ 11
- 3
Luticate2.Utils/Dbo/Filter/LuFilterBinder.cs View File

@@ -8,7 +8,7 @@ namespace Luticate2.Utils.Dbo.Filter
8 8
 {
9 9
     public class LuFilterBinder : IModelBinder
10 10
     {
11
-        private const string RegExp = "^([\\w\\-]+) *: *([^ \"\']+|\"[^\"]+\"|\'[^\']+\') *";
11
+        private const string RegExp = "^([\\w\\-]+) *: *([^ \"\']+|\"[^\"]*\"|\'[^\']*\') *";
12 12
 
13 13
         public static LuResult<LuFilterDbo> FromString(string data)
14 14
         {
@@ -24,13 +24,21 @@ namespace Luticate2.Utils.Dbo.Filter
24 24
                 match = Regex.Match(data, RegExp);
25 25
                 if (match.Success)
26 26
                 {
27
+                    var key = match.Groups[1].Value;
27 28
                     var value = match.Groups[2].Value;
28 29
                     if ((value.StartsWith("\"") && value.EndsWith("\"")) ||
29 30
                         (value.StartsWith("\'") && value.EndsWith("\'")))
30 31
                     {
31
-                        value = value.Remove(value.Length - 1, 1).Remove(0, 1);
32
+                        value = value.Remove(value.Length - 1, 1).Remove(0, 1).Trim();
33
+                    }
34
+                    if (!filters.ContainsKey(key))
35
+                    {
36
+                        filters.Add(key, value);
37
+                    }
38
+                    else
39
+                    {
40
+                        filters[key] = value;
32 41
                     }
33
-                    filters.Add(match.Groups[1].Value, value);
34 42
                     data = data.Remove(match.Index, match.Length);
35 43
                 }
36 44
             } while (match.Success);

+ 3
- 3
Luticate2.Utils/Dbo/Filter/LuFilterDbo.cs View File

@@ -17,7 +17,7 @@ namespace Luticate2.Utils.Dbo.Filter
17 17
             return Filters[key];
18 18
         }
19 19
 
20
-        public bool GetFilterBool(string key, bool defaultValue)
20
+        public bool? GetFilterBool(string key, bool? defaultValue)
21 21
         {
22 22
             var value = GetFilterString(key, "").ToLower();
23 23
             if (value == "true")
@@ -31,7 +31,7 @@ namespace Luticate2.Utils.Dbo.Filter
31 31
             return defaultValue;
32 32
         }
33 33
 
34
-        public int GetFilterInt(string key, int defaultValue)
34
+        public int? GetFilterInt(string key, int? defaultValue)
35 35
         {
36 36
             int v;
37 37
             if (int.TryParse(GetFilterString(key, ""), out v))
@@ -41,7 +41,7 @@ namespace Luticate2.Utils.Dbo.Filter
41 41
             return defaultValue;
42 42
         }
43 43
 
44
-        public double GetFilterFloat(string key, double defaultValue)
44
+        public double? GetFilterFloat(string key, double? defaultValue)
45 45
         {
46 46
             double v;
47 47
             if (double.TryParse(GetFilterString(key, ""), out v))

+ 7
- 2
TestUtils/Binders/LuFilterBinderTest.cs View File

@@ -98,12 +98,17 @@ namespace TestUtils.Binders
98 98
         [Fact]
99 99
         public void Test9()
100 100
         {
101
-            var res = LuFilterBinder.FromString("test: \'42 a filter value\' some text");
101
+            var res = LuFilterBinder.FromString("test: \'42 a filter value\' test-2: ' 42 ' test-3: ' 4 ' test-3: ' ' some text");
102 102
             Assert.NotNull(res);
103 103
             Assert.NotNull(res.Data);
104 104
             Assert.Equal(LuStatus.Success, res.Status);
105 105
             Assert.Equal("some text", res.Data.Query);
106
-            Assert.Equal(new Dictionary<string, string>{{"test", "42 a filter value"}}, res.Data.Filters);
106
+            Assert.Equal(new Dictionary<string, string>
107
+            {
108
+                {"test", "42 a filter value"},
109
+                {"test-2", "42"},
110
+                {"test-3", ""}
111
+            }, res.Data.Filters);
107 112
         }
108 113
 
109 114
         [Fact]

+ 5
- 1
TestUtils/DataAccess/LuUtilsPkGuidDataAccess.cs View File

@@ -63,7 +63,11 @@ namespace TestUtils.DataAccess
63 63
 
64 64
         protected override Expression<Func<pk_guid, bool>> GetFilterExpression(LuFilterDbo filter)
65 65
         {
66
-            return model => LuUtilsDbContext.lu_texts_match(filter.Query, model.some_text + " " + model.some_int.ToString());
66
+            var someText = filter.GetFilterString("someText", null);
67
+            var someInt = filter.GetFilterInt("someInt", null);
68
+            return model => LuUtilsDbContext.lu_texts_match(filter.Query, model.some_text + " " + model.some_int.ToString())
69
+                && (someText == null || LuUtilsDbContext.lu_texts_match(someText, model.some_text))
70
+                            && (someInt == null || model.some_int == someInt);
67 71
         }
68 72
 
69 73
         protected override LuResult<bool> _Add(pk_guid model, PkGuidAddDbo dbo, LuUtilsDbContext db, DbSet<pk_guid> table)

Loading…
Cancel
Save