예제 #1
0
 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]
예제 #2
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"]
예제 #3
0
 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"]
예제 #4
0
    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
예제 #5
0
 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"]
예제 #6
0
    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)
예제 #7
0
 def test_parse_returning_given_invalid_args(self, args):
     parser = FoodParser(Food)
     with pytest.raises(InvalidColumn):
         parser.parse_returning(args)
예제 #8
0
 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]
예제 #9
0
 def test_parse_limit_given_invalid_args(self, args):
     parser = FoodParser(Food)
     with pytest.raises(InvalidLimit):
         parser.parse_limit(args)
예제 #10
0
 def test_parse_limit_given_valid_args(self, args, expected):
     parser = FoodParser(Food)
     parser.parse_limit(args)
     assert parser.limit_clause == expected
예제 #11
0
 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
예제 #12
0
 def test_parse_name_given_invalid_args(self, args):
     parser = FoodParser(Food)
     with pytest.raises(InvalidName):
         parser.parse_name(args)
예제 #13
0
 def test_parse_id_given_invalid_args(self, args):
     parser = FoodParser(Food)
     with pytest.raises(InvalidId):
         parser.parse_id(args)
예제 #14
0
    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 == []
예제 #15
0
 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)
예제 #16
0
 def parse_args(args):
     parser = FoodParser(Food)
     parser.parse(args)
     return Food.delete().where(parser.where_clause)