Esempio 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)
Esempio n. 2
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)
Esempio 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)
Esempio n. 4
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)
Esempio 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))
Esempio n. 6
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))
Esempio n. 7
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))
Esempio n. 8
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))
Esempio n. 9
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))
Esempio n. 10
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))