Example #1
0
    async def test_disconnect(self):
        db = RedisDatabase({})
        db.client = MockRedisClient()
        db.client.close = amock.CoroutineMock()

        result = await db.disconnect()

        self.assertTrue(db.client.close.called)
Example #2
0
    async def test_get_return_None(self):
        db = RedisDatabase({})
        db.client = MockRedisClient()
        db.client.get = amock.CoroutineMock(return_value=None)

        result = await db.get("string")

        self.assertEqual(result, None)
Example #3
0
    async def test_get(self):
        db = RedisDatabase({})
        db.client = MockRedisClient()
        db.client.execute = amock.CoroutineMock(return_value='{"key":"value"}')

        result = await db.get("string")

        self.assertDictEqual(result, dict(key="value"))
Example #4
0
    async def test_delete(self):
        db = RedisDatabase({})
        db.client = MockRedisClient()
        db.client.execute = amock.CoroutineMock()
        db.client.execute.return_value = None

        result = await db.delete("string")

        self.assertEqual(result, None)
Example #5
0
async def test_disconnect(mocker):
    database = RedisDatabase({})
    database.client = mocker.Mock()
    attrs = {"close.return_value": return_async_value("None")}
    database.client.configure_mock(**attrs)

    await database.disconnect()

    assert database.client.close.called
Example #6
0
async def test_get_return_none(mocker, caplog):
    caplog.set_level(logging.DEBUG)
    database = RedisDatabase({})
    database.client = mocker.Mock()
    attrs = {"execute_command.return_value": return_async_value(None)}
    database.client.configure_mock(**attrs)

    result = await database.get("key")

    assert result is None
    assert "Getting" in caplog.text
Example #7
0
async def test_delete(mocker, caplog):
    caplog.set_level(logging.DEBUG)
    database = RedisDatabase({})
    database.client = mocker.Mock()
    attrs = {"execute_command.return_value": return_async_value("None")}
    database.client.configure_mock(**attrs)

    await database.delete("key")

    database.client.execute_command.assert_called_with("DEL", "key")
    assert "Deleting" in caplog.text
Example #8
0
async def test_put(mocker, caplog):
    caplog.set_level(logging.DEBUG)
    database = RedisDatabase({})
    database.client = mocker.Mock()
    attrs = {"execute.return_value": return_async_value("None")}
    database.client.configure_mock(**attrs)

    await database.put("key", dict(data_key="data_value"))

    database.client.execute.assert_called_with(
        "SET", "key", json.dumps(dict(data_key="data_value"), cls=JSONEncoder))
    assert "Putting" in caplog.text
Example #9
0
async def test_get(mocker, caplog):
    caplog.set_level(logging.DEBUG)
    database = RedisDatabase({})
    database.client = mocker.Mock()
    attrs = {
        "execute_command.return_value": return_async_value('{"data_key":"data_value"}')
    }
    database.client.configure_mock(**attrs)

    result = await database.get("key")

    assert result == dict(data_key="data_value")
    database.client.execute_command.assert_called_with("GET", "key")
    assert "Getting" in caplog.text
Example #10
0
async def test_connect(mocker, caplog):
    caplog.set_level(logging.DEBUG)
    database = RedisDatabase({})
    mocked_connection = mocker.patch("aioredis.create_pool")

    await database.connect()

    assert mocked_connection.called
    assert "Connected to Redis database" in caplog.text
Example #11
0
    async def test_connect_logging(self):
        opsdroid = amock.CoroutineMock()
        database = RedisDatabase({}, opsdroid=opsdroid)

        with amock.patch.object(aioredis, "create_pool") as mocked_connection:
            mocked_connection.set_result = amock.CoroutineMock()

            await database.connect()
            self.assertTrue(mocked_connection.called)
            self.assertLogs("_LOGGER", "info")
Example #12
0
async def test_connect_failure(mocker, caplog):
    caplog.set_level(logging.DEBUG)
    database = RedisDatabase({})
    mocked_connection = mocker.patch("aioredis.Redis.ping", side_effect=OSError)

    with suppress(OSError):
        await database.connect()

    assert mocked_connection.called
    assert "Unable to connect to Redis database" in caplog.text
Example #13
0
    async def test_connect_failure(self):
        opsdroid = amock.CoroutineMock()
        database = RedisDatabase({}, opsdroid=opsdroid)

        with amock.patch.object(aioredis, "create_pool") as mocked_connection:
            mocked_connection.side_effect = OSError()

            with suppress(OSError):
                await database.connect()
                self.assertLogs("_LOGGER", "warning")
Example #14
0
def test_init(caplog):
    caplog.set_level(logging.DEBUG)
    database = RedisDatabase({})
    assert None is database.client
    assert 0 == database.database
    assert "localhost" == database.host
    assert 6379 == database.port
    assert None is database.password

    assert "Loaded Redis database" in caplog.text
Example #15
0
    async def test_connect(self):
        opsdroid = amock.CoroutineMock()
        database = RedisDatabase({}, opsdroid=opsdroid)
        import asyncio_redis
        with amock.patch.object(asyncio_redis.Connection, 'create') as mocked_connection:
            mocked_connection.side_effect = NotImplementedError

            with suppress(NotImplementedError):
                await database.connect()
                self.assertTrue(mocked_connection.called)
Example #16
0
    async def test_connect(self):
        opsdroid = amock.CoroutineMock()
        database = RedisDatabase({}, opsdroid=opsdroid)

        with amock.patch.object(aioredis, "create_pool") as mocked_connection:
            mocked_connection.side_effect = NotImplementedError

            with suppress(NotImplementedError):
                await database.connect()
                self.assertTrue(mocked_connection.called)
                self.assertLogs("_LOGGER", "info")
Example #17
0
async def test_connect(mocker, caplog):
    caplog.set_level(logging.DEBUG)
    database = RedisDatabase({})
    mocked_connection = mocker.patch(
        "aioredis.Redis.ping", return_value=return_async_value(True)
    )

    await database.connect()

    assert mocked_connection.called
    assert "Connected to Redis database" in caplog.text
Example #18
0
    def test_init(self):
        """Test initialisation of database class.

        This method will test the initialisation of the database
        class. It will assert if the database class properties are
        declared and equated to None.

        """
        database = RedisDatabase({})
        self.assertEqual(None, database.client)
        self.assertEqual(0, database.database)
        self.assertEqual("localhost", database.host)
        self.assertEqual(6379, database.port)
        self.assertEqual(None, database.password)
Example #19
0
    def test_convert_timestamp_to_object(self):
        serialized_data = {
            "example_date": "date::1538366400",
            "example_datetime": "datetime::1538389815",
            "example_string": "test"
        }

        unserialized_data = RedisDatabase.convert_timestamp_to_object(serialized_data)

        self.assertEqual(unserialized_data["example_string"], "test")
        # Typically I would do assertDictEqual on the result, but as datetime are parsed based on the
        # timezone of the computer it makes the unittest fragile depending on the timezone of the user.
        self.assertIsInstance(unserialized_data["example_datetime"], datetime.datetime)
        self.assertIsInstance(unserialized_data["example_date"], datetime.date)
Example #20
0
    def test_other(self):
        unserialized_data = {
            "example_string": "test",
            "example_datetime": datetime.datetime.utcfromtimestamp(1538389815),
            "example_date": datetime.date.fromtimestamp(1538366400),
        }

        serialized_data = RedisDatabase.convert_object_to_timestamp(unserialized_data)

        self.assertEqual(serialized_data["example_string"], "test")
        # Typically I would do assertDictEqual on the result, but as datetime are parsed based on the
        # timezone of the computer it makes the unittest fragile depending on the timezone of the user.
        self.assertEqual(serialized_data["example_datetime"][0:10], "datetime::")
        self.assertEqual(serialized_data["example_date"][0:6], "date::")
Example #21
0
    async def test_put(self):
        db = RedisDatabase({})
        db.client = MockRedisClient()
        db.client.set = amock.CoroutineMock(return_value='{"key":"value"}')

        result = await db.put("string", dict(key="value"))