Ejemplo n.º 1
0
    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")
Ejemplo n.º 2
0
    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")
Ejemplo n.º 3
0
    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')
Ejemplo n.º 4
0
    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')
Ejemplo n.º 5
0
 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\'')
Ejemplo n.º 6
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")
Ejemplo n.º 7
0
 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')
Ejemplo n.º 8
0
 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\'')
Ejemplo n.º 9
0
 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")
Ejemplo n.º 10
0
    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)
Ejemplo n.º 11
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)
Ejemplo n.º 12
0
    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")
Ejemplo n.º 13
0
    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'})
Ejemplo n.º 14
0
    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"])
Ejemplo n.º 15
0
    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)
Ejemplo n.º 16
0
    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)
Ejemplo n.º 17
0
    def test_put_song(self):

        fileA = models.File(name='fileA.mp3')
        songA = models.Song(file_id=fileA.id)
        fileB = models.File(name='fileB.mp3')

        session.add_all([fileA, fileB, 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, 200)
        self.assertEqual(response.mimetype, "application/json")

        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)
        self.assertEqual(song.file_id, 2)
Ejemplo n.º 18
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)
Ejemplo n.º 19
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")
Ejemplo n.º 20
0
    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")
Ejemplo n.º 21
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)
Ejemplo n.º 22
0
    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)
Ejemplo n.º 23
0
    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")
Ejemplo n.º 24
0
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()
Ejemplo n.º 25
0
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()
Ejemplo n.º 26
0
    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")
Ejemplo n.º 27
0
    def test_delete_song(self):
        """ Remove a song from the database """
        testfile = File(name="chords.wav")
        newsong = Song(file_id=testfile.id)
        session.add_all([testfile, newsong])
        session.commit()

        data = {"id": newsong.id}
        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")

        data = json.loads(response.data.decode("ascii"))
        self.assertEqual(data["message"], "Song #1 deleted")
Ejemplo n.º 28
0
 def test_delete_song(self):
     ''' delete a song '''
     
     file = models.File(filename = 'Test Song A.mp3')
     song = models.Song(file = file)
     session.add_all([file, song])
     session.commit()
     
     response = self.client.delete('/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(data['message'],
         'Successfully deleted song with id 1')
Ejemplo n.º 29
0
    def test_get_song(self):
        """ Getting a single post from a populated db """
        fileA = models.File(filename="testA.wav")
        songA = models.Song(file=fileA)
        
        fileB = models.File(filename="testB.wav")
        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")

        song = json.loads(response.data.decode("ascii"))
        self.assertEqual(song["file"]["filename"], "testB.wav")
Ejemplo n.º 30
0
 def testGetSingleSong(self):
   """ Get a single song """
   fileA = models.File(filename = "FileA.mp3")
   fileB = models.File(filename = "FileB.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/2", headers=[("Accept", "application/json")])
   
   self.assertEqual(response.status_code, 200)
   self.assertEqual(response.mimetype, "application/json")
   
   song = json.loads(response.data)
   self.assertEqual(song["id"], 2)
   self.assertEqual(song["file"]["id"], 2)
   self.assertEqual(song["file"]["name"], "FileB.mp3")
Ejemplo n.º 31
0
 def test_delete_song(self):
     """ Deleting a single song from a populated database """
     fileA = models.File(filename = "hello.mp3")
     fileB = models.File(filename = "bye.mp3")
     songA = models.Song(file = fileA)
     songB = models.Song(file = fileB)
     
     session.add_all([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")
     
     song = json.loads(response.data.decode("ascii"))
     self.assertEqual(song["file"]["filename"], fileB.filename)
Ejemplo n.º 32
0
    def testGetSongs(self):
        """ Get a list of all the songs from a populated DB """

        # Create testing data
        # Create a couple of sample files
        fileA = models.File(name="BornThisWay.mp3")
        fileB = models.File(name="PokerFace.mp3")
        # Add them to the session and commit
        session.add_all([fileA, fileB])
        session.commit()
        # Create a couple of songs from the files
        songA = models.Song(file_id=fileA.id)
        songB = models.Song(file_id=fileB.id)
        # And add/commit
        session.add_all([songA, songB])
        session.commit()

        # Go to the page and get the response from the server, store it here
        # This is the part actually being tested
        response = self.client.get(
            "/api/songs",
            headers=[("Accept", "application/json")],
        )

        # This is the actual test
        # Was the request to the endpoint successful?
        self.assertEqual(response.status_code, 200)
        # Did the request return a JSON object?
        self.assertEqual(response.mimetype, "application/json")
        # Decode the data using json.loads
        data = json.loads(response.data)
        # Verify that two songs have been returned
        self.assertEqual(len(data), 2)
        # Verify the contents of both songs as correct
        songA = data[0]
        self.assertEqual(songA["id"], 1)
        fileA = songA["file"]
        self.assertEqual(fileA["name"], "BornThisWay.mp3")
        songB = data[1]
        self.assertEqual(songB["id"], 2)
        fileB = songB["file"]
        self.assertEqual(fileB["name"], "PokerFace.mp3")
Ejemplo n.º 33
0
    def test_update_song_name(self):
        """ Rename a song """
        testfile = File(name="chords.wav")
        testfile2 = File(name="blah.flac")
        newsong = Song(file_id=testfile.id)
        session.add_all([testfile, testfile2, newsong])
        session.commit()

        data = {"id": newsong.id, "file": {"id": testfile2.id}}

        response = self.client.put("/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")

        data = json.loads(response.data.decode("ascii"))
        self.assertEqual(data["message"], "Song #1 updated")
Ejemplo n.º 34
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)
Ejemplo n.º 35
0
 def test_get_songs(self):
     """ Gets songs from populated database """
     
     FileA = models.File(filename="beyonce.mp3")
     FileB=  models.File(filename="charliePuth.mp3")
     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.decode("ascii"))
     self.assertEqual(len(data), 2)
     songA=data[0]
     self.assertEqual(songA["file"]["id"], FileA.id)
     songB=data[1]
     self.assertEqual(songB["file"]["id"], FileB.id)
Ejemplo n.º 36
0
    def test_get_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)
        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/{}'.format(song_B.id),
            headers=[("Accept", "application/json")])

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

        song = json.loads(response.data.decode('ascii'))
        self.assertEqual(song['id'], song_B.id)
        self.assertEqual(song['file']['id'], song_B.file_.id)
        self.assertEqual(song['file']['name'], song_B.file_.name)
Ejemplo n.º 37
0
 def test_post_update_song_successful(self):
     fileobj1 = models.File(filename="Soulful Strut.mp3")
     fileobj2 = models.File(filename="Baker Street.mp3")
     song = models.Song()
     fileobj1.song_id = song.id
     session.add_all([fileobj1, fileobj2, song])
     session.commit()
     data = {
         "file": {
             "id": fileobj2.id
         }
     }
     
     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, 201)
     self.assertEqual(response.mimetype, "application/json")
Ejemplo n.º 38
0
    def testGetSongs(self):
        """ Get a list of all the songs from a populated DB """

        # Create testing data
        # Create a couple of sample files
        fileA = models.File(name="BornThisWay.mp3")
        fileB = models.File(name="PokerFace.mp3")
        # Add them to the session and commit
        session.add_all([fileA, fileB])
        session.commit()
        # Create a couple of songs from the files
        songA = models.Song(file_id=fileA.id)
        songB = models.Song(file_id=fileB.id)
        # And add/commit
        session.add_all([songA, songB])
        session.commit()

        # Go to the page and get the response from the server, store it here
        # This is the part actually being tested
        response = self.client.get("/api/songs",
                                   headers=[("Accept", "application/json")],
                                   )

        # This is the actual test
        # Was the request to the endpoint successful?
        self.assertEqual(response.status_code, 200)
        # Did the request return a JSON object?
        self.assertEqual(response.mimetype, "application/json")
        # Decode the data using json.loads
        data = json.loads(response.data)
        # Verify that two songs have been returned
        self.assertEqual(len(data), 2)
        # Verify the contents of both songs as correct
        songA = data[0]
        self.assertEqual(songA["id"], 1)
        fileA = songA["file"]
        self.assertEqual(fileA["name"], "BornThisWay.mp3")
        songB = data[1]
        self.assertEqual(songB["id"], 2)
        fileB = songB["file"]
        self.assertEqual(fileB["name"], "PokerFace.mp3")
Ejemplo n.º 39
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)
Ejemplo n.º 40
0
 def testGetSongs(self):
     """ Test the GET method on /api/songs. Should return a big old dictionary with their      names"""
     
     #Make some songs and files
     filea = models.File(name="test")
     songa = models.Song(file=1)
     fileb = models.File(name="another test")
     songb = models.Song(file=2)
     filec = models.File(name="yet another test")
     songc = models.Song(file=3)
     
     session.add_all([filea, songa, fileb, songb, filec, songc])
     session.commit()
     
     #Call up the list of songs
     response = self.client.get("/api/songs",
                headers=[("Accept", "application/json")]                                  
                )
     
     #See that it's returning the right code and the right data type
     self.assertEqual(response.status_code, 200)
     self.assertEqual(response.mimetype, "application/json")
     
     #Make sure there's the right number of posts
     data = json.loads(response.data)
     self.assertEqual(len(data), 3)
         
     #Test to see that they're formatted right
     songa = data[0]
     print songa
     self.assertEqual(songa["id"], 1)
     self.assertEqual(songa["file"]["name"], "test")
     
     songb = data[1]
     self.assertEqual(songb["id"], 2)
     self.assertEqual(songb["file"]["name"], "another test")
     
     songc = data[2]
     self.assertEqual(songc["id"], 3)
     self.assertEqual(songc["file"]["name"], "yet another test")
Ejemplo n.º 41
0
    def test_get_songs(self):
        """ Getting songs from a populated database """
        
        songA = models.Song()
        fileA = models.File(name="A test", song_id= 1)
        
        songB = models.Song()
        fileB = models.File(name="A test", song_id= 2)


        session.add_all([songA, fileA, songB, fileB])
        session.commit()
        

        response = self.client.get("/api/songs")
        
        self.assertEqual(response.mimetype, "application/json")
        self.assertEqual(response.status_code, 200)

        data = json.loads(response.data.decode("ascii"))

        self.assertEqual(len(data), 2)
Ejemplo n.º 42
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'})
Ejemplo n.º 43
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)
Ejemplo n.º 44
0
    def test_post_song(self):
        """ Test POSTing a song to the database"""
        # Add test files to 'files' table
        fileA = models.File(filename='love_song.mp3')
        fileB = models.File(filename='another_song.mp3')
        session.add_all([fileA, fileB])
        session.commit()

        # Post a song to the database
        data = {'file': {'id': fileA.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['file']['name'], 'love_song.mp3')
        self.assertEqual(data['file']['id'], fileA.id)
Ejemplo n.º 45
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')
Ejemplo n.º 46
0
    def test_get_songs(self):
        """ Test GET songs from populated database """
        # Add test files and songs to database
        fileA = models.File(filename='love_song.mp3')
        fileB = models.File(filename='another_song.mp3')
        session.add_all([fileA, fileB])

        songA = models.Song(files=fileA)
        songB = models.Song(files=fileB)
        session.add_all([songA, songB])
        session.commit()

        # Ensure endpoint exists and is returning JSON
        response = self.client.get("/api/songs",
                                   headers=[("Accept", "application/json")])
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.mimetype, "application/json")

        # Ensure 'songs' table contains two entries related to those in the 'files' table
        data = json.loads(response.data.decode('ascii'))
        self.assertEqual(len(data), 2)
        self.assertEqual(data[0]['file']['id'], fileA.id)
        self.assertEqual(data[1]['file']['id'], fileB.id)
Ejemplo n.º 47
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")
Ejemplo n.º 48
0
    def test_get_songs(self):
        """ Getting songs from a populated database """
        fileA = models.File(name="Shady_Grove.mp3")

        songA = models.Song(filename=fileA)
        session.add_all([songA, fileA])
        session.commit()

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

        #Was request to endpoint successful?
        self.assertEqual(response.status_code, 200)
        #Was the response a JSON object
        self.assertEqual(response.mimetype, "application/json")

        #decode data
        data = json.loads(response.data.decode("ascii"))
        #check to see if it is only one song in data
        self.assertEqual(len(data), 1)

        songA = data[0]
        self.assertEqual(fileA.name, "Shady_Grove.mp3")
        self.assertEqual(songA["id"], 1)
Ejemplo n.º 49
0
from tuneful.database import session
from tuneful.models import Song, File
fileA = File(name="Shady_Grove.mp3")
songA = Song(filename=fileA)

session.add_all(songA, fileA)
session.commit()

print(songA)
Ejemplo n.º 50
0
def add_song():
    drones = models.File(filename="Drones.mp3")
    drones_id = models.Song(file_id=1)
    session.add_all([drones, drones_id])
    session.commit()