def test_delete_song(self): with self.app() as client: with self.app_context(): test_id = 1 # Check record exists before deleting self.assertIsNotNone(Song.find_by_id(test_id)) request = client.delete(f"/song/{test_id}") # Check record doesn't exist after deleting self.assertIsNone(Song.find_by_id(test_id))
def test_get_song_by_id(self): # Check a record exists in the db Song("Test song", 123).save_to_db() song = Song.query.first() self.assertIsNotNone(song) id = song.id # Check find_by_id method record = Song.find_by_id(id) self.assertIsNotNone(record) self.assertEqual(record.name, song.name) self.assertEqual(record.uploaded_time, song.uploaded_time)
def setUp(self): BaseTest.setUp(self) self.post_data = { "audioFileType": "song", "audioFileMetadata": { "name": "Test API Song", "duration": 360 } } self.update_data = {"name": "Updated Test API Song", "duration": 34223} # Create some dummy data for testing Song("Test 1", 100).save_to_db() Song("Test 2", 200).save_to_db()
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_create_song_object(self): a = Song("Test", 123) self.assertEqual("Test", a.name) self.assertEqual(123, a.duration) self.assertEqual('song', a.audio_type) self.assertEqual(None, a.uploaded_time) self.assertEqual(None, a.id)
def test_get_song(self): with self.app() as client: with self.app_context(): test_id = 1 request = client.get(f"/song/{test_id}") self.assertEqual(request.status_code, 200) self.assertDictEqual(SongSchema().dump(Song.find_by_id(1)), json.loads(request.data))
def test_post_song(self): with self.app() as client: with self.app_context(): # Check record named "Test API Song" does not exist self.assertIsNone( Song.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( Song.find_by_name( self.post_data["audioFileMetadata"]["name"])) self.assertDictEqual( SongSchema().dump( Song.find_by_name( self.post_data["audioFileMetadata"]["name"])), json.loads(request.data))
def test_song_dump(self): song = Song(name="Test", duration=33) song.save_to_db() # Retrieve DB record for the song created song = Song.find_by_name(song.name) expected = OrderedDict({ "id": 1, "name": "Test", "duration": 33, "uploaded_time": "T".join(str(song.uploaded_time).split()) }) self.assertDictEqual(self.schema().dump(song), expected)
def test_delete_song(self): # Check a record exists in the db song = Song("Test song", 123) song.save_to_db() self.assertIsNotNone(Song.find_by_name("Test song")) # Delete song song.delete_from_db() # Check record doesn't exist anymore self.assertIsNone(Song.find_by_name("Test song"))
def test_update_song(self): with self.app() as client: with self.app_context(): test_id = 1 # Check record exists self.assertIsNotNone(Song.find_by_id(test_id)) request = client.put(f"/song/{test_id}", \ data=json.dumps(self.update_data),\ headers={'content-type': 'application/json'}) self.assertEqual(request.status_code, 200) record = Song.find_by_id(test_id) self.assertIsNotNone(record) self.assertDictEqual( SongSchema().dump(Song.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"])
def test_update_song(self): song = Song("Test song", 123) song.save_to_db() # Get the record and check field values record = Song.find_by_name("Test song") self.assertIsNotNone(record) self.assertEqual(record.name, "Test song") self.assertEqual(record.duration, 123) # Update field values data = {"name": "Updated Test Song", "duration": 300} Song.update(data, record) # Check updated field values updated_record = Song.find_by_id(record.id) self.assertIsNotNone(updated_record) self.assertEqual(updated_record.name, "Updated Test Song") self.assertEqual(updated_record.duration, 300)
def test_create_song(self): song = Song(name="Test Song", duration=300) # Check there is no exisiting song named "Test Song" self.assertIsNone(Song.find_by_name(song.name)) # Save song to database song.save_to_db() # Check song exists record = Song.find_by_name(song.name) self.assertIsNotNone(record) self.assertIsNotNone(record.id) self.assertIsNotNone(record.uploaded_time) self.assertEqual(record.name, song.name) self.assertEqual(record.duration, song.duration)
def test_song_repr(self): song = Song("Test", 300) self.assertEqual("name: Test, Audio type: song, Uploaded:None", song.__repr__())
def test_create_song_init_with_unexpected_argument(self): with self.assertRaises(TypeError): Song("name", duration=22, unexpected=234)