Пример #1
0
    def test_get_device(self):
        """Test that adding and then getting a device works."""

        add_room('bedroom', "Jake's Bedroom")

        device = {
            'identifier': 'test',
            'name': 'Test',
            'device_type': 'switch',
            'depends_on': None,
            'controller_name': 'controller-1',
            'room': {
                'identifier': 'bedroom',
                'name': "Jake's Bedroom",
            },
            'state_providers': None,
        }

        # Add the device to the registry
        add_device(device['identifier'], device['name'], device['device_type'],
                   device['controller_name'], device['room']['identifier'])

        # Ask the registry for the device's details
        response = self.app.get('/device/test')

        # Assert that we got an OK response code
        self.assertEqual(200, response.status_code)

        # Decode the json
        decoded_response = decode_response(response)

        self.assertEqual(device, decoded_response)
Пример #2
0
    def test_add_duplicate_device(self):
        """Test that adding a device with an identifier that is already used replaces the device."""

        # Add the same device twice
        add_device('device1', 'device 1', 'switch', 'controller', 'room')
        response = add_device('device1', 'device new', 'switch', 'controller',
                              'room')

        # Assert that we get a 201 response code the second time
        self.assertEqual(201, response.status_code)

        response = self.app.get('/device/device1')
        decoded_response = decode_response(response)

        self.assertEqual('device new', decoded_response['name'])
Пример #3
0
    def test_get_room(self):
        """Test that getting a room with devices works."""

        device1 = {
            'identifier': 'device1',
            'name': 'Test Device 1',
            'device_type': 'switch',
            'depends_on': None,
            'controller_name': 'controller-1',
            'state_providers': None,
        }

        device2 = {
            'identifier': 'device2',
            'name': 'Test Device 2',
            'device_type': 'switch',
            'depends_on': None,
            'controller_name': 'controller-1',
            'state_providers': None,
        }

        room = {
            'identifier': 'bedroom',
            'name': "Jake's Bedroom",
            'devices': [device1, device2],
        }

        add_room(room['identifier'], room['name'])
        add_device(
            device1['identifier'],
            device1['name'],
            device1['device_type'],
            device1['controller_name'],
            room['identifier'],
        )

        add_device(
            device2['identifier'],
            device2['name'],
            device2['device_type'],
            device2['controller_name'],
            room['identifier'],
        )

        response = self.app.get('/room/bedroom')
        decoded_response = decode_response(response)

        self.assertEqual(room, decoded_response)
Пример #4
0
    def test_add_device(self):
        """Test that a new device can be added to the registry."""

        device = {
            'identifier': 'device1',
            'name': 'Device 1',
            'device_type': 'switch',
            'depends_on': None,
            'controller_name': 'controller',
            'room': {
                'identifier': 'room',
                'name': 'Room',
            },
            'state_providers': None,
        }

        # Add a new device and get the response
        response = add_device(
            device['identifier'],
            device['name'],
            device['device_type'],
            device['controller_name'],
            device['room']['identifier'],
        )
        decoded_response = decode_response(response)

        self.assertEqual(201, response.status_code)
        self.assertEqual(device, decoded_response)
Пример #5
0
    def test_delete_device(self):
        """Test that a device is no longer available after deleting it"""

        # Add a device
        add_room('bedroom', "Jake's Bedroom")
        add_device('test-del', 'name', 'type', 'controller', 'bedroom')

        # Try to get the device
        response = self.app.get('/device/test-del')
        self.assertEqual(200, response.status_code)

        # Delete the device
        response = self.app.delete('/device/test-del')
        self.assertEqual(204, response.status_code)

        # Try to get the device again
        response = self.app.get('/device/test-del')
        self.assertEqual(404, response.status_code)

        # Try to delete the device again
        response = self.app.delete('/device/test-del')
        self.assertEqual(404, response.status_code)
Пример #6
0
    def test_add_device_invalid_room(self):
        """Test that adding a device with an unknown room returns a 400"""

        response = add_device('invalid-room-test', 'name', 'type',
                              'controller', 'unknown-room')
        self.assertEqual(400, response.status_code)