def test_should_override_properties(self, mock_open): mock_open.return_value = FakeFileObject(b'''\ [section] key=overridden value new-key=new-value [new-section] key1=value1''') cfg = Config(collections.defaultdict(dict, **{ 'section': { 'key': 'original value' } })) cfg.override('/path/to/file') self.assertDictEqual({ 'section': { 'key': 'overridden value', 'new-key': 'new-value' }, 'new-section': { 'key1': 'value1' } }, cfg.get_all())
def test_should_ignore_unsued_template_variables(self): cfg = Config(collections.defaultdict(dict, **{ 'section': { 'key': 'There are no variables here.' } })) self.assertEqual('There are no variables here.', cfg.get('section', 'key', **{'lorem': 'ipsum', 'dolor': 'sit amet'}))
def setUp(self): self.cfg = Config(collections.defaultdict(dict, **{ 'section1': { 'zażółć': 'gęślą jaźń' }, 'section2': { 'key': 'żółw na starość wydziela wstrętną woń', 'pchnąć': 'łódź jeża lub ośm skrzyń fig' } }))
def test_should_interpolate_plural_templates(self): cfg = Config(collections.defaultdict(dict, **{ 'section': { 'key': 'There {count(is,are)} {count} {count(day,days)} left.' } })) self.assertEqual('There are 0 days left.', cfg.get('section', 'key', count=0)) self.assertEqual('There is 1 day left.', cfg.get('section', 'key', count=1)) self.assertEqual('There are 2 days left.', cfg.get('section', 'key', count=2))
def test_should_interpolate_templates(self): cfg = Config(collections.defaultdict(dict, **{ 'section': { 'key': 'Hello {name}, your age is {age}.' } })) self.assertEqual( 'Hello Paweł, your age is 12.', cfg.get('section', 'key', name='Paweł', age=12))
def test_should_render_watermark_with_date_interpolation(self): ogre.config._INSTANCE = Config( collections.defaultdict( dict, **{ 'template': { 'watermark': 'year={year} month={month} day={day}' } })) mock_canvas = mock.Mock() watermark = Watermark() watermark.render(mock_canvas) mock_canvas.assert_has_calls([ mock.call.push_state(), mock.call.set_default_state(), mock.call.text('year=1970 month=1 day=1', 0, 2, mock.ANY, halign=HAlign.CENTER), mock.call.pop_state() ])
def test_should_not_render_watermark_if_not_configured(self): ogre.config._INSTANCE = Config(collections.defaultdict(dict)) mock_canvas = mock.Mock() watermark = Watermark() watermark.render(mock_canvas) self.assertListEqual([], mock_canvas.mock_calls)
def test_should_decode_escaped_unicode_in_sections_keys_values(self, mock_open): mock_open.return_value = FakeFileObject(rb'''[ja\u017a\u0144] za\u017c\xf3\u0142\u0107=g\u0119\u015bl\u0105 [section2] key=\u017c\xf3\u0142w na staro\u015b\u0107 wydziela wstr\u0119tn\u0105 wo\u0144 pchn\u0105\u0107=\u0142\xf3d\u017a je\u017ca lub o\u015bm skrzy\u0144 fig ''') cfg = Config(_load_from_file('/path/to/file')) self.assertDictEqual({ 'jaźń': { 'zażółć': 'gęślą' }, 'section2': { 'key': 'żółw na starość wydziela wstrętną woń', 'pchnąć': 'łódź jeża lub ośm skrzyń fig' } }, cfg.get_all())
def test_should_render_watermark_without_interpolation(self): ogre.config._INSTANCE = Config( collections.defaultdict( dict, **{'template': { 'watermark': 'lorem ipsum' }})) mock_canvas = mock.Mock() watermark = Watermark() watermark.render(mock_canvas) mock_canvas.assert_has_calls([ mock.call.push_state(), mock.call.set_default_state(), mock.call.text('lorem ipsum', 0, 2, mock.ANY, halign=HAlign.CENTER), mock.call.pop_state() ])
def test_should_interpolate_default_value(self): cfg = Config(collections.defaultdict(dict)) self.assertEqual('Hello Jan', cfg.get('no-such-section', 'no-such-property', default='Hello {name}', name='Jan'))
def test_should_raise_exception_on_not_default_dict(self): with self.assertRaises(AssertionError): Config({})
class TestConfig(unittest.TestCase): def setUp(self): self.cfg = Config(collections.defaultdict(dict, **{ 'section1': { 'zażółć': 'gęślą jaźń' }, 'section2': { 'key': 'żółw na starość wydziela wstrętną woń', 'pchnąć': 'łódź jeża lub ośm skrzyń fig' } })) def test_should_raise_exception_on_not_default_dict(self): with self.assertRaises(AssertionError): Config({}) def test_str(self): self.assertEqual( '--- section1 ---\nzażółć = gęślą jaźń\n\n--- section2 ---\nkey = żółw na starość wydziela wstrętną woń\npchnąć = łódź jeża lub ośm skrzyń fig\n', str(self.cfg)) def test_should_return_all_sections_and_properties(self): self.assertIsInstance(self.cfg.get_all(), collections.defaultdict) self.assertDictEqual({ 'section1': { 'zażółć': 'gęślą jaźń' }, 'section2': { 'key': 'żółw na starość wydziela wstrętną woń', 'pchnąć': 'łódź jeża lub ośm skrzyń fig' } }, self.cfg.get_all()) def test_should_return_all_properties_from_section(self): self.assertDictEqual({ 'key': 'żółw na starość wydziela wstrętną woń', 'pchnąć': 'łódź jeża lub ośm skrzyń fig' }, self.cfg.get_all('section2')) def test_should_return_empty_dict_on_missing_section(self): self.assertDictEqual({}, self.cfg.get_all('no such section')) def test_should_return_section_property(self): self.assertEqual( 'żółw na starość wydziela wstrętną woń', self.cfg.get('section2', 'key')) def test_should_return_default_value_on_missing_property(self): self.assertEqual('', self.cfg.get('section2', 'no-such-property')) self.assertEqual('default', self.cfg.get('section2', 'no-such-property', default='default')) def test_should_return_default_value_on_missing_section(self): self.assertEqual('', self.cfg.get('no-such-section', 'no-such-property')) self.assertEqual('default', self.cfg.get('no-such-section', 'no-such-property', default='default')) def test_should_interpolate_templates(self): cfg = Config(collections.defaultdict(dict, **{ 'section': { 'key': 'Hello {name}, your age is {age}.' } })) self.assertEqual( 'Hello Paweł, your age is 12.', cfg.get('section', 'key', name='Paweł', age=12)) def test_should_interpolate_plural_templates(self): cfg = Config(collections.defaultdict(dict, **{ 'section': { 'key': 'There {count(is,are)} {count} {count(day,days)} left.' } })) self.assertEqual('There are 0 days left.', cfg.get('section', 'key', count=0)) self.assertEqual('There is 1 day left.', cfg.get('section', 'key', count=1)) self.assertEqual('There are 2 days left.', cfg.get('section', 'key', count=2)) def test_should_interpolate_default_value(self): cfg = Config(collections.defaultdict(dict)) self.assertEqual('Hello Jan', cfg.get('no-such-section', 'no-such-property', default='Hello {name}', name='Jan')) def test_should_ignore_unsued_template_variables(self): cfg = Config(collections.defaultdict(dict, **{ 'section': { 'key': 'There are no variables here.' } })) self.assertEqual('There are no variables here.', cfg.get('section', 'key', **{'lorem': 'ipsum', 'dolor': 'sit amet'})) @mock.patch('ogre.config.open') def test_should_override_properties(self, mock_open): mock_open.return_value = FakeFileObject(b'''\ [section] key=overridden value new-key=new-value [new-section] key1=value1''') cfg = Config(collections.defaultdict(dict, **{ 'section': { 'key': 'original value' } })) cfg.override('/path/to/file') self.assertDictEqual({ 'section': { 'key': 'overridden value', 'new-key': 'new-value' }, 'new-section': { 'key1': 'value1' } }, cfg.get_all())