Пример #1
0
    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")
Пример #2
0
    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)
Пример #3
0
    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')
Пример #4
0
    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)
Пример #5
0
    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)
Пример #6
0
    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)
Пример #7
0
    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)
Пример #8
0
    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")
Пример #9
0
    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")
Пример #10
0
    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")
Пример #11
0
 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")
Пример #12
0
    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")
Пример #13
0
    def testGetSongs(self):
        """ Getting songs from a populated database """
        fileA = models.File(filename="FileA.mp3")
        fileB = models.File(filename="FileB.mp3")

        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)
        
        # Test number of songs
        songs = session.query(models.Song).all()
        self.assertEqual(len(songs), 2)

        songA = data[0]
        self.assertEqual(songA["id"], songA["file"]["id"])
        self.assertEqual(songA["file"]["name"], "FileA.mp3")

        songB = data[1]
        self.assertEqual(songB["id"], songB["file"]["id"])
        self.assertEqual(songB["file"]["name"], "FileB.mp3")
Пример #14
0
    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")
Пример #15
0
    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")
Пример #16
0
    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)
Пример #17
0
 def test_song_post(self):
     """ Posting a song to the database """
     data={
         "id" : 1,
         "file":{
             "id": 1,
             "filename" : "hello.mp3"
         }
     }
     
     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"]["id"], 1)
     self.assertEqual(data["file"]["filename"], "hello.mp3")
     
     songs = session.query(models.Song).all()
     self.assertEqual(len(songs), 1)
     
     song = songs[0]
     
     self.assertEqual(song.file.filename, "hello.mp3")
Пример #18
0
    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'})
Пример #19
0
    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")
Пример #20
0
    def test_post_song(self):
        #Add a new song
        data = {
            "file": {
                "name": "Example.mp3",
            }
        }

        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"], "Example.mp3")

        songs = session.query(models.Song).all()
        self.assertEqual(len(songs), 1)

        song = songs[0]
        self.assertEqual(song.file.name, "Example.mp3")
 def test_edit_song(self):
     self.test_add_song()
     
     data = {
         "filename": "edited_file_name!",
         "id": 1
     }
     
     response = self.client.put("/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["name"], "edited_file_name!")
     
     songs = session.query(models.Song).all()
     self.assertEqual(len(songs), 1)
     
     song = songs[0]
     self.assertEqual(song.file.filename, "edited_file_name!")
Пример #22
0
    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")
Пример #23
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)
Пример #24
0
    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)
Пример #25
0
    def test_delete_song(self):
        song = models.Song()
        file = models.File(name='fileA.mp3')
        song.file = file

        session.add_all([song, file])
        session.commit()

        self.assertEqual(session.query(models.Song).get(1), song)
        self.assertEqual(session.query(models.File).get(1), file)
        response = self.client.delete('/api/songs',
                headers=[('Accept', 'application/json')],
                content_type='application/json',
                data=json.dumps({'file': {'id': 1}})
                )

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.mimetype, 'application/json')
        self.assertEqual(session.query(models.Song).get(1), None)
        self.assertEqual(session.query(models.File).get(1), None)
Пример #26
0
    def test_song_update_post(self):
        """ Updating a song """
        song = models.Song()
        fileA = models.File(name='fileA.mp3')
        fileB = models.File(name='fileB.mp3')
        song.file = fileA

        session.add_all([song, fileA, fileB])
        session.commit()

        self.assertEqual(session.query(models.Song).get(1), song)
        self.assertEqual(session.query(models.File).get(1), fileA)
        self.assertEqual(session.query(models.File).get(2), fileB)

        data = {
                "file": {'id': 2} 
                }

        response = self.client.post("/api/songs/1",
                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.id, 1)
        self.assertEqual(song.file.id, 2)
        self.assertEqual(song.file.name, 'fileB.mp3')
        
        data = json.loads(response.data.decode('ascii'))

        self.assertEqual(data['id'], 1)
        self.assertEqual(data['file'], {'id': 2, 'name': 'fileB.mp3'})
Пример #27
0
    def test_post_put(self):
        """ Editing a post """
        data = {"id": 1, "file": {"id": 1, "name": "SongA"}}

        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")

        response = self.client.get("/api/songs",
                                   headers=[("Accept", "application/json")])

        data = json.loads(response.data.decode("ascii"))
        song = data[0]
        self.assertEqual(song["id"], 1)
        self.assertEqual(song["file"]["file_id"], 1)
        self.assertEqual(song["file"]["file_name"], "1")

        songs = session.query(models.Song).all()
        self.assertEqual(len(songs), 1)

        song = songs[0]
        self.assertEqual(song.id, 1)
        self.assertEqual(song.file.name, "1")

        data = {"id": 1, "file": {"id": 1, "name": "SongA remix"}}

        response = self.client.put("/api/songs/1",
                                   data=json.dumps(data),
                                   content_type="application/json",
                                   headers=[("Accept", "application/json")])
        self.assertEqual(response.status_code, 202)
        self.assertEqual(response.mimetype, "application/json")

        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"))

        song = data
        file = song["file"]
        self.assertEqual(song["id"], 1)
        self.assertEqual(file["file_name"], "SongA remix")
Пример #28
0
    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")
Пример #29
0
    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")
Пример #30
0
    def test_delete_single_song(self):
        """ Deleting a single song from the database """
        fileA = models.File(filename="FileA.mp3")
        session.add(fileA)
        session.commit()

        songA = models.Song(song_file_id=fileA.id)
        session.add(songA)
        session.commit()

        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")

        songs = session.query(models.Song).all()
        self.assertEqual(len(songs), 0)
Пример #31
0
    def test_post_songs(self):
        """ Post songs Test """
        test_file = models.File(name="test_file")

        session.add(test_file)
        session.commit()

        file_test = session.query(models.File).order_by(
            models.File.name).first()

        new_song = models.Song(file=file_test)

        session.add(new_song)
        session.commit()

        response = self.client.post("/api/songs")

        self.assertEqual(response.status_code, 415)
        self.assertEqual(response.mimetype, "application/json")
Пример #32
0
    def test_delete_song(self):

        new_file = models.File(name='New_File')
        session.add(new_file)
        session.commit()
        new_song = models.Song(file_id=new_file.id)
        session.add(new_song)
        session.commit()

        response = self.client.delete("/api/songs/{}".format(new_song.id),
                                      headers=[("Accept", "application/json")])

        session.delete(new_file)
        session.commit()

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.mimetype, "application/json")

        songs = session.query(models.Song).all()
        self.assertEqual(len(songs), 0)
Пример #33
0
    def test_update_single_song(self):
        """ Testing update a song """
        # First, create a new post
        fileB = models.File(filename="FileB.mp3")
        data_inject = {
            "file": {
                  "id": fileB.id
                    }
        }
        
        fileA = models.File(filename="FileA.mp3")
        

        session.add_all([fileA])
        session.commit()
        
        songA = models.Song(song_file_id= fileA.id)
        songB = models.Song(song_file_id= fileB.id)
        session.add_all([songA])
        session.commit()
  
        #Now edit the post with new data
        response = self.client.put("/api/songs/{}".format(songA.id),
                                      data=json.dumps(data_inject),
                                      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)
        
        # Test that it contains the new data
        
        self.assertEqual(data["id"], fileB.id)
        
        songs = session.query(models.Song).all()
        self.assertEqual(len(songs), 1)

        song = songs[0]
        self.assertEqual(song.id, fileB.id)
Пример #34
0
    def test_delete_song(self):
        """ Deleting a song from the app """
        new_file = models.File(filename='New_File')
        session.add(new_file)
        session.commit()
        new_song = models.Song(song_file_id=new_file.id)
        session.add(new_song)
        session.commit()

        response = self.client.delete(
            "/api/songs/{}".format(new_song.id),
            headers=[("Accept", "application/json")])

        session.delete(new_file)
        session.commit()

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.mimetype, "application/json")

        songs = session.query(models.Song).all()
        self.assertEqual(len(songs), 0)
Пример #35
0
    def test_song_delete(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)
        song2 = models.Song(original_file_id=file2.id)
        session.add_all([song1, song2])
        session.commit()
        
        response = self.client.delete(
            "/api/songs/{}".format(song1.id),
            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.id, file2.id)
Пример #36
0
 def test_put_song(self):
     ''' updating a song '''
     
     fileA = models.File(filename = 'Test Song A.mp3')
     fileB = models.File(filename = 'Test Song B.mp3')
     song = models.Song(file = fileA)
     session.add_all([fileA, fileB, song])
     session.commit()
     
     data = {
         "file": {
             "id": 2
         }
     }
     
     response = self.client.put('/api/songs/1',
         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/1')
     
     data = json.loads(response.data.decode('ascii'))
     self.assertEqual(data['id'], 1)
     self.assertEqual(data['file']['id'], 2)
     self.assertEqual(data['file']['name'], 'Test Song 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.filename, 'Test Song B.mp3')
Пример #37
0
    def test_delete_song(self):
        #Deleting songs 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.delete("/api/songs/{}".format(songA.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 with id 1")

        # Assert that there is only one song in the database
        songs = session.query(models.Song).all()
        self.assertEqual(len(songs), 1)
        # Assert that the right song was deleted
        song = songs[0]
        self.assertEqual(song.file.name, "SongB.mp3")
Пример #38
0
    def test_post_song(self):
        """ Posting a song """
        file = models.File(name="fileA")
        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")
        self.assertEqual(
            urlparse(response.headers.get("Location")).path, "/api/songs")

        data = json.loads(response.data.decode("ascii"))
        self.assertEqual(data["id"], 1)

        songs = session.query(models.Song).all()
        self.assertEqual(len(songs), 1)
 def test_delete_song(self):
     self.test_add_song()
     
     data = {
         "file": {
             "id": 1
         }
     }
     
     response = self.client.delete("/api/songs",
                 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()
     data = json.loads(response.data.decode("ascii"))
     self.assertEqual(data["message"], "Song has been deleted!")
     self.assertEqual(len(data), 1)
     self.assertEqual(len(songs), 0)
Пример #40
0
    def test_post_song(self):
        """ Posting a new song """
        songA = models.File(filename="Drones.mp3")
        session.add(songA)
        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")

        data = json.loads(response.data.decode("ascii"))
        self.assertEqual(data["file"]["id"], 1)
        self.assertEqual(data["file"]["filename"], "Drones.mp3")

        songs = session.query(models.Song).all()
        self.assertEqual(len(songs), 1)

        song = songs[0]
        self.assertEqual(song.file_id, 1)