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)
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)
def test_no_rooms(self): """Test that an empty array is returned when there are no rooms known by the registry.""" response = self.app.get('/rooms') decoded_response = decode_response(response) # Assert that the status code is correct self.assertEqual(200, response.status_code) # Assert that no rooms were returned self.assertEqual([], decoded_response)
def test_no_devices(self): """Test that an empty array is returned when there are no devices known by the registry.""" # Make a get request to /devices response = self.app.get('/devices') decoded_response = decode_response(response) # Assert that the status code is correct self.assertEqual(200, response.status_code) # Assert that no devices were returned self.assertEqual([], decoded_response)
def test_add_duplicate_room(self): """Test that adding a room with an identifier that is already used replaces the room.""" # Add the same room twice add_room('room1', "room") response = add_room('room1', "room new") # Assert that we get a 201 response code the second time self.assertEqual(201, response.status_code) response = self.app.get('/room/room1') decoded_response = decode_response(response) self.assertEqual('room new', decoded_response['name'])
def test_add_room(self): """Test that a new room can be added to the registry.""" room = { 'identifier': 'bedroom', 'name': "Jake's Bedroom", 'devices': [], } # Add a new room and get the response response = add_room(room['identifier'], room['name']) decoded_response = decode_response(response) self.assertEqual(201, response.status_code) self.assertEqual(room, decoded_response)
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'])
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)