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='metaopt_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='metaopt_test', username='******', password='******') assert "Authentication" in str(exc_info.value)
def test_bad_authentication(self): """Raise when authentication cannot be achieved.""" with pytest.raises(DatabaseError) as exc_info: MongoDB(name='metaopt_test', username='******', password='******') assert "Authentication" in str(exc_info.value)
def test_connection_with_uri(self): """Check the case when connecting with ready `uri`.""" moptdb = MongoDB('mongodb://*****:*****@localhost/metaopt_test') assert moptdb.host == 'localhost' assert moptdb.port == 27017 assert moptdb.username == 'user' assert moptdb.password == 'pass' assert moptdb.name == 'metaopt_test'
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='metaopt', username='******', password='******') assert "Name or service not known" in str(exc_info.value) monkeypatch.undo() # Verify that the wrapper converts it properly to DatabaseError with pytest.raises(DatabaseError) as exc_info: MongoDB(host='asdfada', port=123, name='metaopt', username='******', password='******') assert "Connection" in str(exc_info.value)
def test_singleton(self): """Test that MongoDB class is a singleton.""" moptdb = MongoDB(username='******', password='******', name='metaopt_test') # reinit connection does not change anything moptdb.initiate_connection() moptdb.close_connection() assert MongoDB() is moptdb
def test_bad_connection(self): """Raise when connection cannot be achieved.""" with pytest.raises(DatabaseError) as exc_info: MongoDB(host='asdfada', port=123, name='metaopt', username='******', password='******') assert "Connection" in str(exc_info.value)
def test_overwrite_uri(self): """Check the case when connecting with ready `uri`.""" moptdb = MongoDB('mongodb://*****:*****@localhost:1231/metaopt', port=27017, name='metaopt_test', username='******', password='******') assert moptdb.host == 'localhost' assert moptdb.port == 27017 assert moptdb.username == 'user' assert moptdb.password == 'pass' assert moptdb.name == 'metaopt_test'
def test_instatiation_and_singleton(self): """Test create just one object, that object persists between calls.""" database = Database(of_type='MongoDB', name='metaopt_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)
def moptdb(): """Return MongoDB wrapper instance initiated with test opts.""" MongoDB.instance = None moptdb = MongoDB(username='******', password='******', name='metaopt_test') return moptdb