コード例 #1
0
    def test_handle_error(self):
        errors = [
            {
                'kind': 'explanation',
                'args': {
                    'message': 'ERROR 1'
                }
            },
            {
                'kind': 'explanation',
                'args': {
                    'message': 'ERROR 2'
                }
            },
        ]
        label = 'fd0ee7c8-6ca8-11eb-9d9d-eba70539ce61'

        conn = fb.FarmbotConnection(FakeFarmbot(), FakeMQTT())
        status_msg = FakeMqttMessage(conn.incoming_chan, '{}')
        conn.bot._handler.on_error = mm = mock.MagicMock()
        conn.handle_error(label, errors)
        args = mm.call_args[0]
        actual_bot = args[0]
        actual_response = args[1]
        assert actual_bot == conn.bot
        assert actual_response.id == label
        assert actual_response.errors == ['ERROR 1', 'ERROR 2']
コード例 #2
0
    def test_unpack_response(self):
        conn = fb.FarmbotConnection(FakeFarmbot(), FakeMQTT())
        # == OK Response
        conn.handle_resp = mock.MagicMock()
        rpc_ok = json.dumps({"kind": "rpc_ok", "args": {"label": "123"}})
        conn.unpack_response(rpc_ok)
        conn.handle_resp.assert_called_with("123")

        # == ERROR Response
        conn.handle_error = mock.MagicMock()
        errors = [
            {
                'kind': 'explanation',
                'args': {
                    'message': 'ERROR 1'
                }
            },
            {
                'kind': 'explanation',
                'args': {
                    'message': 'ERROR 2'
                }
            },
        ]
        rpc_err = json.dumps({
            "kind": "rpc_error",
            "args": {
                "label": "456"
            },
            "body": errors
        })

        conn.unpack_response(rpc_err)
        conn.handle_error.assert_called_with("456", errors)
コード例 #3
0
 def test_handle_resp(self):
     conn = fb.FarmbotConnection(FakeFarmbot(), FakeMQTT())
     my_mock = mock.MagicMock()
     conn.bot._handler.on_response = my_mock
     conn.handle_resp("any_label")
     args = my_mock.call_args[0]
     bot = args[0]
     response = args[1]
     assert bot == conn.bot
     assert response.id == "any_label"
コード例 #4
0
 def test_start_connection(self):
     my_farmbot = FakeFarmbot()
     client = FakeMQTT()
     connection = fb.FarmbotConnection(my_farmbot, client)
     connection.start_connection()
     client.connect.assert_called_with('tob.mraf.ym', 1883, 60)
     client.loop_forever.assert_called()
     client.username_pw_set.assert_called_with('emanresu', 'drowssap')
     assert (client.on_connect)
     assert (client.on_message)
コード例 #5
0
 def test_init(self):
     my_farmbot = FakeFarmbot()
     connection = fb.FarmbotConnection(my_farmbot)
     assert (connection.bot == my_farmbot)
     assert (connection.mqtt)
     assert (connection.incoming_chan == "bot/emanresu/from_device")
     assert (connection.logs_chan == "bot/emanresu/logs")
     assert (connection.outgoing_chan == "bot/emanresu/from_clients")
     assert (connection.status_chan == "bot/emanresu/status")
     expected = ('bot/emanresu/status', 'bot/emanresu/logs',
                 'bot/emanresu/from_device')
     assert (connection.channels == expected)
コード例 #6
0
 def test_handle_connect(self):
     # https://docs.python.org/3/library/unittest.mock.html
     my_farmbot = FakeFarmbot()
     my_farmbot._handler.on_connect = mock.MagicMock()
     client = FakeMQTT()
     connection = fb.FarmbotConnection(my_farmbot, client)
     connection.handle_connect(mqtt=client,
                               userdata=None,
                               flags=None,
                               rc=None)
     for channel in connection.channels:
         client.subscribe.assert_has_calls([mock.call(channel)])
     my_farmbot._handler.on_connect.assert_called_with(my_farmbot, client)
     my_farmbot.read_status.assert_called()
コード例 #7
0
 def test_send_rpc(self, _):
     mqtt = FakeMQTT()
     mqtt.publish = mock.MagicMock()
     conn = fb.FarmbotConnection(FakeFarmbot(), mqtt)
     # === NON-ARRAY
     result = conn.send_rpc({})
     assert result == "FAKE_UUID"
     expected_chan = 'bot/emanresu/from_clients'
     expected_json = '{"kind": "rpc_request", "args": {"label": "FAKE_UUID"}, "body": [{}]}'
     mqtt.publish.assert_called_with(expected_chan, expected_json)
     # === ARRAY
     result2 = conn.send_rpc([{}])
     assert result2 == "FAKE_UUID"
     expected_chan2 = 'bot/emanresu/from_clients'
     expected_json2 = '{"kind": "rpc_request", "args": {"label": "FAKE_UUID"}, "body": [{}]}'
     mqtt.publish.assert_called_with(expected_chan2, expected_json2)
コード例 #8
0
    def test_handle_message(self):
        my_farmbot = FakeFarmbot()
        conn = fb.FarmbotConnection(my_farmbot, FakeMQTT())

        # == STATUS Message
        conn.handle_status = mock.MagicMock()
        status_msg = FakeMqttMessage(conn.status_chan, '{}')
        conn.handle_message(conn.mqtt, None, status_msg)
        conn.handle_status.assert_called_with(status_msg)

        # == LOG Message
        conn.handle_log = mock.MagicMock()
        log_msg = FakeMqttMessage(conn.logs_chan, '{}')
        conn.handle_message(conn.mqtt, None, log_msg)
        conn.handle_log.assert_called_with(log_msg)

        # == RPC Message
        conn.unpack_response = mock.MagicMock()
        rpc_json = '{}'
        rpc_msg = FakeMqttMessage(conn.incoming_chan, rpc_json)
        conn.handle_message(conn.mqtt, None, rpc_msg)
        conn.unpack_response.assert_called_with(rpc_json)
コード例 #9
0
 def test_handle_log(self):
     conn = fb.FarmbotConnection(FakeFarmbot(), FakeMQTT())
     status_msg = FakeMqttMessage(conn.logs_chan, '{}')
     conn.bot._handler.on_log = mock.MagicMock()
     conn.handle_log(status_msg)
     conn.bot._handler.on_log.assert_called_with(conn.bot, {})