def test_reject_friendship_request_successfully(): with patch('app_server.relationships_functions.send_notification_to_user') as _mock: client = MongoClient() collection = client[DB]['users'] data = [] for i in range(0, 2): data.append({'email': 'test_{0}@test.com'.format(i), 'full name': ''}) insert_new_user(data[i], collection) status_code = insert_new_friendship_request(data[1]['email'], data[0]['email'], collection) assert status_code == HTTP_CREATED user = collection.find_one({'email': data[0]['email']}) assert len(user['requests']) == 1 result, status_code = respond_to_friendship_request(data[0]['email'], data[1]['email'], collection, accept=False) assert status_code == HTTP_OK assert result == {'Reject_friendship_request': 'Solicitud de amistad rechazada con éxito'} client.close()
def test_get_user_friends(): with patch('app_server.relationships_functions.send_notification_to_user') as _mock: client = MongoClient() collection = client[DB]['users'] data = [] for i in range(0, 4): data.append({'email': 'test_{0}@test.com'.format(i), 'full name': '{0}'.format(i)}) insert_new_user(data[i], collection) result = collection.find({}) assert len(list(result)) == 4 my_user = data[3] for i in range(0, 3): insert_new_friendship_request(data[i]['email'], my_user['email'], collection) for i in range(0, 3): _, status_code = respond_to_friendship_request(my_user['email'], data[i]['email'], collection, accept=True) assert status_code == 200 result = get_user_friends_from_db(my_user['email'], collection) assert len(result) == 3 list_result = list(result) for i in range(0, 3): assert data[i]['email'] == list_result[i]['email'] assert my_user not in result client.close()
def test_comment_video_fails_video_does_not_exist(): client = MongoClient() collection = client[DB]['videos'] video_data = { 'title': 'test', 'url': 'test.com', 'user': '******', 'isPrivate': True, 'comments': [] } _id = '5edbc9196ab5430010391c79' insert_video_into_db(_id, video_data, collection) user = '******' text = 'Este es un comentario de prueba' __inexistent_id = '5edbc9196ab5430010391c78' status_code = insert_comment_into_video(__inexistent_id, user, text, collection) assert status_code == HTTP_INTERNAL_SERVER_ERROR this_video = collection.find_one({'_id': ObjectId(_id)}) assert len(this_video['comments']) == 0 client.close()
def test_new_collection_is_empty(): client = MongoClient() coll = client[DB]['users'] result = list(coll.find({})) client.close() assert len(result) == 0
def test_second_comment_video_comes_first(): client = MongoClient() collection = client[DB]['videos'] video_data = { 'title': 'test', 'url': 'test.com', 'user': '******', 'isPrivate': True, 'comments': [] } _id = '5edbc9196ab5430010391c79' insert_video_into_db(_id, video_data, collection) user = '******' text_01 = 'Este es un comentario de prueba' insert_comment_into_video(_id, user, text_01, collection) text_02 = 'Este es el segundo comentario' status_code = insert_comment_into_video(_id, user, text_02, collection) assert status_code == HTTP_CREATED this_video = collection.find_one({'_id': ObjectId(_id)}) assert len(this_video['comments']) == 2 comment = this_video['comments'][0] assert comment['text'] == text_02 assert comment['user'] == user assert 'timestamp' in comment.keys() client.close()
def test_ten_comments_in_order(): client = MongoClient() collection = client[DB]['videos'] video_data = { 'title': 'test', 'url': 'test.com', 'user': '******', 'isPrivate': True, 'comments': [] } _id = '5edbc9196ab5430010391c79' insert_video_into_db(_id, video_data, collection) user = '******' text = 'Este es el comentario numero {0}' for i in range(0, 10): text = text.format(i) insert_comment_into_video(_id, user, text, collection) this_video = collection.find_one({'_id': ObjectId(_id)}) assert len(this_video['comments']) == 10 for i in range(10, 0): comment = this_video['comments'][i] assert comment['text'] == text.format(i) client.close()
def test_insert_ten_videos(): client = MongoClient() collection = client[DB]['videos'] for i in range(0, 10): data = { 'title': 'test_{0}'.format(i), 'url': 'test.com', 'user': '******', 'isPrivate': False } # Esto se hace porque sino el id es repetido y tira conflicto. _id = '5edbc9196ab5430010391c7' + str(i) insert_video_into_db(_id, data, collection) fifth_video = collection.find_one({'title': 'test_5'}) assert fifth_video is not None assert not fifth_video['is_private'] eleventh_video = collection.find_one({'title': 'test_11'}) assert eleventh_video is None result = list(collection.find({})) client.close() assert len(result) == 10 for counter, document in enumerate(result): assert document['title'] == 'test_{0}'.format(counter)
def test_delete_friendship_relationship_fails_when_friendship_does_not_exist(): with patch('app_server.relationships_functions.send_notification_to_user') as _mock: client = MongoClient() collection = client[DB]['users'] data = [] for i in range(0, 3): data.append({'email': 'test_{0}@test.com'.format(i), 'full name': '{0}'.format(i)}) insert_new_user(data[i], collection) for i in range(0, 1): insert_new_friendship_request(data[i]['email'], data[2]['email'], collection) respond_to_friendship_request(data[2]['email'], data[i]['email'], collection, accept=True) user = collection.find_one({'email': data[2]['email']}) assert len(user['friends']) == 1 status_code = delete_friendship_relationship(data[2]['email'], data[1]['email'], collection) assert status_code == HTTP_FORBIDDEN user = collection.find_one({'email': data[2]['email']}) assert len(user['friends']) == 1 client.close()
def test_delete_friendship_relationship_fails_when_user_is_not_found(): with patch('app_server.relationships_functions.send_notification_to_user') as _mock: client = MongoClient() collection = client[DB]['users'] data = [] for i in range(0, 3): data.append({'email': 'test_{0}@test.com'.format(i), 'full name': '{0}'.format(i)}) insert_new_user(data[i], collection) for i in range(0, 1): insert_new_friendship_request(data[i]['email'], data[2]['email'], collection) respond_to_friendship_request(data[2]['email'], data[i]['email'], collection, accept=True) user = collection.find_one({'email': data[2]['email']}) assert len(user['friends']) == 1 status_code = delete_friendship_relationship(data[2]['email'], data[0]['email'], collection) assert status_code == HTTP_OK third_user = '******' # Not existing status_code = delete_friendship_relationship(data[2]['email'], third_user, collection) assert status_code == HTTP_NOT_FOUND user = collection.find_one({'email': data[2]['email']}) assert len(user['friends']) == 0 client.close()
def test_reject_friendship_non_existing_user_fails(): with patch('app_server.relationships_functions.send_notification_to_user') as _mock: client = MongoClient() collection = client[DB]['users'] data = [] for i in range(0, 2): data.append({'email': 'test_{0}@test.com'.format(i), 'full name': ''}) insert_new_user(data[i], collection) status_code = insert_new_friendship_request(data[1]['email'], data[0]['email'], collection) assert status_code == HTTP_CREATED user = collection.find_one({'email': data[0]['email']}) assert len(user['requests']) == 1 respond_to_friendship_request(data[0]['email'], data[1]['email'], collection, accept=True) third_user = '******' # Not existing result, status_code = respond_to_friendship_request(data[0]['email'], third_user, collection, accept=False) assert status_code == HTTP_NOT_FOUND assert result == {'Reject_friendship_request': 'La solicitud no se pudo completar porque uno de los usuarios no existe'} client.close()
def test_reject_friendship_non_existing_fails(): with patch('app_server.relationships_functions.send_notification_to_user') as _mock: client = MongoClient() collection = client[DB]['users'] data = [] for i in range(0, 2): data.append({'email': 'test_{0}@test.com'.format(i), 'full name': ''}) insert_new_user(data[i], collection) status_code = insert_new_friendship_request(data[1]['email'], data[0]['email'], collection) assert status_code == HTTP_CREATED user = collection.find_one({'email': data[0]['email']}) assert len(user['requests']) == 1 # La request la tiene que aceptar el usuario 0, no el 1 result, status_code = respond_to_friendship_request(data[1]['email'], data[0]['email'], collection, accept=False) assert status_code == HTTP_FORBIDDEN assert result == {'Reject_friendship_request': 'La solicitud de amistad que queres responder no existe'} client.close()
def test_delete_inexistent_dislike_fails(): client = MongoClient() collection = client[DB]['videos'] _id = '5df1679ee0acd518e5cfd002' status_code = insert_video_into_db(_id, empty_video_01, collection) assert status_code == HTTP_CREATED fake_user = {'email': '*****@*****.**'} status_code = delete_like_video(_id, fake_user, collection) assert status_code == HTTP_CONFLICT client.close()
def test_insert_complete_user_successfully(): client = MongoClient() collection = client[DB]['users'] data = {'email': '*****@*****.**', 'full name': '', 'friends': [], 'requests': []} insert_new_user(data, collection) result = list(collection.find({})) first_user = result[0] client.close() assert len(result) == 1 assert first_user['email'] == '*****@*****.**'
def test_insert_new_user(): client = MongoClient() collection = client[DB]['users'] data = {'email': '*****@*****.**', 'full name': ''} insert_new_user(data, collection) result = list(collection.find({})) first_user = result[0] client.close() assert len(result) == 1 assert first_user['email'] == '*****@*****.**'
def test_video_not_found_fails(): client = MongoClient() collection = client[DB]['videos'] status_code = insert_video_into_db('5df1679ee0acd518e5cfd002', empty_video_01, collection) assert status_code == HTTP_CREATED result = list(collection.find({})) assert len(result) == 1 fake_user = {'email': '*****@*****.**'} status_code = like_video('22222225ef1679ee0acd518e', fake_user, collection) assert status_code == HTTP_NOT_FOUND client.close()
def test_insert_new_friendship_request_fails_my_id_is_invalid(): with patch('app_server.relationships_functions.send_notification_to_user') as _mock: client = MongoClient() collection = client[DB]['users'] data = [] for i in range(0, 2): data.append({'email': 'test_{0}@test.com'.format(i), 'full name': ''}) insert_new_user(data[i], collection) third_user = '******' # Not existing status_code = insert_new_friendship_request(third_user, data[0]['email'], collection) assert status_code == HTTP_NOT_FOUND client.close()
def test_like_is_successful(): client = MongoClient() collection = client[DB]['videos'] _id = '5df1679ee0acd518e5cfd002' status_code = insert_video_into_db(_id, empty_video_01, collection) assert status_code == HTTP_CREATED fake_user = {'email': '*****@*****.**'} status_code = like_video(_id, fake_user, collection) assert status_code == HTTP_CREATED video = collection.find_one({'_id': ObjectId(_id)}) assert fake_user['email'] in video['likes'] client.close()
def set_up_db(self): """ Setup In memory MongoDB database """ self.client = MongoClient() db = self.client['bankaccountservice'] collection = db['bank_account'] bank_account_data = [{ 'user_id': '105398890', 'account_number': '000100', 'balance': 100000, 'create_date': '2021-04-06 01:37:14.422Z' }, { 'user_id': '105398891', 'account_number': '000101', 'balance': 60000, 'create_date': '2021-04-06 01:37:14.422Z' }] for bank_account in bank_account_data: collection.insert_one(bank_account) collection = db['transaction'] transaction_data = [{ 'account_number': '000100', 'currency': 'COP', 'type': 'DEPOSIT', 'previous_balance': 0, 'amount': 200000, 'trm': 0, 'create_date': '2021-05-06 01:37:14.422Z' }, { 'account_number': '000100', 'currency': 'COP', 'type': 'WITHDRAWAL', 'previous_balance': 200000, 'amount': 100000, 'trm': 0, 'create_date': '2021-05-06 01:47:14.422Z' }, { 'account_number': '000101', 'currency': 'COP', 'type': 'DEPOSIT', 'previous_balance': 0, 'amount': 100000, 'trm': 0, 'create_date': '2021-05-06 01:17:14.422Z' }, { 'account_number': '000101', 'currency': 'COP', 'type': 'WITHDRAWAL', 'previous_balance': 100000, 'amount': 40000, 'trm': 0, 'create_date': '2021-05-06 01:27:14.422Z' }] for transaction in transaction_data: collection.insert_one(transaction) return db
def test_get_users_when_filter_is_empty_gets_all_except_me(): client = MongoClient() collection = client[DB]['users'] data = [] for i in range(0, 5): data.append({'email': 'test_{0}@test.com'.format(i), 'full name': '{0}'.format(i)}) insert_new_user(data[i], collection) _all = get_users_by_query('', '*****@*****.**', collection) assert len(list(_all)) == 4 emails = [item['email'] for item in list(_all)] assert '*****@*****.**' not in emails client.close()
def set_up_db(self): """ Setup In memory MongoDB database """ self.client = MongoClient() db = self.client['userservice'] collection = db['user'] user_data = [{ 'user_id': '105398890', 'pin': 2091, 'user_name': 'playvox', 'password': '******', 'create_date': '2021-03-06 01:37:14.422Z' }, { 'user_id': '105398891', 'pin': 2090, 'user_name': 'docker', 'password': '******', 'create_date': '2021-03-06 01:57:14.422Z' }] for user in user_data: collection.insert_one(user) return db
def test_get_user_token_fails(): client = MongoClient() collection = client[DB]['users'] data = {'email': '@test.com', 'full name': ''} insert_new_user(data, collection) result = list(collection.find({})) first_user = result[0] user_token = get_user_token('@test.com', collection) client.close() assert len(result) == 1 assert first_user['email'] == '@test.com' assert user_token == ''
def test_get_user_friends_fails(): client = MongoClient() collection = client[DB]['users'] data = [] for i in range(0, 4): data.append({'email': 'test_{0}@test.com'.format(i), 'full name': '{0}'.format(i)}) insert_new_user(data[i], collection) result = collection.find({}) assert len(list(result)) == 4 third_user = '******' # Not existing result = get_user_friends_from_db(third_user, collection) assert result == 404 client.close()
class TestAbstractFetcher(unittest.TestCase): def setUp(self) -> None: self.client = MongoClient() self.db = PriceDatabase(collection_name="UnitTests", client=self.client) self.fetcher = MockedFetcher(self.db) def tearDown(self) -> None: self.client.close() def test_no_data_case(self): self.assertRaises(Exception, self.fetcher._format_cheapest_product_tweet, "2080 TI") @staticmethod def create_basic_post(): return { "product_name": "Cheapest 2080 TI", "product_brand": "ASUS", "product_type": "2080 TI", "product_price": 900.0, "source": "Vendor", "url": "http://www.vendor.com" } def test_only_yesterday_post_case(self): yesterday_post = self.create_basic_post() yesterday_post["timestamp"] = get_yesterday_datetime() self.db.collection.insert_one(yesterday_post) self.assertRaises(Exception, self.fetcher._format_cheapest_product_tweet, "2080 TI") def test_only_today_post_case(self): today_post = self.create_basic_post() today_post["timestamp"] = get_today_datetime() self.db.collection.insert_one(today_post) self.fetcher._format_cheapest_product_tweet("2080 TI") def test_yesterday_and_today_post_case(self): posts = [self.create_basic_post(), self.create_basic_post()] posts[0]["timestamp"] = get_yesterday_datetime() posts[1]["timestamp"] = get_today_datetime() posts[1]["product_price"] = posts[0]["product_price"] * 2 self.db.collection.insert_many(posts) self.fetcher._format_cheapest_product_tweet("2080 TI")
def test_filter_videos_for_specific_user_successfuly_all_own_videos(): client = MongoClient() collection = client[DB]['videos'] users_collection = client[DB]['users'] user_filtering = '*****@*****.**' data = { 'title': 'test', 'url': 'test.com', 'user': user_filtering, 'isPrivate': True } data2 = { 'title': 'test2', 'url': 'test2.com', 'user': user_filtering, 'isPrivate': True } _id1 = '5edbc9196ab5430010391c79' _id2 = '5edbc9196ab5430010391c78' insert_video_into_db(_id1, data, collection) insert_video_into_db(_id2, data2, collection) data['_id'] = _id1 data2['_id'] = _id2 result = list(collection.find({})) assert len(result) == 2 user = {'email': user_filtering, 'full name': 'Prueba'} insert_new_user(user, users_collection) filtered_videos = filter_videos_for_specific_user([data, data2], user_filtering, users_collection, collection) assert len(filtered_videos) == 2 assert filtered_videos[0] == data assert filtered_videos[1] == data2 client.close()
def test_get_user_requests_fails(): with patch('app_server.relationships_functions.send_notification_to_user') as _mock: client = MongoClient() collection = client[DB]['users'] data = [] for i in range(0, 4): data.append({'email': 'test_{0}@test.com'.format(i), 'full name': '{0}'.format(i)}) insert_new_user(data[i], collection) result = collection.find({}) assert len(list(result)) == 4 third_user = '******' # Not existing result = get_user_requests_from_db(third_user, collection) assert result == HTTP_NOT_FOUND client.close()
def test_get_user_by_email_successfully(): client = MongoClient() collection = client[DB]['users'] email = '*****@*****.**' data = {'email': email, 'full name': ''} insert_new_user(data, collection) result = list(collection.find({})) first_user = result[0] assert len(result) == 1 assert first_user['email'] == '*****@*****.**' result_get = get_user_by_email(email, collection) result = list(collection.find({})) assert result_get['email'] == email client.close()
def test_add_notification_token(): client = MongoClient() collection = client[DB]['users'] data = {'email': '@test.com', 'full name': ''} insert_new_user(data, collection) status_code = add_notifications_token('@test.com', 'FAKE_TOKEN', collection) result = list(collection.find({})) first_user = result[0] client.close() assert len(result) == 1 assert first_user['email'] == '@test.com' assert first_user['notifications_token'] == 'FAKE_TOKEN' assert status_code == HTTP_CREATED
def test_insert_new_friendship_request_success(): with patch('app_server.relationships_functions.send_notification_to_user') as _mock: client = MongoClient() collection = client[DB]['users'] data = [] for i in range(0, 2): data.append({'email': 'test_{0}@test.com'.format(i), 'full name': ''}) insert_new_user(data[i], collection) # First user requests second user to be his/her friend status_code = insert_new_friendship_request(data[0]['email'], data[1]['email'], collection) assert status_code == HTTP_CREATED assert _mock.called client.close()
def test_insert_new_friendship_request_two_users(): with patch('app_server.relationships_functions.send_notification_to_user') as _mock: client = MongoClient() collection = client[DB]['users'] data = [] for i in range(0, 3): data.append({'email': 'test_{0}@test.com'.format(i), 'full name': ''}) insert_new_user(data[i], collection) for i in range(1, 3): status_code = insert_new_friendship_request(data[i]['email'], data[0]['email'], collection) assert status_code == HTTP_CREATED requests = collection.find_one({'email': data[0]['email']})['requests'] assert len(requests) == 2 client.close()
def test_get_user_that_doesnt_exist(): client = MongoClient() collection = client[DB]['users'] email = '*****@*****.**' data = {'email': email, 'full name': ''} insert_new_user(data, collection) result = list(collection.find({})) first_user = result[0] assert len(result) == 1 assert first_user['email'] == '*****@*****.**' result_get = get_user_by_email('*****@*****.**', collection) result = list(collection.find({})) assert result_get is None client.close()