Beispiel #1
0
    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)
Beispiel #2
0
 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')
Beispiel #3
0
    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)
Beispiel #4
0
def load(settings_path):
    with open(settings_path) as f:
        settings = json.load(f)

    bbapi = BlackboardAPI(settings)
    db = bbapi._BlackboardAPI__db

    return bbapi, db
Beispiel #5
0
 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)
Beispiel #6
0
    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)
Beispiel #7
0
    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)
Beispiel #8
0
    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])
Beispiel #9
0
 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']))
Beispiel #10
0
 def setUp(self):
     self.api = BlackboardAPI(mock_data_generator.settings(),
                              MongoClient=mock_data_generator.mock_client)
     self.bb = self.api.load_blackboard('ARTICLE')