def test_podcast_dump(self): podcast = Podcast(name="Test", duration=33, host="Tester", participants=["A", "B"]) podcast.save_to_db() # Retrieve DB record for the podcast created podcast = Podcast.find_by_name(podcast.name) expected = OrderedDict({ "id": 1, "name": "Test", "duration": 33, "host": "Tester", "participants": ["A", "B"], "uploaded_time": "T".join(str(podcast.uploaded_time).split()) }) # Get the serialized data json_data = self.schema().dump(podcast) # Separate out participant lists from json_data and expected data to compare separately participantList = json_data.pop("participants") expectedParticipantList = expected.pop("participants") self.assertDictEqual(json_data, expected) self.assertCountEqual(participantList, expectedParticipantList)
def setUp(self): BaseTest.setUp(self) self.post_data = { "audioFileType": "podcast", "audioFileMetadata": { "name": "Test API Podcast", "duration": 360, "host": "Robert", "participants": [ "Rambo" ] } } self.update_data = { "name": "Updated Test API Podcast", "duration": 34223, "host": "Sukanta", "participants": [ "Norman" ] } # Create some dummy data Podcast("Test 1", 13, "Host 1", ["Participant 1"]).save_to_db() Podcast("Test 2", 23, "Host 2", ["Participant 2"]).save_to_db()
def test_podcast_repr(self): test = Podcast("name", 12, "ss", ["ab", "cd", "ef"]) self.assertEqual( "name: name, Audio type: podcast, Host:ss, participants: [ab, cd, ef]", test.__repr__() )
def test_podcast_participants_list_optional(self): test = Podcast("Test", 12, "ss") self.assertEqual( "name: Test, Audio type: podcast, Host:ss, participants: []", test.__repr__() ) self.assertCountEqual([], test.participants)
def test_delete_podcast(self): with self.app() as client: with self.app_context(): test_id = 1 # Check record exists before deleting self.assertIsNotNone(Podcast.find_by_id(test_id)) request = client.delete(f"/podcast/{test_id}") # Check record doesn't exist after deleting self.assertIsNone(Podcast.find_by_id(test_id))
def test_post_podcast(self): with self.app() as client: with self.app_context(): # Check record named "Test API Podcast" does not exist self.assertIsNone(Podcast.find_by_name(self.post_data["audioFileMetadata"]["name"])) request = client.post("/", data=json.dumps(self.post_data)) self.assertEqual(request.status_code, 200) self.assertIsNotNone(Podcast.find_by_name(self.post_data["audioFileMetadata"]["name"])) self.assertDictEqual( PodcastSchema().dump(Podcast.find_by_name(self.post_data["audioFileMetadata"]["name"])), json.loads(request.data) )
def test_update_podcast(self): podcast = Podcast(**self.data) podcast.save_to_db() # Get the record and check field values record = Podcast.find_by_name(self.data["name"]) self.assertIsNotNone(record) self.assertEqual(record.name, self.data["name"]) self.assertEqual(record.duration, self.data["duration"]) # Update field values update_data = { "name": "Updated Test Podcast", "duration": 120, "host": Host("Updated host"), "participants": [ Participant("Rohan") ] } Podcast.update(update_data, record) # Check updated field values updated_record = Podcast.find_by_id(record.id) self.assertIsNotNone(updated_record) self.assertEqual(updated_record.name, update_data["name"]) self.assertEqual(updated_record.duration, update_data["duration"]) self.assertIsInstance(updated_record.host, Host) self.assertEqual(str(updated_record.host.name), update_data["host"].name) self.assertEqual(len(updated_record.participants), 1) self.assertEqual(updated_record.participants[0].name, update_data["participants"][0].name)
def test_podcast_load_with_existing_record(self): podcast = self.schema().load(self.data, session=db.session) self.assertIsNone(Podcast.find_by_name(podcast.name)) podcast.save_to_db() db_podcast = Podcast.find_by_name(podcast.name) self.assertIsNotNone(db_podcast) # load another podcast with same data and check id is same self.data["id"] = db_podcast.id podcast2 = self.schema().load(self.data, session=db.session,\ instance=Podcast.find_by_name(self.data["name"]), unknown=EXCLUDE) self.assertIsNotNone(podcast2) self.assertEqual(podcast2.id, db_podcast.id)
def get(self, audioFileType, audioFileID): if self.isValidInput(audioFileType, audioFileID): responseData = None audioFileID = int(audioFileID) if audioFileType == 'song': schema = SongSchema() song = Song.find_by_id(audioFileID) if song: responseData = schema.dump(song) elif audioFileType == 'podcast': schema = PodcastSchema() podcast = Podcast.find_by_id(audioFileID) if podcast: responseData = schema.dump(podcast) elif audioFileType == 'audiobook': schema = AudioBookSchema() audiobook = AudioBook.find_by_id(audioFileID) if audiobook: responseData = schema.dump(audiobook) if responseData: return responseData, 200 logging.warn(f"File with ID: {audioFileID} not found") return {'Message': 'File not found'}, 400 logging.warn(f"AudioFileType or AudioFileID is not validd") return {'Message': 'AudioFileType or AudioFileID is not valid'}, 400
def test_delete_podcast(self): # Check a record exists in the db podcast = Podcast(**self.data) podcast.save_to_db() self.assertIsNotNone(Podcast.find_by_name(self.data["name"])) # Delete podcast podcast.delete_from_db() # Check record doesn't exist anymore self.assertIsNone(Podcast.find_by_name(self.data["name"]))
def test_get_podcast(self): with self.app() as client: with self.app_context(): test_id = 1 request = client.get(f"/podcast/{test_id}") self.assertEqual(request.status_code, 200) self.assertDictEqual( PodcastSchema().dump(Podcast.find_by_id(1)), json.loads(request.data) )
def test_update_podcast(self): with self.app() as client: with self.app_context(): test_id = 1 # Check record exists self.assertIsNotNone(Podcast.find_by_id(test_id)) request = client.put(f"/podcast/{test_id}", \ data=json.dumps(self.update_data),\ headers={'content-type': 'application/json'}) self.assertEqual(request.status_code, 200) record = Podcast.find_by_id(test_id) self.assertIsNotNone(record) self.assertDictEqual( PodcastSchema().dump(Podcast.find_by_id(test_id)), json.loads(request.data) ) self.assertEqual(record.name, self.update_data["name"]) self.assertEqual(record.duration, self.update_data["duration"]) self.assertEqual(record.host.name, self.update_data["host"]) self.assertEqual(record.participants[0].name, self.update_data["participants"][0])
def test_podcast_create_object(self): test = Podcast( name="Test", duration=3600, host="Tester", participants=["One", "Two"] ) self.assertEqual("Test", test.name) self.assertEqual(3600, test.duration) self.assertIsInstance(test.host, Host) self.assertEqual("Tester", test.host.name) self.assertIsInstance(test.participants, list) self.assertEqual(2, len(test.participants)) self.assertIsInstance(test.participants[0], Participant) self.assertEqual("One", test.participants[0].name)
def test_get_podcast_by_id_or_name(self): # Check a record exists in the db podcast = Podcast(**self.data) podcast.save_to_db() podcast_byname = Podcast.find_by_name(self.data["name"]) self.assertIsNotNone(podcast_byname) id = podcast_byname.id # Check find_by_id method podcast_byid = Podcast.find_by_id(id) self.assertIsNotNone(podcast_byid) self.assertEqual(podcast_byid.name, podcast_byname.name) self.assertEqual(podcast_byid.uploaded_time, podcast_byname.uploaded_time)
def test_create_podcast(self): podcast = Podcast(**self.data) # Check there is no exisiting podcast named "Test Podcast" self.assertIsNone(Podcast.find_by_name(podcast.name)) # Save podcast to database podcast.save_to_db() # Check podcast exists record = Podcast.find_by_name(podcast.name) self.assertIsNotNone(record) self.assertIsNotNone(record.id) self.assertIsNotNone(record.uploaded_time) self.assertEqual(record.name, podcast.name) self.assertEqual(record.duration, podcast.duration) # check host name and type self.assertIsInstance(record.host, Host) self.assertEqual(record.host.name, "Robert") self.assertEqual(len(record.participants), 3) self.assertIsInstance(record.participants[0], Participant)
def test_podcast_create_object_with_str_duration_error(self): with self.assertRaises(TypeError) as e: test = Podcast("name", "12", host="tester", participants=["ab"]) self.assertIn(b"A required field does not have the correct type", str(e))
def test_podcast_create_object_without_host_error(self): with self.assertRaises(TypeError) as e: test = Podcast("name", 12, participants=["ab"]) self.assertIn(b"__init__() missing 1 required positional argument: 'host'", str(e))
def test_podcast_create_object_with_11_participants_error(self): with self.assertRaises(ValidationError) as e: test = Podcast("name", 12, host="ss", \ participants=["ab", "cd", "ef", "a", "c", "b", "w", "wx", "xxx", "xcv", "xc"]) self.assertIn(b"Podcast cannot have more than 10 participants", str(e))