def testRentalCheckOut(self): for n in xrange(NUM_TESTS): # Create User user = User() user.first_name = self.v.validRandomString(MAX_STRING_LENGTH) user.family_name = self.v.validRandomString(MAX_STRING_LENGTH) user.group = randomGroupEnum() user.device_id = None user.start_datetime = None user_key = user.put() # Verify User has no device q = User.query().fetch(n + 1) self.assertEqual(q[n].first_name, user.first_name) self.assertEqual(q[n].family_name, user.family_name) self.assertEqual(q[n].group, user.group) self.assertEqual(q[n].device_id, user.device_id) self.assertEqual(q[n].start_datetime, user.start_datetime) # Create Device device = Device() device.color = randomColorEnum() device.model = randomDeviceModelEnum() device.serial_no = self.v.validRandomString(MAX_STRING_LENGTH) device_key = device.put() # Verify Device is not rented q = Device.query().fetch(n + 1) device_id = q[n].key.urlsafe() self.assertEqual(q[n].color, device.color) self.assertEqual(q[n].model, device.model) self.assertEqual(q[n].serial_no, device.serial_no) self.assertEqual(q[n].is_rented, False) # Add Device to User start_datetime = datetime.now() result = user.checkOutDevice(device_id, start_datetime) self.assertNotEqual(result, None) # Verify User has device ID q = User.query().fetch(n + 1) self.assertEqual(q[n].first_name, user.first_name) self.assertEqual(q[n].family_name, user.family_name) self.assertEqual(q[n].group, user.group) self.assertEqual(q[n].device_id, user.device_id) self.assertEqual(q[n].device_id, device_id) self.assertEqual(q[n].start_datetime, user.start_datetime) self.assertEqual(q[n].start_datetime, start_datetime) # Verify Device is rented q = Device.query().fetch(n + 1) device_id = q[n].key.urlsafe() self.assertEqual(q[n].color, device.color) self.assertEqual(q[n].model, device.model) self.assertEqual(q[n].serial_no, device.serial_no) self.assertEqual(q[n].is_rented, True)
def testInsertDevice(self): for n in xrange(NUM_TESTS): device = Device() device.color = randomColorEnum() device.model = randomDeviceModelEnum() device.serial_no = self.v.validRandomString(MAX_STRING_LENGTH) device_key = device.put() q = Device.query().fetch(n + 1) self.assertEqual(q[n].color, device.color) self.assertEqual(q[n].model, device.model) self.assertEqual(q[n].serial_no, device.serial_no) self.assertEqual(q[n].is_rented, False)
def testSerializeDevice(self): for n in xrange(NUM_TESTS): device = Device() device.color = randomColorEnum() device.model = randomDeviceModelEnum() device.serial_no = self.v.validRandomString(MAX_STRING_LENGTH) device.is_rented = (n % 2 == 0) # Toggle is_rented device_key = device.put() q = Device.query().fetch(n + 1) v_json = q[n].serializeDevice(devicesURL) # v_json = json.loads(v) self.assertNotEqual(v_json, None) self.assertEqual(v_json["id"], device_key.urlsafe()) self.assertEqual(v_json["color"], getColorStringFromEnum(q[n].color)) self.assertEqual(v_json["model"], getDeviceModelStringFromEnum(q[n].model)) self.assertEqual(v_json["is_rented"], q[n].is_rented)
def testRentalCheckOutFails(self): for n in xrange(NUM_TESTS): # Create User user = User() user.first_name = self.v.validRandomString(MAX_STRING_LENGTH) user.family_name = self.v.validRandomString(MAX_STRING_LENGTH) user.group = randomGroupEnum() user.device_id = None user.start_datetime = None user_key = user.put() # Verify User has no device u = User.query().fetch(n + 1) self.assertEqual(u[n].first_name, user.first_name) self.assertEqual(u[n].family_name, user.family_name) self.assertEqual(u[n].group, user.group) self.assertEqual(u[n].device_id, user.device_id) self.assertEqual(u[n].start_datetime, user.start_datetime) # Create Device device = Device() device.color = randomColorEnum() device.model = randomDeviceModelEnum() device.serial_no = self.v.validRandomString(MAX_STRING_LENGTH) device_key = device.put() # Verify Device is not rented d = Device.query().fetch(n + 1) device_id = d[n].key.urlsafe() self.assertEqual(d[n].color, device.color) self.assertEqual(d[n].model, device.model) self.assertEqual(d[n].serial_no, device.serial_no) self.assertEqual(d[n].is_rented, False) if (n % 2 == 0): # Set Device to unavailable d[n].is_rented = True d[n].put() elif (n % 2 == 1): # Set User to another device u[n].device_id = "random_device_id" u[n].start_datetime = datetime.now() u[n].put() # Add Device to User start_datetime = datetime.now() result = user.checkOutDevice(device_id, start_datetime) self.assertEqual(result, None) # Verify User is unchanged u2 = User.query().fetch(n + 1) self.assertEqual(u2[n].first_name, u[n].first_name) self.assertEqual(u2[n].family_name, u[n].family_name) self.assertEqual(u2[n].group, u[n].group) self.assertEqual(u2[n].device_id, u[n].device_id) self.assertEqual(u2[n].start_datetime, u[n].start_datetime) # Verify Device is unchanged d2 = Device.query().fetch(n + 1) device_id = d2[n].key.urlsafe() self.assertEqual(d2[n].color, d[n].color) self.assertEqual(d2[n].model, d[n].model) self.assertEqual(d2[n].serial_no, d[n].serial_no) self.assertEqual(d2[n].is_rented, d[n].is_rented)
def testRentalHandlerCheckOut(self): for n in xrange(NUM_TESTS): # Create User user = User() user.first_name = self.v.validRandomString(MAX_STRING_LENGTH) user.family_name = self.v.validRandomString(MAX_STRING_LENGTH) user.group = randomGroupEnum() user.device_id = None user.start_datetime = None user_key = user.put() # Verify User has no device q = User.query().fetch(n + 1) user_id = q[n].key.urlsafe() self.assertEqual(q[n].first_name, user.first_name) self.assertEqual(q[n].family_name, user.family_name) self.assertEqual(q[n].group, user.group) self.assertEqual(q[n].device_id, user.device_id) self.assertEqual(q[n].start_datetime, user.start_datetime) # Create Device device = Device() device.color = randomColorEnum() device.model = randomDeviceModelEnum() device.serial_no = self.v.validRandomString(MAX_STRING_LENGTH) device_key = device.put() # Verify Device is not rented q = Device.query().fetch(n + 1) device_id = q[n].key.urlsafe() self.assertEqual(q[n].color, device.color) self.assertEqual(q[n].model, device.model) self.assertEqual(q[n].serial_no, device.serial_no) self.assertEqual(q[n].is_rented, False) # Add Device to User via PUT if (n % 2 == 0): url = baseURL url += usersPath + "/" + user_id + devicesPath + "/" + device_id elif (n % 2 == 1): url = baseURL url += devicesPath + "/" + device_id + usersPath + "/" + user_id response = self.testapp.put(url) # Check Return self.assertEqual(response.status_int, 204) # Test Changes Occurred # Verify User has device ID q = User.query().fetch(n + 1) self.assertEqual(q[n].first_name, user.first_name) self.assertEqual(q[n].family_name, user.family_name) self.assertEqual(q[n].group, user.group) self.assertEqual(q[n].device_id, user.device_id) self.assertEqual(q[n].device_id, device_id) self.assertNotEqual(q[n].start_datetime, None) # Verify Device is rented q = Device.query().fetch(n + 1) device_id = q[n].key.urlsafe() self.assertEqual(q[n].color, device.color) self.assertEqual(q[n].model, device.model) self.assertEqual(q[n].serial_no, device.serial_no) self.assertEqual(q[n].is_rented, True)
def testRentalHandlerCheckOutError(self): for n in xrange(NUM_TESTS): # Create User user = User() user.first_name = self.v.validRandomString(MAX_STRING_LENGTH) user.family_name = self.v.validRandomString(MAX_STRING_LENGTH) user.group = randomGroupEnum() user.device_id = None user.start_datetime = None user_key = user.put() # Verify User has no device q = User.query().fetch(n + 1) user_id = q[n].key.urlsafe() self.assertEqual(q[n].first_name, user.first_name) self.assertEqual(q[n].family_name, user.family_name) self.assertEqual(q[n].group, user.group) self.assertEqual(q[n].device_id, user.device_id) self.assertEqual(q[n].start_datetime, user.start_datetime) # Create Device device = Device() device.color = randomColorEnum() device.model = randomDeviceModelEnum() device.serial_no = self.v.validRandomString(MAX_STRING_LENGTH) device_key = device.put() # Verify Device is not rented q = Device.query().fetch(n + 1) device_id = q[n].key.urlsafe() self.assertEqual(q[n].color, device.color) self.assertEqual(q[n].model, device.model) self.assertEqual(q[n].serial_no, device.serial_no) self.assertEqual(q[n].is_rented, False) # Add Device to User via PUT url = baseURL url += usersPath + "/" + user_id + devicesPath + "/" + device_id response = self.testapp.put(url) # Check Return self.assertEqual(response.status_int, 204) # Test Changes Occurred # Verify User has device ID q1 = User.query().fetch(n + 1) self.assertEqual(q1[n].first_name, user.first_name) self.assertEqual(q1[n].family_name, user.family_name) self.assertEqual(q1[n].group, user.group) self.assertEqual(q1[n].device_id, user.device_id) self.assertEqual(q1[n].device_id, device_id) self.assertNotEqual(q1[n].start_datetime, None) # Verify Device is rented q = Device.query().fetch(n + 1) device_id = q[n].key.urlsafe() self.assertEqual(q[n].color, device.color) self.assertEqual(q[n].model, device.model) self.assertEqual(q[n].serial_no, device.serial_no) self.assertEqual(q[n].is_rented, True) # Add Try to RE-CHECK OUT Device to User via PUT, get same result response = self.testapp.put(url) # Check Return self.assertEqual(response.status_int, 204) # Test Changes DID NOT Occurred after re-attempt # Verify User stll has device ID q2 = User.query().fetch(n + 1) self.assertEqual(q2[n].first_name, q1[n].first_name) self.assertEqual(q2[n].family_name, q1[n].family_name) self.assertEqual(q2[n].group, q1[n].group) self.assertEqual(q2[n].device_id, q1[n].device_id) self.assertEqual(q2[n].start_datetime, q1[n].start_datetime) # Verify Device is rented q = Device.query().fetch(n + 1) device_id = q[n].key.urlsafe() self.assertEqual(q[n].color, device.color) self.assertEqual(q[n].model, device.model) self.assertEqual(q[n].serial_no, device.serial_no) self.assertEqual(q[n].is_rented, True) # Add INVALID Device to User via PUT, get error url = baseURL url += usersPath + "/" + user_id + devicesPath + "/" + device_id + "1" response = self.testapp.put(url, expect_errors=True) # Check Return self.assertEqual(response.status_int, 400) q3 = User.query().fetch(n + 1) self.assertEqual(q2[n].first_name, q3[n].first_name) self.assertEqual(q2[n].family_name, q3[n].family_name) self.assertEqual(q2[n].group, q3[n].group) self.assertEqual(q2[n].device_id, q3[n].device_id) self.assertEqual(q2[n].start_datetime, q3[n].start_datetime) q = Device.query().fetch(n + 1) device_id = q[n].key.urlsafe() self.assertEqual(q[n].color, device.color) self.assertEqual(q[n].model, device.model) self.assertEqual(q[n].serial_no, device.serial_no) self.assertEqual(q[n].is_rented, True)