示例#1
0
def test_get_spaces():
    config = PNConfiguration()
    config.subscribe_key = SUB_KEY
    config.auth_key = AUTH
    spaces = PubNub(config).get_spaces()
    spaces.include(['a', 'b']).limit(30).end('XXX')

    assert spaces.build_path() == GetSpaces.GET_SPACES_PATH % SUB_KEY

    params = spaces.custom_params()
    assert params['include'] == ['a', 'b']
    assert params['limit'] == 30
    assert params['end'] == 'XXX'
    assert 'count' not in params

    spaces.start('YYY').count(True)
    params = spaces.custom_params()
    assert 'end' not in params
    assert params['start'] == 'YYY'
    assert params['count'] is True

    assert AUTH == spaces.build_params_callback()({})['auth']
def test_create_space():
    config = PNConfiguration()
    config.subscribe_key = SUB_KEY
    config.auth_key = AUTH
    space = PubNub(config).create_space()
    with pytest.raises(PubNubException):
        space.validate_params()
    space.include({'name': 'a'})
    with pytest.raises(PubNubException):
        space.validate_params()
    space.include({'id': 'x'})
    with pytest.raises(PubNubException):
        space.validate_params()
    space.include('custom')
    with pytest.raises(PubNubException):
        space.validate_params()
    space.data({'id': 'x', 'name': 'a'})
    space.validate_params()

    assert space.build_path() == CreateSpace.CREATE_SPACE_PATH % SUB_KEY
    assert AUTH == space.build_params_callback()({})['auth']
    assert json.loads(space.build_data()) == {'id': 'x', 'name': 'a'}