def get(self, audioFileType): if self.isValidAudioFileType(audioFileType): if audioFileType == 'song': schema = SongSchema() songs = Song.query.all() data = schema.dump(songs, many=True) return data, 200 elif audioFileType == 'podcast': schema = PodcastSchema() podcasts = Podcast.query.all() data = schema.dump(podcasts, many=True) return data, 200 elif audioFileType == 'audiobook': schema = AudioBookSchema() audiobooks = AudioBook.query.all() data = schema.dump(audiobooks, many=True) return data, 200 logging.warn(f"{audioFileType} is not a supported file type") return { 'Message': f"{audioFileType} is not a supported file type" }, 400
def post(self): data = request.get_json(force=True) isValid, message = self.validateJSONData(data) if isValid: schema = None if data['audioFileType'] == 'song': schema = SongSchema() elif data['audioFileType'] == 'podcast': schema = PodcastSchema() elif data['audioFileType'] == 'audiobook': schema = AudioBookSchema() try: record = schema.load(data['audioFileMetadata'], session=db.session) if record: record.save_to_db() logging.info(f"{record.name} saved to database") return schema.dump(record), 200 except Exception as e: logging.warn("There was an problem in creating the record") return str(e), 400 else: return {'Message': message}, 400
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_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_get_podcast_list(self): with self.app() as client: with self.app_context(): request = client.get(f"/podcast") self.assertEqual(request.status_code, 200) json_data = json.loads(request.data) self.assertEqual(len(json_data), 2) self.assertListEqual( PodcastSchema().dump(Podcast.query.all(), many=True), json_data )
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): 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])