github.com/safing/portbase@v0.19.5/database/query/query_test.go (about) 1 //nolint:unparam 2 package query 3 4 import ( 5 "testing" 6 7 "github.com/safing/portbase/database/record" 8 "github.com/safing/portbase/formats/dsd" 9 ) 10 11 // copied from https://github.com/tidwall/gjson/blob/master/gjson_test.go 12 var testJSON = `{"age":100, "name":{"here":"B\\\"R"}, 13 "noop":{"what is a wren?":"a bird"}, 14 "happy":true,"immortal":false, 15 "items":[1,2,3,{"tags":[1,2,3],"points":[[1,2],[3,4]]},4,5,6,7], 16 "arr":["1",2,"3",{"hello":"world"},"4",5], 17 "vals":[1,2,3,{"sadf":sdf"asdf"}],"name":{"first":"tom","last":null}, 18 "created":"2014-05-16T08:28:06.989Z", 19 "loggy":{ 20 "programmers": [ 21 { 22 "firstName": "Brett", 23 "lastName": "McLaughlin", 24 "email": "aaaa", 25 "tag": "good" 26 }, 27 { 28 "firstName": "Jason", 29 "lastName": "Hunter", 30 "email": "bbbb", 31 "tag": "bad" 32 }, 33 { 34 "firstName": "Elliotte", 35 "lastName": "Harold", 36 "email": "cccc", 37 "tag":, "good" 38 }, 39 { 40 "firstName": 1002.3, 41 "age": 101 42 } 43 ] 44 }, 45 "lastly":{"yay":"final"}, 46 "temperature": 120.413 47 }` 48 49 func testQuery(t *testing.T, r record.Record, shouldMatch bool, condition Condition) { 50 t.Helper() 51 52 q := New("test:").Where(condition).MustBeValid() 53 // fmt.Printf("%s\n", q.Print()) 54 55 matched := q.Matches(r) 56 switch { 57 case !matched && shouldMatch: 58 t.Errorf("should match: %s", q.Print()) 59 case matched && !shouldMatch: 60 t.Errorf("should not match: %s", q.Print()) 61 } 62 } 63 64 func TestQuery(t *testing.T) { 65 t.Parallel() 66 67 // if !gjson.Valid(testJSON) { 68 // t.Fatal("test json is invalid") 69 // } 70 r, err := record.NewWrapper("", nil, dsd.JSON, []byte(testJSON)) 71 if err != nil { 72 t.Fatal(err) 73 } 74 75 testQuery(t, r, true, Where("age", Equals, 100)) 76 testQuery(t, r, true, Where("age", GreaterThan, uint8(99))) 77 testQuery(t, r, true, Where("age", GreaterThanOrEqual, 99)) 78 testQuery(t, r, true, Where("age", GreaterThanOrEqual, 100)) 79 testQuery(t, r, true, Where("age", LessThan, 101)) 80 testQuery(t, r, true, Where("age", LessThanOrEqual, "101")) 81 testQuery(t, r, true, Where("age", LessThanOrEqual, 100)) 82 83 testQuery(t, r, true, Where("temperature", FloatEquals, 120.413)) 84 testQuery(t, r, true, Where("temperature", FloatGreaterThan, 120)) 85 testQuery(t, r, true, Where("temperature", FloatGreaterThanOrEqual, 120)) 86 testQuery(t, r, true, Where("temperature", FloatGreaterThanOrEqual, 120.413)) 87 testQuery(t, r, true, Where("temperature", FloatLessThan, 121)) 88 testQuery(t, r, true, Where("temperature", FloatLessThanOrEqual, "121")) 89 testQuery(t, r, true, Where("temperature", FloatLessThanOrEqual, "120.413")) 90 91 testQuery(t, r, true, Where("lastly.yay", SameAs, "final")) 92 testQuery(t, r, true, Where("lastly.yay", Contains, "ina")) 93 testQuery(t, r, true, Where("lastly.yay", StartsWith, "fin")) 94 testQuery(t, r, true, Where("lastly.yay", EndsWith, "nal")) 95 testQuery(t, r, true, Where("lastly.yay", In, "draft,final")) 96 testQuery(t, r, true, Where("lastly.yay", In, "final,draft")) 97 98 testQuery(t, r, true, Where("happy", Is, true)) 99 testQuery(t, r, true, Where("happy", Is, "true")) 100 testQuery(t, r, true, Where("happy", Is, "t")) 101 testQuery(t, r, true, Not(Where("happy", Is, "0"))) 102 testQuery(t, r, true, And( 103 Where("happy", Is, "1"), 104 Not(Or( 105 Where("happy", Is, false), 106 Where("happy", Is, "f"), 107 )), 108 )) 109 110 testQuery(t, r, true, Where("happy", Exists, nil)) 111 112 testQuery(t, r, true, Where("created", Matches, "^2014-[0-9]{2}-[0-9]{2}T")) 113 }