Exemple #1
0
def test_we_can_register_endpoint_without_namespace(reset_state_of_router):
    router.enable(version=1.0, endpoints=[SimpleEndpoint])

    version = router.Router.versions[0]

    assert version.as_str() == '1.0'
    assert len(version) == 1
    assert version.has('') is True
    assert version.get('').has(SimpleEndpoint)
Exemple #2
0
def test_basic_routing(reset_state_of_router):
    router.enable('1.0', 'user', endpoints=[SimpleEndpoint])

    version = router.Router.versions[0]

    assert version.as_str() == '1.0'
    assert len(version) == 1
    assert version.has('user') is True
    assert version.get('user').has(SimpleEndpoint) is True
Exemple #3
0
def test_we_can_register_same_endpoint_twice_at_different_namespaces(
        reset_state_of_router):
    router.enable('1.0', 'user', endpoints=[SimpleEndpoint])
    router.enable('1.0', 'vehicle', endpoints=[SimpleEndpoint])

    version = router.Router.versions[0]

    assert version.as_str() == '1.0'
    assert len(version) == 2
    assert version.has('user') is True
    assert version.has('vehicle') is True

    assert version.get('user').has(SimpleEndpoint)
    assert version.get('vehicle').has(SimpleEndpoint)
Exemple #4
0
def test_we_can_override_endpoint_at_will(reset_state_of_router):
    class BetterSimpleEndpoint(BaseEndpoint):
        url = '/'

    router.enable('1.0', 'user', endpoints=[SimpleEndpoint])

    router.enable('1.1', 'user', endpoints=[BetterSimpleEndpoint])

    version = router.Router.versions[0]

    assert version.as_str() == '1.0'
    assert len(version) == 1
    assert version.has('user') is True
    assert version.get('user').has(SimpleEndpoint)

    version = router.Router.versions[1]

    assert version.as_str() == '1.1'
    assert len(version) == 1
    assert version.has('user') is True
    assert version.get('user').has(BetterSimpleEndpoint)
Exemple #5
0
from carp_api.routing import router

from . import endpoints

router.enable('1.0', 'car', endpoints=[
    endpoints.GetListOfCars,
])
router.enable('1.0', 'tree', endpoints=[
    endpoints.GetListOfTrees,
])
Exemple #6
0
def test_router_will_materialise_url_structure_properly(reset_state_of_router):
    router.enable(1.0, 'user', endpoints=[
        CreateUser,
        GetListOfUsers,
    ])

    # version is a bit abomination but want to check if framework is resiliant
    router.enable((1, '0'), 'user', endpoints=[
        GetUserByPk,
    ])

    router.enable('1.1', 'user', endpoints=[
        GetUserWithAddressByPk,
    ])

    # sub-namespace, thus we add slash, it will have no meaning for url
    # resolving as those endpint will use custom urls
    router.enable('1.1',
                  'user/address',
                  endpoints=[
                      CreateUserAddress,
                      ChangeUserAddress,
                      DeleteUserAddress,
                  ])

    router.enable('1.2', 'tag', endpoints=[
        CreateTag,
        GetListOfTags,
    ])

    final_routing = router.Router.get_final_routing()

    assert final_routing[0].as_str() == '1.0'
    assert len(final_routing[0]) == 1
    assert final_routing[0].has('user') is True
    assert len(final_routing[0].get('user')) == 3
    assert final_routing[0].has('user/address') is False
    assert final_routing[0].has('tag') is False

    assert final_routing[1].as_str() == '1.1'
    assert len(final_routing[1]) == 2
    assert final_routing[1].has('user') is True
    assert len(final_routing[1].get('user')) == 3
    assert final_routing[1].has('user/address') is True
    assert len(final_routing[1].get('user/address')) == 3
    assert final_routing[0].has('tag') is False

    assert final_routing[2].as_str() == '1.2'
    assert len(final_routing[2]) == 3
    assert final_routing[2].has('user') is True
    assert len(final_routing[2].get('user')) == 3
    assert final_routing[2].has('user/address') is True
    assert len(final_routing[2].get('user/address')) == 2
    assert final_routing[2].has('tag') is True
    assert len(final_routing[2].get('tag')) == 2

    list_of_urls = final_routing.as_flat_list()

    assert list_of_urls[0][0] == '1.0'
    assert list_of_urls[0][1] == 'user'

    assert 'CreateUser' in list_of_urls[0][2].keys()
    assert 'GetListOfUsers' in list_of_urls[0][2].keys()
    assert 'GetUserByPk' in list_of_urls[0][2].keys()

    assert list_of_urls[1][0] == '1.1'
    assert list_of_urls[1][1] == 'user'

    assert 'CreateUser' in list_of_urls[1][2].keys()
    assert 'GetListOfUsers' in list_of_urls[1][2].keys()
    assert 'GetUserByPk' in list_of_urls[1][2].keys()

    assert list_of_urls[2][0] == '1.1'
    assert list_of_urls[2][1] == 'user/address'

    assert 'ChangeUserAddress' in list_of_urls[2][2].keys()
    assert 'CreateUserAddress' in list_of_urls[2][2].keys()
    assert 'DeleteUserAddress' in list_of_urls[2][2].keys()

    assert list_of_urls[3][0] == '1.2'
    assert list_of_urls[3][1] == 'tag'

    assert 'CreateTag' in list_of_urls[3][2].keys()
    assert 'GetListOfTags' in list_of_urls[3][2].keys()

    assert list_of_urls[4][0] == '1.2'
    assert list_of_urls[4][1] == 'user'

    assert 'CreateUser' in list_of_urls[4][2].keys()
    assert 'GetListOfUsers' in list_of_urls[4][2].keys()
    assert 'GetUserByPk' in list_of_urls[4][2].keys()

    assert list_of_urls[5][0] == '1.2'
    assert list_of_urls[5][1] == 'user/address'

    assert 'ChangeUserAddress' in list_of_urls[5][2].keys()
    assert 'CreateUserAddress' in list_of_urls[5][2].keys()
from carp_api.routing import router

from sample_api import endpoints

# version number can be string, tuple of strings/integers/mix of both or float
# number (the last one is not encourage as it may cause quirks)
router.enable('1.0',
              'user',
              endpoints=[
                  endpoints.user.v10.CreateUser,
                  endpoints.user.v10.GetListOfUsers,
              ])

# we can run enable as many times as we wish in as many places as we wish
router.enable((1, 0), 'user', endpoints=[
    endpoints.user.v10.GetUserByPk,
])

# namespaces is whatever we put there, in this case we have slash and it does
# not make it related to user, it's for developer to build mental
# classification at most
router.enable(
    1.1,
    'user/address',
    endpoints=[
        # endpoints.address.CreateUserAddress,
        endpoints.address.ChangeUserAddress,
        endpoints.address.DeleteUserAddress,
    ])

# version 1.1 has new implementation of GetUserByPk, one that returns Address
Exemple #8
0
from carp_api.routing import router

from . import endpoints  # NOQA

# to use pong we need to disable common routes as they are using conflicting
# urls
router.enable(endpoints=[
    endpoints.UberPong,
])
Exemple #9
0
def test_that_we_cannot_register_same_endpoint_twice(reset_state_of_router):
    router.enable('1.0', 'user', endpoints=[SimpleEndpoint])

    with pytest.raises(exception.DuplicateEndpointError):
        router.enable('1.0', 'user', endpoints=[SimpleEndpoint])