async def test_start(consumer_thread): connection = mock.MagicMock() consumer_thread.set_connection(connection) with mock.patch("threading.Thread.start") as Thread: consumer_thread.start_event = AsyncMock() await consumer_thread.start() Thread.assert_called_once()
async def test_get_confluent_kafka_settings(): kafka_resource = KafkaResource() settings = mock.MagicMock() settings.get = AsyncMock() kafka_resource.settings = settings settings_response = await kafka_resource.get_confluent_kafka_settings() assert isinstance(settings_response, dict)
async def test_stop_twice(consumer_thread): connection = mock.MagicMock() consumer_thread.set_connection(connection) consumer_thread.stop_event = AsyncMock() consumer_thread._started = True await consumer_thread.stop() with pytest.raises(Exception): await consumer_thread.stop()
async def test_stop(consumer_thread): connection = mock.MagicMock() consumer_thread.set_connection(connection) consumer_thread._started = True consumer_thread.stop_event = AsyncMock() await consumer_thread.stop() assert not consumer_thread._started assert consumer_thread._stop.is_set()
async def test_start_twice(consumer_thread): connection = mock.MagicMock() consumer_thread.set_connection(connection) with mock.patch("threading.Thread.start"): consumer_thread.start_event = AsyncMock() await consumer_thread.start() with pytest.raises(Exception): await consumer_thread.start()
async def mock_http_client(http_client_mock, poesessid, profile_name) -> PoeHttpClient: http_client = http_client_mock.return_value http_client.shutdown = AsyncMock() yield http_client http_client_mock.assert_called_once_with(poesessid, profile_name, ANY) http_client.shutdown.assert_called_once_with()
async def test_auth_failed_no_credentials(auth_params, poe_plugin_mock, http_client_mock): http_client_mock.return_value.shutdown = AsyncMock() with pytest.raises(InvalidCredentials): assert await poe_plugin_mock.pass_login_credentials(*auth_params) http_client_mock.assert_not_called() http_client_mock.return_value.shutdown.assert_not_called()
def setUp(self) -> None: super().setUp() self.dialog = storage.Dialog( id=1, customer=storage.Customer(name="Анатолий")) self.gateway_stub = MagicMock() self.gateway_stub.handle_message = AsyncMock( return_value=storage.Message(dialog=self.dialog)) repository.register_gateway("example", self.gateway_stub)
async def wrapper_test(): insert_one.return_value = AsyncMock(return_value=Mock(inserted_id=ObjectId('5c19d2fe7aca19816f57b285'))) class Payment(self.engine.Document): __collection_name__ = 'payments' _id = MongoId() amount = MongoNumber() payment = Payment(amount=30) await self.engine.save(payment) self.assertEqual(payment._id, '5c19d2fe7aca19816f57b285')
def __init__(self, collection_name, **kwargs): super().__init__() self.name = collection_name find_one_result = kwargs.get("find_one_result", {"_id": 1}) inserted_id = kwargs.get("inserted_id", 1) create_indexes_result = kwargs.get("create_indexes_result", None) self.insert_one = AsyncMock(return_value=InsertOneResult( inserted_id=inserted_id, acknowledged=True)) self.delete_many = AsyncMock( return_value=DeleteResult(raw_result={}, acknowledged=True)) self.update_one = AsyncMock( return_value=UpdateResult(raw_result={}, acknowledged=True)) self.update_many = AsyncMock( return_value=UpdateResult(raw_result={}, acknowledged=True)) self.count_documents = AsyncMock(return_value=1) self.find_one = AsyncMock(return_value=find_one_result) self.create_indexes = AsyncMock(return_value=create_indexes_result)
def test_Document_with_ForeignKey_serializes_as_string_after_saving( self, insert_one): insert_one.return_value = AsyncMock(return_value=Mock( inserted_id=ObjectId('5c19d2fe7aca19816f57b285'))) async def wrapper_test(): class User(self.engine.Document): __collection_name__ = 'users' _id = MongoId() name = MongoString() address = MongoForeignKey() user = User(address='5c19ce717aca19800a01af9d') await self.engine.save(user) self.assertEqual( user.as_dict(), { '_id': '5c19d2fe7aca19816f57b285', 'name': None, 'address': '5c19ce717aca19800a01af9d' }) asyncio.get_event_loop().run_until_complete(wrapper_test())
def test_Document_with_MongoObject_as_dict_with_default(self, insert_one): insert_one.return_value = AsyncMock(return_value=Mock( inserted_id=ObjectId('5c19d2fe7aca19816f57b285'))) async def wrapper_test(): class Address(MongoObject): street = MongoString(default='Circle street') house_number = MongoNumber() class User(self.engine.Document): __collection_name__ = 'users' _id = MongoId() name = MongoString(default='Tammy') address = Address() user = User(address=Address.new(house_number=42)) self.assertEqual( user.as_dict(), { '_id': None, 'name': 'Tammy', 'address': { 'street': 'Circle street', 'houseNumber': 42 } }) await self.engine.save(user) self.assertEqual( user.as_dict(), { '_id': '5c19d2fe7aca19816f57b285', 'name': 'Tammy', 'address': { 'street': 'Circle street', 'houseNumber': 42 } }) asyncio.get_event_loop().run_until_complete(wrapper_test())
assert response["code"] == 403 assert response["fields"] == [] def test_invalid_auth_header_keyword(): with TestClient(app) as client: response = client.get("/me/", headers={"Authorization": f"Bearer asd"}) assert response.status_code == 403 response = response.json() assert response["code"] == 403 assert response["fields"] == [] @patch( "fastapi_contrib.auth.models.User.get", new=AsyncMock(return_value=User(username="******")), ) @patch( "fastapi_contrib.auth.models.Token.get", new=AsyncMock(return_value=None), ) def test_invalid_token(): with TestClient(app) as client: response = client.get("/me/", headers={"Authorization": f"Token t"}) assert response.status_code == 200 response = response.json() assert response["username"] is None @patch( "fastapi_contrib.auth.models.User.get",
async def test_register_in_etcd(registry): registry.etcd_client = AsyncMock() registry.etcd_client.set = AsyncMock() await registry.register_in_etcd("key") registry.etcd_client.set.assert_called_once_with( key="key", value=registry.own_ip, ttl=ETCD_REGISTRY_TTL_SECONDS)
class k8s_manager: create_custom_object = AsyncMock() update_custom_object = AsyncMock() get_custom_object = AsyncMock() delete_custom_object = AsyncMock()