def test_add_characters_films(self): char_url = utils.generate_url("people") char_insert_data = [("test character 1", char_url(1)), ("test character 2", char_url(2)), ("test character 3", char_url(3))] film_url = utils.generate_url("films") film_insert_data = [("test film 1", film_url(1)), ("test film 2", film_url(2)), ("test film 3", film_url(3))] characters_films_insert_data = [(character[0], character[1], film_insert_data) for character in char_insert_data] with contextlib.closing(db_utils.open_db(True)) as conn: task_one.add_characters_films(conn, characters_films_insert_data, char_insert_data, film_insert_data) with contextlib.closing(conn.cursor()) as cursor: for row in char_insert_data: character = db_utils.get_character(cursor, row[0]) self.assertEqual(character.get("name"), row[0]) self.assertEqual(character.get("endpoint"), row[1]) for row in film_insert_data: film = db_utils.get_film(cursor, row[0]) self.assertEqual(film.get("title"), row[0]) self.assertEqual(film.get("endpoint"), row[1])
def test_main(self): char_url = utils.generate_url("people") film_url = utils.generate_url("films") mock_char_res = { "name": "test people", "url": char_url(100), "films": [ film_url(100), film_url(200), film_url(300), ] } mock_film_res = {"title": "test film", "url": film_url(100)} with responses.RequestsMock() as rsps: rsps.add(responses.GET, re.compile(char_url("[0-9]*")), json=mock_char_res, status=200) rsps.add(responses.GET, re.compile(film_url("[0-9]*")), json=mock_film_res, status=200) json_output = task_one.main() rsps.reset() self.assertIsInstance(json_output, str) dict_output = json.loads(json_output) self.assertIsInstance(dict_output, list) for row in dict_output: self.assertIsInstance(row, dict) self.assertIn("film", row) self.assertIn("character", row) self.assertIsInstance(row["film"], str) self.assertIsInstance(row["character"], list)
def test_format_film_data(self): film_url = utils.generate_url("films")(1) with responses.RequestsMock() as rsps: rsps.add(responses.GET, film_url, json=mock_film_res, status=200) rsps.add(responses.GET, mock_film_res[task_two.CHARACTERS][0], json=mock_char_res, status=200) rsps.add(responses.GET, mock_film_res[task_two.PLANETS][0], json=mock_planet_res, status=200) rsps.add(responses.GET, mock_film_res[task_two.SPECIES][0], json=mock_species_res, status=200) rsps.add(responses.GET, mock_film_res[task_two.STARSHIPS][0], json=mock_starship_res, status=200) rsps.add(responses.GET, mock_film_res[task_two.VEHICLES][0], json=mock_vehicles_res, status=200) resp = task_two.format_film_data(copy.deepcopy(mock_film_res)) rsps.reset() self.assertIsInstance(resp, dict) self.assertIn("weight", resp[task_two.CHARACTERS][0]) self.assertNotIn("mass", resp[task_two.CHARACTERS][0]) self.assertEqual(resp[task_two.CHARACTERS][0]["height"], utils.convert_height(mock_char_res["height"])) self.assertEqual(resp[task_two.CHARACTERS][0]["weight"], utils.convert_weight(mock_char_res["mass"]))
def test_get_data(self): url = utils.generate_url("people")(1) with responses.RequestsMock() as rsps: rsps.add(responses.GET, url, json={"test": "tester"}, status=200) resp = utils.get_data(url) rsps.reset() self.assertEqual(resp, {"test": "tester"})
def test_get_film(self): url = utils.generate_url("films")(1) with responses.RequestsMock() as rsps: rsps.add(responses.GET, url, json=mock_film_res, status=200) resp = task_two.get_film(1) rsps.reset() self.assertEqual(resp, mock_film_res)
def test_get_characters_films(self): char_url = utils.generate_url("people")(1) film_url = utils.generate_url("films") mock_char_res = { "name": "test people", "url": char_url, "films": [ film_url(1), film_url(2), film_url(3), ] } mock_film_resps = [{ "title": "test film 1", "url": film_url(1) }, { "title": "test film 2", "url": film_url(2) }, { "title": "test film 3", "url": film_url(3) }] with responses.RequestsMock() as rsps: rsps.add(responses.GET, char_url, json=mock_char_res, status=200) rsps.add(responses.GET, film_url(1), json=mock_film_resps[0], status=200) rsps.add(responses.GET, film_url(2), json=mock_film_resps[1], status=200) rsps.add(responses.GET, film_url(3), json=mock_film_resps[2], status=200) resp = task_one.get_characters_films(1) rsps.reset() expected_result = (mock_char_res.get("name"), mock_char_res.get("url"), [(mock_res.get("title"), mock_res.get("url")) for mock_res in mock_film_resps]) self.assertEqual(resp, expected_result)
def test_get_film_data(self): url = utils.generate_url("films")(1) mock_res = {"title": "test film", "url": url} with responses.RequestsMock() as rsps: rsps.add(responses.GET, url, json=mock_res, status=200) resp = task_one.get_film_data(url) rsps.reset() expected_result = (mock_res.get("title"), mock_res.get("url")) self.assertEqual(resp, expected_result)
def get_characters_films(character_resource_id): """Retrieves the character's name, url, and their list of films""" endpoint = utils.generate_url("people")(character_resource_id) res = utils.get_data(endpoint) if not res: return film_endpoints = res.get("films") films = [] if film_endpoints: films = [ get_film_data(film_endpoint) for film_endpoint in film_endpoints ] return res.get("name"), res.get("url"), utils.remove_none(films)
def test_main(self): film_url = utils.generate_url("films")(1) with responses.RequestsMock() as rsps: rsps.add(responses.GET, film_url, json=mock_film_res, status=200) rsps.add(responses.GET, mock_film_res[task_two.CHARACTERS][0], json=mock_char_res, status=200) rsps.add(responses.GET, mock_film_res[task_two.PLANETS][0], json=mock_planet_res, status=200) rsps.add(responses.GET, mock_film_res[task_two.SPECIES][0], json=mock_species_res, status=200) rsps.add(responses.GET, mock_film_res[task_two.STARSHIPS][0], json=mock_starship_res, status=200) rsps.add(responses.GET, mock_film_res[task_two.VEHICLES][0], json=mock_vehicles_res, status=200) json_output = task_two.main() rsps.reset() self.assertIsInstance(json_output, str) dict_output = json.loads(json_output) self.assertIsInstance(dict_output, dict) self.assertIn("weight", dict_output[task_two.CHARACTERS][0]) self.assertNotIn("mass", dict_output[task_two.CHARACTERS][0]) self.assertEqual(dict_output[task_two.CHARACTERS][0]["height"], utils.convert_height(mock_char_res["height"])) self.assertEqual(dict_output[task_two.CHARACTERS][0]["weight"], utils.convert_weight(mock_char_res["mass"])) parent_dir = pathlib.Path(__file__).parent.parent.parent filepath = parent_dir.joinpath("task_two.json") self.assertTrue(filepath.is_file())
def helper__insert_characters(cursor): url = utils.generate_url("people") insert_data = [("test 1", url(1)), ("test 2", url(2)), ("test 3", url(3))] db_utils.insert_characters(cursor, insert_data) return insert_data
def get_film(api_film_id=1): """Retrieves data from the film endpoint""" return utils.get_data(utils.generate_url("films")(api_film_id))
def test_generate_url(self): self.assertEqual(f"{utils.STAR_WARS_API}/films/23/", utils.generate_url("films")(23))