def test_api_get_blackboard_type(self): self.api = BlackboardAPI(mock_data_generator.settings(), MongoClient=mock_data_generator.mock_client) # Test getting date-based self.assertEqual(self.api.get_blackboard_type('ARTICLE'), CounterManager.counter_type_date_based) self.assertEqual( self.api.get_blackboard_type('ARTICLE', date_based=True), CounterManager.counter_type_date_based) with self.assertRaises(ValueError): self.api.get_blackboard_type('ARTICLE', date_based=False) # Test getting standard self.assertEqual(self.api.get_blackboard_type('FEED'), CounterManager.counter_type_standard) self.assertEqual( self.api.get_blackboard_type('FEED', date_based=False), CounterManager.counter_type_standard) with self.assertRaises(ValueError): self.api.get_blackboard_type('FEED', date_based=True) # Test getting a different date-based self.assertEqual(self.api.get_blackboard_type('ARTICLE2'), CounterManager.counter_type_date_based) # Test getting a non-existing blackboard self.assertEqual(self.api.get_blackboard_type('MISSING'), None) self.assertEqual( self.api.get_blackboard_type('MISSING', date_based=True), CounterManager.counter_type_date_based) self.assertEqual( self.api.get_blackboard_type('MISSING', date_based=False), CounterManager.counter_type_standard)
def test_api_load_blackboard(self): self.api = BlackboardAPI(mock_data_generator.settings(), MongoClient=mock_data_generator.mock_client) # Correctly called returns correct type self.assertIsInstance(self.api.load_blackboard('ARTICLE'), DateBasedBlackboard) self.assertIsInstance( self.api.load_blackboard('ARTICLE', date_based=True), DateBasedBlackboard) self.assertIsInstance(self.api.load_blackboard('FEED'), Blackboard) self.assertIsInstance( self.api.load_blackboard('FEED', date_based=False), Blackboard) # Incorrectly called raises error with self.assertRaises(ValueError): self.api.load_blackboard('ARTICLE_TAGS') with self.assertRaises(ValueError): self.api.load_blackboard('article_counter') with self.assertRaises(ValueError): self.api.load_blackboard('FEED', date_based=True) with self.assertRaises(ValueError): self.api.load_blackboard('ARTICLE', date_based=False) with self.assertRaises(ValueError): self.api.load_blackboard('ARTICLE2', date_based=False)
def setUp(self): random.seed(1234) # Settings dont need to be changed when mocking self.settings = {'user' : 'test_user', 'password' : 'password', 'dbname' : 'testdb', 'dburl' : 'mongodb://localhost:27017'} self.api = BlackboardAPI(self.settings, MongoClient=mock_data_generator.mock_client) self.bb = self.api.load_blackboard('ARTICLE')
def test_api_drop_blackboard(self): # Non-admin user self.api = BlackboardAPI(mock_data_generator.settings(), MongoClient=mock_data_generator.mock_client) # Check they exist before attempting drop blackboards = [('ARTICLE', True), ('ARTICLE2', True), ('FEED', True), ('article', False)] for bb in blackboards: self.assertEqual(self.api.blackboard_exists(bb[0]), bb[1]) with self.assertRaises(ValueError): self.api.drop_blackboard('ARTICLE_TAGS') with self.assertRaises(ValueError): self.api.drop_blackboard('article_counter') with self.assertRaises(PermissionError): self.api.drop_blackboard('ARTICLE') with self.assertRaises(PermissionError): self.api.drop_blackboard('article') # Check they still exist after failed drop for bb in blackboards: self.assertEqual(self.api.blackboard_exists(bb[0]), bb[1]) # Test admin drop self.api = BlackboardAPI(mock_data_generator.admin_settings(), MongoClient=mock_data_generator.mock_client) self.api.drop_blackboard('ARTICLE') self.assertEqual(self.api.blackboard_exists(blackboards[0][0]), not blackboards[0][1]) for bb in blackboards[1:]: self.assertEqual(self.api.blackboard_exists(bb[0]), bb[1]) self.api.drop_blackboard('ARTICLE2') self.assertEqual(self.api.blackboard_exists('ARTICLE2'), False) self.api.drop_blackboard('FEED') self.assertEqual(self.api.blackboard_exists('FEED'), False)
class TestManagers(unittest.TestCase): def setUp(self): random.seed(1234) # Settings dont need to be changed when mocking self.settings = {'user' : 'test_user', 'password' : 'password', 'dbname' : 'testdb', 'dburl' : 'mongodb://localhost:27017'} self.api = BlackboardAPI(self.settings, MongoClient=mock_data_generator.mock_client) self.bb = self.api.load_blackboard('ARTICLE') def tearDown(self): del self.api del self.bb def test_init_tag_manager(self): with self.assertRaises(UserWarning): TagManager(self.bb) with self.assertRaises(UserWarning): TagManager(None) def test_init_document_manager(self): with self.assertRaises(UserWarning): DocumentManager(self.bb) with self.assertRaises(UserWarning): DocumentManager(None) def test_init_date_based_document_manager(self): with self.assertRaises(UserWarning): DateBasedDocumentManager(self.bb) with self.assertRaises(UserWarning): DateBasedDocumentManager(None) def test_init_counter_manager(self): with self.assertRaises(UserWarning): CounterManager(self.bb) with self.assertRaises(UserWarning): CounterManager(None)
def test_delete(self): obj_id = self.bb.insert({'Deleted' : False}) expected = 11 with self.assertRaises(PermissionError): self.bb.delete(obj_id) self.assertEqual(self.bb.count(), expected) # Add test as admin self.api = BlackboardAPI(mock_data_generator.admin_settings(), MongoClient=mock_data_generator.mock_client) self.bb = self.api.load_blackboard('FEED') obj_id = self.bb.insert({'Deleted' : False}) expected = 11 self.assertEqual(self.bb.count(), expected) self.assertEqual(self.bb.count(query={'Deleted' : False}), 1) self.bb.delete(obj_id) self.assertEqual(self.bb.count(query={'Deleted' : False}), 0) self.assertEqual(self.bb.count(), expected-1)
def test_delete_tag(self): with self.assertRaises(PermissionError): self.bb.delete_tag(1) self.assertEqual(self.bb.get_tag(1)['Nm'], 'Tag_1') # Add test as admin self.api = BlackboardAPI(mock_data_generator.admin_settings(), MongoClient=mock_data_generator.mock_client) self.bb = self.api.load_blackboard('ARTICLE') obj_id = self.bb.insert_tag('To_be_deleted') expected = 11 self.assertEqual(self.bb.get_tag('To_be_deleted')['Nm'], 'To_be_deleted') self.assertEqual(self.bb.delete_tag(obj_id)['n'], 1) self.assertEqual(self.bb.get_tag('To_be_deleted'), None) self.assertEqual(self.bb.count(tags=[2]), 2) self.assertEqual(self.bb.delete_tag(2)['n'], 1) with self.assertRaises(ValueError): self.bb.count(tags=[2])
def load(settings_path): with open(settings_path) as f: settings = json.load(f) bbapi = BlackboardAPI(settings) db = bbapi._BlackboardAPI__db return bbapi, db
def test_setting_validation(self): settings = { 'user': '******', 'dbname': 'testdb', 'dburl': 'mongodb://localhost:27017' } with self.assertRaises(ValueError): self.api = BlackboardAPI( settings, MongoClient=mock_data_generator.mock_client) settings = { 'user': '******', 'dog': 'dog', 'dbname': 'testdb', 'dburl': 'mongodb://localhost:27017' } with self.assertRaises(ValueError): self.api = BlackboardAPI( settings, MongoClient=mock_data_generator.mock_client)
def test_api_load_blackboard(self): self.api = BlackboardAPI(mock_data_generator.settings(), MongoClient=mock_data_generator.mock_client) # Correctly called returns correct type self.assertIsInstance(self.api.load_blackboard('ARTICLE'), DateBasedBlackboard) self.assertIsInstance(self.api.load_blackboard('ARTICLE', date_based=True), DateBasedBlackboard) self.assertIsInstance(self.api.load_blackboard('FEED'), Blackboard) self.assertIsInstance(self.api.load_blackboard('FEED', date_based=False), Blackboard) # Incorrectly called raises error with self.assertRaises(ValueError): self.api.load_blackboard('ARTICLE_TAGS') with self.assertRaises(ValueError): self.api.load_blackboard('article_counter') with self.assertRaises(ValueError): self.api.load_blackboard('FEED', date_based=True) with self.assertRaises(ValueError): self.api.load_blackboard('ARTICLE', date_based=False) with self.assertRaises(ValueError): self.api.load_blackboard('ARTICLE2', date_based=False)
def test_delete(self): obj_id = self.bb.insert({'Deleted' : False}) expected = 11 with self.assertRaises(PermissionError): self.bb.delete(obj_id) self.assertEqual(self.bb.count(), expected) # Add test as admin self.api = BlackboardAPI(mock_data_generator.admin_settings(), MongoClient=mock_data_generator.mock_client) self.bb = self.api.load_blackboard('ARTICLE') obj_id = self.bb.insert({'Deleted' : False}) expected = 11 self.assertEqual(self.bb.count(), expected) self.assertEqual(self.bb.count(query={'Deleted' : False}), 1) self.bb.delete(obj_id) self.assertEqual(self.bb.count(query={'Deleted' : False}), 0) self.assertEqual(self.bb.count(), expected-1)
def test_api_get_blackboard_type(self): self.api = BlackboardAPI(mock_data_generator.settings(), MongoClient=mock_data_generator.mock_client) # Test getting date-based self.assertEqual(self.api.get_blackboard_type('ARTICLE'), CounterManager.counter_type_date_based) self.assertEqual(self.api.get_blackboard_type('ARTICLE', date_based=True), CounterManager.counter_type_date_based) with self.assertRaises(ValueError): self.api.get_blackboard_type('ARTICLE', date_based=False) # Test getting standard self.assertEqual(self.api.get_blackboard_type('FEED'), CounterManager.counter_type_standard) self.assertEqual(self.api.get_blackboard_type('FEED', date_based=False), CounterManager.counter_type_standard) with self.assertRaises(ValueError): self.api.get_blackboard_type('FEED', date_based=True) # Test getting a different date-based self.assertEqual(self.api.get_blackboard_type('ARTICLE2'), CounterManager.counter_type_date_based) # Test getting a non-existing blackboard self.assertEqual(self.api.get_blackboard_type('MISSING'), None) self.assertEqual(self.api.get_blackboard_type('MISSING', date_based=True), CounterManager.counter_type_date_based) self.assertEqual(self.api.get_blackboard_type('MISSING', date_based=False), CounterManager.counter_type_standard)
def test_api_get_blackboard_names(self): self.api = BlackboardAPI(mock_data_generator.settings(), MongoClient=mock_data_generator.mock_client) self.assertSetEqual(set(self.api.get_blackboard_names()), set(['FEED', 'ARTICLE', 'ARTICLE2']))
class TestDateBasedBlackboards(unittest.TestCase): def setUp(self): self.api = BlackboardAPI(mock_data_generator.settings(), MongoClient=mock_data_generator.mock_client) self.bb = self.api.load_blackboard('ARTICLE') def tearDown(self): del self.api del self.bb def test_bb_count(self): self.assertEqual(self.bb.count(), 10) self.assertEqual(self.bb.count(query={'T' : 'Title 3'}), 1) self.assertEqual(self.bb.count(max_date=['02-01-2016'], tags = ['FOR>Tag_11', 12]), 8) self.assertEqual(self.bb.count(max_date=['03-01-2016'], min_date=['02-01-2012'], tags = ['FOR>Tag_11', 12]), 4) self.assertEqual(self.bb.count(query={'BLANK' : 'Title 3'}), 0) def test_bb_find(self): self.assertEqual(len(self.bb.find()), 10) self.assertEqual(len([x for x in self.bb.find(tags = [3])]), 2) self.assertEqual(len(self.bb.find(tags = ['FOR>Tag_11', 12])), 10) self.assertEqual(len(self.bb.find(min_date=['01-01-2016'], tags = ['FOR>Tag_11', 12])), 3) self.assertEqual(len([x for x in self.bb.find(max_date=['02-01-2016'], tags = ['FOR>Tag_11', 12])]), 8) self.assertEqual(len(self.bb.find(tags = ['FOR>Tag_11', 5])), 2) self.assertEqual([x for x in self.bb.find(query={'T' : 'Title 3'})][0]['T'], 'Title 3') with self.assertRaises(ValueError): self.bb.find(tags = [1, 13]) with self.assertRaises(ValueError): self.bb.find(tags = ['Tag_4', 13]) with self.assertRaises(ValueError): self.bb.find(tags = ['Tag_4', -5]) with self.assertRaises(ValueError): self.bb.find(min_date='01-01-2016') self.assertEqual(len([x for x in self.bb.find(tags = ['FOR>Tag_11', 12], max = 5)]), 5) self.assertEqual(len(self.bb.find(tags = ['FOR>Tag_11', 12], max = 5)), 5) self.assertEqual(len(self.bb.find(tags = ['FOR>Tag_11', 12], max = 1)), 1) self.assertEqual(len(self.bb.find(min_date=['01-01-2016'], tags = ['FOR>Tag_11', 12], max = 2)), 2) def test_insert(self): from macsy import utils # Generate a doc, check # of docs, insert it, check it's incremented obj_id = ObjectId.from_datetime(dtparser.parse('21-10-2017')) hsh = utils.java_string_hashcode('515TitleDescription') expected = 11 self.assertEqual(self.bb.count(), expected-1) self.assertEqual(self.bb.insert({DateBasedDocumentManager.doc_id : obj_id, 'HSH' : hsh, 'oID' : 515, 'T' : 'Title', 'D' : 'Description', 'Overwritten' : False, 'Inserted' : True, 'Tg' : [1, 2, 3]}), obj_id) self.assertEqual(self.bb.count(), expected) self.assertEqual([x for x in self.bb.find(query={'Inserted' : True})][0]['_id'], obj_id) # Try to insert it again self.assertEqual(self.bb.insert({DateBasedDocumentManager.doc_id : obj_id, 'oID' : 515, 'T' : 'Title', 'D' : 'Description', 'Overwritten' : True, 'Updated' : True, 'Tg' : [4, 5]}), obj_id) self.assertEqual(self.bb.count(), expected) self.assertEqual([x for x in self.bb.find(query={'Updated' : True})][0]['_id'], obj_id) self.assertEqual(self.bb.count(query={'Overwritten' : False}), 0) self.assertEqual([x for x in self.bb.find(query={'Overwritten' : True})][0]['_id'], obj_id) self.assertEqual([x for x in self.bb.find(tags=[1, 2, 3, 4, 5])][0]['_id'], obj_id) # Insert a document without an id and generate one self.assertEqual(self.bb.insert({'Blank_id' : True}).generation_time.date(), datetime.now().date()) def test_update(self): obj_id = self.bb.insert({'Overwritten' : False, 'Inserted' : True, 'Tg' : [1, 2, 3]}) self.assertEqual(self.bb.update(obj_id, {'Overwritten' : True, 'Inserted' : False, 'Fds' : [104], 'Tg' : [1, 4, 6]}), obj_id) # Fails due to bug if older version of mockmongo is used self.assertEqual([x for x in self.bb.find(query={'Overwritten' : True, 'Tg' : [1, 2, 3, 4, 6]})][0]['_id'], obj_id) self.assertEqual(self.bb.update(obj_id, {'Fds' : 111, 'Tg' : 5}), obj_id) self.assertEqual([x for x in self.bb.find(query={'Overwritten' : True, 'Fds' : [104, 111], 'Tg' : [1, 2, 3, 4, 6, 5]})][0]['_id'], obj_id) def test_delete(self): obj_id = self.bb.insert({'Deleted' : False}) expected = 11 with self.assertRaises(PermissionError): self.bb.delete(obj_id) self.assertEqual(self.bb.count(), expected) # Add test as admin self.api = BlackboardAPI(mock_data_generator.admin_settings(), MongoClient=mock_data_generator.mock_client) self.bb = self.api.load_blackboard('ARTICLE') obj_id = self.bb.insert({'Deleted' : False}) expected = 11 self.assertEqual(self.bb.count(), expected) self.assertEqual(self.bb.count(query={'Deleted' : False}), 1) self.bb.delete(obj_id) self.assertEqual(self.bb.count(query={'Deleted' : False}), 0) self.assertEqual(self.bb.count(), expected-1) def test_add_tag(self): obj_id = self.bb.insert({'hasTags' : False}) self.bb.add_tag(obj_id, 1) self.assertEqual(self.bb.count(tags=[1]), 3) self.bb.add_tag(obj_id, [2]) self.assertEqual(self.bb.count(tags=[1, 2]), 2) self.bb.add_tag(obj_id, [3, 4]) self.assertEqual([x for x in self.bb.find(tags=[1, 2, 3])][0][DateBasedDocumentManager.doc_id], obj_id) self.bb.add_tag(obj_id, 4) self.assertEqual([x for x in self.bb.find(query={'Tg' : [1, 2, 3, 4]})][0][DateBasedDocumentManager.doc_id], obj_id) self.bb.add_tag(obj_id, [6,7]) self.assertEqual([x for x in self.bb.find(query={'Tg' : [1, 2, 3, 4, 6, 7]})][0][DateBasedDocumentManager.doc_id], obj_id) def test_remove_tag(self): obj_id = self.bb.insert({'hasTags' : True, 'Tg' : [1, 2, 3, 4, 5]}) result = self.bb.remove_tag(obj_id, 3) self.assertEqual(result, obj_id) self.assertEqual([x for x in self.bb.find(tags=[1, 2, 4, 5])][0][DateBasedDocumentManager.doc_id], obj_id) result = self.bb.remove_tag(obj_id, [1,5]) self.assertEqual(result, obj_id) self.assertEqual([x for x in self.bb.find(query={'Tg' : [2, 4]})][0][DateBasedDocumentManager.doc_id], obj_id) result = self.bb.remove_tag(obj_id, [2,8]) self.assertEqual(result, obj_id) self.assertEqual([x for x in self.bb.find(query={'Tg' : [4]})][0][DateBasedDocumentManager.doc_id], obj_id) def test_insert_tag(self): self.assertEqual(self.bb.insert_tag('Tag_Not_Control'), 13) self.assertEqual(self.bb.get_tag('Tag_Not_Control')[TagManager.tag_id], 13) self.assertEqual(self.bb.get_tag('Tag_Not_Control')[TagManager.tag_control], 0) self.assertEqual(self.bb.is_control_tag(13), False) self.assertEqual(self.bb.insert_tag('FOR>Tag_14'), 14) self.assertEqual(self.bb.get_tag('FOR>Tag_14')[TagManager.tag_id], 14) self.assertEqual(self.bb.get_tag('FOR>Tag_14')[TagManager.tag_control], 1) self.assertEqual(self.bb.is_control_tag(14), True) self.assertEqual(self.bb.is_inheritable_tag(14), False) with self.assertRaises(ValueError): self.bb.insert_tag('Tag_1') with self.assertRaises(ValueError): self.bb.insert_tag('FOR>Tag_14') with self.assertRaises(ValueError): self.bb.insert_tag('POST>Tag_12') def test_update_tag(self): self.bb.update_tag(1, "Tag_1_Renamed") self.assertEqual(self.bb.get_tag(1)['Nm'], "Tag_1_Renamed") self.bb.update_tag(1, "Tag_1_Renamed", True) self.assertEqual(self.bb.get_tag(1)['DInh'], 1) self.bb.update_tag(1, "FOR>Tag_1_Renamed", True) self.assertEqual(self.bb.get_tag(1)['Ctrl'], 1) with self.assertRaises(ValueError): self.bb.update_tag(3,"FOR>Tag_1_Renamed") def test_delete_tag(self): with self.assertRaises(PermissionError): self.bb.delete_tag(1) self.assertEqual(self.bb.get_tag(1)['Nm'], 'Tag_1') # Add test as admin self.api = BlackboardAPI(mock_data_generator.admin_settings(), MongoClient=mock_data_generator.mock_client) self.bb = self.api.load_blackboard('ARTICLE') obj_id = self.bb.insert_tag('To_be_deleted') expected = 11 self.assertEqual(self.bb.get_tag('To_be_deleted')['Nm'], 'To_be_deleted') self.assertEqual(self.bb.delete_tag(obj_id)['n'], 1) self.assertEqual(self.bb.get_tag('To_be_deleted'), None) self.assertEqual(self.bb.count(tags=[2]), 2) self.assertEqual(self.bb.delete_tag(2)['n'], 1) with self.assertRaises(ValueError): self.bb.count(tags=[2]) def test_bb_get_tag(self): # Bad input self.assertEqual(self.bb.get_tag(55), None) self.assertEqual(self.bb.get_tag('3'), None) self.assertEqual(self.bb.get_tag(3333), None) # Retrieval self.assertEqual(self.bb.get_tag(1), {'Ctrl': 0, '_id': 1, 'Nm': 'Tag_1'}) self.assertEqual(self.bb.get_tag(2), {'Ctrl': 0, '_id': 2, 'Nm': 'Tag_2'}) self.assertEqual(self.bb.get_tag('Tag_3'), {'Ctrl': 0, '_id': 3, 'Nm': 'Tag_3'}) self.assertEqual(self.bb.get_tag(12), {'Ctrl': 1, 'Nm': 'POST>Tag_12', '_id': 12}) def test_bb_tag_has_property(self): # Bad input for control self.assertEqual(self.bb.is_control_tag(55), False) self.assertEqual(self.bb.is_control_tag('3'), False) # Bad input for inheritance self.assertEqual(self.bb.is_inheritable_tag(55), False) self.assertEqual(self.bb.is_inheritable_tag('3'), False) # Checking for control self.assertEqual(self.bb.is_control_tag(11), True) self.assertEqual(self.bb.is_control_tag('FOR>Tag_11'), True) self.assertEqual(self.bb.is_control_tag(4), False) self.assertEqual(self.bb.is_control_tag('Tag_4'), False) # Checking for inheritance (should add True test) self.assertEqual(self.bb.is_inheritable_tag(11), False) self.assertEqual(self.bb.is_inheritable_tag(4), False) self.assertEqual(self.bb.is_inheritable_tag(4), False) self.assertEqual(self.bb.is_inheritable_tag('Tag_4'), False) def test_bb_get_date(self): self.assertEqual(str(self.bb.get_date({DateBasedDocumentManager.doc_id: ObjectId.from_datetime(dtparser.parse('21-10-2017'))})), '2017-10-21 00:00:00+00:00') with self.assertRaises(ValueError): self.bb.get_date({'different_id': ObjectId.from_datetime(dtparser.parse('21-10-2017'))}) with self.assertRaises(ValueError): self.bb.get_date({DateBasedDocumentManager.doc_id: 1}) def test_get_extremal_date(self): self.assertEqual(str(self.bb.get_earliest_date()), '2009-01-01 00:00:00+00:00') self.assertEqual(str(self.bb.get_latest_date()), '2018-01-01 00:00:00+00:00')
def setUp(self): self.api = BlackboardAPI(mock_data_generator.settings(), MongoClient=mock_data_generator.mock_client) self.bb = self.api.load_blackboard('ARTICLE')
class TestBlackboardAPI(unittest.TestCase): def teatDown(self): del self.api def test_api_load_blackboard(self): self.api = BlackboardAPI(mock_data_generator.settings(), MongoClient=mock_data_generator.mock_client) # Correctly called returns correct type self.assertIsInstance(self.api.load_blackboard('ARTICLE'), DateBasedBlackboard) self.assertIsInstance( self.api.load_blackboard('ARTICLE', date_based=True), DateBasedBlackboard) self.assertIsInstance(self.api.load_blackboard('FEED'), Blackboard) self.assertIsInstance( self.api.load_blackboard('FEED', date_based=False), Blackboard) # Incorrectly called raises error with self.assertRaises(ValueError): self.api.load_blackboard('ARTICLE_TAGS') with self.assertRaises(ValueError): self.api.load_blackboard('article_counter') with self.assertRaises(ValueError): self.api.load_blackboard('FEED', date_based=True) with self.assertRaises(ValueError): self.api.load_blackboard('ARTICLE', date_based=False) with self.assertRaises(ValueError): self.api.load_blackboard('ARTICLE2', date_based=False) def test_api_drop_blackboard(self): # Non-admin user self.api = BlackboardAPI(mock_data_generator.settings(), MongoClient=mock_data_generator.mock_client) # Check they exist before attempting drop blackboards = [('ARTICLE', True), ('ARTICLE2', True), ('FEED', True), ('article', False)] for bb in blackboards: self.assertEqual(self.api.blackboard_exists(bb[0]), bb[1]) with self.assertRaises(ValueError): self.api.drop_blackboard('ARTICLE_TAGS') with self.assertRaises(ValueError): self.api.drop_blackboard('article_counter') with self.assertRaises(PermissionError): self.api.drop_blackboard('ARTICLE') with self.assertRaises(PermissionError): self.api.drop_blackboard('article') # Check they still exist after failed drop for bb in blackboards: self.assertEqual(self.api.blackboard_exists(bb[0]), bb[1]) # Test admin drop self.api = BlackboardAPI(mock_data_generator.admin_settings(), MongoClient=mock_data_generator.mock_client) self.api.drop_blackboard('ARTICLE') self.assertEqual(self.api.blackboard_exists(blackboards[0][0]), not blackboards[0][1]) for bb in blackboards[1:]: self.assertEqual(self.api.blackboard_exists(bb[0]), bb[1]) self.api.drop_blackboard('ARTICLE2') self.assertEqual(self.api.blackboard_exists('ARTICLE2'), False) self.api.drop_blackboard('FEED') self.assertEqual(self.api.blackboard_exists('FEED'), False) def test_api_get_blackboard_names(self): self.api = BlackboardAPI(mock_data_generator.settings(), MongoClient=mock_data_generator.mock_client) self.assertSetEqual(set(self.api.get_blackboard_names()), set(['FEED', 'ARTICLE', 'ARTICLE2'])) def test_api_get_blackboard_type(self): self.api = BlackboardAPI(mock_data_generator.settings(), MongoClient=mock_data_generator.mock_client) # Test getting date-based self.assertEqual(self.api.get_blackboard_type('ARTICLE'), CounterManager.counter_type_date_based) self.assertEqual( self.api.get_blackboard_type('ARTICLE', date_based=True), CounterManager.counter_type_date_based) with self.assertRaises(ValueError): self.api.get_blackboard_type('ARTICLE', date_based=False) # Test getting standard self.assertEqual(self.api.get_blackboard_type('FEED'), CounterManager.counter_type_standard) self.assertEqual( self.api.get_blackboard_type('FEED', date_based=False), CounterManager.counter_type_standard) with self.assertRaises(ValueError): self.api.get_blackboard_type('FEED', date_based=True) # Test getting a different date-based self.assertEqual(self.api.get_blackboard_type('ARTICLE2'), CounterManager.counter_type_date_based) # Test getting a non-existing blackboard self.assertEqual(self.api.get_blackboard_type('MISSING'), None) self.assertEqual( self.api.get_blackboard_type('MISSING', date_based=True), CounterManager.counter_type_date_based) self.assertEqual( self.api.get_blackboard_type('MISSING', date_based=False), CounterManager.counter_type_standard) def test_setting_validation(self): settings = { 'user': '******', 'dbname': 'testdb', 'dburl': 'mongodb://localhost:27017' } with self.assertRaises(ValueError): self.api = BlackboardAPI( settings, MongoClient=mock_data_generator.mock_client) settings = { 'user': '******', 'dog': 'dog', 'dbname': 'testdb', 'dburl': 'mongodb://localhost:27017' } with self.assertRaises(ValueError): self.api = BlackboardAPI( settings, MongoClient=mock_data_generator.mock_client)
def test_setting_validation(self): settings = {'user' : 'dbadmin', 'dbname' : 'testdb', 'dburl' : 'mongodb://localhost:27017'} with self.assertRaises(ValueError): self.api = BlackboardAPI(settings, MongoClient=mock_data_generator.mock_client) settings = {'user' : 'dbadmin', 'dog' : 'dog', 'dbname' : 'testdb', 'dburl' : 'mongodb://localhost:27017'} with self.assertRaises(ValueError): self.api = BlackboardAPI(settings, MongoClient=mock_data_generator.mock_client)
class TestBlackboards(unittest.TestCase): def setUp(self): self.api = BlackboardAPI(mock_data_generator.settings(), MongoClient=mock_data_generator.mock_client) self.bb = self.api.load_blackboard('FEED') def tearDown(self): del self.api del self.bb def test_bb_count(self): self.assertEqual(self.bb.count(), 10) self.assertEqual(self.bb.count(query={'Nm' : 'Feed 3'}), 1) self.assertEqual(self.bb.count(tags = ['FOR>Tag_11', 12]), 10) self.assertEqual(self.bb.count(query={'BLANK' : 'Title 3'}), 0) with self.assertRaises(ValueError): self.bb.find(tags = [1, 13]) with self.assertRaises(ValueError): self.bb.find(tags = ['Tag_4', 13]) with self.assertRaises(ValueError): self.bb.find(tags = ['Tag_4', -5]) def test_bb_find(self): self.assertEqual(len(self.bb.find()), 10) self.assertEqual(len(self.bb.find(tags = [3])), 1) self.assertEqual(len(self.bb.find(fields = ['Nm'])), 10) self.assertEqual(len(self.bb.find(fields = ['Single'])), 1) self.assertEqual(len(self.bb.find(without_fields = ['Single'])), 9) self.assertEqual([x for x in self.bb.find(max = 3, sort = 1)][0]['_id'], 1) with self.assertRaises(IndexError): [x for x in self.bb.find(max = 3, sort = 1)][3] self.assertEqual([x for x in self.bb.find(sort = 1)][0]['_id'], 1) self.assertEqual([x for x in self.bb.find(sort = -1)][0]['_id'], 10) def test_insert(self): # Generate a doc, check # of docs, insert it, check it's incremented obj_id = 15 expected = 11 self.assertEqual(self.bb.count(), expected-1) self.assertEqual(self.bb.insert({DocumentManager.doc_id : obj_id, 'Overwritten' : False, 'Inserted' : True, 'Tg' : [1, 2, 3]}), obj_id) self.assertEqual(self.bb.count(), expected) self.assertEqual([x for x in self.bb.find(query={'Inserted' : True})][0]['_id'], obj_id) # Try to insert it again self.assertEqual(self.bb.insert({DocumentManager.doc_id : obj_id, 'Overwritten' : True, 'Updated' : True, 'Tg' : [4, 5]}), obj_id) self.assertEqual(self.bb.count(), expected) self.assertEqual([x for x in self.bb.find(query={'Updated' : True})][0]['_id'], obj_id) self.assertEqual(self.bb.count(query={'Overwritten' : False}), 0) self.assertEqual([x for x in self.bb.find(query={'Overwritten' : True})][0]['_id'], obj_id) self.assertEqual([x for x in self.bb.find(tags=[1, 2, 3, 4, 5])][0]['_id'], obj_id) # Insert a document without an id and generate one self.assertEqual(self.bb.insert({'Blank_id' : True}), 11) def test_update(self): obj_id = self.bb.insert({'Overwritten' : False, 'Inserted' : True, 'Tg' : [1, 2, 3]}) self.assertEqual(self.bb.update(obj_id, {'Overwritten' : True, 'Inserted' : False, 'Fds' : [104], 'Tg' : [1, 4, 6]}), obj_id) # Fails due to bug if older version of mockmongo is used self.assertEqual([x for x in self.bb.find(query={'Overwritten' : True, 'Tg' : [1, 2, 3, 4, 6]})][0]['_id'], obj_id) self.assertEqual(self.bb.update(obj_id, {'Fds' : 111, 'Tg' : 5}), obj_id) self.assertEqual([x for x in self.bb.find(query={'Overwritten' : True, 'Fds' : 111, 'Tg' : [1, 2, 3, 4, 6, 5]})][0]['_id'], obj_id) # Trying to update a missing document, should this raise an error or return None? self.assertEqual(self.bb.update(obj_id+1, {"Fds" : 12}), None) def test_delete(self): obj_id = self.bb.insert({'Deleted' : False}) expected = 11 with self.assertRaises(PermissionError): self.bb.delete(obj_id) self.assertEqual(self.bb.count(), expected) # Add test as admin self.api = BlackboardAPI(mock_data_generator.admin_settings(), MongoClient=mock_data_generator.mock_client) self.bb = self.api.load_blackboard('FEED') obj_id = self.bb.insert({'Deleted' : False}) expected = 11 self.assertEqual(self.bb.count(), expected) self.assertEqual(self.bb.count(query={'Deleted' : False}), 1) self.bb.delete(obj_id) self.assertEqual(self.bb.count(query={'Deleted' : False}), 0) self.assertEqual(self.bb.count(), expected-1) def test_get_all_tags(self): self.assertEqual(len([x for x in self.bb.get_all_tags()]), 12) self.assertEqual([x for x in self.bb.get_all_tags()][0]['Nm'],'Tag_1') def test_add_tag(self): obj_id = self.bb.insert({'hasTags' : False}) self.assertEqual(obj_id, 11) self.bb.add_tag(obj_id, 1) self.assertEqual(self.bb.count(tags=[1]), 2) self.bb.add_tag(obj_id, [2]) self.assertEqual(self.bb.count(tags=[1, 2]), 1) self.bb.add_tag(obj_id, [3, 4]) self.assertEqual([x for x in self.bb.find(tags=[1, 2, 3])][0][DocumentManager.doc_id], obj_id) self.bb.add_tag(obj_id, 4) self.assertEqual([x for x in self.bb.find(query={'Tg' : [1, 2, 3, 4]})][0][DocumentManager.doc_id], obj_id) self.bb.add_tag(obj_id, [6,7]) self.assertEqual([x for x in self.bb.find(query={'Tg' : [1, 2, 3, 4, 6, 7]})][0][DocumentManager.doc_id], obj_id) def test_remove_tag(self): obj_id = self.bb.insert({'hasTags' : True, 'Tg' : [1, 2, 3, 4, 5]}) result = self.bb.remove_tag(obj_id, 3) self.assertEqual(result, obj_id) self.assertEqual([x for x in self.bb.find(tags=[1, 2, 4, 5])][0][DocumentManager.doc_id], obj_id) result = self.bb.remove_tag(obj_id, [1,5]) self.assertEqual(result, obj_id) self.assertEqual([x for x in self.bb.find(query={'Tg' : [2, 4]})][0][DocumentManager.doc_id], obj_id) result = self.bb.remove_tag(obj_id, [2,8]) self.assertEqual(result, obj_id) self.assertEqual([x for x in self.bb.find(query={'Tg' : [4]}, sort=1)][1][DocumentManager.doc_id], obj_id) self.assertEqual([x for x in self.bb.find(query={'Tg' : [4]}, sort=-1)][0][DocumentManager.doc_id], obj_id) def test_insert_tag(self): self.assertEqual(self.bb.get_tag('Non-existed-tag'), None) self.assertEqual(self.bb.insert_tag('Tag_Not_Control'), 13) self.assertEqual(self.bb.get_tag('Tag_Not_Control')[TagManager.tag_id], 13) self.assertEqual(self.bb.get_tag('Tag_Not_Control')[TagManager.tag_control], 0) self.assertEqual(self.bb.is_control_tag(13), False) self.assertEqual(self.bb.insert_tag('FOR>Tag_14'), 14) self.assertEqual(self.bb.get_tag('FOR>Tag_14')[TagManager.tag_id], 14) self.assertEqual(self.bb.get_tag('FOR>Tag_14')[TagManager.tag_control], 1) self.assertEqual(self.bb.is_control_tag(14), True) self.assertEqual(self.bb.is_inheritable_tag(14), False) with self.assertRaises(ValueError): self.bb.insert_tag('Tag_1') with self.assertRaises(ValueError): self.bb.insert_tag('FOR>Tag_14') with self.assertRaises(ValueError): self.bb.insert_tag('POST>Tag_12') def test_update_tag(self): self.bb.update_tag(1, "Tag_1_Renamed") self.assertEqual(self.bb.get_tag(1)['Nm'], "Tag_1_Renamed") self.bb.update_tag(1, "Tag_1_Renamed", True) self.assertEqual(self.bb.get_tag(1)['DInh'], 1) self.bb.update_tag(1, "FOR>Tag_1_Renamed", True) self.assertEqual(self.bb.get_tag(1)['Ctrl'], 1) with self.assertRaises(ValueError): self.bb.update_tag(3,"FOR>Tag_1_Renamed") def test_delete_tag(self): with self.assertRaises(PermissionError): self.bb.delete_tag(1) self.assertEqual(self.bb.get_tag(1)['Nm'], 'Tag_1') # Add test as admin self.api = BlackboardAPI(mock_data_generator.admin_settings(), MongoClient=mock_data_generator.mock_client) self.bb = self.api.load_blackboard('ARTICLE') obj_id = self.bb.insert_tag('To_be_deleted') expected = 11 self.assertEqual(self.bb.get_tag('To_be_deleted')['Nm'], 'To_be_deleted') self.assertEqual(self.bb.delete_tag(obj_id)['n'], 1) self.assertEqual(self.bb.get_tag('To_be_deleted'), None) self.assertEqual(self.bb.count(tags=[2]), 2) self.assertEqual(self.bb.delete_tag(2)['n'], 1) with self.assertRaises(ValueError): self.bb.count(tags=[2]) def test_bb_get_tag(self): # Bad input self.assertEqual(self.bb.get_tag(55), None) self.assertEqual(self.bb.get_tag('3'), None) self.assertEqual(self.bb.get_tag(3333), None) # Retrieval self.assertEqual(self.bb.get_tag(1), {'Ctrl': 0, '_id': 1, 'Nm': 'Tag_1'}) self.assertEqual(self.bb.get_tag(2), {'Ctrl': 0, '_id': 2, 'Nm': 'Tag_2'}) self.assertEqual(self.bb.get_tag('Tag_3'), {'Ctrl': 0, '_id': 3, 'Nm': 'Tag_3'}) self.assertEqual(self.bb.get_tag(12), {'Ctrl': 1, 'Nm': 'POST>Tag_12', '_id': 12}) def test_bb_tag_has_property(self): # Bad input for control self.assertEqual(self.bb.is_control_tag(55), False) self.assertEqual(self.bb.is_control_tag('3'), False) # Bad input for inheritance self.assertEqual(self.bb.is_inheritable_tag(55), False) self.assertEqual(self.bb.is_inheritable_tag('3'), False) # Checking for control self.assertEqual(self.bb.is_control_tag(11), True) self.assertEqual(self.bb.is_control_tag('FOR>Tag_11'), True) self.assertEqual(self.bb.is_control_tag(4), False) self.assertEqual(self.bb.is_control_tag('Tag_4'), False) # Checking for inheritance (should add True test) self.assertEqual(self.bb.is_inheritable_tag(11), False) self.assertEqual(self.bb.is_inheritable_tag(4), False) self.assertEqual(self.bb.is_inheritable_tag(4), False) self.assertEqual(self.bb.is_inheritable_tag('Tag_4'), False)
class TestDateBasedBlackboards(unittest.TestCase): def setUp(self): self.api = BlackboardAPI(mock_data_generator.settings(), MongoClient=mock_data_generator.mock_client) self.bb = self.api.load_blackboard('ARTICLE') def tearDown(self): del self.api del self.bb def test_bb_count(self): self.assertEqual(self.bb.count(), 10) self.assertEqual(self.bb.count(query={'T': 'Title 3'}), 1) self.assertEqual( self.bb.count(max_date=['02-01-2016'], tags=['FOR>Tag_11', 12]), 8) self.assertEqual( self.bb.count(max_date=['03-01-2016'], min_date=['02-01-2012'], tags=['FOR>Tag_11', 12]), 4) self.assertEqual(self.bb.count(query={'BLANK': 'Title 3'}), 0) def test_bb_find(self): self.assertEqual(len(self.bb.find()), 10) self.assertEqual(len([x for x in self.bb.find(tags=[3])]), 2) self.assertEqual(len(self.bb.find(tags=['FOR>Tag_11', 12])), 10) self.assertEqual( len(self.bb.find(min_date=['01-01-2016'], tags=['FOR>Tag_11', 12])), 3) self.assertEqual( len([ x for x in self.bb.find(max_date=['02-01-2016'], tags=['FOR>Tag_11', 12]) ]), 8) self.assertEqual(len(self.bb.find(tags=['FOR>Tag_11', 5])), 2) self.assertEqual([x for x in self.bb.find(query={'T': 'Title 3'}) ][0]['T'], 'Title 3') with self.assertRaises(ValueError): self.bb.find(tags=[1, 13]) with self.assertRaises(ValueError): self.bb.find(tags=['Tag_4', 13]) with self.assertRaises(ValueError): self.bb.find(tags=['Tag_4', -5]) with self.assertRaises(ValueError): self.bb.find(min_date='01-01-2016') self.assertEqual( len([x for x in self.bb.find(tags=['FOR>Tag_11', 12], max=5)]), 5) self.assertEqual(len(self.bb.find(tags=['FOR>Tag_11', 12], max=5)), 5) self.assertEqual(len(self.bb.find(tags=['FOR>Tag_11', 12], max=1)), 1) self.assertEqual( len( self.bb.find(min_date=['01-01-2016'], tags=['FOR>Tag_11', 12], max=2)), 2) def test_insert(self): from macsy import utils # Generate a doc, check # of docs, insert it, check it's incremented obj_id = ObjectId.from_datetime(dtparser.parse('21-10-2017')) hsh = utils.java_string_hashcode('515TitleDescription') expected = 11 self.assertEqual(self.bb.count(), expected - 1) self.assertEqual( self.bb.insert({ DateBasedDocumentManager.doc_id: obj_id, 'HSH': hsh, 'oID': 515, 'T': 'Title', 'D': 'Description', 'Overwritten': False, 'Inserted': True, 'Tg': [1, 2, 3] }), obj_id) self.assertEqual(self.bb.count(), expected) self.assertEqual([x for x in self.bb.find(query={'Inserted': True}) ][0]['_id'], obj_id) # Try to insert it again self.assertEqual( self.bb.insert({ DateBasedDocumentManager.doc_id: obj_id, 'oID': 515, 'T': 'Title', 'D': 'Description', 'Overwritten': True, 'Updated': True, 'Tg': [4, 5] }), obj_id) self.assertEqual(self.bb.count(), expected) self.assertEqual([x for x in self.bb.find(query={'Updated': True}) ][0]['_id'], obj_id) self.assertEqual(self.bb.count(query={'Overwritten': False}), 0) self.assertEqual([ x for x in self.bb.find(query={'Overwritten': True}) ][0]['_id'], obj_id) self.assertEqual([x for x in self.bb.find(tags=[1, 2, 3, 4, 5]) ][0]['_id'], obj_id) # Insert a document without an id and generate one self.assertEqual( self.bb.insert({ 'Blank_id': True }).generation_time.date(), datetime.now().date()) def test_update(self): obj_id = self.bb.insert({ 'Overwritten': False, 'Inserted': True, 'Tg': [1, 2, 3] }) self.assertEqual( self.bb.update( obj_id, { 'Overwritten': True, 'Inserted': False, 'Fds': [104], 'Tg': [1, 4, 6] }), obj_id) # Fails due to bug if older version of mockmongo is used self.assertEqual([ x for x in self.bb.find(query={ 'Overwritten': True, 'Tg': [1, 2, 3, 4, 6] }) ][0]['_id'], obj_id) self.assertEqual(self.bb.update(obj_id, {'Fds': 111, 'Tg': 5}), obj_id) self.assertEqual([ x for x in self.bb.find(query={ 'Overwritten': True, 'Fds': [104, 111], 'Tg': [1, 2, 3, 4, 6, 5] }) ][0]['_id'], obj_id) def test_delete(self): obj_id = self.bb.insert({'Deleted': False}) expected = 11 with self.assertRaises(PermissionError): self.bb.delete(obj_id) self.assertEqual(self.bb.count(), expected) # Add test as admin self.api = BlackboardAPI(mock_data_generator.admin_settings(), MongoClient=mock_data_generator.mock_client) self.bb = self.api.load_blackboard('ARTICLE') obj_id = self.bb.insert({'Deleted': False}) expected = 11 self.assertEqual(self.bb.count(), expected) self.assertEqual(self.bb.count(query={'Deleted': False}), 1) self.bb.delete(obj_id) self.assertEqual(self.bb.count(query={'Deleted': False}), 0) self.assertEqual(self.bb.count(), expected - 1) def test_add_tag(self): obj_id = self.bb.insert({'hasTags': False}) self.bb.add_tag(obj_id, 1) self.assertEqual(self.bb.count(tags=[1]), 3) self.bb.add_tag(obj_id, [2]) self.assertEqual(self.bb.count(tags=[1, 2]), 2) self.bb.add_tag(obj_id, [3, 4]) self.assertEqual([x for x in self.bb.find(tags=[1, 2, 3]) ][0][DateBasedDocumentManager.doc_id], obj_id) self.bb.add_tag(obj_id, 4) self.assertEqual([x for x in self.bb.find(query={'Tg': [1, 2, 3, 4]}) ][0][DateBasedDocumentManager.doc_id], obj_id) self.bb.add_tag(obj_id, [6, 7]) self.assertEqual([ x for x in self.bb.find(query={'Tg': [1, 2, 3, 4, 6, 7]}) ][0][DateBasedDocumentManager.doc_id], obj_id) def test_remove_tag(self): obj_id = self.bb.insert({'hasTags': True, 'Tg': [1, 2, 3, 4, 5]}) result = self.bb.remove_tag(obj_id, 3) self.assertEqual(result, obj_id) self.assertEqual([x for x in self.bb.find(tags=[1, 2, 4, 5]) ][0][DateBasedDocumentManager.doc_id], obj_id) result = self.bb.remove_tag(obj_id, [1, 5]) self.assertEqual(result, obj_id) self.assertEqual([x for x in self.bb.find(query={'Tg': [2, 4]}) ][0][DateBasedDocumentManager.doc_id], obj_id) result = self.bb.remove_tag(obj_id, [2, 8]) self.assertEqual(result, obj_id) self.assertEqual([x for x in self.bb.find(query={'Tg': [4]}) ][0][DateBasedDocumentManager.doc_id], obj_id) def test_insert_tag(self): self.assertEqual(self.bb.insert_tag('Tag_Not_Control'), 13) self.assertEqual( self.bb.get_tag('Tag_Not_Control')[TagManager.tag_id], 13) self.assertEqual( self.bb.get_tag('Tag_Not_Control')[TagManager.tag_control], 0) self.assertEqual(self.bb.is_control_tag(13), False) self.assertEqual(self.bb.insert_tag('FOR>Tag_14'), 14) self.assertEqual(self.bb.get_tag('FOR>Tag_14')[TagManager.tag_id], 14) self.assertEqual( self.bb.get_tag('FOR>Tag_14')[TagManager.tag_control], 1) self.assertEqual(self.bb.is_control_tag(14), True) self.assertEqual(self.bb.is_inheritable_tag(14), False) with self.assertRaises(ValueError): self.bb.insert_tag('Tag_1') with self.assertRaises(ValueError): self.bb.insert_tag('FOR>Tag_14') with self.assertRaises(ValueError): self.bb.insert_tag('POST>Tag_12') def test_update_tag(self): self.bb.update_tag(1, "Tag_1_Renamed") self.assertEqual(self.bb.get_tag(1)['Nm'], "Tag_1_Renamed") self.bb.update_tag(1, "Tag_1_Renamed", True) self.assertEqual(self.bb.get_tag(1)['DInh'], 1) self.bb.update_tag(1, "FOR>Tag_1_Renamed", True) self.assertEqual(self.bb.get_tag(1)['Ctrl'], 1) with self.assertRaises(ValueError): self.bb.update_tag(3, "FOR>Tag_1_Renamed") def test_delete_tag(self): with self.assertRaises(PermissionError): self.bb.delete_tag(1) self.assertEqual(self.bb.get_tag(1)['Nm'], 'Tag_1') # Add test as admin self.api = BlackboardAPI(mock_data_generator.admin_settings(), MongoClient=mock_data_generator.mock_client) self.bb = self.api.load_blackboard('ARTICLE') obj_id = self.bb.insert_tag('To_be_deleted') expected = 11 self.assertEqual( self.bb.get_tag('To_be_deleted')['Nm'], 'To_be_deleted') self.assertEqual(self.bb.delete_tag(obj_id)['n'], 1) self.assertEqual(self.bb.get_tag('To_be_deleted'), None) self.assertEqual(self.bb.count(tags=[2]), 2) self.assertEqual(self.bb.delete_tag(2)['n'], 1) with self.assertRaises(ValueError): self.bb.count(tags=[2]) def test_bb_get_tag(self): # Bad input self.assertEqual(self.bb.get_tag(55), None) self.assertEqual(self.bb.get_tag('3'), None) self.assertEqual(self.bb.get_tag(3333), None) # Retrieval self.assertEqual(self.bb.get_tag(1), { 'Ctrl': 0, '_id': 1, 'Nm': 'Tag_1' }) self.assertEqual(self.bb.get_tag(2), { 'Ctrl': 0, '_id': 2, 'Nm': 'Tag_2' }) self.assertEqual(self.bb.get_tag('Tag_3'), { 'Ctrl': 0, '_id': 3, 'Nm': 'Tag_3' }) self.assertEqual(self.bb.get_tag(12), { 'Ctrl': 1, 'Nm': 'POST>Tag_12', '_id': 12 }) def test_bb_tag_has_property(self): # Bad input for control self.assertEqual(self.bb.is_control_tag(55), False) self.assertEqual(self.bb.is_control_tag('3'), False) # Bad input for inheritance self.assertEqual(self.bb.is_inheritable_tag(55), False) self.assertEqual(self.bb.is_inheritable_tag('3'), False) # Checking for control self.assertEqual(self.bb.is_control_tag(11), True) self.assertEqual(self.bb.is_control_tag('FOR>Tag_11'), True) self.assertEqual(self.bb.is_control_tag(4), False) self.assertEqual(self.bb.is_control_tag('Tag_4'), False) # Checking for inheritance (should add True test) self.assertEqual(self.bb.is_inheritable_tag(11), False) self.assertEqual(self.bb.is_inheritable_tag(4), False) self.assertEqual(self.bb.is_inheritable_tag(4), False) self.assertEqual(self.bb.is_inheritable_tag('Tag_4'), False) def test_bb_get_date(self): self.assertEqual( str( self.bb.get_date({ DateBasedDocumentManager.doc_id: ObjectId.from_datetime(dtparser.parse('21-10-2017')) })), '2017-10-21 00:00:00+00:00') with self.assertRaises(ValueError): self.bb.get_date({ 'different_id': ObjectId.from_datetime(dtparser.parse('21-10-2017')) }) with self.assertRaises(ValueError): self.bb.get_date({DateBasedDocumentManager.doc_id: 1}) def test_get_extremal_date(self): self.assertEqual(str(self.bb.get_earliest_date()), '2009-01-01 00:00:00+00:00') self.assertEqual(str(self.bb.get_latest_date()), '2018-01-01 00:00:00+00:00')
class TestBlackboardAPI(unittest.TestCase): def teatDown(self): del self.api def test_api_load_blackboard(self): self.api = BlackboardAPI(mock_data_generator.settings(), MongoClient=mock_data_generator.mock_client) # Correctly called returns correct type self.assertIsInstance(self.api.load_blackboard('ARTICLE'), DateBasedBlackboard) self.assertIsInstance(self.api.load_blackboard('ARTICLE', date_based=True), DateBasedBlackboard) self.assertIsInstance(self.api.load_blackboard('FEED'), Blackboard) self.assertIsInstance(self.api.load_blackboard('FEED', date_based=False), Blackboard) # Incorrectly called raises error with self.assertRaises(ValueError): self.api.load_blackboard('ARTICLE_TAGS') with self.assertRaises(ValueError): self.api.load_blackboard('article_counter') with self.assertRaises(ValueError): self.api.load_blackboard('FEED', date_based=True) with self.assertRaises(ValueError): self.api.load_blackboard('ARTICLE', date_based=False) with self.assertRaises(ValueError): self.api.load_blackboard('ARTICLE2', date_based=False) def test_api_drop_blackboard(self): # Non-admin user self.api = BlackboardAPI(mock_data_generator.settings(), MongoClient=mock_data_generator.mock_client) # Check they exist before attempting drop blackboards = [('ARTICLE', True), ('ARTICLE2', True), ('FEED', True), ('article', False)] for bb in blackboards: self.assertEqual(self.api.blackboard_exists(bb[0]), bb[1]) with self.assertRaises(ValueError): self.api.drop_blackboard('ARTICLE_TAGS') with self.assertRaises(ValueError): self.api.drop_blackboard('article_counter') with self.assertRaises(PermissionError): self.api.drop_blackboard('ARTICLE') with self.assertRaises(PermissionError): self.api.drop_blackboard('article') # Check they still exist after failed drop for bb in blackboards: self.assertEqual(self.api.blackboard_exists(bb[0]), bb[1]) # Test admin drop self.api = BlackboardAPI(mock_data_generator.admin_settings(), MongoClient=mock_data_generator.mock_client) self.api.drop_blackboard('ARTICLE') self.assertEqual(self.api.blackboard_exists(blackboards[0][0]), not blackboards[0][1]) for bb in blackboards[1:]: self.assertEqual(self.api.blackboard_exists(bb[0]), bb[1]) self.api.drop_blackboard('ARTICLE2') self.assertEqual(self.api.blackboard_exists('ARTICLE2'), False) self.api.drop_blackboard('FEED') self.assertEqual(self.api.blackboard_exists('FEED'), False) def test_api_get_blackboard_names(self): self.api = BlackboardAPI(mock_data_generator.settings(), MongoClient=mock_data_generator.mock_client) self.assertSetEqual(set(self.api.get_blackboard_names()), set(['FEED', 'ARTICLE', 'ARTICLE2'])) def test_api_get_blackboard_type(self): self.api = BlackboardAPI(mock_data_generator.settings(), MongoClient=mock_data_generator.mock_client) # Test getting date-based self.assertEqual(self.api.get_blackboard_type('ARTICLE'), CounterManager.counter_type_date_based) self.assertEqual(self.api.get_blackboard_type('ARTICLE', date_based=True), CounterManager.counter_type_date_based) with self.assertRaises(ValueError): self.api.get_blackboard_type('ARTICLE', date_based=False) # Test getting standard self.assertEqual(self.api.get_blackboard_type('FEED'), CounterManager.counter_type_standard) self.assertEqual(self.api.get_blackboard_type('FEED', date_based=False), CounterManager.counter_type_standard) with self.assertRaises(ValueError): self.api.get_blackboard_type('FEED', date_based=True) # Test getting a different date-based self.assertEqual(self.api.get_blackboard_type('ARTICLE2'), CounterManager.counter_type_date_based) # Test getting a non-existing blackboard self.assertEqual(self.api.get_blackboard_type('MISSING'), None) self.assertEqual(self.api.get_blackboard_type('MISSING', date_based=True), CounterManager.counter_type_date_based) self.assertEqual(self.api.get_blackboard_type('MISSING', date_based=False), CounterManager.counter_type_standard) def test_setting_validation(self): settings = {'user' : 'dbadmin', 'dbname' : 'testdb', 'dburl' : 'mongodb://localhost:27017'} with self.assertRaises(ValueError): self.api = BlackboardAPI(settings, MongoClient=mock_data_generator.mock_client) settings = {'user' : 'dbadmin', 'dog' : 'dog', 'dbname' : 'testdb', 'dburl' : 'mongodb://localhost:27017'} with self.assertRaises(ValueError): self.api = BlackboardAPI(settings, MongoClient=mock_data_generator.mock_client)