def get_pending_sets(): ''' Gets all the pending sets from the collections''' try: set_list = _db.potential_sets.find() except pymongo.errors.PyMongoError: _log.exception('get_pending_sets has failed in the database') return [Set.from_dict(each_set) for each_set in set_list]
def test_encoder(self): ''' Tests the encoder ''' new_set = Set(-2, 1, 'TEST TITLE', ['PATH 1', 'PATH 2']) encoded_set = SetEncoderTestSuite.encoder.default(new_set) self.assertEqual(encoded_set['_id'], -2) self.assertEqual(encoded_set['correct_option'], 1) self.assertEqual(encoded_set['title'], 'TEST TITLE') self.assertEqual(encoded_set['paths'], ['PATH 1', 'PATH 2'])
def test_get_by_collection(self, mock_db): ''' Tests to make sure the database properly sends the correct keyword in all lower case''' _set = Set(-2, 1, 'TEST TITLE', ['PATH 1', 'PATH 2']) _set.add_keyword('test') mock_db.find.return_value = None set_list = db.get_sets_by_keyword('test') mock_db.find.assert_called_with({'keywords': 'test'}) self.assertEqual(set_list, None)
def test_acc_helper(self, mock_calc, mock_db): ''' tests the accuracy helper''' set1 = Set(1, 1, 'test 1', ['test', 'test2'], ['test', 'test2']) set2 = Set(2, 2, 'test 1', ['test', 'test2'], ['test', 'test2']) set_list = [set1, set2] user_1 = User(1, 'test', 'test', 'voter') user_2 = User(2, 'test', 'test', 'voter') user_3 = User(3, 'test', 'test', 'voter') user_4 = User(4, 'test', 'test', 'voter') user_list = [user_1, user_2, user_3, user_4] mock_db.return_value = user_list mock_calc.return_value = .67 ret_list = set_accuracy_helper(set_list) _log.debug(mock_db) _log.debug(mock_calc) self.assertEqual(ret_list[0].accuracy, .67) self.assertEqual(ret_list[1].accuracy, .67)
def get_set_by_id(_id: int): ''' Gets the set with the given id ''' query = {'_id': _id} try: retrieved_set = _db.sets.find_one(query) except pymongo.errors.PyMongoError: _log.exception('get_sets has failed in the database') return Set.from_dict(retrieved_set) if retrieved_set else None
def get_sets_by_keyword(keyword): ''' Gets all sets with the given keyword ''' query = {'keywords': keyword.lower()} set_list = None try: set_list = _db.sets.find(query) except pymongo.errors.PyMongoError: _log.exception( 'get_sets_by_keyword has failed in the database for keyword %s', keyword) return [Set.from_dict(each_set) for each_set in set_list] if set_list else None
def submit_set(query: dict): new_set = None _id = _db.counter.find_one_and_update( {'_id': 'SET_COUNT'}, {'$inc': { 'count': 1 }}, return_document=pymongo.ReturnDocument.AFTER)['count'] query["_id"] = _id _log.debug(query) try: _db.potential_sets.insert_one(query) new_set = Set.from_dict(_db.potential_sets.find_one({"_id": _id})) except pymongo.errors.PyMongoError: _log.exception('set_set has failed in the database') return new_set
def test_from_dict(self): ''' Tests the from_dict method ''' input_dict = { '_id': -2, 'correct_option': 1, 'title': 'TEST TITLE 3', 'paths': ['ONE', 'TWO'], 'deck_tags': ['deck2', 'deck5'], 'keywords': ['autumn'] } new_set = Set.from_dict(input_dict) self.assertEqual(new_set.correct_option, 1) self.assertEqual(new_set.title, 'TEST TITLE 3') self.assertEqual(new_set.paths, ['ONE', 'TWO']) self.assertEqual(new_set.deck_tags, ['deck2', 'deck5']) self.assertEqual(new_set.keywords, ['autumn']) self.assertEqual(new_set._id, -2)
def setUp(self): ''' Create a new set for testing ''' SetTestSuite.set = Set(-2, 1, 'TEST TITLE', ['PATH 1', 'PATH 2'])
return _db.counter.find_one_and_update( {'_id': 'SET_COUNT'}, {'$inc': { 'count': 1 }}, return_document=pymongo.ReturnDocument.AFTER)['count'] if __name__ == '__main__': _db.sets.drop() _db.users.drop() _db.counter.drop() _db.counter.insert_one({'_id': 'SET_COUNT', 'count': 0}) set_list = [] set_list.append( Set( _get_set_id(), 1, 'Which scientist is known for developing the planetary model of the atom?', ['bohr.jpg', 'rutherford.jpg']).to_dict()) _db.sets.insert_one(set_list[0]) user_list = [] user_list.append( User(_get_set_id(), 'username', 'password', 'voter').to_dict()) user_list.append( User(_get_set_id(), 'jotaro', 'star_platinum', 'voter').to_dict()) user_list.append( User(_get_set_id(), 'diavolo', 'king_crimson', 'moderator').to_dict()) _db.users.insert_many(user_list)