func TestKeyInMap(t *testing.T) { paramsMap["testmap"] = "two" keyin := testmap.Check(data.CreateParameters(paramsMap)) if !keyin { t.Error("Key 'testmap' has value, that present in map, check should return true") } }
func TestKeyNotInMap(t *testing.T) { paramsMap["testmap"] = "not in map" keynotin := testmap.Check(data.CreateParameters(paramsMap)) if keynotin { t.Error("Key 'testmap' has value, that is not exists in map, check should return false") } }
func CreateParameters(call Call) data.Parameters { return data.CreateParameters(map[string]data.Parameter{ "Time": time.Now().Unix(), "callTime": call.CallTime.Format(time.RFC822), "code": call.CountryCode, "prefix": call.Prefix, "number": call.Number, "duration": uint(call.Duration), }) }
func TestStaticModifier(t *testing.T) { s := Static{&data.SingleWeight{5}} params := data.CreateParameters(map[string]data.Parameter{"Type": uint(0), "Time": int64(0)}) s1 := data.SingleWeight{6} result := s.Modify(&s1, params) if result.(*data.SingleWeight).Value != 5 { t.Error("Static modifier should return weight, stored in value, it is 5, not ", result.(*data.SingleWeight).Value) } }
func BenchmarkMap(b *testing.B) { paramsMap["testmap"] = "two" for i := int64(0); i < 500; i++ { paramsMap[strconv.FormatInt(i, 10)+"qqq"] = strconv.FormatInt(i, 16) testmap.Values[strconv.FormatInt(i, 10)+"qwe"] = struct{}{} } params := data.CreateParameters(paramsMap) for i := 0; i < b.N; i++ { testmap.Check(params) } }
// middle condition failed, modifier not applied -> initial weight func TestNotApplied(t *testing.T) { failedParamsMap := make(map[string]data.Parameter) for key, value := range paramsMap { failedParamsMap[key] = value } failedParamsMap["second"] = "failed" rule := RuleRecord{"rule", conditions[0:3], mods} _, affected := rule.Calculate(&weight, data.CreateParameters(failedParamsMap)) if affected { t.Error("Second condition of rule should fail, so the affected should be false") } }
func TestSingleModifier(t *testing.T) { f := func(value data.Comparable) data.Comparable { return value.(int) + 5 } iw := data.SingleWeight{7} iwm := SingleModifier{f} params := data.CreateParameters(map[string]data.Parameter{"Type": uint(0), "Time": int64(0)}) result := iwm.Modify(&iw, params) iwmResult := result.(*data.SingleWeight) if iwmResult.Value != 12 { t.Error("Int value modifier error, result should be 12, but given ", iwmResult.Value) } }
func TestExclude(t *testing.T) { testmap := Map{ Name: "testmap", Values: map[data.Parameter]struct{}{ "one": struct{}{}, "two": struct{}{}, "three": struct{}{}, }, Exclude: true, } paramsMap["testmap"] = "four" keyin := testmap.Check(data.CreateParameters(paramsMap)) if !keyin { t.Error("Key 'testmap' has value, that not present in map, but exclude is true, result should be true") } }
var mods = []Modifier{ &modifier.Static{&data.SingleWeight{9}}, &modifier.Static{&data.SingleWeight{10}}, } var weight = data.SingleWeight{5} var paramsMap = map[string]data.Parameter{ "zero": "hour", "first": "two", "second": "four", "third": "nine", "last": "unused", "Type": uint(2), "Time": int64(3), } var params = data.CreateParameters(paramsMap) // conditions passed, modifier applied -> mod weight func TestApplied(t *testing.T) { rule := RuleRecord{"rule", conditions[0:3], mods} calculated, affected := rule.Calculate(&weight, params) if !affected { t.Error("Rule affects weight cause conditions should be passed, but false given") } result := calculated.(*data.SingleWeight) if result.Value.(int) != 10 { t.Error("Modifier of rule was set to return 10, but given ", result.Value) } }
}), false, }, &condition.Map{ "prefix", condition.SliceToMap([]data.Parameter{ "900", "902", "904", "908", "950", "951", "952", "953", }), false, }, }, []expert.Modifier{ &modifier.Static{ &data.ParamsWeight{ data.CreateParameters(map[string]data.Parameter{ "operator": "Tele2", }), nil, }, }, }, } var local = &expert.RuleRecord{ "Tele2 locations", []expert.Condition{ &condition.Map{ "code", condition.SliceToMap([]data.Parameter{ "+7", "8", }),
package condition import ( "github.com/graarh/golang/expert/data" "testing" ) var intervalParams = data.CreateParameters(map[string]data.Parameter{ "Time": int64(10), "Type": uint(0), }) var Any = Point{0, false} func getPoint(value int64) Point { if value == 0 { return Any } return Point{value, true} } func createInterval(from, to int64) Interval { return Interval{getPoint(from), getPoint(to), TimeValue} } func TestAlwaysPass(t *testing.T) { interval := createInterval(0, 0) if interval.Check(intervalParams) != true { t.Error("0,0 interval should always pass") } }
func TestKeyAbsent(t *testing.T) { nokey := testmap.Check(data.CreateParameters(paramsMap)) if nokey { t.Error("Key 'testmap' is not present in params, check should return false") } }