Skip to content

Commit

Permalink
Various improvements to unit tests
Browse files Browse the repository at this point in the history
This PR includes:
- Many more mocked functionality
- Various new tests
  • Loading branch information
dagwieers committed May 12, 2019
1 parent 8e80b78 commit 22243dd
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 18 deletions.
37 changes: 31 additions & 6 deletions test/apihelpertests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,63 @@

# GNU General Public License v3.0 (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

# pylint: disable=unused-variable

from __future__ import absolute_import, division, unicode_literals
from __future__ import absolute_import, division, print_function, unicode_literals
import mock
import os
import polib
import unittest

from resources.lib.vrtplayer import vrtapihelper

PO = polib.pofile('resources/language/resource.language.en_gb/strings.po')


def get_localized_string(msgctxt):
for entry in PO:
if entry.msgctxt == '#%s' % msgctxt:
return entry.msgstr
return 'vrttest'


def log_notice(msg, level):
print('%s: %s' % (level, msg))


class ApiHelperTests(unittest.TestCase):

_kodiwrapper = mock.MagicMock()
_kodiwrapper.get_proxies = mock.MagicMock(return_value=dict())

_kodiwrapper.check_if_path_exists = mock.MagicMock(side_effect=os.path.exists)
_kodiwrapper.get_localized_dateshort = mock.MagicMock(return_value='%d-%m-%Y')
_kodiwrapper.get_localized_string.return_value = '"[B][COLOR blue]Available until %s[/COLOR][/B]\n"'
_kodiwrapper.get_localized_string = mock.MagicMock(side_effect=get_localized_string)
_kodiwrapper.get_proxies = mock.MagicMock(return_value=dict())
_kodiwrapper.log_notice = mock.MagicMock(side_effect=log_notice)
_kodiwrapper.make_dir.return_value = None
_apihelper = vrtapihelper.VRTApiHelper(_kodiwrapper)

def test_get_api_data_single_season(self):
title_items, sort, ascending = self._apihelper.get_episode_items(path='/vrtnu/a-z/het-journaal.relevant/')
self.assertTrue(123 < len(title_items) < 129)
self.assertTrue(123 < len(title_items) < 129, 'We got %s items instead.' % len(title_items))
self.assertEqual(sort, 'dateadded')
self.assertFalse(ascending)

def test_get_api_data_multiple_seasons(self):
title_items, sort, ascending = self._apihelper.get_episode_items(path='/vrtnu/a-z/thuis.relevant/')
self.assertTrue(len(title_items) < 5)
self.assertEqual(sort, 'label')
self.assertFalse(ascending)

def test_get_api_data_specific_season(self):
title_items, sort, ascending = self._apihelper.get_episode_items(path='/vrtnu/a-z/pano.relevant/')
self.assertEqual(len(title_items), 4)
self.assertEqual(sort, 'label')
self.assertFalse(ascending)

def test_get_api_data_specific_season_without_broadcastdate(self):
title_items, sort, ascending = self._apihelper.get_episode_items(path='/vrtnu/a-z/postbus-x.relevant/')
self.assertEqual(len(title_items), 3)
self.assertEqual(sort, 'label')
self.assertTrue(ascending)


if __name__ == '__main__':
Expand Down
24 changes: 22 additions & 2 deletions test/searchtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,38 @@

# GNU General Public License v3.0 (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, unicode_literals
from __future__ import absolute_import, division, print_function, unicode_literals
from contextlib import contextmanager
import mock
import os
import unittest

from resources.lib.vrtplayer import vrtapihelper


@contextmanager
def open_file(path, flags='r'):
f = open(path, flags)
yield f
f.close()


def log_notice(msg, level):
print('%s: %s' % (level, msg))


class TestVRTPlayer(unittest.TestCase):

_kodiwrapper = mock.MagicMock()
_kodiwrapper.get_proxies = mock.MagicMock(return_value=dict())
_kodiwrapper.check_if_path_exists = mock.MagicMock(side_effect=os.path.exists)
_kodiwrapper.get_localized_dateshort = mock.MagicMock(return_value='%d-%m-%Y')
_kodiwrapper.get_localized_string.return_value = '"[B][COLOR blue]Available until %s[/COLOR][/B]\n"'
_kodiwrapper.get_proxies = mock.MagicMock(return_value=dict())
_kodiwrapper.get_userdata_path.return_value = './userdata/'
_kodiwrapper.log_notice = mock.MagicMock(side_effect=log_notice)
_kodiwrapper.make_dir.return_value = None
_kodiwrapper.open_file = mock.MagicMock(side_effect=open_file)
_kodiwrapper.stat_file = mock.MagicMock(side_effect=os.stat)
_apihelper = vrtapihelper.VRTApiHelper(_kodiwrapper)

def test_search_journaal(self):
Expand Down
16 changes: 10 additions & 6 deletions test/streamservicetests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# pylint: disable=unused-variable

from __future__ import absolute_import, division, unicode_literals
from __future__ import absolute_import, division, print_function, unicode_literals
from datetime import datetime, timedelta
import dateutil.tz
import mock
Expand Down Expand Up @@ -36,6 +36,10 @@ def get_localized_string(msgctxt):
return 'vrttest'


def log_notice(msg, level):
print('%s: %s' % (level, msg))


use_drm = False
now = datetime.now(dateutil.tz.tzlocal())
yesterday = now + timedelta(days=-1)
Expand All @@ -44,15 +48,15 @@ def get_localized_string(msgctxt):
class StreamServiceTests(unittest.TestCase):

_kodiwrapper = mock.MagicMock()
_kodiwrapper.get_proxies = mock.MagicMock(return_value=dict())
_kodiwrapper.check_if_path_exists.return_value = False
_kodiwrapper.check_inputstream_adaptive.return_value = True
_kodiwrapper.get_localized_dateshort = mock.MagicMock(return_value='%d-%m-%Y')
_kodiwrapper.get_localized_string.return_value = mock.MagicMock(side_effect=get_localized_string)
_kodiwrapper.get_localized_string = mock.MagicMock(side_effect=get_localized_string)
_kodiwrapper.get_proxies = mock.MagicMock(return_value=dict())
_kodiwrapper.get_setting = mock.MagicMock(side_effect=get_settings)
_kodiwrapper.get_userdata_path.return_value = 'vrttest'
_kodiwrapper.check_if_path_exists.return_value = False
_kodiwrapper.log_notice = mock.MagicMock(side_effect=log_notice)
_kodiwrapper.make_dir.return_value = None
_kodiwrapper.open_path.return_value = False
_kodiwrapper.check_inputstream_adaptive.return_value = True
_apihelper = vrtapihelper.VRTApiHelper(_kodiwrapper)
_tokenresolver = tokenresolver.TokenResolver(_kodiwrapper)
_streamservice = streamservice.StreamService(_kodiwrapper, _tokenresolver)
Expand Down
9 changes: 7 additions & 2 deletions test/tvguidetests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# GNU General Public License v3.0 (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, unicode_literals
from __future__ import absolute_import, division, print_function, unicode_literals
from datetime import datetime
import dateutil.tz
import mock
Expand All @@ -14,11 +14,16 @@
channels = ['een', 'canvas', 'ketnet']


def log_notice(msg, level):
print('%s: %s' % (level, msg))


class TestTVGuide(unittest.TestCase):

_kodiwrapper = mock.MagicMock()
_kodiwrapper.get_proxies = mock.MagicMock(return_value=dict())
_kodiwrapper.get_localized_datelong = mock.MagicMock(return_value='%a %d-%m-%Y')
_kodiwrapper.get_proxies = mock.MagicMock(return_value=dict())
_kodiwrapper.log_notice = mock.MagicMock(side_effect=log_notice)
_tvguide = tvguide.TVGuide(_kodiwrapper)

def test_tvguide_date_menu(self):
Expand Down
22 changes: 20 additions & 2 deletions test/vrtplayertests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,37 @@

# pylint: disable=unused-variable

from __future__ import absolute_import, division, unicode_literals
from __future__ import absolute_import, division, print_function, unicode_literals
from contextlib import contextmanager
import mock
import os
import random
import unittest

from resources.lib.vrtplayer import CATEGORIES, CHANNELS, vrtapihelper, vrtplayer


@contextmanager
def open_file(path, flags='r'):
f = open(path, flags)
yield f
f.close()


def log_notice(msg, level):
print('%s: %s' % (level, msg))


class TestVRTPlayer(unittest.TestCase):

_kodiwrapper = mock.MagicMock()
_kodiwrapper.get_proxies = mock.MagicMock(return_value=dict())
_kodiwrapper.check_if_path_exists = mock.MagicMock(side_effect=os.path.exists)
_kodiwrapper.get_localized_dateshort = mock.MagicMock(return_value='%d-%m-%Y')
_kodiwrapper.get_proxies = mock.MagicMock(return_value=dict())
_kodiwrapper.get_userdata_path.return_value = './userdata/'
_kodiwrapper.log_notice = mock.MagicMock(side_effect=log_notice)
_kodiwrapper.open_file = mock.MagicMock(side_effect=open_file)
# _kodiwrapper.stat_file = mock.MagicMock(side_effect=os.stat)
_apihelper = vrtapihelper.VRTApiHelper(_kodiwrapper)
_vrtplayer = vrtplayer.VRTPlayer(_kodiwrapper, _apihelper)

Expand Down

0 comments on commit 22243dd

Please sign in to comment.