示例#1
0
    async def test_connect_to_db(self, core1, json_data_values):
        json_data_values = [
            {
                "content_type": "type1",
                "ts": utcnow(),
                "data": '{"a": "aaa", "b": "bbb"}'
            },
            {
                "content_type": "type1",
                "ts": utcnow(),
                "data": '{"a": "xxx", "b": "yyy"}'
            },
        ]

        b = SqlBehav(core1)
        await core1.add_runtime_dependency(b)
        assert b.started

        # Execute many
        query = json_data.insert()
        await b.db.execute_many(query=query, values=json_data_values)

        query = json_data.select()
        rows = await b.db.fetch_all(query=query)
        assert len(rows) == len(json_data_values)

        await b.stop()
        assert b.state == 'shutdown'
示例#2
0
    async def test_insert(self, adb, json_data_values):
        query = json_data.insert()
        await adb.execute_many(query=query, values=json_data_values)

        query = json_data.select()
        rows = await adb.fetch_all(query=query)
        assert len(rows) == len(json_data_values)
示例#3
0
    async def test_receive_topic_and_store(self, sql_behav):
        # Given SerializableObject message
        msg = DemoData(message="xxxx",
                       date=datetime(2019, 1, 1, tzinfo=pytz.UTC)).serialize()

        # when messages are published
        await sql_behav.publish(msg, "x.y")
        await asyncio.sleep(0.1)  # relinquish cpu

        # then message must be written in database
        query = json_data.select()
        rows = await sql_behav.db.fetch_all(query=query)
        assert len(rows) == 1
示例#4
0
    async def test_receive_topic_and_store_many(self, sql_behav):
        """ time sensitive, depends on performance/sleep """
        n = 30
        for i in range(n):
            # Given SerializableObject message
            msg = DemoData(message=f"message: {i}",
                           date=datetime(2019, 1, 1,
                                         tzinfo=pytz.UTC)).serialize()
            # when n messages are published
            await sql_behav.publish(msg, "x.y")

        await asyncio.sleep(1)  # relinquish cpu

        # then n message must be written in database
        query = json_data.select()
        rows = await sql_behav.db.fetch_all(query=query)
        assert len(rows) == n
示例#5
0
    async def test_receive_topic_and_store_wrong_format(
            self, sql_behav, caplog):
        caplog.set_level(logging.DEBUG)

        # Given SerializableObject message
        msg = "xxx"

        # when messages are published
        await sql_behav.publish(msg, "x.y")
        await asyncio.sleep(0.1)  # relinquish cpu

        # then message must not be written in database due to wrong format
        query = json_data.select()
        rows = await sql_behav.db.fetch_all(query=query)

        assert any([
            record for record in caplog.records if record.levelname == "ERROR"
        ])
        assert 'Wrong message format:' in caplog.text
示例#6
0
    async def test_receive_topic_and_store_serializable_obj(self, sql_behav):
        # Given SerializableObject message
        @dataclass_json
        @dataclass
        class MyData(SerializableObject):
            message: str
            date: datetime = None

        # when this message type is added to behaviour
        sql_behav.add_msg_type(MyData)

        # when message of this type is sent to correct topic
        msg = MyData(message="xxxx",
                     date=datetime(2019, 1, 1, tzinfo=pytz.UTC)).serialize()
        # when messages are published
        await sql_behav.publish(msg, "x.y")
        await asyncio.sleep(0.1)  # relinquish cpu

        # then message must be written in database
        query = json_data.select()
        rows = await sql_behav.db.fetch_all(query=query)
        assert len(rows) == 1