Skip to content

Commit

Permalink
Improve test coverage: Part 1
Browse files Browse the repository at this point in the history
This covers the model tests.
  • Loading branch information
DocMarty84 committed Mar 27, 2018
1 parent f72077e commit 53ec5b2
Show file tree
Hide file tree
Showing 15 changed files with 544 additions and 11 deletions.
8 changes: 7 additions & 1 deletion models/oomusic_converter.py
Expand Up @@ -3,8 +3,10 @@
import multiprocessing.dummy as mp
from multiprocessing import cpu_count
import os
import random
from shutil import copyfile, move
from tempfile import gettempdir
from time import sleep

from odoo import fields, models, api

Expand Down Expand Up @@ -202,7 +204,11 @@ class MusicConverterLine(models.Model):
def convert(self):
with api.Environment.manage():
with self.pool.cursor() as cr:
new_self = self.with_env(self.env(cr))
if not self.env.context.get('test_mode'):
new_self = self.with_env(self.env(cr))
else:
new_self = self
sleep(random.random())
# In case we manually canceled the job between line selection and conversion
if new_self.state != 'waiting':
return
Expand Down
6 changes: 5 additions & 1 deletion tests/__init__.py
@@ -1,3 +1,7 @@
# -*- coding: utf-8 -*-
from . import test_common
from . import test_album
from . import test_artist
from . import test_converter
from . import test_folder_scan
from . import test_folder
from . import test_playlist
Binary file modified tests/folder_scan/Artist1/Album1/song1.mp3
Binary file not shown.
Binary file modified tests/folder_scan/Artist1/Album1/song2.mp3
Binary file not shown.
Binary file modified tests/folder_scan/Artist1/Album2/song3.mp3
Binary file not shown.
Binary file modified tests/folder_scan/Artist1/Album2/song4.mp3
Binary file not shown.
Binary file modified tests/folder_scan/Artist2/Album3/song5.mp3
Binary file not shown.
Binary file modified tests/folder_scan/Artist2/Album3/song6.mp3
Binary file not shown.
44 changes: 44 additions & 0 deletions tests/test_album.py
@@ -0,0 +1,44 @@
# -*- coding: utf-8 -*-

from . import test_common


class TestOomusicAlbum(test_common.TestOomusicCommon):

def test_00_action_add_to_playlist(self):
'''
Test adding an album to a playlist
'''
self.FolderScanObj.with_context(test_mode=True)._scan_folder(self.Folder.id)

album1 = self.AlbumObj.search([('name', '=', 'Album1')])
album2 = self.AlbumObj.search([('name', '=', 'Album2')])
playlist = self.PlaylistObj.search([('current', '=', True)], limit=1)

# Check data prior any action
self.assertEqual(len(album1), 1)
self.assertEqual(len(album2), 1)
self.assertEqual(len(playlist), 1)

# Add Album1
album1.action_add_to_playlist()
self.assertEqual(
playlist.mapped('playlist_line_ids').mapped('track_id').mapped('name'),
[u'Song1', u'Song2']
)

# Check no duplicate is created
album1.action_add_to_playlist()
self.assertEqual(
playlist.mapped('playlist_line_ids').mapped('track_id').mapped('name'),
[u'Song1', u'Song2']
)

# Purge and replace by Album2
album2.with_context(purge=True).action_add_to_playlist()
self.assertEqual(
playlist.mapped('playlist_line_ids').mapped('track_id').mapped('name'),
[u'Song3', u'Song4']
)

self.cleanUp()
44 changes: 44 additions & 0 deletions tests/test_artist.py
@@ -0,0 +1,44 @@
# -*- coding: utf-8 -*-

from . import test_common


class TestOomusicArtist(test_common.TestOomusicCommon):

def test_00_action_add_to_playlist(self):
'''
Test adding an artist to a playlist
'''
self.FolderScanObj.with_context(test_mode=True)._scan_folder(self.Folder.id)

artist1 = self.ArtistObj.search([('name', '=', 'Artist1')])
artist2 = self.ArtistObj.search([('name', '=', 'Artist2')])
playlist = self.PlaylistObj.search([('current', '=', True)], limit=1)

# Check data prior any action
self.assertEqual(len(artist1), 1)
self.assertEqual(len(artist2), 1)
self.assertEqual(len(playlist), 1)

# Add Artist1
artist1.action_add_to_playlist()
self.assertEqual(
playlist.mapped('playlist_line_ids').mapped('track_id').mapped('name'),
[u'Song3', u'Song4', u'Song1', u'Song2']
)

# Check no duplicate is created
artist1.action_add_to_playlist()
self.assertEqual(
playlist.mapped('playlist_line_ids').mapped('track_id').mapped('name'),
[u'Song3', u'Song4', u'Song1', u'Song2']
)

# Purge and replace by Artist2
artist2.with_context(purge=True).action_add_to_playlist()
self.assertEqual(
playlist.mapped('playlist_line_ids').mapped('track_id').mapped('name'),
[u'Song5', u'Song6']
)

self.cleanUp()
5 changes: 3 additions & 2 deletions tests/test_common.py
Expand Up @@ -7,18 +7,19 @@
from odoo.tests import common


@odoo.tests.common.at_install(True)
@odoo.tests.common.post_install(True)
class TestOomusicCommon(common.TransactionCase):

def setUp(self):
super(TestOomusicCommon, self).setUp()

self.AlbumObj = self.env['oomusic.album']
self.ArtistObj = self.env['oomusic.artist']
self.ConverterObj = self.env['oomusic.converter']
self.FolderScanObj = self.env['oomusic.folder.scan']
self.FolderObj = self.env['oomusic.folder']
self.GenreObj = self.env['oomusic.genre']
self.PlaylistLineObj = self.env['oomusic.playlist.line']
self.PlaylistObj = self.env['oomusic.playlist']
self.TrackObj = self.env['oomusic.track']

self.cleanUp()
Expand Down
202 changes: 202 additions & 0 deletions tests/test_converter.py
@@ -0,0 +1,202 @@
# -*- coding: utf-8 -*-

import hashlib
from multiprocessing import cpu_count
import os
import shutil
from tempfile import gettempdir

from . import test_common
from odoo import fields


class TestOomusicConverter(test_common.TestOomusicCommon):

def test_00_create_interact(self):
'''
Test creation and basic interaction
'''
self.FolderScanObj.with_context(test_mode=True)._scan_folder(self.Folder.id)
conv = self.ConverterObj.create({})

# Check default data
data_default = (
conv.name,
conv.user_id,
conv.state,
conv.transcoder_id,
conv.dest_folder,
conv.max_threads,
conv.norm,
conv.progress,
conv.show_waiting,
)
data_ref = (
fields.Date.today(),
self.env.user,
u'draft',
self.env.ref('oomusic.oomusic_transcoder_0'),
os.path.join(gettempdir(), 'koozic', fields.Date.today()),
cpu_count(),
False,
0.0,
False,
)
self.assertEqual(data_default, data_ref)

# _onchange_album_id
album1 = self.AlbumObj.search([('name', '=', 'Album1')])
conv.album_id = album1
conv._onchange_album_id()
self.assertEqual(conv.album_id, self.AlbumObj)
self.assertEqual(
conv.mapped('converter_line_ids').mapped('track_id').mapped('name'),
[u'Song1', u'Song2']
)
conv.action_purge()

# _onchange_artist_id
artist1 = self.ArtistObj.search([('name', '=', 'Artist1')])
conv.artist_id = artist1
conv._onchange_artist_id()
self.assertEqual(conv.artist_id, self.ArtistObj)
self.assertEqual(
conv.mapped('converter_line_ids').mapped('track_id').mapped('name'),
[u'Song3', u'Song4', u'Song1', u'Song2']
)
conv.action_purge()

# _onchange_playlist_id
artist2 = self.ArtistObj.search([('name', '=', 'Artist2')])
artist2.action_add_to_playlist()
playlist = self.PlaylistObj.search([('current', '=', True)], limit=1)
conv.playlist_id = playlist
conv._onchange_playlist_id()
self.assertEqual(conv.playlist_id, self.PlaylistObj)
self.assertEqual(
conv.mapped('converter_line_ids').mapped('track_id').mapped('name'),
[u'Song5', u'Song6']
)
conv.action_purge()

# _onchange_transcoder_id
conv._onchange_transcoder_id()
self.assertEqual(
conv.bitrate,
self.env.ref('oomusic.oomusic_transcoder_0').bitrate
)

# action_run
conv.artist_id = artist2
conv._onchange_artist_id()
conv.action_run()
conv.invalidate_cache()
self.assertEqual(conv.state, u'running')
self.assertEqual(
conv.converter_line_ids.mapped('state'),
[u'waiting', u'waiting']
)

# action_cancel
conv.action_cancel()
conv.invalidate_cache()
self.assertEqual(conv.state, u'cancel')
self.assertEqual(
conv.converter_line_ids.mapped('state'),
[u'cancel', u'cancel']
)

# action_draft
conv.action_draft()
conv.invalidate_cache()
self.assertEqual(conv.state, u'draft')
self.assertEqual(
conv.converter_line_ids.mapped('state'),
[u'draft', u'draft']
)

self.cleanUp()

def test_10_convert(self):
'''
Test conversion when files are converted thanks to FFMpeg
'''

self.FolderScanObj.with_context(test_mode=True)._scan_folder(self.Folder.id)
conv = self.ConverterObj.create({})

conv.album_id = self.AlbumObj.search([('name', '=', 'Album1')])
conv.bitrate = 92
conv._onchange_album_id()
conv.action_run()
conv.invalidate_cache()
conv.with_context(test_mode=True).action_convert()

self.assertEqual(conv.state, u'done')
self.assertEqual(
conv.converter_line_ids.mapped('state'),
[u'done', u'done']
)
path = os.path.join(conv.dest_folder, u'Artist1', u'Album1')
file1 = os.path.join(path, u'song1.mp3')
file2 = os.path.join(path, u'song2.mp3')
self.assertEqual(path, path)
self.assertEqual(file1, file1)
self.assertEqual(file2, file2)

sha1 = {}
for path in conv.converter_line_ids.mapped('track_id').mapped('path') + [file1, file2]:
sha1[path] = hashlib.sha1()
with open(path, 'rb') as f:
while True:
data = f.read(65536)
if not data:
sha1[path] = sha1[path].hexdigest()
break
sha1[path].update(data)
self.assertEqual(len(set(sha1.values())), 4)

self.cleanUp()
shutil.rmtree(conv.dest_folder, True)

def test_20_convert_copy(self):
'''
Test conversion when files are copied to avoid upsampling
'''

self.FolderScanObj.with_context(test_mode=True)._scan_folder(self.Folder.id)
conv = self.ConverterObj.create({})

conv.album_id = self.AlbumObj.search([('name', '=', 'Album1')])
conv.bitrate = 320
conv._onchange_album_id()
conv.action_run()
conv.invalidate_cache()
conv.with_context(test_mode=True).action_convert()

self.assertEqual(conv.state, u'done')
self.assertEqual(
conv.converter_line_ids.mapped('state'),
[u'done', u'done']
)
path = os.path.join(conv.dest_folder, u'Artist1', u'Album1')
file1 = os.path.join(path, u'song1.mp3')
file2 = os.path.join(path, u'song2.mp3')
self.assertEqual(path, path)
self.assertEqual(file1, file1)
self.assertEqual(file2, file2)

sha1 = {}
for path in conv.converter_line_ids.mapped('track_id').mapped('path') + [file1, file2]:
sha1[path] = hashlib.sha1()
with open(path, 'rb') as f:
while True:
data = f.read(65536)
if not data:
sha1[path] = sha1[path].hexdigest()
break
sha1[path].update(data)
self.assertEqual(len(set(sha1.values())), 2)

self.cleanUp()
shutil.rmtree(conv.dest_folder, True)

0 comments on commit 53ec5b2

Please sign in to comment.