Exemplo n.º 1
0
def test_redis_communication_writer_hash(mock_write_communication_to_buffer,
                                         right_to_left, inferred,
                                         uuid_hash_key):
    redis = Mock(
        hset=Mock(side_effect=[0]),
        type=Mock(return_value=b'hash'),
    )
    comm = Mock(id=sentinel.comm_id, uuid=Mock(uuidString=sentinel.comm_uuid))
    mock_write_communication_to_buffer.side_effect = [sentinel.buf]

    writer = RedisCommunicationWriter(redis,
                                      sentinel.key,
                                      key_type=None if inferred else 'hash',
                                      right_to_left=right_to_left,
                                      uuid_hash_key=uuid_hash_key)

    if inferred:
        redis.type.assert_called_once_with(sentinel.key)
    else:
        assert not redis.type.called

    writer.write(comm)

    redis.hset.assert_called_once_with(
        sentinel.key,
        sentinel.comm_uuid if uuid_hash_key else sentinel.comm_id,
        sentinel.buf)
    assert not getattr(redis, 'rpush' if right_to_left else 'lpush').called

    mock_write_communication_to_buffer.assert_called_once_with(comm)
Exemplo n.º 2
0
def test_redis_communication_writer_list(
        mock_write_communication_to_buffer, right_to_left, inferred):
    redis = Mock(
        lpush=Mock(side_effect=[3]),
        rpush=Mock(side_effect=[3]),
        type=Mock(return_value=b'list'),
    )
    mock_write_communication_to_buffer.side_effect = [sentinel.buf]

    writer = RedisCommunicationWriter(
        redis, sentinel.key, key_type=None if inferred else 'list',
        right_to_left=right_to_left)

    if inferred:
        redis.type.assert_called_once_with(sentinel.key)
    else:
        assert not redis.type.called

    writer.write(sentinel.comm)

    getattr(redis, 'lpush' if right_to_left else 'rpush').assert_called_once_with(
        sentinel.key, sentinel.buf)
    assert not getattr(redis, 'rpush' if right_to_left else 'lpush').called

    mock_write_communication_to_buffer.assert_called_once_with(sentinel.comm)
Exemplo n.º 3
0
def test_redis_communication_writer_list(mock_write_communication_to_buffer,
                                         right_to_left, inferred):
    redis = Mock(
        lpush=Mock(side_effect=[3]),
        rpush=Mock(side_effect=[3]),
        type=Mock(return_value=b'list'),
    )
    mock_write_communication_to_buffer.side_effect = [sentinel.buf]

    writer = RedisCommunicationWriter(redis,
                                      sentinel.key,
                                      key_type=None if inferred else 'list',
                                      right_to_left=right_to_left)

    if inferred:
        redis.type.assert_called_once_with(sentinel.key)
    else:
        assert not redis.type.called

    writer.write(sentinel.comm)

    getattr(redis,
            'lpush' if right_to_left else 'rpush').assert_called_once_with(
                sentinel.key, sentinel.buf)
    assert not getattr(redis, 'rpush' if right_to_left else 'lpush').called

    mock_write_communication_to_buffer.assert_called_once_with(sentinel.comm)
Exemplo n.º 4
0
def test_redis_communication_writer_hash(
        mock_write_communication_to_buffer, right_to_left, inferred,
        uuid_hash_key):
    redis = Mock(
        hset=Mock(side_effect=[0]),
        type=Mock(return_value=b'hash'),
    )
    comm = Mock(id=sentinel.comm_id, uuid=Mock(uuidString=sentinel.comm_uuid))
    mock_write_communication_to_buffer.side_effect = [sentinel.buf]

    writer = RedisCommunicationWriter(
        redis, sentinel.key, key_type=None if inferred else 'hash',
        right_to_left=right_to_left, uuid_hash_key=uuid_hash_key)

    if inferred:
        redis.type.assert_called_once_with(sentinel.key)
    else:
        assert not redis.type.called

    writer.write(comm)

    redis.hset.assert_called_once_with(
        sentinel.key,
        sentinel.comm_uuid if uuid_hash_key else sentinel.comm_id,
        sentinel.buf)
    assert not getattr(redis, 'rpush' if right_to_left else 'lpush').called

    mock_write_communication_to_buffer.assert_called_once_with(comm)
Exemplo n.º 5
0
 def test_set_implicit(self):
     key = 'dataset'
     with RedisServer(loglevel='warning') as server:
         redis_db = Redis(port=server.port)
         redis_db.sadd(key, self.buf1)
         w = RedisCommunicationWriter(redis_db, key)
         w.write(self.comm2)
         w.write(self.comm3)
         self.assertEquals(3, redis_db.scard(key))
Exemplo n.º 6
0
 def test_hash_implicit(self):
     key = 'dataset'
     with RedisServer(loglevel='warning') as server:
         redis_db = Redis(port=server.port)
         redis_db.hset(key, self.comm1.id, self.buf1)
         w = RedisCommunicationWriter(redis_db, key)
         w.write(self.comm2)
         w.write(self.comm3)
         self.assertEquals(3, redis_db.hlen(key))
         self.assertEquals(self.buf1, redis_db.hget(key, self.comm1.id))
         self.assertEquals(self.buf2, redis_db.hget(key, self.comm2.id))
         self.assertEquals(self.buf3, redis_db.hget(key, self.comm3.id))
Exemplo n.º 7
0
 def test_list_implicit(self):
     key = 'dataset'
     with RedisServer(loglevel='warning') as server:
         redis_db = Redis(port=server.port)
         redis_db.lpush(key, self.buf1)
         w = RedisCommunicationWriter(redis_db, key)
         w.write(self.comm2)
         w.write(self.comm3)
         self.assertEquals(3, redis_db.llen(key))
         self.assertEquals(self.buf1, redis_db.lindex(key, -1))
         self.assertEquals(self.buf2, redis_db.lindex(key, -2))
         self.assertEquals(self.buf3, redis_db.lindex(key, -3))
Exemplo n.º 8
0
def test_redis_communication_writer_clear(right_to_left, inferred, key_type):
    redis = Mock(
        delete=Mock(side_effect=[0]),
        type=Mock(return_value=key_type.encode('utf-8')),
    )

    writer = RedisCommunicationWriter(
        redis, sentinel.key, key_type=None if inferred else 'hash',
        right_to_left=right_to_left)

    if inferred:
        redis.type.assert_called_once_with(sentinel.key)
    else:
        assert not redis.type.called

    writer.clear()

    redis.delete.assert_called_once_with(sentinel.key)
Exemplo n.º 9
0
def test_redis_communication_writer_clear(right_to_left, inferred, key_type):
    redis = Mock(
        delete=Mock(side_effect=[0]),
        type=Mock(return_value=key_type.encode('utf-8')),
    )

    writer = RedisCommunicationWriter(redis,
                                      sentinel.key,
                                      key_type=None if inferred else 'hash',
                                      right_to_left=right_to_left)

    if inferred:
        redis.type.assert_called_once_with(sentinel.key)
    else:
        assert not redis.type.called

    writer.clear()

    redis.delete.assert_called_once_with(sentinel.key)
Exemplo n.º 10
0
 def test_set(self):
     key = 'dataset'
     with RedisServer(loglevel='warning') as server:
         redis_db = Redis(port=server.port)
         w = RedisCommunicationWriter(redis_db, key, key_type='set')
         w.write(self.comm1)
         self.assertEquals(1, redis_db.scard(key))
         self.assertEquals(self.buf1, redis_db.srandmember(key))
         w.write(self.comm2)
         w.write(self.comm3)
         self.assertEquals(3, redis_db.scard(key))
Exemplo n.º 11
0
 def test_list_left_to_right(self):
     key = 'dataset'
     with RedisServer(loglevel='warning') as server:
         redis_db = Redis(port=server.port)
         w = RedisCommunicationWriter(redis_db, key, key_type='list',
                                      right_to_left=False)
         w.write(self.comm1)
         self.assertEquals(1, redis_db.llen(key))
         self.assertEquals(self.buf1, redis_db.lindex(key, 0))
         w.write(self.comm2)
         w.write(self.comm3)
         self.assertEquals(3, redis_db.llen(key))
         self.assertEquals(self.buf3, redis_db.lindex(key, -1))
         self.assertEquals(self.buf2, redis_db.lindex(key, -2))
         self.assertEquals(self.buf1, redis_db.lindex(key, -3))
Exemplo n.º 12
0
 def test_hash_uuid_key(self):
     key = 'dataset'
     with RedisServer(loglevel='warning') as server:
         redis_db = Redis(port=server.port)
         w = RedisCommunicationWriter(
             redis_db, key, key_type='hash', uuid_hash_key=True)
         w.write(self.comm1)
         self.assertEquals(1, redis_db.hlen(key))
         self.assertEquals(self.buf1, redis_db.hget(
             key, self.comm1.uuid.uuidString))
         w.write(self.comm2)
         w.write(self.comm3)
         self.assertEquals(3, redis_db.hlen(key))
         self.assertEquals(self.buf1, redis_db.hget(
             key, self.comm1.uuid.uuidString))
         self.assertEquals(self.buf2, redis_db.hget(
             key, self.comm2.uuid.uuidString))
         self.assertEquals(self.buf3, redis_db.hget(
             key, self.comm3.uuid.uuidString))
Exemplo n.º 13
0
def test_redis_communication_writer_failed_key_type(key_type):
    with raises(ValueError):
        RedisCommunicationWriter(sentinel.redis,
                                 sentinel.key,
                                 key_type=key_type)
Exemplo n.º 14
0
def test_redis_communication_writer_failed_infer():
    redis = Mock(type=Mock(return_value=b'none'), )
    with raises(Exception):
        RedisCommunicationWriter(redis, sentinel.key)
    redis.type.assert_called_with(sentinel.key)
Exemplo n.º 15
0
 def test_implicit_empty(self):
     key = 'dataset'
     with RedisServer(loglevel='warning') as server:
         redis_db = Redis(port=server.port)
         with self.assertRaises(Exception):
             RedisCommunicationWriter(redis_db, key)