def test_all_locations_of_given_campus_for_response_with_inserted_data( self): c1 = Campus(latitude=12.32434, longitude=56.4324, name='Some Campus1') self.test_db.session.add(c1) self.test_db.session.commit() i_c1 = Campus.query.filter_by(latitude=c1.latitude, longitude=c1.longitude, name=c1.name).first() l1 = Location(latitude=12.32434, longitude=56.4324, name='location 1', campus_id=i_c1.id) l2 = Location(latitude=22.32434, longitude=46.4324, name='location 2') self.test_db.session.add(l1) self.test_db.session.commit() result = self.app.get('/api/locations?campusId' + str(i_c1.id)) dict_val = json.loads(result.data) self.assertEqual(result.status_code, 200) self.assertEqual(len(dict_val["items"]), 1) self.assertEqual(dict_val["items"][0]["latitude"], l1.latitude) self.assertEqual(dict_val["items"][0]["name"], l1.name) self.assertIsNotNone(dict_val["items"][0]["campus"]) self.assertEqual(dict_val["items"][0]["campus"]["name"], c1.name) self.assertEqual(dict_val["success"], True)
def test_delete_location_with_valid_id(self): new_loc = Location(name='loc7', latitude=62.64654, longitude=63.54465) new_loc.save() result = self.app.delete('/api/locations/%r' % (new_loc.id)) dict_val = json.loads(result.data) self.assertEqual(result.status_code, 200) self.assertEqual(dict_val['success'], True) self.assertEqual(dict_val['item']['name'], new_loc.name) self.assertEqual(dict_val['item']['latitude'], new_loc.latitude)
def test_add_user_should_fail_invalid_data(self): loc = Location(name='Loc4', latitude=12.454, longitude=43.23234) loc.save() data = { 'firstName': 'User4', 'username': '******', 'password': '******', 'locationId': loc.id } result = self.app.post('/api/users', data=data) self.assertEqual(result.status_code, 400) dict_val = json.loads(result.data) self.assertEqual(dict_val["message"], 'Missing required fields!') self.assertEqual(dict_val["success"], False) users = User.query.all() self.assertEqual(len(users), 0)
def test_all_users_with_location_without_campus_for_response_with_inserted_data( self): l1 = Location(latitude=12.32434, longitude=56.4324) self.test_db.session.add(l1) self.test_db.session.commit() i_l1 = Location.query.filter_by(latitude=l1.latitude, longitude=l1.longitude).first() u1 = User(first_name='user1', last_name='last1', username='******', password='******', location_id=i_l1.id) self.test_db.session.add(u1) self.test_db.session.commit() result = self.app.get('/api/users') dict_val = json.loads(result.data) self.assertEqual(result.status_code, 200) self.assertEqual(len(dict_val["items"]), 1) self.assertEqual(dict_val["items"][0]["firstName"], u1.first_name) self.assertIsNotNone(dict_val["items"][0]["location"]) self.assertEqual(dict_val["items"][0]["location"]["latitude"], l1.latitude) self.assertEqual(dict_val["success"], True)
def test_should_return_processed_data_for_valid_data_for_location_no_campus_in_action_view_location_info( self): l1 = Location(latitude=12.32434, longitude=56.4324, name="loc123") self.test_db.session.add(l1) self.test_db.session.commit() payload_data = { 'result': { 'parameters': { 'campus-location': 'loc123' }, 'fulfillment': { 'speech': 'Some message for view-location-info-request!' }, 'contexts': [], 'action': 'view-location-info-request' } } result = self.app.post('/api/ai/fulfillment', content_type='application/json', data=json.dumps(payload_data)) dict_val = json.loads(result.data) self.assertEqual(result.status_code, 200) # print(dict_val) speech_resp = 'Location: %r with co-ordinates(%r, %r)' % ( l1.name, str(l1.latitude), str(l1.longitude)) self.assertEqual(dict_val['speech'], speech_resp)
def test_add_event_with_valid_data_with_location_without_participants( self): l1 = Location(name='loc2', latitude=62.64654, longitude=63.54465, campus_id=0) self.test_db.session.add(l1) self.test_db.session.commit() l_id = l1.id now = datetime.now() later = (now + timedelta(hours=1)) data = { 'title': 'Event 5', 'description': 'Some event 5', 'eventStart': now.timestamp() * 1e3, 'eventEnd': later.timestamp() * 1e3, 'locationId': l_id } result = self.app.post('/api/events', data=data) self.assertEqual(result.status_code, 200) dict_val = json.loads(result.data) self.assertEqual(dict_val["item"]["title"], data['title']) self.assertEqual(dict_val["item"]["eventStart"], data['eventStart']) self.assertEqual(dict_val["item"]["location"]["name"], l1.name) self.assertEqual(dict_val["success"], True) events = Event.query.all() self.assertEqual(len(events), 1) self.assertEqual(events[0].event_start, now) self.assertEqual(events[0].description, data['description']) self.assertEqual(events[0].event_end, later)
def test_download_locations_through_endpoint(self): new_loc = Location(name='loc2', latitude=62.64654, longitude=63.54465, campus_id=0, floor=1) new_loc.save() result = self.app.get('/api/locations/download') content = b'' for i in result.response: content = content + i dict_items = json.loads(content.decode().replace("'", '"')) # dict_val = json.loads(result.data) self.assertEqual(result.status_code, 200) self.assertEqual(result.mimetype, 'application/json') self.assertEqual(len(dict_items), 1) self.assertEqual(dict_items[0]['name'], 'loc2')
def test_all_locations_without_campus_for_response_with_inserted_data( self): l1 = Location(latitude=12.32434, longitude=56.4324) l2 = Location(latitude=15.32434, longitude=57.4324) self.test_db.session.add(l1) self.test_db.session.add(l2) self.test_db.session.commit() result = self.app.get('/api/locations') dict_val = json.loads(result.data) self.assertEqual(result.status_code, 200) self.assertEqual(len(dict_val["items"]), 2) self.assertEqual(dict_val["items"][0]["latitude"], l1.latitude) self.assertIsNone(dict_val["items"][0]["campus"]) self.assertEqual(dict_val["items"][1]["longitude"], l2.longitude) self.assertIsNone(dict_val["items"][1]["campus"]) self.assertEqual(dict_val["success"], True)
def validate_and_save_location(form, skip_campus): latitude = form.get('latitude', None) longitude = form.get('longitude', None) name = form.get('name', None) campus = form.get('campusId', None) floor = form.get('floor', 1) if latitude and longitude and name: if (not skip_campus) and (not campus): return False, None else: new_loc = Location(name=name, latitude=latitude, longitude=longitude, campus_id=campus, floor=floor) new_loc.save() return True, new_loc return False, None
def test_add_user_with_valid_data(self): loc = Location(name='Loc4', latitude=12.454, longitude=43.23234) loc.save() data = { 'firstName': 'User4', 'lastName': 'Last4', 'username': '******', 'password': '******', 'locationId': loc.id } result = self.app.post('/api/users', data=data) self.assertEqual(result.status_code, 200) dict_val = json.loads(result.data) self.assertEqual(dict_val["item"]["firstName"], data['firstName']) self.assertEqual(dict_val["item"]["username"], data['username']) self.assertEqual(dict_val["success"], True) users = User.query.all() self.assertEqual(len(users), 1) self.assertEqual(users[0].last_name, data['lastName'])
def test_should_return_processed_data_for_valid_and_data_input_in_action_schedule_meeting( self): u1 = User(first_name='user2', last_name='last2', username='******', password='******') l1 = Location(latitude=15.32434, longitude=57.432, name='loc1') self.test_db.session.add(u1) self.test_db.session.add(l1) self.test_db.session.commit() u1_id = u1.id payload_data = { 'result': { 'parameters': { 'campus-location': 'loc1', 'office-user': '******', 'date': '2017-09-08', 'time': '13:30:00' }, 'fulfillment': { 'speech': 'Hi Ana! Nice to meet you!' }, 'contexts': [{ 'name': 'auth', 'parameters': { 'token': 'uname2' } }], 'action': 'schedule-meeting-request' } } result = self.app.post('/api/ai/fulfillment', content_type='application/json', data=json.dumps(payload_data)) dict_val = json.loads(result.data) self.assertEqual(result.status_code, 200) self.assertEqual( dict_val['speech'], 'Added!' + payload_data['result']['fulfillment']['speech']) self.assertEqual( dict_val['displayText'], 'Added!' + payload_data['result']['fulfillment']['speech']) meetings = Event.query.all() self.assertEqual(len(meetings), 1) self.assertEqual(meetings[0].title, 'user2\'s meeting with user2 at loc1') self.assertEqual(len(meetings[0].event_participants), 1) self.assertEqual(meetings[0].event_participants[0].participant_id, u1_id)
def test_should_return_processed_data_for_valid_data_for_location_and_campus_in_action_view_location_info( self): c1 = Campus(latitude=12.32434, longitude=56.4324, name='Some Campus1') self.test_db.session.add(c1) self.test_db.session.commit() c1_id = c1.id l1 = Location(latitude=12.32434, longitude=56.4324, name="loc332", campus_id=c1_id) self.test_db.session.add(l1) self.test_db.session.commit() payload_data = { 'result': { 'parameters': { 'campus-location': 'loc332' }, 'fulfillment': { 'speech': 'Some message for view-location-info-request!' }, 'contexts': [], 'action': 'view-location-info-request' } } result = self.app.post('/api/ai/fulfillment', content_type='application/json', data=json.dumps(payload_data)) dict_val = json.loads(result.data) self.assertEqual(result.status_code, 200) # print(dict_val) str_resp = 'Location: %r with co-ordinates(%r, %r) is located at Campus: %r' % ( l1.name, str(l1.latitude), str(l1.longitude), c1.name) self.assertEqual(dict_val['speech'], str_resp) self.assertEqual( dict_val['data']['web']['parameters']['location']['name'], l1.name) self.assertEqual( dict_val['data']['web']['parameters']['location']['latitude'], l1.latitude) self.assertEqual(dict_val['parameters']['location']['name'], l1.name) self.assertEqual(dict_val['parameters']['location']['latitude'], l1.latitude)
def test_should_redirect_to_action_view_person_info_for_action_view_person_location_info( self): l1 = Location(latitude=22.32434, longitude=86.4324, name="New user loc1221") self.test_db.session.add(l1) self.test_db.session.commit() u1 = User(first_name='User22', last_name='Last32', username='******', password='******', location_id=l1.id) self.test_db.session.add(u1) self.test_db.session.commit() payload_data = { 'result': { 'parameters': { 'office-user': '******' }, 'fulfillment': { 'speech': 'Some message for view-person-info-request!' }, 'contexts': [], 'action': 'view-person-location-request' } } result = self.app.post('/api/ai/fulfillment', content_type='application/json', data=json.dumps(payload_data)) dict_val = json.loads(result.data) self.assertEqual(result.status_code, 200) # print(dict_val) self.assertEqual(dict_val['speech'], 'User: User22 Last32 is located at New user loc1221') self.assertEqual( dict_val['data']['web']['parameters']['location']['name'], l1.name) self.assertEqual( dict_val['data']['web']['parameters']['location']['latitude'], l1.latitude) self.assertEqual(dict_val['parameters']['location']['name'], l1.name) self.assertEqual(dict_val['parameters']['location']['latitude'], l1.latitude)
def test_add_event_with_valid_data_with_location_partially_valid_participants( self): u1 = User(first_name='User1', last_name='Last3', username='******', password='******') l1 = Location(name='loc2', latitude=62.64654, longitude=63.54465, campus_id=0) self.test_db.session.add(u1) self.test_db.session.add(l1) self.test_db.session.commit() u_id = u1.id l_id = l1.id now = datetime.now() later = (now + timedelta(hours=1)) data = { 'title': 'Event 5', 'description': 'Some event 5', 'eventStart': now.timestamp() * 1e3, 'eventEnd': later.timestamp() * 1e3, 'participantIds': '%r,%r' % (u_id, 3), 'locationId': l_id } result = self.app.post('/api/events', data=data) self.assertEqual(result.status_code, 200) dict_val = json.loads(result.data) self.assertEqual(dict_val["item"]["title"], data['title']) self.assertEqual(dict_val["item"]["eventStart"], data['eventStart']) self.assertEqual(dict_val["item"]["location"]["latitude"], l1.latitude) self.assertEqual(dict_val["success"], True) events = Event.query.all() self.assertEqual(len(events), 1) self.assertEqual(events[0].event_start, now) self.assertEqual(events[0].description, data['description']) self.assertEqual(events[0].event_end, later) event_participants = EventParticipant.query.filter_by( event_id=events[0].id).all() self.assertEqual(len(event_participants), 1) self.assertEqual(event_participants[0].participant_id, u_id)
def test_should_return_not_process_data_for_invalid_data_for_no_location_in_action_route_to_location_info( self): l1 = Location(latitude=12.32434, longitude=56.4324, name="loc332") self.test_db.session.add(l1) self.test_db.session.commit() l1_id = l1.id u1 = User(first_name='User22', last_name='Last32', username='******', password='******', location_id=l1_id) self.test_db.session.add(u1) self.test_db.session.commit() payload_data = { 'result': { 'parameters': { 'campus-location': 'loc255' }, 'fulfillment': { 'speech': 'Some message for route-to-location-request!' }, 'contexts': [{ 'name': 'auth', 'parameters': { 'token': 'uname39' } }], 'action': 'route-to-location-request' } } result = self.app.post('/api/ai/fulfillment', content_type='application/json', data=json.dumps(payload_data)) dict_val = json.loads(result.data) self.assertEqual(result.status_code, 200) # print(dict_val) str_resp = 'Not a valid Location' self.assertEqual(dict_val['speech'], str_resp)
def test_should_return_unprocessed_data_for_valid_data_for_location_and_no_user_diff_campus_in_action_route_to_location_info( self): c1 = Campus(latitude=12.32434, longitude=56.4324, name='Some Campus1') self.test_db.session.add(c1) self.test_db.session.commit() c1_id = c1.id l1 = Location(latitude=12.32434, longitude=56.4324, name="loc332", campus_id=c1_id) self.test_db.session.add(l1) self.test_db.session.commit() payload_data = { 'result': { 'parameters': { 'campus-location': 'loc255' }, 'fulfillment': { 'speech': 'Some message for route-to-location-request!' }, 'contexts': [{ 'name': 'auth', 'parameters': { 'token': 'uname39' } }], 'action': 'route-to-location-request' } } result = self.app.post('/api/ai/fulfillment', content_type='application/json', data=json.dumps(payload_data)) dict_val = json.loads(result.data) self.assertEqual(result.status_code, 200) # print(dict_val) str_resp = 'Invalid User Token!' self.assertEqual(dict_val['speech'], str_resp)
def test_should_return_processed_data_for_valid_data_for_location_and_campus_diff_campus_in_action_route_to_location_info( self): c1 = Campus(latitude=12.32434, longitude=56.4324, name='Some Campus1') self.test_db.session.add(c1) self.test_db.session.commit() c1_id = c1.id l1 = Location(latitude=12.32434, longitude=56.4324, name="loc332", campus_id=c1_id) l2 = Location(latitude=22.32434, longitude=95.4324, name="loc255") self.test_db.session.add(l1) self.test_db.session.add(l2) self.test_db.session.commit() l1_id = l1.id l2_id = l2.id u1 = User(first_name='User22', last_name='Last32', username='******', password='******', location_id=l1_id) self.test_db.session.add(u1) self.test_db.session.commit() payload_data = { 'result': { 'parameters': { 'campus-location': 'loc255' }, 'fulfillment': { 'speech': 'Some message for route-to-location-request!' }, 'contexts': [{ 'name': 'auth', 'parameters': { 'token': 'uname39' } }], 'action': 'route-to-location-request' } } result = self.app.post('/api/ai/fulfillment', content_type='application/json', data=json.dumps(payload_data)) dict_val = json.loads(result.data) self.assertEqual(result.status_code, 200) # print(dict_val) str_resp = 'You are not in the same campus to View Routes!' self.assertEqual(dict_val['speech'], str_resp) self.assertEqual( dict_val['data']['web']['parameters']['fromLocation']['name'], 'loc332') self.assertEqual( dict_val['data']['web']['parameters']['fromLocation']['latitude'], 12.32434) self.assertEqual( dict_val['data']['web']['parameters']['toLocation']['name'], 'loc255') self.assertEqual( dict_val['data']['web']['parameters']['toLocation']['longitude'], 95.4324) self.assertEqual(dict_val['parameters']['fromLocation']['name'], 'loc332') self.assertEqual(dict_val['parameters']['fromLocation']['latitude'], 12.32434) self.assertEqual(dict_val['parameters']['toLocation']['name'], 'loc255') self.assertEqual(dict_val['parameters']['toLocation']['longitude'], 95.4324) self.assertEqual(dict_val['parameters']['noRoute'], 'true')