import orjson
from asynctest import TestCase, Mock, patch
from freezegun import freeze_time
from driftage.monitor import Monitor


class TestMonitor(TestCase):

    def setUp(self):

        self.monitor = Monitor(
            "user_test@local", "pass_test", "identif"
        )

    def tearDown(self):
        self.monitor.container.stop()

    def test_should_set_identifier_or_agent_name(self):
        self.assertEqual(
            self.monitor._identifier,
            "identif"
        )
        monitor = Monitor(
            "user_test2@local", "pass_test"
        )
        self.assertEqual(
            monitor._identifier,
            "user_test2"
        )
        monitor.container.stop()

    @patch("driftage.monitor.WaitMonitorSubscriptions")
    async def test_should_add_subscription_behaviour(self, behaviour_mock):
        self.monitor.add_behaviour = Mock()
        await self.monitor.setup()
        self.monitor.add_behaviour.assert_called_once_with(
            behaviour_mock()
        )

    @freeze_time("1989-08-12")
    @patch("driftage.monitor.FastNotifyContacts")
    @patch("driftage.monitor.Template")
    def test_should_notify_contacts_on_new_data(
            self, template_mock, behaviour_mock):
        self.monitor.add_behaviour = Mock()
        self.monitor.collect({"my data": 1})
        self.monitor.add_behaviour.assert_called_once_with(
            behaviour_mock(),
            template=template_mock.return_value
        )
        template_mock.assert_called_once_with(
            body=str(orjson.dumps({
                "data": {"my data": 1},
                "metadata": {
                    "timestamp": 618883200.0,
                    "identifier": "identif"
                }
            }), "utf-8")
        )

    @freeze_time("1989-08-12")
    @patch("driftage.monitor.FastNotifyContacts")
    @patch("driftage.monitor.Template")
    def test_should_notify_contacts_on_new_data_with_call(
            self, template_mock, behaviour_mock):
        self.monitor.add_behaviour = Mock()
        self.monitor({"my data": 1})
        self.monitor.add_behaviour.assert_called_once_with(
            behaviour_mock(),
            template=template_mock.return_value
        )
        template_mock.assert_called_once_with(
            body=str(orjson.dumps({
                "data": {"my data": 1},
                "metadata": {
                    "timestamp": 618883200.0,
                    "identifier": "identif"
                }
            }), "utf-8")
        )

[((709, 759), 'asynctest.patch', 'patch', (['"""driftage.monitor.WaitMonitorSubscriptions"""'], {}), "('driftage.monitor.WaitMonitorSubscriptions')\n", (714, 759), False, 'from asynctest import TestCase, Mock, patch\n'), ((1020, 1045), 'freezegun.freeze_time', 'freeze_time', (['"""1989-08-12"""'], {}), "('1989-08-12')\n", (1031, 1045), False, 'from freezegun import freeze_time\n'), ((1051, 1095), 'asynctest.patch', 'patch', (['"""driftage.monitor.FastNotifyContacts"""'], {}), "('driftage.monitor.FastNotifyContacts')\n", (1056, 1095), False, 'from asynctest import TestCase, Mock, patch\n'), ((1101, 1135), 'asynctest.patch', 'patch', (['"""driftage.monitor.Template"""'], {}), "('driftage.monitor.Template')\n", (1106, 1135), False, 'from asynctest import TestCase, Mock, patch\n'), ((1774, 1799), 'freezegun.freeze_time', 'freeze_time', (['"""1989-08-12"""'], {}), "('1989-08-12')\n", (1785, 1799), False, 'from freezegun import freeze_time\n'), ((1805, 1849), 'asynctest.patch', 'patch', (['"""driftage.monitor.FastNotifyContacts"""'], {}), "('driftage.monitor.FastNotifyContacts')\n", (1810, 1849), False, 'from asynctest import TestCase, Mock, patch\n'), ((1855, 1889), 'asynctest.patch', 'patch', (['"""driftage.monitor.Template"""'], {}), "('driftage.monitor.Template')\n", (1860, 1889), False, 'from asynctest import TestCase, Mock, patch\n'), ((206, 256), 'driftage.monitor.Monitor', 'Monitor', (['"""user_test@local"""', '"""pass_test"""', '"""identif"""'], {}), "('user_test@local', 'pass_test', 'identif')\n", (213, 256), False, 'from driftage.monitor import Monitor\n'), ((513, 553), 'driftage.monitor.Monitor', 'Monitor', (['"""user_test2@local"""', '"""pass_test"""'], {}), "('user_test2@local', 'pass_test')\n", (520, 553), False, 'from driftage.monitor import Monitor\n'), ((873, 879), 'asynctest.Mock', 'Mock', ([], {}), '()\n', (877, 879), False, 'from asynctest import TestCase, Mock, patch\n'), ((1272, 1278), 'asynctest.Mock', 'Mock', ([], {}), '()\n', (1276, 1278), False, 'from asynctest import TestCase, Mock, patch\n'), ((2036, 2042), 'asynctest.Mock', 'Mock', ([], {}), '()\n', (2040, 2042), False, 'from asynctest import TestCase, Mock, patch\n'), ((1540, 1647), 'orjson.dumps', 'orjson.dumps', (["{'data': {'my data': 1}, 'metadata': {'timestamp': 618883200.0,\n    'identifier': 'identif'}}"], {}), "({'data': {'my data': 1}, 'metadata': {'timestamp': 618883200.0,\n    'identifier': 'identif'}})\n", (1552, 1647), False, 'import orjson\n'), ((2296, 2403), 'orjson.dumps', 'orjson.dumps', (["{'data': {'my data': 1}, 'metadata': {'timestamp': 618883200.0,\n    'identifier': 'identif'}}"], {}), "({'data': {'my data': 1}, 'metadata': {'timestamp': 618883200.0,\n    'identifier': 'identif'}})\n", (2308, 2403), False, 'import orjson\n')]