Browse Source

fixed LuFilterDbo; tests; added filters in pkguid

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

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

8
 {
8
 {
9
     public class LuFilterBinder : IModelBinder
9
     public class LuFilterBinder : IModelBinder
10
     {
10
     {
11
-        private const string RegExp = "^([\\w\\-]+) *: *([^ \"\']+|\"[^\"]+\"|\'[^\']+\') *";
11
+        private const string RegExp = "^([\\w\\-]+) *: *([^ \"\']+|\"[^\"]*\"|\'[^\']*\') *";
12
 
12
 
13
         public static LuResult<LuFilterDbo> FromString(string data)
13
         public static LuResult<LuFilterDbo> FromString(string data)
14
         {
14
         {
24
                 match = Regex.Match(data, RegExp);
24
                 match = Regex.Match(data, RegExp);
25
                 if (match.Success)
25
                 if (match.Success)
26
                 {
26
                 {
27
+                    var key = match.Groups[1].Value;
27
                     var value = match.Groups[2].Value;
28
                     var value = match.Groups[2].Value;
28
                     if ((value.StartsWith("\"") && value.EndsWith("\"")) ||
29
                     if ((value.StartsWith("\"") && value.EndsWith("\"")) ||
29
                         (value.StartsWith("\'") && value.EndsWith("\'")))
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
                     data = data.Remove(match.Index, match.Length);
42
                     data = data.Remove(match.Index, match.Length);
35
                 }
43
                 }
36
             } while (match.Success);
44
             } while (match.Success);

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

17
             return Filters[key];
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
             var value = GetFilterString(key, "").ToLower();
22
             var value = GetFilterString(key, "").ToLower();
23
             if (value == "true")
23
             if (value == "true")
31
             return defaultValue;
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
             int v;
36
             int v;
37
             if (int.TryParse(GetFilterString(key, ""), out v))
37
             if (int.TryParse(GetFilterString(key, ""), out v))
41
             return defaultValue;
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
             double v;
46
             double v;
47
             if (double.TryParse(GetFilterString(key, ""), out v))
47
             if (double.TryParse(GetFilterString(key, ""), out v))

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

98
         [Fact]
98
         [Fact]
99
         public void Test9()
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
             Assert.NotNull(res);
102
             Assert.NotNull(res);
103
             Assert.NotNull(res.Data);
103
             Assert.NotNull(res.Data);
104
             Assert.Equal(LuStatus.Success, res.Status);
104
             Assert.Equal(LuStatus.Success, res.Status);
105
             Assert.Equal("some text", res.Data.Query);
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
         [Fact]
114
         [Fact]

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

63
 
63
 
64
         protected override Expression<Func<pk_guid, bool>> GetFilterExpression(LuFilterDbo filter)
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
         protected override LuResult<bool> _Add(pk_guid model, PkGuidAddDbo dbo, LuUtilsDbContext db, DbSet<pk_guid> table)
73
         protected override LuResult<bool> _Add(pk_guid model, PkGuidAddDbo dbo, LuUtilsDbContext db, DbSet<pk_guid> table)

Loading…
Cancel
Save