GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — qlbridge/vm/vm_test.go at master · lytics/qlbridge Expand file tree/ Copy pathvm_test.goMore file actions 366 lines (326 loc) · 13.2 KB / Copy pathvm_test.goFile metadata and controls366 lines (326 loc) · 13.2 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 package vm_test import ( "flag" "log" "os" "testing" "time" "github.com/araddon/dateparse" u "github.com/araddon/gou" "github.com/lytics/qlbridge/datasource" "github.com/lytics/qlbridge/expr" "github.com/lytics/qlbridge/expr/builtins" "github.com/lytics/qlbridge/value" "github.com/lytics/qlbridge/vm" ) const ( noError = true parseOk = true evalError = false ) var ( VerboseTests *bool = flag.Bool("vv", false, "Verbose Logging?") Trace *bool = flag.Bool("t", false, "Trace Logging?") ) func TestMain(m *testing.M) { flag.Parse() if *VerboseTests { u.SetLogger(log.New(os.Stderr, "", log.Lshortfile), "debug") u.SetColorOutput() } if *Trace { expr.Trace = true } builtins.LoadAllBuiltins() // Now run the actual Tests os.Exit(m.Run()) } var ( t0 = dateparse.MustParse("12/18/2015") t1 = dateparse.MustParse("12/18/2039") tcreated = time.Now().AddDate(0, 0, -14) // 14 days ago // This is the message context which will be added to all tests below // and be available to the VM runtime for evaluation by using // key's such as "int5" or "user_id" msgContext = datasource.NewContextMap(map[string]any{ "int5": value.NewIntValue(5), "str5": value.NewStringValue("5"), "created": value.NewTimeValue(tcreated), "bvalt": value.NewBoolValue(true), "bvalf": value.NewBoolValue(false), "user_id": value.NewStringValue("abc"), "urls": value.NewStringsValue([]string{"abc", "123"}), "hits": value.NewMapIntValue(map[string]int64{"google.com": 5, "bing.com": 1}), "empty_str": value.NewStringValue(""), "mt": value.NewMapTimeValue(map[string]time.Time{"event0": t0, "event1": t1}), }, true) // list of tests vmTests = []vmTest{ // Date math vmt(`created > "now-1M"`, true, noError), vmt(`now() > todate("01/01/2014")`, true, noError), vmt(`todate("now+3d") > now()`, true, noError), vmt(`created < 2032220220175`, true, noError), // Really not sure i want to support this? // date operations on map[string]time data types vmt(`mt.event0 > now()`, false, noError), vmt(`mt.event1 > now()`, true, noError), vmt(`mt.not_event > now()`, false, noError), vmt(`!exists(user_id) OR toint(not_a_field) > 21`, false, noError), vmt(`exists(user_id) OR toint(not_a_field) > 21`, true, noError), vmt(`!exists(user_id) OR toint(str5) >= 1`, true, noError), vmt(`!exists(user_id) OR toint(str5) < 1`, false, noError), // Contains func vmt(`contains(key,"-")`, false, noError), vmt(`not(contains(key,"-"))`, true, noError), vmt(`contains(email,"@")`, true, noError), vmt(`not(contains(email,"@"))`, false, noError), vmt(`not(contains(key,"-")) AND not(contains(email,"@"))`, false, noError), vmt(`not(contains(key,"-")) OR not(contains(email,"@"))`, true, noError), vmt(`not(contains(key,"-")) OR not(contains(not_real,"@"))`, true, noError), // eac of these is true, but some are missing (but bc not are true) vmt(`str5 NOT IN ("nope") AND userid NOT IN ("abc") AND email NOT IN ("[email protected]")`, true, noError), // Native LIKE keyword vmt(`["portland"] LIKE "*land"`, true, noError), vmt(`["chicago"] LIKE "*land"`, false, noError), vmt(`["New York"] LIKE "New York"`, true, noError), vmt(`"New York" LIKE ["Boston","New York"]`, true, noError), vmt(`"New York" LIKE split("Boston,New York", ",")`, true, noError), vmt(`"New York" LIKE split("Boston",",")`, false, noError), vmtall(`user_id LIKE mt`, nil, parseOk, evalError), vmt(`urls LIKE "a*"`, true, noError), vmt(`urls LIKE "d*"`, false, noError), vmt(`split("chicago,portland",",") LIKE "*land"`, true, noError), vmt(`split("chicago,portland",",") LIKE "*sea"`, false, noError), vmt(`email LIKE "bob*"`, true, noError), vmt(`email LIKE "bob"`, false, noError), vmt(`email LIKE "*.com"`, true, noError), // Native Contains keyword vmt(`[1,2,3] contains int5`, false, noError), vmt(`[1,2,3] NOT contains int5`, true, noError), vmt(`[1,2,3,5] contains int5`, true, noError), vmt(`[1,2,3,5] NOT contains int5`, false, noError), vmt(`email contains "bob"`, true, noError), vmt(`email contains ["lss","bob"]`, true, noError), vmt(`email contains split("lss,bob",",")`, true, noError), vmt(`email contains split("lss,qr",",")`, false, noError), vmt(`email NOT contains "bob"`, false, noError), vmt(`urls contains "abc"`, true, noError), vmt(`urls NOT contains "abc"`, false, noError), // Should this be correct? By "Contains" do we change behavior // depending on if is array, and we mean equality on array entry or? vmt(`urls contains "ab"`, true, noError), // Between: Ternary Node Tests vmt(`10 BETWEEN 1 AND 50`, true, noError), vmt(`10 BETWEEN "1" AND 50`, true, noError), vmt(`10 BETWEEN 1 AND "50"`, true, noError), vmt(`10 BETWEEN 1 AND "55.5"`, true, noError), vmt(`15.5 BETWEEN 1 AND "55.5"`, true, noError), vmt(`10 BETWEEN 20 AND 50`, false, noError), vmt(`10 BETWEEN 5 AND toint("50.5")`, true, noError), vmt(`10 BETWEEN int5 AND 50`, true, noError), vmtall(`10 BETWEEN 20 AND true`, nil, parseOk, evalError), vmt(`created BETWEEN "12/18/2015" AND "12/18/2050"`, true, noError), vmt(`created BETWEEN "now-50w" AND "12/18/2050"`, true, noError), vmtall(`mt BETWEEN "12/18/2015" AND "12/18/2050"`, nil, parseOk, evalError), vmtall(`mt NOT BETWEEN "12/18/2015" AND "12/18/2050"`, nil, parseOk, evalError), vmtall(`urls BETWEEN "a" AND "z"`, nil, parseOk, evalError), // In: Multi Arg Tests vmtall(`10 IN ("a","b",10, 4.5)`, true, parseOk, evalError), vmtall(`10 IN ("a","b",20, 4.5)`, false, parseOk, evalError), vmtall(`"a" IN ("a","b",10, 4.5)`, true, parseOk, evalError), // this is little different, it is an array vmtall(`"a" IN ["a","b",10, 4.5]`, true, parseOk, evalError), // NEGATED vmtall(`10 NOT IN ("a","b" 4.5)`, true, parseOk, evalError), vmtall(`NOT (10 IN ("a","b" 4.5))`, true, parseOk, evalError), //vmtall(`NOT 10 IN ("a","b" 4.5)`, true, parseOk, evalError), vmtall(`"a" NOT IN ("a","b" 4.5)`, false, parseOk, evalError), // true because negated vmtall(`toint(not_a_field) NOT IN ("a","b" 4.5)`, true, parseOk, noError), vmt(`"a" IN urls`, false, noError), vmt(`"abc" IN urls`, true, noError), vmt(`"com" IN hits`, false, noError), vmt(`"google.com" IN hits`, true, noError), vmt(`"event0" IN mt`, true, noError), vmt(`"event_no" IN mt`, false, noError), vmt(`emaildomain(email) in "google.com"`, false, noError), vmtall(`"hello" == split("hell-no", ",")`, nil, parseOk, evalError), // Binary String vmt(`user_id == "abc"`, true, noError), vmt(`user_id != "abcd"`, true, noError), vmt(`user_id == "abcd"`, false, noError), vmt(`user_id != "abc"`, false, noError), vmtall(`user_id > "abc"`, nil, parseOk, evalError), vmt(`user_id LIKE "*bc"`, true, noError), vmt(`user_id LIKE "\*bc"`, false, noError), vmt(`user_id != NULL`, true, noError), // Binary Bool vmt(`bvalt == true`, true, noError), vmt(`bvalt = true`, true, noError), vmt(`bvalf == false`, true, noError), vmt(`bvalf = false`, true, noError), vmt(`bvalt == bvalf`, false, noError), vmt(`bvalt != bvalf`, true, noError), vmt(`(toint(not_a_field) > 0) || true`, true, noError), vmt(`user_id == true`, false, noError), // Boolean Logic DSL vmt(` OR ( email != "[email protected]" AND ( NOT EXISTS not_a_field int5 == 5 ) )`, true, noError), // Math vmt(`5 + 4`, int64(9), noError), vmt(`5.2 + 4`, float64(9.2), noError), vmt(`(4 + 5) / 2`, int64(4), noError), vmt(`6 > 5`, true, noError), vmt(`6 > 5.5`, true, noError), vmt(`6.5 > 5.5`, true, noError), vmt(`6 == 6`, true, noError), vmt(`6 != 5`, true, noError), vmt(`!eq(5,6)`, true, noError), // Number on left vmtall(`5.5 + ["hello"]`, false, parseOk, noError), vmtall(`5.5 == ["5.5"]`, true, parseOk, noError), vmtall(`5.5 == ["hello", 3, "5.5"]`, true, parseOk, noError), vmtall(`5.5 == ["5.9", 99, "hello"]`, false, parseOk, noError), // Numeric Boolean coerce vmt(`"5.5" == 5.5`, true, noError), vmt(`"5.5" > 5`, true, noError), // Boolean, with Context vmt(`bvalt == true`, true, noError), vmt(`bvalf == false`, true, noError), vmt(`bvalf == true`, false, noError), vmt(`!(bvalf == true)`, true, noError), vmt(`EXISTS int5`, true, noError), vmt(`EXISTS not_a_field`, false, noError), vmt(`EXISTS bvalt`, true, noError), vmt(`EXISTS bvalf`, true, noError), vmt(`EXISTS toint(not_a_field)`, false, noError), // TODO: support () wrapping parts of binary expression vmt(`6 == (5 + 1)`, true, noError), // TODO: urnary vmt(`true || !eq(5,6)`, true, noError), // Context Based Tests vmt(`int5 + 5`, int64(10), noError), vmt(`int5 * 6`, int64(30), noError), vmt(`toint(str5 * 6)`, int64(30), noError), vmt(`toint(str5 + 6)`, int64(11), noError), // context lookups? simple vmt(`user_id`, "abc", noError), // functional syntax vmt(`eq(toint(int5),5)`, true, noError), vmt(`eq(toint(int5),6)`, false, noError), // TODO: //vmt("eq/toint types", `eq(toint(notreal || 1),6)`, false, noError), //vmt("eq/toint types", `eq(toint(notreal || 6),6)`, true, noError), vmt(`2 * (3 + 5)`, int64(16), noError), vmt(`(bvalt == true && bvalf == false)`, true, noError), vmt(`(fld1 != "stuff" AND (field2 == "stuff" AND toint(fieldx) > 7))`, false, noError), // Ensure we can parse even with all the parens, linebreaks etc vmt(`( (fld1 != "stuff" AND field = true) OR (field2 == "stuff" AND toint(fieldx) > 7) )`, false, noError), // Code Elide/Collapse into simplest form vmt(`user_id == "abc"`, true, noError), vmt(`NOT (user_id != "abc")`, true, noError), vmt(`user_id != "abcd"`, true, noError), vmt(`NOT (user_id == "abcd")`, true, noError), vmt(`email contains "bob"`, true, noError), vmt(`NOT (email NOT contains "bob")`, true, noError), vmt(`exists email`, true, noError), vmt(`NOT (NOT EXISTS email)`, true, noError), vmt(`exists not_a_field`, false, noError), vmt(`exists empty_str`, true, noError), vmt(`exists(empty_str)`, true, noError), vmt(`NOT (NOT EXISTS not_a_field)`, false, noError), vmt(`int5 > 10`, false, noError), vmt(`NOT (int5 <= 10)`, false, noError), vmt(`int5 < 10`, true, noError), vmt(`NOT (int5 >= 10)`, true, noError), vmt(`int5 >= 10`, false, noError), vmt(`NOT (int5 < 10)`, false, noError), vmt(`int5 <= 10`, true, noError), vmt(`NOT (int5 > 10)`, true, noError), // Test some error/nil/non-eval expressions vmtall(`namex + true`, nil, parseOk, evalError), vmtall(`namex + true || namex2 + true`, false, parseOk, noError), vmtall(`(namex + true) == (namex2 + true)`, false, parseOk, noError), vmtall(`(namex + true) != (namex2 + true)`, false, parseOk, noError), vmtall(`(namex + true) > (namex2 + true)`, false, parseOk, noError), vmtall(`(namex + true) + (namex2 + true)`, nil, parseOk, evalError), } ) func TestRunExpr(t *testing.T) { for _, test := range vmTests { u.Debugf("about to parse: %v", test.qlText) n, err := expr.ParseExpression(test.qlText) //u.Debugf("After Parse: %v err=%v", test.qlText, err) switch { case err == nil && !test.parseok: t.Errorf("%q: 1 expected error; got none", test.qlText) continue case err != nil && test.parseok: t.Errorf("%q: 2 unexpected error: %v", test.qlText, err) continue case err != nil && !test.parseok: // expected error, got one if testing.Verbose() { u.Infof("%s: %s\n\t%s", test.qlText, test.qlText, err) } continue } val, ok := vm.Eval(test.context, n) errVal := false if val != nil { if _, isErr := val.(value.ErrorValue); isErr { errVal = true } } //u.Infof("results: %T %v ok?=%v", val, val, ok) if !ok && test.evalok { t.Errorf("\n%s \n\t%v\nexpected\n\t'%v'", test.qlText, val, test.result) } if test.result == nil && (val != nil && !errVal) { t.Errorf("%s - should have nil result, but got: %v", test.qlText, val) continue } if test.result != nil && val == nil { t.Errorf("%s - should have non-nil result but was nil", test.qlText) continue } //u.Infof("results=%T %#v", val, val) if test.result != nil && val.Value() != test.result { t.Fatalf("\n%s \n\t%v--%T\nexpected\n\t%v--%T", test.qlText, val.Value(), val.Value(), test.result, test.result) } else if test.result == nil { // we expected nil } } } type vmTest struct { qlText string parseok bool evalok bool context expr.EvalContext result any // ?? what is this? } func vmt(qltext string, result any, ok bool) vmTest { return vmTest{qlText: qltext, parseok: ok, evalok: ok, result: result, context: &includer{msgContext}} } func vmtall(qltext string, result any, parseOk, evalOk bool) vmTest { return vmTest{qlText: qltext, parseok: parseOk, evalok: evalOk, result: result, context: &includer{msgContext}} } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |