Example #1
0
    def test_bad_connection(self, monkeypatch):
        """Raise when connection cannot be achieved."""
        monkeypatch.setattr(MongoDB, "initiate_connection",
                            MongoDB.initiate_connection.__wrapped__)
        with pytest.raises(pymongo.errors.ConnectionFailure) as exc_info:
            MongoDB(
                host="asdfada",
                port=123,
                name="orion",
                username="******",
                password="******",
            )

        monkeypatch.undo()

        # Verify that the wrapper converts it properly to DatabaseError
        with pytest.raises(DatabaseError) as exc_info:
            MongoDB(
                host="asdfada",
                port=123,
                name="orion",
                username="******",
                password="******",
            )
        assert "Connection" in str(exc_info.value)
Example #2
0
    def test_bad_authentication(self, monkeypatch):
        """Raise when authentication cannot be achieved."""
        monkeypatch.setattr(MongoDB, "initiate_connection",
                            MongoDB.initiate_connection.__wrapped__)
        with pytest.raises(pymongo.errors.OperationFailure) as exc_info:
            MongoDB(name='orion_test', username='******', password='******')
        assert any(m in str(exc_info.value) for m in AUTH_FAILED_MESSAGES)

        monkeypatch.undo()

        with pytest.raises(DatabaseError) as exc_info:
            MongoDB(name='orion_test', username='******', password='******')
        assert "Authentication" in str(exc_info.value)
Example #3
0
 def test_singleton(self):
     """Test that MongoDB class is a singleton."""
     orion_db = MongoDB(
         "mongodb://localhost",
         port=27017,
         name="orion_test",
         username="******",
         password="******",
     )
     # reinit connection does not change anything
     orion_db.initiate_connection()
     orion_db.close_connection()
     assert MongoDB() is orion_db
Example #4
0
    def test_overwrite_partial_uri(self, monkeypatch):
        """Check the case when connecting with partial `uri`."""
        monkeypatch.setattr(MongoDB, 'initiate_connection', lambda self: None)

        orion_db = MongoDB('mongodb://localhost',
                           port=1231,
                           name='orion',
                           username='******',
                           password='******')
        orion_db._sanitize_attrs()
        assert orion_db.host == 'localhost'
        assert orion_db.port == 1231
        assert orion_db.username == 'lala'
        assert orion_db.password == 'none'
        assert orion_db.name == 'orion'
Example #5
0
 def test_change_server_timeout(self):
     """Test that the server timeout is correctly changed."""
     assert timeit(lambda: MongoDB(username='******',
                                   password='******',
                                   name='orion_test',
                                   serverSelectionTimeoutMS=1000),
                   number=1) <= 2
Example #6
0
 def test_connection_with_uri(self):
     """Check the case when connecting with ready `uri`."""
     orion_db = MongoDB('mongodb://*****:*****@localhost/orion_test')
     assert orion_db.host == 'localhost'
     assert orion_db.port == 27017
     assert orion_db.username == 'user'
     assert orion_db.password == 'pass'
     assert orion_db.name == 'orion_test'
Example #7
0
 def test_connection_with_uri(self):
     """Check the case when connecting with ready `uri`."""
     orion_db = MongoDB("mongodb://*****:*****@localhost/orion_test")
     assert orion_db.host == "localhost"
     assert orion_db.port == 27017
     assert orion_db.username == "user"
     assert orion_db.password == "pass"
     assert orion_db.name == "orion_test"
Example #8
0
    def test_overwrite_partial_uri(self, monkeypatch):
        """Check the case when connecting with partial `uri`."""
        monkeypatch.setattr(MongoDB, "initiate_connection", lambda self: None)

        orion_db = MongoDB(
            "mongodb://localhost",
            port=1231,
            name="orion",
            username="******",
            password="******",
        )
        orion_db._sanitize_attrs()
        assert orion_db.host == "localhost"
        assert orion_db.port == 1231
        assert orion_db.username == "lala"
        assert orion_db.password == "none"
        assert orion_db.name == "orion"
Example #9
0
 def test_singleton(self):
     """Test that MongoDB class is a singleton."""
     orion_db = MongoDB(username='******', password='******', name='orion_test')
     # reinit connection does not change anything
     orion_db.initiate_connection()
     orion_db.close_connection()
     assert MongoDB() is orion_db
Example #10
0
 def test_overwrite_uri(self):
     """Check the case when connecting with ready `uri`."""
     orion_db = MongoDB('mongodb://*****:*****@localhost:27017/orion_test',
                        port=1231, name='orion', username='******',
                        password='******')
     assert orion_db.host == 'localhost'
     assert orion_db.port == 27017
     assert orion_db.username == 'user'
     assert orion_db.password == 'pass'
     assert orion_db.name == 'orion_test'
Example #11
0
 def test_change_server_timeout(self):
     """Test that the server timeout is correctly changed."""
     assert (timeit(
         lambda: MongoDB(
             username="******",
             password="******",
             name="orion_test",
             serverSelectionTimeoutMS=1000,
         ),
         number=1,
     ) <= 2)
Example #12
0
    def test_instatiation_and_singleton(self):
        """Test create just one object, that object persists between calls."""
        database = Database(of_type='MongoDB', name='orion_test',
                            username='******', password='******')

        assert isinstance(database, MongoDB)
        assert database is MongoDB()
        assert database is Database()
        with pytest.raises(ValueError) as exc_info:
            Database('fire', [], {'it_matters': 'it\'s singleton'})
        assert 'singleton' in str(exc_info.value)
Example #13
0
    def test_instantiation_and_singleton(self):
        """Test create just one object, that object persists between calls."""
        database = Database(
            of_type="MongoDB", name="orion_test", username="******", password="******"
        )

        assert isinstance(database, MongoDB)
        assert database is MongoDB()
        assert database is Database()

        with pytest.raises(SingletonAlreadyInstantiatedError):
            Database("fire", [], {"it_matters": "it's singleton"})
Example #14
0
 def test_overwrite_uri(self):
     """Check the case when connecting with ready `uri`."""
     orion_db = MongoDB(
         "mongodb://*****:*****@localhost:27017/orion_test",
         port=1231,
         name="orion",
         username="******",
         password="******",
     )
     assert orion_db.host == "localhost"
     assert orion_db.port == 27017
     assert orion_db.username == "user"
     assert orion_db.password == "pass"
     assert orion_db.name == "orion_test"
Example #15
0
def orion_db():
    """Return MongoDB wrapper instance initiated with test opts."""
    MongoDB.instance = None
    orion_db = MongoDB(username='******', password='******', name='orion_test')
    return orion_db
Example #16
0
def orion_db():
    """Return MongoDB wrapper instance initiated with test opts."""
    MongoDB.instance = None
    orion_db = MongoDB(username="******", password="******", name="orion_test")
    return orion_db