Beispiel #1
0
def test_parser_limit_negative_fail():
    """Make sure providing a negative limit fails."""
    query_params = {"limit": "-1"}
    parser = QueryParamParser(query_params)
    with raises(OffsetLimitParseError) as excinfo:
        parser.parse_offset_limit()
    assert excinfo.value.code == "invalid_limit_value"
Beispiel #2
0
def test_parser_limit_override():
    """Ensure providing a page_max_size overrides a high limit."""
    query_params = {"limit": "1000"}
    parser = QueryParamParser(query_params)
    with raises(OffsetLimitParseError) as excinfo:
        parser.parse_offset_limit(30)
    assert excinfo.value.code == "limit_too_high"
Beispiel #3
0
def test_parser_bad_page_num():
    """Test that providing a negative page number fails."""
    query_params = {"page": "-1"}
    parser = QueryParamParser(query_params)
    with raises(OffsetLimitParseError) as excinfo:
        parser.parse_offset_limit()
    assert excinfo.value.code == "invalid_page_value"
Beispiel #4
0
def test_parser_offset_fail():
    """Make sure providing a bad offset query_param fails."""
    query_params = {"offset": "test"}
    parser = QueryParamParser(query_params)
    with raises(OffsetLimitParseError) as excinfo:
        parser.parse_offset_limit()
    assert excinfo.value.code == "invalid_offset_value"
Beispiel #5
0
def test_parser_page_invalid_type():
    """Providing a non integer page should fail."""
    query_params = {"page": "test"}
    parser = QueryParamParser(query_params)
    with raises(OffsetLimitParseError) as excinfo:
        parser.parse_offset_limit()
    assert excinfo.value.code == "invalid_page_value"
Beispiel #6
0
def test_parser_no_page_max_size_fail():
    """Not providing a max page size with page > 1 should fail."""
    query_params = {"page": "2"}
    parser = QueryParamParser(query_params)
    with raises(OffsetLimitParseError) as excinfo:
        parser.parse_offset_limit()
    assert excinfo.value.code == "page_no_max"
Beispiel #7
0
def test_parser_embeds():
    """Test embed parsing."""
    parser = QueryParamParser(query_params={"embeds": "a,b,c"})
    embeds = parser.parse_embeds()
    assert len(embeds) == 3
    assert embeds[0] == "a"
    assert embeds[1] == "b"
    assert embeds[2] == "c"
Beispiel #8
0
def test_parser_fields():
    """Test fields parsing."""
    parser = QueryParamParser(query_params={"fields": "a,b,c"})
    fields = parser.parse_fields()
    assert len(fields) == 3
    assert fields[0] == "a"
    assert fields[1] == "b"
    assert fields[2] == "c"
Beispiel #9
0
 def test_limit(self):
     """Make sure providing a limit query_param works."""
     query_params = {"limit": "1"}
     parser = QueryParamParser(query_params)
     album_resource = AlbumResource(session=self.db_session)
     offset, limit = parser.parse_offset_limit(page_max_size=30)
     result = album_resource.get_collection(
         filters=parser.parse_filters(album_resource.model),
         sorts=parser.parse_sorts(),
         limit=limit,
         offset=offset
     )
     self.assertTrue(len(result) == 1)
Beispiel #10
0
 def test_get_resources_ordered(self):
     """Test simple get_resources sort functionality."""
     query_params = {
         "sort": "-album_id,title"
     }
     parser = QueryParamParser(query_params)
     album_resource = AlbumResource(session=self.db_session)
     result = album_resource.get_collection(
         filters=parser.parse_filters(album_resource.model),
         sorts=parser.parse_sorts()
     )
     self.assertTrue(
         len(result) == 347 and
         result[0]["album_id"] == 347)
Beispiel #11
0
 def test_get_first_page(self):
     """Test that we can get the first page of a set of objects."""
     query_params = {
         "sort": "album_id"
     }
     album_resource = AlbumResource(session=self.db_session)
     parser = QueryParamParser(query_params)
     offset, limit = parser.parse_offset_limit(page_max_size=30)
     result = album_resource.get_collection(
         filters=parser.parse_filters(album_resource.model),
         sorts=parser.parse_sorts(),
         limit=limit,
         offset=offset
     )
     self.assertTrue(
         len(result) == 30 and
         result[0]["album_id"] == 1)
Beispiel #12
0
def test_parser_context_dict():
    """Test parser context uses a dict properly."""
    parser = QueryParamParser(query_params={}, context={"a": "b"})
    assert parser.context.get("a") == "b"
Beispiel #13
0
def test_parser_context_setter():
    """Test parser context uses a callable properly."""
    parser = QueryParamParser(query_params={}, context={})
    parser.context = {"a": "b"}
    assert parser.context.get("a") == "b"
Beispiel #14
0
def test_parser_context_callable():
    """Test parser context uses a callable properly."""
    parser = QueryParamParser(query_params={}, context=dict)
    assert isinstance(parser.context, dict)