def test_expression_empty(self): param = { "type": "group", "name": "字段名", "fid": "f1", "data_type": "date", "info": { "type": "expression", "groups": [ { "name": "Big", "expression": "" }, { "name": "Small", "expression": "" }, ], "default": "February" } } builder = GroupFormulaBuilder(param) self.assertEqual("", builder.build())
def test_number_custom(self): param = { "type": "group", "name": "新字段名称", "fid": "f1", "data_type": "number", "info": { "type": "custom", "groups": [{ "name": "Excellent", "range": [90, None], "boundary": [1, 0] }, { "name": "Good", "range": [80, 90], "boundary": [1, 0] }, { "name": "Normal", "range": [60, 80], "boundary": [1, 0] }], "default": "Fail" } } builder = GroupFormulaBuilder(param) expect = "(CASE WHEN f1>=90 THEN 'Excellent' " \ "WHEN (80<=f1 AND f1<90) THEN 'Good' " \ "WHEN (60<=f1 AND f1<80) THEN 'Normal' " \ "ELSE 'Fail' END)" self.assertEqual(expect, builder.build())
def test_expression_constant(self): param = { "type": "group", "name": "字段名", "fid": "f1", "data_type": "date", "info": { "type": "expression", "groups": [{ "name": "Zero", "expression": 0 }, { "name": "One", "expression": 1 }, { "name": "Invalid", "items": "" }], "default_name": "Oops" } } builder = GroupFormulaBuilder(param) expect = "(CASE WHEN (0) THEN 'Zero' " \ "WHEN (1) THEN 'One' " \ "ELSE 'Oops' END)" self.assertEqual(expect, builder.build())
def test_date_expression(self): param = { "type": "group", "name": "字段名", "fid": "f1", "data_type": "date", "info": { "type": "expression", "groups": [ { "name": "Big", "expression": "ARRAY_CONTAINS(ARRAY(1,3,5,7,8,10,12), MONTH([_field_id_]))" }, { "name": "Small", "expression": "ARRAY_CONTAINS(ARRAY(4,6,9,11), MONTH([_field_id_]))" }, ], "default": "February" } } builder = GroupFormulaBuilder(param) expect = "(CASE WHEN (ARRAY_CONTAINS(ARRAY(1,3,5,7,8,10,12), MONTH(f1))) THEN 'Big' " \ "WHEN (ARRAY_CONTAINS(ARRAY(4,6,9,11), MONTH(f1))) THEN 'Small' " \ "ELSE 'February' END)" self.assertEqual(expect, builder.build())
def test_number_expression(self): param = { "type": "group", "name": "字段名", "fid": "f1", "data_type": "number", "info": { "type": "expression", "groups": [ { "name": "Natural Number", "expression": "[_field_id_] >= 0" }, { "name": "Negative Number", "expression": "[_field_id_] < 0" }, ], "default": "Oops" } } builder = GroupFormulaBuilder(param) expect = "(CASE WHEN (f1 >= 0) THEN 'Natural Number' " \ "WHEN (f1 < 0) THEN 'Negative Number' " \ "ELSE 'Oops' END)" self.assertEqual(expect, builder.build())
def test_string_expression(self): param = { "type": "group", "name": "字段名", "fid": "f1", "data_type": "string", "info": { "type": "expression", "groups": [ { "name": "1or2", "expression": "[_field_id_] = '1' or [_field_id_] = '2'" }, { "name": "3or4", "expression": "[_field_id_] = '3' or [_field_id_] = '4'" }, ], "default": "Oops" } } builder = GroupFormulaBuilder(param) expect = "(CASE WHEN (f1 = '1' or f1 = '2') THEN '1or2' " \ "WHEN (f1 = '3' or f1 = '4') THEN '3or4' " \ "ELSE 'Oops' END)" self.assertEqual(expect, builder.build())
def test_string_item(self): param = { "type": "group", "name": "字段名", "fid": "f1", "data_type": "string", "info": { "type": "item", "groups": [ { "name": "Balls", "conditions": ["Football", "Basketball", "Volleyball"] }, { "name": "Oxygen Sports", "conditions": ["Running", "Swimming"] }, ], "default": "Oops" } } builder = GroupFormulaBuilder(param) expect = "(CASE WHEN (ARRAY_CONTAINS(ARRAY('Football', 'Basketball', 'Volleyball'), f1)) THEN 'Balls' " \ "WHEN (ARRAY_CONTAINS(ARRAY('Running', 'Swimming'), f1)) THEN 'Oxygen Sports' " \ "ELSE 'Oops' END)" self.assertEqual(expect, builder.build())
def test_number_fixed_1(self): param = { "type": "", "name": "新字段名称", "fid": "f1", "data_type": "number", "info": { "type": "fixed", "step": 40, "range": [0, 100] } } builder = GroupFormulaBuilder(param) expect = 'FIXED_NUMBER_GROUP(f1, 0, 100, 40, "zh")' self.assertEqual(expect, builder.build())
def test_day_fixed(self): param = { "type": "group", "name": "字段名", "fid": "f1", "data_type": "date", "info": { "type": "fixed", "aggregator": "day", "start": ["2012-01-01"], "step": 2 } } builder = GroupFormulaBuilder(param) self.assertEqual('FIXED_DATE_GROUP(f1, "2012-01-01", 2, "day", "zh")', builder.build())
def test_string_condition(self): param = { "type": "group", "name": "字段名", "fid": "f1", "data_type": "string", "info": { "groups": [{ "name": "Group1", "conditions": [{ "operator": 0, "value": "foo" }, { "operator": 0, "value": "guy" }] }, { "name": "Group2", "conditions": [{ "operator": 1, "value": "bar" }] }, { "name": "Group3", "conditions": [{ "operator": 2, "value": "baz" }] }], "default": "Oops" } } builder = GroupFormulaBuilder(param) expect = "(CASE WHEN (f1='foo' OR f1='guy') THEN 'Group1' " \ "WHEN (INSTRING(f1,'bar')!=0) THEN 'Group2' " \ "WHEN (INSTRING(f1,'baz')=0) THEN 'Group3' " \ "ELSE 'Oops' END)" self.assertEqual(expect, builder.build())
def test_number_custom_empty_interval(self): param = { "type": "group", "name": "新字段名称", "fid": "f1", "data_type": "number", "info": { "type": "custom", "groups": [ { "name": "Excellent", "range": [None, None], "boundary": [1, 0] }, ], "default": "Fail" } } builder = GroupFormulaBuilder(param) self.assertEqual("", builder.build())
def test_date_fixed_real_time(self): param = { "type": "group", "name": "字段名", "fid": "f1", "data_type": "date", "info": { "type": "fixed_real_time", "aggregator": "year", "start": ["2012"], "end": ["2016"], "step": 2 } } builder = GroupFormulaBuilder(param) result = builder.build().lower() self.assertTrue(result != 'fixed_real_time') self.assertTrue("case" in result) self.assertTrue("when" in result) self.assertTrue("then" in result) self.assertTrue("else" in result) self.assertTrue("end" in result)
def get_fixed_formula(self, tb_id, field_info, locale_code="zh"): # 调用tassadar client 取得相应的最大值 user_id = field_info[VField.OWNER] try: tsd = self._get_query_client() param = json.loads(field_info['param']) aggregator = param['info']['aggregator'] fid_formula = param.get("fid_formula", "") if param.get( "fid_formula", "") else param.get("fid", "") if aggregator == "year": fields = [ 'YEAR(MAX(%s))' % fid_formula, ] res = tsd.query_tb( tb_id, user_id, fields=fields, ) end = [ int(res['data'][0][0]), ] elif aggregator == "quarter": fields = [ 'YEAR(MAX(%s))' % fid_formula, 'QUARTER(MAX(%s))' % fid_formula ] res = tsd.query_tb( tb_id, user_id, fields=fields, ) end = [int(res['data'][0][0]), int(res['data'][0][1])] elif aggregator == "month": fields = [ 'YEAR(MAX(%s))' % fid_formula, 'MONTH(MAX(%s))' % fid_formula ] res = tsd.query_tb( tb_id, user_id, fields=fields, ) end = [int(res['data'][0][0]), int(res['data'][0][1])] elif aggregator == "week": fields = [ 'YEAR(MAX(%s))' % fid_formula, 'WEEK(MAX(%s))' % fid_formula ] res = tsd.query_tb(tb_id, user_id, fields=fields) end = [ int(res['data'][0][0]), int(res['data'][0][1].split(" ")[1]) ] elif aggregator == "day": fields = ['MAX(%s)' % fid_formula] res = tsd.query_tb(tb_id, user_id, fields=fields) end = [ res['data'][0][0].split(" ")[0], ] param['info']['end'] = end param['info']['type'] = 'fixed_real_time' return GroupFormulaBuilder(param, locale_code).build() except Exception as e: print_exc() return ''