1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
import os
import sys
from types import ModuleType
from unittest import TestCase
from moderngl_window.conf import SETTINGS_ENV_VAR, Settings
from moderngl_window.exceptions import ImproperlyConfigured
class SettingsTests(TestCase):
"""Test settings system"""
# Non-standard window settings
window_setting = {
'class': 'moderngl_window.context.headless.Window',
'name': 'ModernGL Headless Test',
'gl_version': (3, 3),
}
def test_default(self):
"""Initialize default settings"""
settings = Settings()
# Attempt to access default properties
settings.WINDOW
settings.PROGRAM_FINDERS
settings.PROGRAM_DIRS
settings.PROGRAM_LOADERS
def test_apply_settings_from_env(self):
"""Config values from module"""
# Create and inject a fake module in python
module_name = "moderngl_window_settings_module"
module = ModuleType(module_name)
setattr(module, 'WINDOW', self.window_setting)
sys.modules[module_name] = module
# Attempt to import it
os.environ[SETTINGS_ENV_VAR] = module_name
settings = Settings()
settings.apply_settings_from_env()
self.assertEqual(settings.WINDOW, self.window_setting)
def test_import_nonexistent_module(self):
"""Import settings module that do no exist"""
os.environ[SETTINGS_ENV_VAR] = 'this.module.does.not.exist'
with self.assertRaises(ImproperlyConfigured):
Settings().apply_settings_from_env()
def test_apply_from_dict(self):
"""Supply config values as dict"""
settings = Settings()
settings.apply_from_dict({'WINDOW': self.window_setting})
self.assertEqual(settings.WINDOW, self.window_setting)
def test_apply_from_cls(self):
"""Supply config values using cls namespace"""
class MyConfig:
WINDOW=self.window_setting
settings = Settings()
settings.apply_from_cls(MyConfig)
self.assertEqual(settings.WINDOW, self.window_setting)
def test_apply_from_generator(self):
"""Test generators specifically"""
def gen():
yield 'WINDOW', self.window_setting
yield 'SOMETHING', 1
settings = Settings()
settings.apply_from_iterable(gen())
self.assertEqual(settings.WINDOW, self.window_setting)
self.assertEqual(settings.SOMETHING, 1)
def test_apply_from_iterator_error(self):
"""Ensure ValueError is raised if not iterable"""
with self.assertRaises(ValueError):
settings = Settings()
settings.apply_from_iterable(1337)
def test_repr(self):
"""Ensure string representation is somewhat reasonable"""
value = str(Settings())
self.assertIsInstance(value, str)
self.assertGreater(len(value), 100)
|