add unit test

This commit is contained in:
Mike McLean 2024-10-25 10:56:11 -04:00 committed by Jana Librova
parent 729c954f3d
commit e1b55ec23c
3 changed files with 58 additions and 5 deletions

32
tests/common.py Normal file
View file

@ -0,0 +1,32 @@
import locale
from functools import wraps
class mylocale:
"""Locale context manager. Resets to user default at exit."""
def __init__(self, value=None):
self.value = value
def __enter__(self):
locale.setlocale(locale.LC_ALL, locale=self.value)
def __exit__(self, _type, value, traceback):
# This resets to user default. Implementing a proper save and restore
# for locale is quite tricky, and this serves our testing needs.
locale.setlocale(locale.LC_ALL, "")
# return false so we don't eat exceptions
return False
def __call__(self, func):
"""When called, act as a decorator"""
@wraps(func)
def wrapped(*args, **kwargs):
with self:
return func(*args, **kwargs)
return wrapped
# the end

View file

@ -0,0 +1,7 @@
# comment ěščřčž
[koji]
# key_file = äöüß
; test comment with unicode 🫠
# test comment with unicode ☢️
setting = hello
value = world 🌍

View file

@ -3,7 +3,6 @@ from __future__ import absolute_import
import calendar import calendar
from datetime import datetime from datetime import datetime
import errno import errno
import locale
import logging import logging
import tempfile import tempfile
import threading import threading
@ -26,6 +25,7 @@ import six
import koji import koji
import koji.util import koji.util
from ..common import mylocale
class EnumTestCase(unittest.TestCase): class EnumTestCase(unittest.TestCase):
@ -407,6 +407,23 @@ class ConfigFileTestCase(unittest.TestCase):
self.assertEqual(listdir_mock.call_count, 2) self.assertEqual(listdir_mock.call_count, 2)
class ConfigFileTestCase2(unittest.TestCase):
"""Additional tests for config file reading functions"""
def setUp(self):
self.datadir = os.path.dirname(__file__) + '/data/cfg'
def tearDown(self):
mock.patch.stopall()
def test_unicode(self):
fn = self.datadir + '/uni1.conf'
if not os.path.exists(fn):
raise Exception('missing config')
with mylocale(value='C'):
koji.read_config_files(fn)
class MavenUtilTestCase(unittest.TestCase): class MavenUtilTestCase(unittest.TestCase):
"""Test maven relative functions""" """Test maven relative functions"""
maxDiff = None maxDiff = None
@ -750,10 +767,9 @@ class MavenUtilTestCase(unittest.TestCase):
config.read_file(conf_file) config.read_file(conf_file)
return config return config
@mylocale(('en_US', 'UTF-8'))
def test_formatChangelog(self): def test_formatChangelog(self):
"""Test formatChangelog function""" """Test formatChangelog function"""
# force locale to compare 'expect' value
locale.setlocale(locale.LC_ALL, ('en_US', 'UTF-8'))
data = [ data = [
{ {
'author': 'Happy Koji User <user1@example.com> - 1.1-1', 'author': 'Happy Koji User <user1@example.com> - 1.1-1',
@ -787,8 +803,6 @@ class MavenUtilTestCase(unittest.TestCase):
result = koji.util.formatChangelog(data) result = koji.util.formatChangelog(data)
self.assertMultiLineEqual(expect, result) self.assertMultiLineEqual(expect, result)
locale.setlocale(locale.LC_ALL, "")
def test_parseTime(self): def test_parseTime(self):
"""Test parseTime function""" """Test parseTime function"""
now = datetime.now() now = datetime.now()