|
1 | 1 | package querybuilder
|
2 | 2 |
|
3 |
| -import ( |
4 |
| - //"encoding/json" |
5 |
| - "testing" |
6 |
| -) |
| 3 | +import "testing" |
7 | 4 |
|
8 |
| -var rulestr = `{ |
| 5 | +var rulesetStr = `{ |
9 | 6 | "condition": "AND",
|
10 |
| - "rules": [{ |
11 |
| - "id": "Decimal_Question", |
12 |
| - "field": "Decimal_Question", |
13 |
| - "type": "double", |
14 |
| - "input": "text", |
15 |
| - "operator": "equal", |
16 |
| - "value": "1.2" |
17 |
| - }, { |
18 |
| - "condition": "AND", |
19 |
| - "rules": [{ |
20 |
| - "id": "Date_Question", |
21 |
| - "field": "Date_Question", |
22 |
| - "type": "date", |
23 |
| - "input": "text", |
24 |
| - "operator": "greater", |
25 |
| - "value": "2016-07-19" |
26 |
| - }, { |
27 |
| - "id": "Yes_No_Question", |
28 |
| - "field": "Yes_No_Question", |
29 |
| - "type": "boolean", |
30 |
| - "input": "select", |
31 |
| - "operator": "equal", |
32 |
| - "value": "true" |
33 |
| - }] |
34 |
| - }] |
35 |
| -}` |
| 7 | + "rules": [ |
| 8 | + {"id": "float_equal","field": "float_equal","type": "double","input": "text","operator": "equal","value": 1.2}, |
| 9 | + {"id": "float_greater","field": "float_greater","type": "double","input": "text","operator": "greater","value": 7.5}, |
| 10 | + {"condition": "OR","rules": [ |
| 11 | + {"id": "int_greater","field": "int_greater","type": "integer","input": "text","operator": "greater","value": 2}, |
| 12 | + {"id": "int_equal","field": "int_equal","type": "integer","input": "text","operator": "equal","value": 5} |
| 13 | + ]} |
| 14 | + ]}` |
36 | 15 |
|
37 |
| -func TestNew(t *testing.T) { |
38 |
| - // |
39 |
| - // |
40 |
| - // err := json.Unmarshal([]byte(rulestr), &rg) |
41 |
| - // t.Log(err) |
42 |
| - // t.Log(rg) |
| 16 | +func TestMatch(t *testing.T) { |
| 17 | + inputs := []struct { |
| 18 | + title string |
| 19 | + dataset string |
| 20 | + want bool |
| 21 | + }{ |
| 22 | + {"dt-01", `{"float_equal": 1.0, "int_equal": 5, "int_greater": 3, "float_greater": 7.7}`, false}, |
| 23 | + {"dt-02", `{"float_equal": 1.2, "int_equal": 5, "int_greater": 3, "float_greater": 7.7}`, true}, |
| 24 | + {"dt-03", `{"float_equal": 1.2}`, false}, |
| 25 | + {"dt-04", `{"int_greater": 3}`, false}, |
| 26 | + } |
| 27 | + |
| 28 | + qb := New(parseJson(rulesetStr)) |
| 29 | + |
| 30 | + for _, input := range inputs { |
| 31 | + t.Run(input.title, func(t *testing.T) { |
| 32 | + if got := qb.Match(parseJson(input.dataset)); got != input.want { |
| 33 | + t.Errorf("Match got %t, want %t", got, input.want) |
| 34 | + } |
| 35 | + }) |
| 36 | + } |
43 | 37 | }
|
0 commit comments