Beispiel #1
0
class TestMongoApiLiveAndDownFeaturesWithMongoOffline(unittest.TestCase):
    def setUp(self) -> None:
        self.logger = logging.getLogger('dummy')
        self.db = TestUserConf.mongo_db_name
        self.host = TestUserConf.mongo_host
        self.port = TestUserConf.mongo_port
        self.live_check_time_interval = timedelta(seconds=3)
        self.live_check_time_interval_with_error_margin = timedelta(
            seconds=3.5)
        self.mongo = MongoApi(
            self.logger,
            self.db,
            self.host,
            self.port,
            live_check_time_interval=self.live_check_time_interval)

    def test_is_live_returns_true_by_default(self):
        self.assertTrue(self.mongo.is_live)

    def test_set_as_live_changes_is_live_to_true(self):
        self.mongo._is_live = False
        self.assertFalse(self.mongo.is_live)

        self.mongo._set_as_live()
        self.assertTrue(self.mongo._is_live)

    def test_set_as_live_leaves_is_live_as_true_if_already_true(self):
        self.mongo._is_live = True
        self.assertTrue(self.mongo.is_live)

        self.mongo._set_as_live()
        self.assertTrue(self.mongo._is_live)

    def test_set_as_down_changes_is_live_to_false(self):
        self.mongo._set_as_down()
        self.assertFalse(self.mongo.is_live)

    def test_set_as_down_leaves_is_live_as_false_if_already_false(self):
        self.mongo._is_live = False
        self.assertFalse(self.mongo.is_live)

        self.mongo._set_as_down()
        self.assertFalse(self.mongo.is_live)

    def test_allowed_to_use_by_default(self):
        # noinspection PyBroadException
        try:
            self.mongo._do_not_use_if_recently_went_down()
        except Exception:
            self.fail('Expected to be allowed to use Mongo.')

    def test_not_allowed_to_use_if_set_as_down_and_within_time_interval(self):
        self.mongo._set_as_down()
        # noinspection PyBroadException
        try:
            self.mongo._do_not_use_if_recently_went_down()
            self.fail('Expected to not be allowed to use Mongo.')
        except Exception:
            pass

    def test_allowed_to_use_if_set_as_down_and_within_time_interval(self):
        self.mongo._set_as_down()
        sleep(self.live_check_time_interval_with_error_margin.seconds)
        # noinspection PyBroadException
        try:
            self.mongo._do_not_use_if_recently_went_down()
        except Exception:
            self.fail('Expected to be allowed to use Mongo.')
Beispiel #2
0
class TestMongoApiWithMongoOffline(unittest.TestCase):
    def setUp(self) -> None:
        self.logger = logging.getLogger('dummy')
        self.db = TestUserConf.mongo_db_name
        self.host = 'dummyhost'
        self.port = TestUserConf.mongo_port
        self.user = TestUserConf.mongo_user
        self.password = TestUserConf.mongo_pass
        self.mongo = MongoApi(self.logger,
                              self.db,
                              self.host,
                              self.port,
                              timeout_ms=1)
        # timeout_ms is set to 1ms to speed up tests. It cannot be 0 :p

        self.col1 = 'collection1'
        self.val1 = {'a': 'b', 'c': 'd'}
        self.val2 = {'e': 'f', 'g': 'h'}
        self.val3 = {'i': 'j'}

    def test_insert_one_throws_exception_first_time_round(self):
        try:
            self.mongo.insert_one(self.col1, self.val1)
            self.fail('Expected ServerSelectionTimeoutError to be thrown.')
        except ServerSelectionTimeoutError:
            pass

    def test_insert_many_throws_exception_first_time_round(self):
        try:
            self.mongo.insert_many(self.col1,
                                   [self.val1, self.val2, self.val3])
            self.fail('Expected ServerSelectionTimeoutError to be thrown.')
        except ServerSelectionTimeoutError:
            pass

    def test_get_all_throws_exception_first_time_round(self):
        try:
            self.mongo.get_all(self.col1)
            self.fail('Expected ServerSelectionTimeoutError to be thrown.')
        except ServerSelectionTimeoutError:
            pass

    def test_drop_collection_throws_exception_first_time_round(self):
        try:
            self.mongo.drop_collection(self.col1)
            self.fail('Expected ServerSelectionTimeoutError to be thrown.')
        except ServerSelectionTimeoutError:
            pass

    def test_drop_db_throws_exception_first_time_round(self):
        try:
            self.mongo.drop_db()
            self.fail('Expected ServerSelectionTimeoutError to be thrown.')
        except ServerSelectionTimeoutError:
            pass

    def test_ping_unsafe_throws_exception_first_time_round(self):
        try:
            self.mongo.ping_unsafe()
            self.fail('Expected ServerSelectionTimeoutError to be thrown.')
        except ServerSelectionTimeoutError:
            pass

    def test_ping_auth_throws_exception_first_time_round(self):
        try:
            self.mongo.ping_auth(username=self.user, password=self.password)
            self.fail('Expected ServerSelectionTimeoutError to be thrown.')
        except ServerSelectionTimeoutError:
            pass

    def test_insert_one_returns_none_if_mongo_already_down(self):
        self.mongo._set_as_down()
        self.assertIsNone(self.mongo.insert_one(self.col1, self.val1))

    def test_insert_many_returns_none_if_mongo_already_down(self):
        self.mongo._set_as_down()
        documents = [self.val1, self.val2, self.val3]
        self.assertIsNone(self.mongo.insert_many(self.col1, documents))

    def test_get_all_returns_none_if_mongo_already_down(self):
        self.mongo._set_as_down()
        self.assertIsNone(self.mongo.get_all(self.col1))

    def test_drop_collection_returns_none_if_mongo_already_down(self):
        self.mongo._set_as_down()
        self.assertIsNone(self.mongo.drop_collection(self.col1))

    def test_drop_db_returns_none_if_mongo_already_down(self):
        self.mongo._set_as_down()
        self.assertIsNone(self.mongo.drop_db())

    def test_ping_unsafe_throws_exception_if_mongo_already_down(self):
        self.mongo._set_as_down()
        try:
            self.mongo.ping_unsafe()
            self.fail('Expected ServerSelectionTimeoutError to be thrown.')
        except ServerSelectionTimeoutError:
            pass

    def test_ping_auth_throws_exception_if_mongo_already_down(self):
        self.mongo._set_as_down()
        try:
            self.mongo.ping_auth(username=self.user, password=self.password)
            self.fail('Expected ServerSelectionTimeoutError to be thrown.')
        except ServerSelectionTimeoutError:
            pass