def test_edit_song(self): #Editing an existing song file = models.File(name="Example.mp3") song = models.Song(file=file) session.add(song) session.commit() data = { "name": "Edited.mp3", } response = self.client.put("/api/songs/{}".format(song.id), data=json.dumps(data), content_type="application/json", headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") self.assertEqual( urlparse(response.headers.get("Location")).path, "/api/songs/{}".format(song.id)) data = json.loads(response.data.decode("ascii")) self.assertEqual(data["id"], 1) self.assertEqual(data["file"]["name"], "Edited.mp3") # Assert that there is only one song in the database songs = session.query(models.Song).all() self.assertEqual(len(songs), 1) # Assert that the name was changed song = songs[0] self.assertEqual(file.name, "Edited.mp3")
def test_get_songs(self): """ Getting songs from a populated database """ #Create example files & songs fileA = models.File(name="SongA.mp3") fileB = models.File(name="SongB.mp3") songA = models.Song(file=fileA) songB = models.Song(file=fileB) session.add_all([fileA, fileB, songA, songB]) session.commit() response = self.client.get("/api/songs", headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") data = json.loads(response.data.decode("ascii")) self.assertEqual(len(data), 2) songA = data[0] self.assertEqual(songA["id"], 1) self.assertEqual(songA["file"]["id"], 1) self.assertEqual(songA["file"]["name"], "SongA.mp3") songB = data[1] self.assertEqual(songB["id"], 2) self.assertEqual(songB["file"]["id"], 2) self.assertEqual(songB["file"]["name"], "SongB.mp3")
def test_delete(self): sA = models.Song() sB = models.Song() songA = models.File(name="Song A") songB = models.File(name="Song B") songA.song = sA songB.song = sB session.add_all([songA, songB]) session.commit() response = self.client.get("/api/songs/{}".format(songA.song.id), headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") data = json.loads(response.data.decode("ascii")) songA = data sA = songA["file"] self.assertEqual(sA["file_name"], "Song A") self.assertEqual(sA["file_id"], 1) response = self.client.delete("/api/songs/{}".format(sA["file_id"]), headers=[("Accept", "application/json")]) response = self.client.get("/api/songs/{}".format(sA["file_id"]), headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 404) self.assertEqual(response.mimetype, "application/json") data = json.loads(response.data.decode("ascii")) self.assertEqual(data["message"], "Could not find song with id 1")
def test_get_songs(self): sA = models.Song() sB = models.Song() songA = models.File(name="Song A") songB = models.File(name="Song B") songA.song = sA songB.song = sB session.add_all([songA, songB]) session.commit() response = self.client.get("/api/songs", headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") data = json.loads(response.data.decode("ascii")) self.assertEqual(len(data), 2) songA = data[0] sA = songA["file"] self.assertEqual(sA["file_name"], "Song A") self.assertEqual(sA["file_id"], 1) songB = data[1] sB = songB["file"] self.assertEqual(sB["file_name"], "Song B") self.assertEqual(sB["file_id"], 2)
def test_post_song(self): """ Test post song """ file1 = models.File(filename="file1Song") session.add(file1) session.commit() data = { "file": { "id": file1.id } } response = self.client.post("/api/songs", data=json.dumps(data), content_type="application/json", headers=[("Accept", "application/json")] ) self.assertEqual(response.status_code, 201) self.assertEqual(response.mimetype, "application/json") self.assertEqual(urlparse(response.headers.get("Location")).path, "/api/songs/1") data = json.loads(response.data.decode("ascii")) self.assertEqual(data["id"], 1) songs = session.query(models.Song).all() self.assertEqual(len(songs), 1) song = songs[0] self.assertEqual(song.id, 1)
def test_songs_get(self): fileA = models.File(filename="a_song.mp3") fileB = models.File(filename="another_song.mp3") session.add_all([fileA, fileB]) songA = models.Song(file=fileA) songB = models.Song(file=fileB) session.add_all([songA, songB]) session.commit() response = self.client.get("/api/songs", headers=[("Accept", "application/json")] ) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") data = json.loads(response.data) self.assertEqual(data[0]["file"]["id"], fileA.id) self.assertEqual(urlparse(data[0]["file"]["path"]).path, "/uploads/a_song.mp3") self.assertEqual(data[1]["file"]["id"], fileB.id) self.assertEqual(urlparse(data[1]["file"]["path"]).path, "/uploads/another_song.mp3")
def test_get_songs(self): fileA = models.File(filename = 'Test Song A.mp3') fileB = models.File(filename = 'Test Song B.mp3') songA = models.Song(file = fileA) songB = models.Song(file = fileB) session.add_all([fileA, fileB, songA, songB]) session.commit() response = self.client.get('/api/songs', headers = [('Accept', 'application/json')] ) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, 'application/json') data = json.loads(response.data.decode('ascii')) self.assertEqual(len(data), 2) songA = data[0] self.assertEqual(songA['id'], 1) self.assertEqual(songA['file']['id'], 1) self.assertEqual(songA['file']['name'], 'Test Song A.mp3') songB = data[1] self.assertEqual(songB['id'], 2) self.assertEqual(songB['file']['id'], 2) self.assertEqual(songB['file']['name'], 'Test Song B.mp3')
def test_get_song(self): ''' get a single song from the API and make sure it is the one that we requested and not a different one from the DB ''' fileA = models.File(filename = 'Test Song A.mp3') fileB = models.File(filename = 'Test Song B.mp3') songA = models.Song(file = fileA) songB = models.Song(file = fileB) session.add_all([fileA, fileB, songA, songB]) session.commit() response = self.client.get('/api/songs/1', headers = [('Accept', 'application/json')] ) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, 'application/json') data = json.loads(response.data.decode('ascii')) self.assertEqual(len(data), 2) self.assertEqual(data['id'], 1) self.assertEqual(data['file']['id'], 1) self.assertEqual(data['file']['name'], 'Test Song A.mp3') self.assertNotEqual(data['id'], 2) self.assertNotEqual(data['file']['id'], 2) self.assertNotEqual(data['file']['name'], 'Test Song B.mp3')
def test_put_song_with_invalid_file_id_type(self): ''' try putting a song with a file that has an id of the wrong type ''' file = models.File(filename = 'Test Song A.mp3') song = models.Song(file = file) session.add_all([file, song]) session.commit() data = { "file": { "id": 'whatsup' } } response = self.client.put('/api/songs/1', data = json.dumps(data), content_type = 'application/json', headers = [('Accept', 'application/json')] ) self.assertEqual(response.status_code, 422) self.assertEqual(response.mimetype, 'application/json') data = json.loads(response.data.decode('ascii')) self.assertEqual(data['message'], '\'whatsup\' is not of type \'number\'')
def test_put_song_with_invalid_json_structure(self): ''' try putting a song with a file that has only a key:value pair insted of a file object ''' file = models.File(filename = 'Test Song A.mp3') song = models.Song(file = file) session.add_all([file, song]) session.commit() data = { "file": 8 } response = self.client.put('/api/songs/1', data = json.dumps(data), content_type = 'application/json', headers = [('Accept', 'application/json')] ) self.assertEqual(response.status_code, 422) self.assertEqual(response.mimetype, 'application/json') data = json.loads(response.data.decode('ascii')) self.assertEqual(data['message'], '8 is not of type \'object\'')
def test_put_song_with_nonexistent_file(self): ''' updating a song ''' file = models.File(filename = 'Test Song A.mp3') song = models.Song(file = file) session.add_all([file, song]) session.commit() data = { "file": { "id": 19 } } response = self.client.put('/api/songs/1', data = json.dumps(data), content_type = 'application/json', headers = [('Accept', 'application/json')] ) self.assertEqual(response.status_code, 404) self.assertEqual(response.mimetype, 'application/json') data = json.loads(response.data.decode('ascii')) self.assertEqual(data['message'], 'Could not find file with id 19')
def test_get_songs(self): fileA = models.File(filename = "Test File 1") fileB = models.File(filename = "Test File 2") session.add_all([fileA, fileB]) session.commit() songA = models.Song(original_file_id = fileA.id) songB = models.Song(original_file_id = fileB.id) session.add_all([songA, songB]) session.commit() response = self.client.get("/api/songs", headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") data = json.loads(response.data.decode("ascii")) songA = data[0] self.assertEqual(songA["file"]["name"], "Test File 1") self.assertEqual(songA["file"]["id"], fileA.id) songB = data[1] self.assertEqual(songB["file"]["name"], "Test File 2") self.assertEqual(songB["file"]["id"], fileB.id)
def test_song_edit(self): file1 = models.File(filename ="File1") file2 = models.File(filename = "File2") session.add_all([file1, file2]) session.commit() song1 = models.Song(original_file_id=file1.id) session.add(song1) session.commit() data = { "file": { "id" : file2.id } } response = self.client.put("/api/songs/{}".format(song1.id), data=json.dumps(data), content_type="application/json", headers=[("Accept", "application/json")] ) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") songs = session.query(models.Song).all() self.assertEqual(len(songs), 1) song = songs[0] self.assertEqual(song.original_file_id, file2.id)
def test_post_file(self): fileA = models.File(filename="testA.wav") session.add(fileA) session.commit() fileA_post = {"file": {"id": fileA.id}} # data = fileA.as_dictionary() data = fileA_post response = self.client.post("api/songs", data=json.dumps(data), content_type="application/json", headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 201) self.assertEqual(response.mimetype, "application/json") self.assertEqual( urlparse(response.headers.get("Location")).path, "/api/songs/1") data = json.loads(response.data.decode("ascii")) self.assertEqual(data["id"], 1) self.assertEqual(data["file"]["filename"], "testA.wav") songs = session.query(models.Song).all() self.assertEqual(len(songs), 1) song = songs[0] self.assertEqual(song.file.filename, "testA.wav")
def test_post_file(self): fileA = models.File(filename="testA.wav") session.add(fileA) session.commit() fileA_post = { "file":{ "id": fileA.id } } # data = fileA.as_dictionary() data = fileA_post response = self.client.post("api/songs", data=json.dumps(data), content_type="application/json", headers=[("Accept", "application/json")] ) self.assertEqual(response.status_code, 201) self.assertEqual(response.mimetype, "application/json") self.assertEqual(urlparse(response.headers.get("Location")).path, "/api/songs/1") data = json.loads(response.data.decode("ascii")) self.assertEqual(data["id"], 1) self.assertEqual(data["file"]["filename"], "testA.wav") songs = session.query(models.Song).all() self.assertEqual(len(songs), 1) song = songs[0] self.assertEqual(song.file.filename, "testA.wav")
def test_song_delete(self): """deleting songs from database""" #create database fileP = models.File(name="purple_song.mp3") fileO = models.File(name="orange_song.mp3") fileG = models.File(name="green_song.mp3") songP = models.Song(file=fileP) songO = models.Song(file=fileO) songG = models.Song(file=fileG) session.add_all([songP, songO, songG, fileP, fileO, fileG]) session.commit() response = self.client.delete("/api/songs/3", headers=[("Accept", "application/json")] ) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") data = json.loads(response.data) self.assertEqual(len(data), 2) songA = data[0] self.assertEqual(songA["file"]["name"], "purple_song.mp3") songB = data[1] self.assertEqual(songB["file"]["name"], "orange_song.mp3")
def test_delete_file(self): fileA = models.File(filename="testA.wav") songA = models.Song(file=fileA) session.add_all([fileA, songA]) session.commit() # 1st, see if song was successfully posted response = self.client.get("api/songs/{}".format(songA.id), headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") # Now test DELETE response = self.client.delete("/api/songs/{}".format(songA.id), headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") # Try GET to check if 404 response = self.client.get("api/songs/{}".format(songA.id), headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 404) self.assertEqual(response.mimetype, "application/json")
def test_edit_song(self): fileA = models.File(filename="testA.wav") fileB = models.File(filename="testB.wav") song = models.Song(file=fileA) session.add_all([fileA, fileB, song]) session.commit() newfile = {"file": {"id": fileB.id}} data = newfile response = self.client.get("api/songs/{}".format(song.id), headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") songjson = json.loads(response.data.decode("ascii")) self.assertEqual(songjson["file"]["filename"], "testA.wav") response = self.client.put("/api/songs/{}".format(song.id), data=json.dumps(data), content_type="application/json", headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") songjson = json.loads(response.data.decode("ascii")) self.assertEqual(songjson["file"]["filename"], "testB.wav") self.assertEqual(songjson["file"]["id"], fileB.id)
def test_get_songs(self): """ Getting songs from a populated database """ songA = models.Song() songB = models.Song() fileA = models.File(name="fileA") fileB = models.File(name="fileB") session.add_all([fileA, fileB]) session.commit() songA.file_id = fileA.id songB.file_id = fileB.id session.add_all([songA, songB]) session.commit() response = self.client.get("/api/songs", headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") data = json.loads(response.data.decode("ascii")) self.assertEqual(len(data), 2) songAR = data[0] self.assertEqual(songAR, songA.as_dictionary()) songBR = data[1] self.assertEqual(songBR, songB.as_dictionary())
def test_delete_song(self): """ Deleting a single song from a populated database """ songA = models.File(filename="Drones.mp3") songA_id = models.Song(file_id=1) songB = models.File(filename="Californication.mp3") songB_id = models.Song(file_id=2) session.add_all([songA, songB, songA_id, songB_id]) session.commit() response = self.client.get("/api/songs/{}".format(songA.id), headers=[("Accept", "application/json")]) session.delete(songA_id) session.delete(songA) session.commit() response = self.client.get("/api/songs/{}".format(songB.id)) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") songs = session.query(models.Song).all() self.assertEqual(len(songs), 1) files = session.query(models.File).all() self.assertEqual(len(files), 1) song = json.loads(response.data.decode("ascii")) print(song) self.assertEqual(song["file"]["filename"], "Californication.mp3")
def test_edit_song(self): fileA = models.File(filename="testA.wav") fileB = models.File(filename="testB.wav") song = models.Song(file=fileA) session.add_all([fileA, fileB, song]) session.commit() newfile = { "file":{ "id": fileB.id } } data = newfile response = self.client.get("api/songs/{}".format(song.id), headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") songjson = json.loads(response.data.decode("ascii")) self.assertEqual(songjson["file"]["filename"], "testA.wav") response = self.client.put("/api/songs/{}".format(song.id), data = json.dumps(data), content_type = "application/json", headers=[("Accept", "application/json")] ) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") songjson = json.loads(response.data.decode("ascii")) self.assertEqual(songjson["file"]["filename"], "testB.wav") self.assertEqual(songjson["file"]["id"], fileB.id)
def test_put_song(self): file_A = models.File(name='file_A.mp3') file_B = models.File(name='file_B.mp3') song_A = models.Song(file_=file_A) session.add_all([file_A, file_B, song_A]) session.commit() data = { "file": { "id": file_B.id } } response = self.client.put( "/api/songs/{}".format(song_A.id), data=json.dumps(data), content_type="application/json", headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") data = json.loads(response.data.decode("ascii")) self.assertEqual(data["id"], 1) self.assertEqual(data["file"]["id"], 2) self.assertEqual(data["file"]["name"], "file_B.mp3") songs = session.query(models.Song).all() self.assertEqual(len(songs), 1) song = songs[0] self.assertEqual(song.id, 1) self.assertEqual(song.file_.id, 2) self.assertEqual(song.file_.name, "file_B.mp3")
def test_delete_file(self): fileA = models.File(filename="testA.wav") songA = models.Song(file=fileA) session.add_all([fileA, songA]) session.commit() # 1st, see if song was successfully posted response = self.client.get("api/songs/{}".format(songA.id), headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") # Now test DELETE response = self.client.delete("/api/songs/{}".format(songA.id), headers=[("Accept", "application/json")] ) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") # Try GET to check if 404 response = self.client.get("api/songs/{}".format(songA.id), headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 404) self.assertEqual(response.mimetype, "application/json")
def test_get_songs(self): file_A = models.File(name='file_A.mp3') song_A = models.Song(file_=file_A) file_B = models.File(name='file_B.mp3') song_B = models.Song(file_=file_B) session.add_all([file_A, file_B, song_A, song_B]) session.commit() response = self.client.get( '/api/songs', headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, 'application/json') data = json.loads(response.data.decode('ascii')) self.assertEqual(len(data), 2) response_song_A, response_song_B = data self.assertEqual(response_song_A['id'], song_A.id) self.assertEqual(response_song_A['file']['id'], song_A.file_.id) self.assertEqual(response_song_A['file']['name'], song_A.file_.name) self.assertEqual(response_song_B['id'], song_B.id) self.assertEqual(response_song_B['file']['id'], song_B.file_.id) self.assertEqual(response_song_B['file']['name'], song_B.file_.name)
def test_delete_song(self): """ Deleting a song """ fileA = models.File(name="test1.mp3") fileB = models.File(name="test2.mp3") songA = models.Song(file=fileA) songB = models.Song(file=fileB) session.add_all([fileA, fileB, songA, songB]) session.commit() response = self.client.delete("/api/songs/{}".format(songB.id), headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") data = json.loads(response.data.decode("ascii")) self.assertEqual(data["message"], "Deleted song 2") songs = session.query(models.Song).all() files = session.query(models.File).all() self.assertEqual(len(songs), 1) self.assertEqual(len(files), 2)
def test_get_songs(self): """Getting posts from a populated database""" songA = models.Song() songB = models.Song() fileA = models.File(name='fileA.mp3') fileB = models.File(name='fileB.mp3') songA.file = fileA songB.file = fileB session.add_all([songA, songB, fileA, fileB]) session.commit() response = self.client.get('/api/songs', headers=[('Accept', 'application/json')] ) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, 'application/json') data = json.loads(response.data.decode('ascii')) self.assertEqual(len(data), 2) songA = data[0] self.assertEqual(songA['id'], 1) self.assertEqual(songA['file'], {'id': 1, 'name': 'fileA.mp3'}) songB = data[1] self.assertEqual(songB['id'], 2) self.assertEqual(songB['file'], {'id': 2, 'name': 'fileB.mp3'})
def test_songs_post(self): """ Posting a new song """ data = { "file": { "id" : 1 } } file = models.File(name="Test File") session.add(file) session.commit() response = self.client.post("/api/songs", data=json.dumps(data), content_type="application/json", headers=[("Accept", "application/json")] ) self.assertEqual(response.status_code, 201) self.assertEqual(response.mimetype, "application/json") # self.assertEqual(urlparse(response.headers.get("Location")).path, # "/api/songs/1") data = json.loads(response.data.decode("ascii")) self.assertEqual(data["file"]["id"], 1) self.assertEqual(data["file"]["name"], "Test File") songs = session.query(models.Song).all() self.assertEqual(len(songs), 1) song = songs[0] self.assertEqual(song.file.name, "Test File")
def test_post_song(self): """ Posting a new song """ fileA = models.File(filename='FileA') session.add(fileA) session.commit() song_payload = { "file": { "id": fileA.id } } response = self.client.post("/api/songs", data=json.dumps(song_payload), content_type="application/json", headers=[("Accept", "application/json")]) print response.data print fileA.id self.assertEqual(response.status_code, 201) self.assertEqual(response.mimetype, "application/json") self.assertEqual(urlparse(response.headers.get( "Location")).path, "/api/songs") data = json.loads(response.data) self.assertEqual(data["id"], fileA.id) # self.assertEqual(data["song_file"], "techno") songs = session.query(models.Song).all() self.assertEqual(len(songs), 1) song = songs[0] self.assertEqual(song.id, fileA.id)
def test_add_song(self): """Getting posts from a populated database""" fileA = models.File(name='fileA.mp3') session.add(fileA) session.commit() response = self.client.post('/api/songs', headers=[('Accept', 'application/json')], content_type='application/json', data=json.dumps({'file': {'id': 1}}) ) self.assertEqual(response.status_code, 201) self.assertEqual(response.mimetype, 'application/json') #self.assertEqual(urlparse(response.headers.get("Location")).path, '/') songs = session.query(models.Song).all() self.assertEqual(len(songs), 1) song = songs[0] self.assertEqual(song.id, 1) self.assertEqual(song.file.id, 1) self.assertEqual(song.file.name, 'fileA.mp3') data = json.loads(response.data.decode('ascii')) self.assertEqual(len(data), 1) songA = data[0] self.assertEqual(songA['id'], 1) self.assertEqual(songA['file'], {'id': 1, 'name': 'fileA.mp3'})
def test_get_songs(self): """ Getting songs from a populated database """ fileA = models.File(name="test1.mp3") fileB = models.File(name="test2.mp3") songA = models.Song(file=fileA) songB = models.Song(file=fileB) session.add_all([fileA, fileB, songA, songB]) session.commit() response = self.client.get("/api/songs", headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") data = json.loads(response.data.decode("ascii")) self.assertEqual(len(data), 2) songA = data[0] self.assertEqual(songA['id'], 1) self.assertEqual(songA['file'], { 'name': 'test1.mp3', 'id': 1, 'path': '/uploads/test1.mp3' }) songB = data[1] self.assertEqual(songB['id'], 2) self.assertEqual(songB['file'], { 'name': 'test2.mp3', 'id': 2, 'path': '/uploads/test2.mp3' })
def test_get_songs(self): """ Getting songs - success """ #Add 2 Songs to DB with File Data file1 = models.File(filename="Awake.mp3") file2 = models.File(filename="Montana.mp3") song1 = models.Song(name="Awake", file=file1) song2 = models.Song(name="Montana", file=file2) session.add_all([song1, song2]) session.commit() #query api response = self.client.get("/api/songs") print(response) #assert api response contains expected response self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") data = json.loads(response.data.decode("ascii")) self.assertEqual(len(data), 2) #assert response contains expected songs/file song1 = data[0] print(song1) self.assertEqual(song1["songname"], "Awake") self.assertEqual(song1["file"]["filename"], "Awake.mp3") self.assertEqual(song1["file"]["id"], song1["id"]) song2 = data[1] self.assertEqual(song2["songname"], "Montana") self.assertEqual(song2["file"]["filename"], "Montana.mp3") self.assertEqual(song2["file"]["id"], song2["id"])
def testPostSong(self): """ Add a new song """ # Add a file to the database to test against fileA = models.File(filename="FileA.mp3") session.add(fileA) session.commit() data = json.dumps({"file": {"id": 1}}) response = self.client.post("/api/songs", data=data, content_type="application/json", headers=[("Accept", "application/json")] ) self.assertEqual(response.status_code, 201) self.assertEqual(response.mimetype, "application/json") self.assertEqual(urlparse(response.headers.get("Location")).path, "/api/songs/1") data = json.loads(response.data) self.assertEqual(data["id"], 1) self.assertEqual(data["file"]["id"], 1) self.assertEqual(data["file"]["name"], "FileA.mp3") songs = session.query(models.Song).all() self.assertEqual(len(songs), 1) song = songs[0] self.assertEqual(song.id, 1) self.assertEqual(song.file.id, 1) self.assertEqual(song.file.filename, "FileA.mp3")
def test_post_song(self): # test posting song into db file_A = models.File(name='file_A.mp3') session.add(file_A) session.commit() data = {"file": {"id": file_A.id}} response = self.client.post("/api/songs", data=json.dumps(data), content_type="application/json", headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 201) self.assertEqual(response.mimetype, "application/json") data = json.loads(response.data.decode("ascii")) self.assertEqual(data["id"], 1) self.assertEqual(data["file"]["id"], 1) self.assertEqual(data["file"]["name"], "file_A.mp3") songs = session.query(models.Song).all() self.assertEqual(len(songs), 1) song = songs[0] self.assertEqual(song.id, 1) self.assertEqual(song.file.id, 1) self.assertEqual(song.file.name, "file_A.mp3")
def test_put_song(self): """ Updating a song """ songA = models.Song() fileA = models.File(name="fileA") fileB = models.File(name="fileB") session.add_all([fileA, fileB]) session.commit() songA.file_id = fileA.id session.add_all([songA]) session.commit() data = {"file": {"id": fileB.id}} response = self.client.put("/api/songs/{}".format(songA.id), data=json.dumps(data), content_type="application/json", headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 201) self.assertEqual(response.mimetype, "application/json") self.assertEqual( urlparse(response.headers.get("Location")).path, "/api/songs") songs = session.query(models.Song).all() self.assertEqual(len(songs), 1) data = json.loads(response.data.decode("ascii")) self.assertEqual(data["file"]["id"], fileB.id)
def test_delete_song(self): """ Delete a song """ data = {} song = models.Song() file = models.File(name="file") session.add_all([file]) session.commit() song.file_id = file.id session.add_all([song]) session.commit() songs = session.query(models.Song).all() self.assertEqual(len(songs), 1) response = self.client.delete("/api/songs/{}".format(song.id), data=json.dumps(data), content_type="application/json", headers=[("Accept", "application/json")]) songs = session.query(models.Song).all() self.assertEqual(len(songs), 0)
def test_add_song(self): """testing adding a song to the database""" fileA = models.File(name="moonlight.mp3") session.add(fileA) session.commit() data = { "file": { "id": fileA.id } } #print(data) response = self.client.post("/api/songs", data=json.dumps(data), content_type="application/json", headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 201) self.assertEqual(response.mimetype, "application/json") self.assertEqual(urlparse(response.headers.get("Location")).path,"/api/songs") data = json.loads(response.data.decode("ascii")) #print(data) self.assertEqual(data["id"], fileA.id) songs = session.query(models.Song).all() self.assertEqual(len(songs), 1) song = songs[0] self.assertEqual(song.id, fileA.id)
def test_get_songs(self): """ Getting songs from a populated database """ songA = models.File(filename="Drones.mp3") songA_id = models.Song(file_id=1) songB = models.File(filename="Californication.mp3") songB_id = models.Song(file_id=2) session.add_all([songA, songB, songA_id, songB_id]) session.commit() response = self.client.get("/api/songs", headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") data = json.loads(response.data.decode("ascii")) self.assertEqual(len(data), 2) songA = data[0] self.assertEqual(songA["file"]["filename"], "Drones.mp3") self.assertEqual(songA["id"], 1) songB = data[1] self.assertEqual(songB["file"]["filename"], "Californication.mp3") self.assertEqual(songB["id"], 2)
def test_post_song(self): "post a song for file in DB - success" #add a song file to the DB for the test file = models.File(filename="Plains.mp3") session.add(file) session.commit() print(file.id) print(file.filename) data = { "songname": "Plains", "file": { "file": file.id } } #query api response = self.client.get("/api/songs") print(response) #assert api response contains expected response self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") data = json.loads(response.data.decode("ascii")) self.assertEqual(len(data), 1) #assert response contains expected songs/file song1 = data[0] print(song1) self.assertEqual(song1["songname"], "Plains") self.assertEqual(song1["file"]["filename"], "Plains.mp3") self.assertEqual(song1["file"]["id"], song1["id"])
def test_delete_song(self): # test deleting a song # populate db file_A = models.File(name='file_A.mp3') song_A = models.Song(file=file_A) file_B = models.File(name='file_B.mp3') song_B = models.Song(file=file_B) session.add_all([file_A, file_B, song_A, song_B]) session.commit() response = self.client.delete("/api/songs/{}".format(song_A.id), content_type="application/json", headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") data = json.loads(response.data.decode("ascii")) self.assertEqual(data["id"], 1) self.assertEqual(data["file"]["id"], 1) self.assertEqual(data["file"]["name"], "file_A.mp3") songs = session.query(models.Song).all() self.assertEqual(len(songs), 1) files = session.query(models.File).all() self.assertEqual(len(files), 1) file = files[0] self.assertEqual(file.id, 2) self.assertEqual(file.name, 'file_B.mp3')
def test_song_edit(self): """edit song from database""" #create populate database fileR = models.File(name="red_song.mp3") fileG = models.File(name="blue_song.mp3") fileB = models.File(name="green_song.mp3") songR = models.Song(file=fileR) songG = models.Song(file=fileG) songB = models.Song(file=fileB) session.add_all([songR, songG, songB, fileR, fileG, fileB]) session.commit() data = { "name": "brown_song.mp3" } response = self.client.put("/api/songs/{}".format(songB.id), data=json.dumps(data), content_type="application/json", headers=[("Accept", "application/json")] ) self.assertEqual(response.status_code, 201) self.assertEqual(response.mimetype, "application/json") data = json.loads(response.data) self.assertEqual(data["file"]["name"], "brown_song.mp3") songs = session.query(models.Song).all() self.assertEqual(len(songs), 3) songB = songs[2] self.assertEqual(songB.file.name, "brown_song.mp3")
def test_get_songs(self): # test getting all songs in db # populate db file_A = models.File(name='file_A.mp3') song_A = models.Song(file=file_A) file_B = models.File(name='file_B.mp3') song_B = models.Song(file=file_B) session.add_all([file_A, file_B, song_A, song_B]) session.commit() response = self.client.get('/api/songs', headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, 'application/json') data = json.loads(response.data.decode('ascii')) self.assertEqual(len(data), 2) # store JSON structured data for each test response_song_A, response_song_B = data # assert tests for first song self.assertEqual(response_song_A['id'], song_A.id) self.assertEqual(response_song_A['file']['id'], song_A.file.id) self.assertEqual(response_song_A['file']['name'], song_A.file.name) # assert tests for second song self.assertEqual(response_song_B['id'], song_B.id) self.assertEqual(response_song_B['file']['id'], song_B.file.id) self.assertEqual(response_song_B['file']['name'], song_B.file.name)
def test_song_post(self): """ Add a new song """ file = models.File(name="green_song.mp3") session.add(file) session.commit() data = { "file": { "id": file.id } } response = self.client.post("/api/songs", data=json.dumps(data), content_type="application/json", headers=[("Accept", "application/json")] ) self.assertEqual(response.status_code, 201) self.assertEqual(response.mimetype, "application/json") songs = session.query(models.Song).all() self.assertEqual(len(songs), 1) song = songs[0] self.assertEqual(song.file.id, file.id) data = json.loads(response.data) self.assertEqual(data["file"]["id"], file.id) self.assertEqual(urlparse(data["file"]["path"]).path, "/uploads/green_song.mp3")
def test_post_song(self): """ Adding a new song """ fileA = models.File(name="test-feb14.mp3") session.add(fileA) session.commit() data = {"file": {"id": 1}} response = self.client.post("/api/songs", data=json.dumps(data), content_type="application/json", headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 201) self.assertEqual(response.mimetype, "application/json") self.assertEqual( urlparse(response.headers.get("Location")).path, "/api/songs/1") data = json.loads(response.data.decode("ascii")) self.assertEqual(data['id'], 1) self.assertEqual(data['file']['name'], 'test-feb14.mp3') file = session.query(models.File).all() song = session.query(models.Song).all() self.assertEqual(len(file), 1) self.assertEqual(len(song), 1) file = file[0] song = song[0] print(file) self.assertEqual(file.id, 1) self.assertEqual(file.name, 'test-feb14.mp3') self.assertEqual(song.id, 1) self.assertEqual(song.file, file)
def test_get_songs(self): """ Getting songs from a populated database """ fileA = models.File(filename='FileA') fileB = models.File(filename='FileB') session.add_all([fileA, fileB]) session.commit() songA = models.Song(song_file_id=fileA.id) songB = models.Song(song_file_id=fileB.id) session.add_all([songA, songB]) session.commit() response = self.client.get( "/api/songs", headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") data = json.loads(response.data) self.assertEqual(len(data), 2) songA = data[0] self.assertEqual(songA["id"], 1) self.assertEqual(songA["file"]["id"], 1) self.assertEqual(songA["file"]["name"], "FileA") songB = data[1] self.assertEqual(songB["id"], 2) self.assertEqual(songB["file"]["id"], 2) self.assertEqual(songB["file"]["name"], "FileB")
def seed(): for i in range(25): song = Song() file = File(filename='test song {}'.format(i) ) song.file = file session.add(song) session.commit()
def seed(): for i in range(25): file = File(name="TestFile{}.mp3".format(i)) session.add(file) session.commit() song = Song(file_id=file.id) session.add(song) session.commit()
def seed(): fileR = File(filename="red_song.mp3") fileB = File(filename="blue_song.mp3") session.add_all([fileR, fileB]) songR = Song(file=fileR) songB = Song(file=fileB) session.add_all([songR, songB]) session.commit()
def test_delete_song(self): """ delete songs from database """ fileA = models.File(name="Shady_Grove.mp3") session.add(fileA) session.commit() response = self.client.delete("/api/songs/{}".format(fileA.id), content_type="application/json", headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 204)
def create_new_songs(self): fileA = models.File(id=1,filename="FileA.mp3") fileB = models.File(id=2,filename="FileB.mp3") session.add_all([fileA, fileB]) songA = models.Song(id=1,song_file_id=fileA.id) songB = models.Song(id=2,song_file_id=fileB.id) session.add_all([songA, songB]) session.commit()
def test_get_songs(self): fileA = models.File(name='fileA.mp3') session.add(fileA) session.commit() songA = models.Song(file_id=fileA.id) session.add(songA) session.commit() fileB = models.File(name='fileB.mp3') session.add(fileB) session.commit() songB = models.Song(file_id=fileB.id) session.add(songB) session.commit() response = self.client.get('/api/songs', headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, 'application/json') data = json.loads(response.data.decode('ascii')) SongA, SongB = data self.assertEqual(SongA['id'], songA.id) self.assertEqual(SongA['file']['id'], songA.file_id) self.assertEqual(SongB['id'], songB.id) self.assertEqual(SongB['file']['id'], songB.file_id)
def testPostNewSong(self): """Post a new song to the DB""" # Create a file in the DB for the app to find file = models.File(name="BornThisWay.mp3") session.add(file) session.commit() # Compile posted data into a dictionary for easy conversion to JSON data = { "file": { "id": 1 } } # Collect the response from the endpoint # use json.dumps to convert dict > JSON # use content_type to indicate the type of content in data response = self.client.post("/api/songs", data=json.dumps(data), content_type="application/json", headers=[("Accept", "application/json")], ) # Verify request to endpoint was successful using 201 created self.assertEqual(response.status_code, 201) # Verify that the response is JSON type self.assertEqual(response.mimetype, "application/json") # Verify the endpoint is setting the correct Location header # This should be the link to the new post self.assertEqual(urlparse(response.headers.get("Location")).path, "/api/songs") # Decode the response with json.loads song = json.loads(response.data) # Extrapolate the file info file = song["file"] # Validate the response self.assertEqual(song["id"], 1) self.assertEqual(file["id"], 1) self.assertEqual(file["name"], "BornThisWay.mp3") # Query DB to validate status song = session.query(models.Song).all() # Verify only one item in DB self.assertEqual(len(song), 1) # Isolate the one item in the list song = song[0] # Validate the content of the item retrieved from the DB self.assertEqual(song.id, 1) self.assertEqual(song.file_id, 1) self.assertEqual(song.file.name, "BornThisWay.mp3")
def test_delete_success(self): """ Test successful song deletion """ song = models.Song() session.add(song) session.commit() data = { "id": song.id } response = self.client.delete("/api/songs", data=json.dumps(data), content_type="application/json", headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 302)
def post_songs(self): """ Post songs Test """ test_song = {"file": {"id": 9}} new_song = models.Song(test_song) session.add(test_song) session.commit() response = self.client.post("/api/songs") self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") data = json.loads(response.data.decode("ascii")) self.assertEqual(len(data), 2)
def testPostNewSong(self): """Post a new song to the DB""" # Create a file in the DB for the app to find file = models.File(name="BornThisWay.mp3") session.add(file) session.commit() # Compile posted data into a dictionary for easy conversion to JSON data = {"file": {"id": 1}} # Collect the response from the endpoint # use json.dumps to convert dict > JSON # use content_type to indicate the type of content in data response = self.client.post( "/api/songs", data=json.dumps(data), content_type="application/json", headers=[("Accept", "application/json")], ) # Verify request to endpoint was successful using 201 created self.assertEqual(response.status_code, 201) # Verify that the response is JSON type self.assertEqual(response.mimetype, "application/json") # Verify the endpoint is setting the correct Location header # This should be the link to the new post self.assertEqual( urlparse(response.headers.get("Location")).path, "/api/songs") # Decode the response with json.loads song = json.loads(response.data) # Extrapolate the file info file = song["file"] # Validate the response self.assertEqual(song["id"], 1) self.assertEqual(file["id"], 1) self.assertEqual(file["name"], "BornThisWay.mp3") # Query DB to validate status song = session.query(models.Song).all() # Verify only one item in DB self.assertEqual(len(song), 1) # Isolate the one item in the list song = song[0] # Validate the content of the item retrieved from the DB self.assertEqual(song.id, 1) self.assertEqual(song.file_id, 1) self.assertEqual(song.file.name, "BornThisWay.mp3")
def test_post_song_successful(self): file = models.File(filename="Soulful Strut.mp3") session.add(file) session.commit() data = { "file": { "id": file.id } } response = self.client.post("/api/songs", data=json.dumps(data), content_type="application/json", headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 302) self.assertEqual(response.mimetype, "text/html")
def test_get_song(self): """ Getting a single song from a populated database """ fileA = models.File(name="SongA.mp3") songA = models.Song(file=fileA) fileB = models.File(name="SongB.mp3") songB = models.Song(file=fileB) session.add_all([fileA, fileB, songA, songB]) session.commit() response = self.client.get("/api/songs/{}".format(songB.id), headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") data = json.loads(response.data.decode("ascii")) self.assertEqual(data["file"]["name"], "SongB.mp3")
def testPostSongInvalidData(self): """ Trying to add a new song but passing invalid data """ fileA = models.File(filename = "FileA.mp3") session.add(fileA) session.commit() data = json.dumps({"file": {"id": "invaliddata"}}) response = self.client.post("/api/songs", data=data, content_type="application/json", headers=[("Accept", "application/json")] ) self.assertEqual(response.status_code, 422) data = json.loads(response.data) self.assertEqual(data["message"], "u'invaliddata' is not of type 'integer'")