Skip to content

Commit

Permalink
Add new tests for updates_test.py (#2050)
Browse files Browse the repository at this point in the history
* tests: remove obsolete update-related test cases; add new tests
* tests: make `updates_test` parametrized
  • Loading branch information
nicomiguelino authored Sep 3, 2024
1 parent 68d908f commit a52d0b1
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 33 deletions.
3 changes: 2 additions & 1 deletion requirements/requirements.dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ mock==3.0.5
nose2==0.15.1
pep8==1.7.1
selenium==3.141.0
time-machine==2.15.0
splinter==0.14.0
time-machine==2.15.0
unittest-parametrize==1.4.0
116 changes: 84 additions & 32 deletions tests/updates_test.py
Original file line number Diff line number Diff line change
@@ -1,49 +1,101 @@
from __future__ import unicode_literals
from unittest import skip, TestCase
from unittest_parametrize import parametrize, ParametrizedTestCase

import logging
import mock
import server
import os

from settings import settings
from lib.github import is_up_to_date

fancy_sha = 'deadbeaf'

GIT_HASH_1 = 'da39a3ee5e6b4b0d3255bfef95601890afd80709'
GIT_SHORT_HASH_1 = 'da39a3e'
GIT_HASH_2 = '6adfb183a4a2c94a2f92dab5ade762a47889a5a1'
GIT_SHORT_HASH_2 = '6adfb18'

@skip('fixme')
class UpdateTest(TestCase):
def setUp(self):
self.get_configdir_m = mock.patch(
'settings.AnthiasSettings.get_configdir',
mock.MagicMock(return_value='/tmp/.screenly/'),
)
self.get_configdir_m.start()

self.sha_file = settings.get_configdir() + 'latest_anthias_sha'
logging.disable(logging.CRITICAL)

if not os.path.exists(settings.get_configdir()):
os.mkdir(settings.get_configdir())

def tearDown(self):
if os.path.isfile(self.sha_file):
os.remove(self.sha_file)

self.get_configdir_m.stop()

class UpdateTest(ParametrizedTestCase):
@mock.patch(
'viewer.settings.get_configdir',
mock.MagicMock(return_value='/tmp/.screenly/'),
'lib.github.fetch_remote_hash',
mock.MagicMock(return_value=(None, False)),
)
def test_if_sha_file_not_exists__is_up_to_date__should_return_false(self):
self.assertEqual(server.is_up_to_date(), True)
def test__if_git_branch_env_does_not_exist__is_up_to_date_should_return_true(self): # noqa: E501
self.assertEqual(is_up_to_date(), True)

@parametrize(
'hashes, expected',
[
(
{
'latest_remote_hash': GIT_HASH_1,
'git_hash': GIT_HASH_1,
'git_short_hash': GIT_SHORT_HASH_1,
'latest_docker_hub_hash': GIT_SHORT_HASH_1,
},
True,
),
(
{
'latest_remote_hash': GIT_HASH_2,
'git_hash': GIT_HASH_1,
'git_short_hash': GIT_SHORT_HASH_1,
'latest_docker_hub_hash': GIT_SHORT_HASH_1,
},
True,
),
(
{
'latest_remote_hash': GIT_HASH_1,
'git_hash': GIT_HASH_1,
'git_short_hash': GIT_SHORT_HASH_1,
'latest_docker_hub_hash': GIT_SHORT_HASH_2,
},
True,
),
(
{
'latest_remote_hash': GIT_HASH_2,
'git_hash': GIT_HASH_1,
'git_short_hash': GIT_SHORT_HASH_1,
'latest_docker_hub_hash': GIT_SHORT_HASH_2,
},
False,
),
],
)
@mock.patch(
'viewer.settings.get_configdir',
mock.MagicMock(return_value='/tmp/.screenly/'),
'lib.github.get_git_branch',
mock.MagicMock(return_value='master'),
)
def test_if_sha_file_not_equals_to_branch_hash__is_up_to_date__should_return_false(self): # noqa: E501
def test_is_up_to_date_should_return_value_depending_on_git_hashes(
self, hashes, expected):
os.environ['GIT_BRANCH'] = 'master'
with open(self.sha_file, 'w+') as f:
f.write(fancy_sha)
self.assertEqual(server.is_up_to_date(), False)
del os.environ['GIT_BRANCH']
os.environ['DEVICE_TYPE'] = 'pi4'

latest_remote_hash = hashes['latest_remote_hash']
git_hash = hashes['git_hash']
git_short_hash = hashes['git_short_hash']
latest_docker_hub_hash = hashes['latest_docker_hub_hash']

with (
mock.patch(
'lib.github.fetch_remote_hash',
mock.MagicMock(return_value=(latest_remote_hash, False)),
),
mock.patch(
'lib.github.get_git_hash',
mock.MagicMock(return_value=git_hash),
),
mock.patch(
'lib.github.get_git_short_hash',
mock.MagicMock(return_value=git_short_hash),
),
mock.patch(
'lib.github.get_latest_docker_hub_hash',
mock.MagicMock(return_value=latest_docker_hub_hash),
),
):
self.assertEqual(is_up_to_date(), expected)

0 comments on commit a52d0b1

Please sign in to comment.