Esempio n. 1
0
    def test_update_audiobook(self):
        with self.app() as client:
            with self.app_context():
                test_id = 1
                # Check record exists
                self.assertIsNotNone(AudioBook.find_by_id(test_id))

                request = client.put(f"/audiobook/{test_id}", \
                    data=json.dumps(self.update_data),\
                    headers={'content-type': 'application/json'})

                self.assertEqual(request.status_code, 200)

                record = AudioBook.find_by_id(test_id)

                self.assertIsNotNone(record)
                self.assertDictEqual(
                    AudioBookSchema().dump(AudioBook.find_by_id(test_id)),
                    json.loads(request.data))
                self.assertEqual(record.name, self.update_data["name"])
                self.assertEqual(record.duration, self.update_data["duration"])
                self.assertEqual(record.author.name,
                                 self.update_data["author"])
                self.assertEqual(record.narrator.name,
                                 self.update_data["narrator"])
Esempio n. 2
0
    def test_delete_audiobook(self):
        with self.app() as client:
            with self.app_context():
                test_id = 1

                # Check record exists before deleting
                self.assertIsNotNone(AudioBook.find_by_id(test_id))

                request = client.delete(f"/audiobook/{test_id}")

                # Check record doesn't exist after deleting
                self.assertIsNone(AudioBook.find_by_id(test_id))
    def test_update_audiobook(self):
        audiobook = AudioBook(**self.data)
        audiobook.save_to_db()

        # Get the record and check field values
        record = AudioBook.find_by_name(self.data["name"])
        self.assertIsNotNone(record)
        self.assertEqual(record.name, self.data["name"])
        self.assertEqual(record.duration, self.data["duration"])

        # Update field values
        update_data = {
            "name": "Updated Test AudioBook",
            "duration": 120,
            "author": Author("Updated author"),
            "narrator": Narrator("Updated Narrator")
        }
        AudioBook.update(update_data, record)

        # Check updated field values
        updated_record = AudioBook.find_by_id(record.id)
        self.assertIsNotNone(updated_record)
        self.assertEqual(updated_record.name, update_data["name"])
        self.assertEqual(updated_record.duration, update_data["duration"])
        self.assertIsInstance(updated_record.author, Author)
        self.assertEqual(str(updated_record.author.name),
                         update_data["author"].name)
        self.assertEqual(updated_record.narrator.name,
                         update_data["narrator"].name)
Esempio n. 4
0
    def get(self, audioFileType, audioFileID):
        if self.isValidInput(audioFileType, audioFileID):
            responseData = None
            audioFileID = int(audioFileID)
            
            if audioFileType == 'song':
                schema = SongSchema()
                song = Song.find_by_id(audioFileID)

                if song:
                    responseData = schema.dump(song)

            elif audioFileType == 'podcast':
                schema = PodcastSchema()
                podcast = Podcast.find_by_id(audioFileID)

                if podcast:
                    responseData = schema.dump(podcast)

            elif audioFileType == 'audiobook':
                schema = AudioBookSchema()
                audiobook = AudioBook.find_by_id(audioFileID)

                if audiobook:
                    responseData = schema.dump(audiobook)
            
            if responseData:
                return responseData, 200
            
            logging.warn(f"File with ID: {audioFileID} not found")
            return {'Message': 'File not found'}, 400

        logging.warn(f"AudioFileType or AudioFileID is not validd")
        return {'Message': 'AudioFileType or AudioFileID is not valid'}, 400 
Esempio n. 5
0
    def test_get_audiobook(self):
        with self.app() as client:
            with self.app_context():
                test_id = 1
                request = client.get(f"/audiobook/{test_id}")

                self.assertEqual(request.status_code, 200)
                self.assertDictEqual(
                    AudioBookSchema().dump(AudioBook.find_by_id(1)),
                    json.loads(request.data))
    def test_get_audiobook_by_id_or_name(self):
        # Check a record exists in the db
        audiobook = AudioBook(**self.data)
        audiobook.save_to_db()

        audiobook_byname = AudioBook.find_by_name(self.data["name"])
        self.assertIsNotNone(audiobook_byname)

        id = audiobook_byname.id

        # Check find_by_id method
        audiobook_byid = AudioBook.find_by_id(id)
        self.assertIsNotNone(audiobook_byid)
        self.assertEqual(audiobook_byid.name, audiobook_byname.name)
        self.assertEqual(audiobook_byid.uploaded_time,
                         audiobook_byname.uploaded_time)