def test_parse_time_given_valid_args(self, args, expected): parser = FoodParser(Food) parser.parse_time(args) assert compare_nested_exprs(parser.where_clause, expected) if isinstance(expected.rhs, time): assert parser.time == expected.rhs else: assert parser.time == expected.rhs.nodes[0]
def test_parse_given_valid_args(self, args, expected): parser = FoodParser(Food) parser.parse(args) assert compare_nested_exprs(parser.where_clause, expected["where_clause"]) assert parser.sort_clause == expected["sort_clause"] assert parser.limit_clause == expected["limit_clause"] assert parser.returning_clause == expected["returning_clause"]
def parse_args(args): parser = FoodParser(Food) parser.parse(args) query = (Food.select(*parser.returning_clause).where( parser.where_clause).order_by( *(parser.sort_clause or (Food.date.asc(), Food.time.asc()))).limit( parser.limit_clause).dicts()) return query, parser.columns or ["id", "name", "time", "date"]
def test_search_keyword(self, string, expr_name, expected): parser = FoodParser(Food) for expr in parser.exprs: if expr["name"] == expr_name: pattern = expr["keyword_pattern"] if expected is None: with pytest.raises(KeywordNotFound): parser.search_keyword(pattern, string) else: match = parser.search_keyword(pattern, string) assert match.matched == expected
def test_parse_given_reset_false(self, arg_list, expected_attrs): assert len(arg_list) == len(expected_attrs) parser = FoodParser(Food) for args, expected in zip(arg_list, expected_attrs): parser.parse(args, reset=False) assert compare_nested_exprs(parser.where_clause, expected["where_clause"]) assert parser.sort_clause == expected["sort_clause"] assert parser.limit_clause == expected["limit_clause"] assert parser.returning_clause == expected["returning_clause"] assert parser.id == expected["id"] assert parser.name == expected["name"] assert parser.date == expected["date"] assert parser.time == expected["time"] assert parser.columns == expected["columns"]
def parse_args(args): parser = FoodParser(Food) parser.parse(args) params = {} if not parser.id: raise IdFieldNotFound params["id"] = parser.id if not parser.name: raise NameFieldNotFound params["name"] = parser.name if parser.date: params["date"] = parser.date if parser.time: params["time"] = parser.time return Food.replace(**params)
def test_parse_returning_given_invalid_args(self, args): parser = FoodParser(Food) with pytest.raises(InvalidColumn): parser.parse_returning(args)
def test_parse_returning_given_valid_args(self, args, expected): parser = FoodParser(Food) parser.parse_returning(args) assert parser.returning_clause == expected assert parser.columns == [col.name for col in expected]
def test_parse_limit_given_invalid_args(self, args): parser = FoodParser(Food) with pytest.raises(InvalidLimit): parser.parse_limit(args)
def test_parse_limit_given_valid_args(self, args, expected): parser = FoodParser(Food) parser.parse_limit(args) assert parser.limit_clause == expected
def test_parse_date_given_valid_args(self, args, expected): parser = FoodParser(Food) parser.parse_date(args) assert compare_nested_exprs(parser.where_clause, expected) assert parser.date == expected.rhs
def test_parse_name_given_invalid_args(self, args): parser = FoodParser(Food) with pytest.raises(InvalidName): parser.parse_name(args)
def test_parse_id_given_invalid_args(self, args): parser = FoodParser(Food) with pytest.raises(InvalidId): parser.parse_id(args)
def test_reset_attributes(self): parser = FoodParser(Food) parser.where_clause_exprs = ["foo"] parser.id = 1 parser.name = "name" parser.date = "date" parser.time = "time" parser.sort_clause = ["foo"] parser.limit_clause = 999 parser.columns = ["foo"] parser.returning_clause = ["foo"] parser.reset_attributes() assert parser.id is None assert parser.name is None assert parser.date is None assert parser.time is None assert parser.sort_clause == [] assert parser.limit_clause == -1 assert parser.columns == [] assert parser.returning_clause == []
def test_parse_given_invalid_args(self, args, error, invalid_value): parser = FoodParser(Food) with pytest.raises(error, match=rf".+?'{invalid_value}'.+"): parser.parse(args)
def parse_args(args): parser = FoodParser(Food) parser.parse(args) return Food.delete().where(parser.where_clause)