Skip to content

Commit

Permalink
Document test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
smlz committed Dec 6, 2018
1 parent 403df5c commit 7d91fe1
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ def testUpload(self):
from werkzeug.datastructures import FileStorage
from io import BytesIO

# Correct upload
resp = self.client.post(
'/music/',
data={'file': (BytesIO(b'testcontent'), 'test.mp3')},
Expand Down Expand Up @@ -149,7 +150,7 @@ def testUpload(self):
self.upload_analyzer.reset_mock()
self.processor.reset_mock()

# wrong attribute name
# Wrong attribute name
resp = self.client.post(
'/music/',
data={'not-file': (BytesIO(b'testcontent'), 'test.mp3')},
Expand All @@ -160,7 +161,7 @@ def testUpload(self):
self.upload_analyzer.assert_not_called()
self.processor.assert_not_called()

# file as normal text attribute
# File as normal text attribute
resp = self.client.post(
'/music/',
data={'file': 'testcontent', 'filename': 'test.mp3'},
Expand Down Expand Up @@ -302,12 +303,15 @@ def testRawFileAnalyzer(self):
from werkzeug.datastructures import FileStorage
from werkzeug.exceptions import UnprocessableEntity

# Missing file
self.assertRaises(UnprocessableEntity, raw_file_analyzer, 'music',
'fileId', '.mp3', None)

# Invalid file
self.assertRaises(UnprocessableEntity, raw_file_analyzer,
'music', 'fileId', '.xml', 'file')

# Correct file
fileStorage = FileStorage(filename='filename')
result = raw_file_analyzer('jingles', 'fileId', '.ogg', fileStorage)

Expand Down Expand Up @@ -351,17 +355,20 @@ def testRawFileProcessor(self):
addition = FileAddition(file_)
change = MetadataChange('key', 'value')
delete = FileDeletion()

# File addition
raw_file_processor('music', 'id1', '.mp3', [addition])
path = os.path.join(self.tempdir, 'music', 'id1.mp3')

self.assertTrue(os.path.isfile(path))
with open(path) as f:
self.assertEqual(f.read(), 'abc')

# File change (nothing happens) and deletion
raw_file_processor('music', 'id1', '.mp3', [change])
raw_file_processor('music', 'id1', '.mp3', [delete])
self.assertTrue(not os.path.isfile(path))

# Invalid deletion (not found)
self.assertRaises(NotFound, raw_file_processor,
'music', 'id1', '.mp3', [delete])

Expand All @@ -385,6 +392,7 @@ def testIndexProcessor(self):
self.assertTrue('fileId1' in data)
self.assertTrue('fileId2' in data)

# Set some initial metadata
index_processor('music', 'fileId1', '.mp3',
[MetadataChange('key1', 'value1')])
index_processor('music', 'fileId2', '.ogg',
Expand All @@ -401,7 +409,7 @@ def testIndexProcessor(self):
self.assertTrue('key2' in data['fileId2'])
self.assertEqual(data['fileId2']['key2'], 'value2')

# Metadata modification
# Modify metadata
index_processor('music', 'fileId1', '.mp3',
[MetadataChange('key1', 'value1-1'),
MetadataChange('key2', 'value2-1')])
Expand Down Expand Up @@ -460,6 +468,7 @@ def testPlaylistProcessor(self):
music_path = os.path.join(self.tempdir, 'music.m3u')
jingles_path = os.path.join(self.tempdir, 'jingles.m3u')

# Update playlist count (initial)
playlist_processor('music', 'fileId1', '.mp3',
[MetadataChange('count', 2)])

Expand All @@ -472,6 +481,7 @@ def testPlaylistProcessor(self):
data = f.read()
self.assertEqual(data, '')

# Update playlist count
playlist_processor('music', 'fileId1', '.mp3',
[MetadataChange('count', 4)])

Expand All @@ -484,6 +494,7 @@ def testPlaylistProcessor(self):
data = f.read()
self.assertEqual(data, '')

# Set playlist count to zero (same as delete)
playlist_processor('music', 'fileId1', '.mp3',
[MetadataChange('count', 0)])

Expand All @@ -495,6 +506,7 @@ def testPlaylistProcessor(self):
data = f.read()
self.assertEqual(data, '')

# Add more files to playlist
playlist_processor('music', 'fileId1', '.mp3',
[MetadataChange('count', 2)])
playlist_processor('music', 'fileId2', '.ogg',
Expand Down

0 comments on commit 7d91fe1

Please sign in to comment.