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")
        )

[((32, 5, 32, 55), 'asynctest.patch', 'patch', ({(32, 11, 32, 54): '"""driftage.monitor.WaitMonitorSubscriptions"""'}, {}), "('driftage.monitor.WaitMonitorSubscriptions')", False, 'from asynctest import TestCase, Mock, patch\n'), ((40, 5, 40, 30), 'freezegun.freeze_time', 'freeze_time', ({(40, 17, 40, 29): '"""1989-08-12"""'}, {}), "('1989-08-12')", False, 'from freezegun import freeze_time\n'), ((41, 5, 41, 49), 'asynctest.patch', 'patch', ({(41, 11, 41, 48): '"""driftage.monitor.FastNotifyContacts"""'}, {}), "('driftage.monitor.FastNotifyContacts')", False, 'from asynctest import TestCase, Mock, patch\n'), ((42, 5, 42, 39), 'asynctest.patch', 'patch', ({(42, 11, 42, 38): '"""driftage.monitor.Template"""'}, {}), "('driftage.monitor.Template')", False, 'from asynctest import TestCase, Mock, patch\n'), ((61, 5, 61, 30), 'freezegun.freeze_time', 'freeze_time', ({(61, 17, 61, 29): '"""1989-08-12"""'}, {}), "('1989-08-12')", False, 'from freezegun import freeze_time\n'), ((62, 5, 62, 49), 'asynctest.patch', 'patch', ({(62, 11, 62, 48): '"""driftage.monitor.FastNotifyContacts"""'}, {}), "('driftage.monitor.FastNotifyContacts')", False, 'from asynctest import TestCase, Mock, patch\n'), ((63, 5, 63, 39), 'asynctest.patch', 'patch', ({(63, 11, 63, 38): '"""driftage.monitor.Template"""'}, {}), "('driftage.monitor.Template')", False, 'from asynctest import TestCase, Mock, patch\n'), ((11, 23, 13, 9), 'driftage.monitor.Monitor', 'Monitor', ({(12, 12, 12, 29): '"""user_test@local"""', (12, 31, 12, 42): '"""pass_test"""', (12, 44, 12, 53): '"""identif"""'}, {}), "('user_test@local', 'pass_test', 'identif')", False, 'from driftage.monitor import Monitor\n'), ((23, 18, 25, 9), 'driftage.monitor.Monitor', 'Monitor', ({(24, 12, 24, 30): '"""user_test2@local"""', (24, 32, 24, 43): '"""pass_test"""'}, {}), "('user_test2@local', 'pass_test')", False, 'from driftage.monitor import Monitor\n'), ((34, 37, 34, 43), 'asynctest.Mock', 'Mock', ({}, {}), '()', False, 'from asynctest import TestCase, Mock, patch\n'), ((45, 37, 45, 43), 'asynctest.Mock', 'Mock', ({}, {}), '()', False, 'from asynctest import TestCase, Mock, patch\n'), ((66, 37, 66, 43), 'asynctest.Mock', 'Mock', ({}, {}), '()', False, 'from asynctest import TestCase, Mock, patch\n'), ((52, 21, 58, 14), 'orjson.dumps', 'orjson.dumps', ({(52, 34, 58, 13): "{'data': {'my data': 1}, 'metadata': {'timestamp': 618883200.0,\n    'identifier': 'identif'}}"}, {}), "({'data': {'my data': 1}, 'metadata': {'timestamp': 618883200.0,\n    'identifier': 'identif'}})", False, 'import orjson\n'), ((73, 21, 79, 14), 'orjson.dumps', 'orjson.dumps', ({(73, 34, 79, 13): "{'data': {'my data': 1}, 'metadata': {'timestamp': 618883200.0,\n    'identifier': 'identif'}}"}, {}), "({'data': {'my data': 1}, 'metadata': {'timestamp': 618883200.0,\n    'identifier': 'identif'}})", False, 'import orjson\n')]