async def test_resolve_rate(aresponses: ResponsesMockServer, ok_response: Dict[str, Any]): aresponses.add(TMDB_API_HOST, URL_PATH, "POST", response=ok_response) result = await resolve_rate(movie_id=MOVIE_ID, rating=1.0) assert result is True aresponses.assert_plan_strictly_followed()
async def test_resolve_rate_tmdb_exception(aresponses: ResponsesMockServer): aresponses.add(TMDB_API_HOST, URL_PATH, "POST", aresponses.Response(status=500)) with pytest.raises(HTTPException): await resolve_rate(movie_id=MOVIE_ID, rating=10.0) aresponses.assert_plan_strictly_followed()
async def test_batch_get_success( aresponses: ResponsesMockServer, context: Dict[str, Any], query_paths: List[str], json_companies: List[Dict[str, Any]], company_keys: List[int], ): for path, json_company in zip(query_paths, json_companies): aresponses.add(TMDB_API_HOST, path, "GET", response=json_company) company_loader = CompanyLoader.for_context(context) companies = await company_loader.load_many(company_keys) assert companies == [Company(**jc) for jc in json_companies] aresponses.assert_plan_strictly_followed()
async def test_batch_get_success( aresponses: ResponsesMockServer, context: Dict[str, Any], query_paths: List[str], db_movies: List[Movie], json_movies: List[Dict[str, Any]], movie_keys: List[int], mocker: MockFixture, ): from movies.core.dataloaders import movie_loader mocker.patch.object(movie_loader.crud, "get_movies").return_value = db_movies for path, json_movie in zip(query_paths, json_movies): aresponses.add(TMDB_API_HOST, path, "GET", response=json_movie) movie_loader = MovieLoader.for_context(context) movies = await movie_loader.load_many(movie_keys) assert movies == [EntityMovie(**jm) for jm in json_movies] aresponses.assert_plan_strictly_followed()
async def test_run(app, client, m_mqtt, aresponses: ResponsesMockServer): feature = publish_example.PublishingFeature(app) # We mock this specific URL # This tests our code in more detail than setting a generic mock on `session.get()` # It also makes it easier to test functions that make multiple HTTP requests. aresponses.add( host_pattern='jsonplaceholder.typicode.com', path_pattern='/todos/1', method_pattern='GET', response={'hello': 'world'}, ) # We don't want to wait the actual poll interval during tests app['config']['poll_interval'] = 0.0001 # We expect these values to be available in config topic = app['config']['history_topic'] name = app['config']['name'] await feature.prepare() await feature.run() # We mocked the response to the HTTP request, # and we mocked the `mqtt.publish()` function. # We expect publish() to be called with the mock data. m_mqtt.publish.assert_awaited_once_with( app, topic, { 'key': name, 'data': { 'hello': 'world' }, }, ) # ... and we expect the mocked requests to have been used aresponses.assert_plan_strictly_followed()
async def test_batch_get_partial_failure( aresponses: ResponsesMockServer, context: Dict[str, Any], query_paths: List[str], json_companies: List[Dict[str, Any]], company_keys: List[int], ): # The request for company_id=2 will fail with a 500. aresponses.add(TMDB_API_HOST, query_paths[0], "GET", response=json_companies[0]) aresponses.add( TMDB_API_HOST, query_paths[1], "GET", aresponses.Response(status=500) ) aresponses.add(TMDB_API_HOST, query_paths[2], "GET", response=json_companies[2]) company_loader = CompanyLoader.for_context(context) companies = await company_loader.load_many(company_keys) assert companies == [ Company(**json_companies[0]), None, Company(**json_companies[2]), ] aresponses.assert_plan_strictly_followed()