From 6b2274645aa6c88421055d3cb3516f322416167b Mon Sep 17 00:00:00 2001 From: Andreas Preikschat Date: Thu, 14 Feb 2013 15:54:45 +0100 Subject: [PATCH 1/5] added test for UiStrings, started with FormattingTags tests --- openlp/core/lib/formattingtags.py | 27 +++----- .../openlp_core_lib/test_formattingtags.py | 66 +++++++++++++++++++ .../openlp_core_lib/test_uistrings.py | 22 +++++++ 3 files changed, 98 insertions(+), 17 deletions(-) create mode 100644 tests/functional/openlp_core_lib/test_formattingtags.py create mode 100644 tests/functional/openlp_core_lib/test_uistrings.py diff --git a/openlp/core/lib/formattingtags.py b/openlp/core/lib/formattingtags.py index b7bbd7322..b2d8f6ea7 100644 --- a/openlp/core/lib/formattingtags.py +++ b/openlp/core/lib/formattingtags.py @@ -36,8 +36,7 @@ from openlp.core.lib import Settings, translate class FormattingTags(object): """ - Static Class to HTML Tags to be access around the code the list is managed - by the Options Tab. + Static Class to HTML Tags to be access around the code the list is managed by the Options Tab. """ html_expands = [] @@ -56,12 +55,11 @@ class FormattingTags(object): tags = [] for tag in FormattingTags.html_expands: if not tag[u'protected'] and not tag.get(u'temporary'): - # Using dict ensures that copy is made and encoding of values - # a little later does not affect tags in the original list + # Using dict ensures that copy is made and encoding of values a little later does not affect tags in + # the original list tags.append(dict(tag)) tag = tags[-1] - # Remove key 'temporary' from tags. - # It is not needed to be saved. + # Remove key 'temporary' from tags. It is not needed to be saved. if u'temporary' in tag: del tag[u'temporary'] for element in tag: @@ -73,15 +71,12 @@ class FormattingTags(object): @staticmethod def load_tags(): """ - Load the Tags from store so can be used in the system or used to - update the display. + Load the Tags from store so can be used in the system or used to update the display. """ - temporary_tags = [tag for tag in FormattingTags.html_expands - if tag.get(u'temporary')] + temporary_tags = [tag for tag in FormattingTags.html_expands if tag.get(u'temporary')] FormattingTags.html_expands = [] base_tags = [] # Append the base tags. - # Hex Color tags from http://www.w3schools.com/html/html_colornames.asp base_tags.append({u'desc': translate('OpenLP.FormattingTags', 'Red'), u'start tag': u'{r}', u'start html': u'', @@ -195,19 +190,17 @@ class FormattingTags(object): The end tag, e. g. ``{/r}`` * start html - The start html tag. For instance ```` + The start html tag. For instance ```` * end html The end html tag. For example ```` * protected - A boolean stating whether this is a build-in tag or not. Should be - ``True`` in most cases. + A boolean stating whether this is a build-in tag or not. Should be ``True`` in most cases. * temporary - A temporary tag will not be saved, but is also considered when - displaying text containing the tag. It has to be a ``boolean``. + A temporary tag will not be saved, but is also considered when displaying text containing the tag. It has + to be a ``boolean``. """ FormattingTags.html_expands.extend(tags) diff --git a/tests/functional/openlp_core_lib/test_formattingtags.py b/tests/functional/openlp_core_lib/test_formattingtags.py new file mode 100644 index 000000000..5a95e195f --- /dev/null +++ b/tests/functional/openlp_core_lib/test_formattingtags.py @@ -0,0 +1,66 @@ +""" +Package to test the openlp.core.lib.formattingtags package. +""" + +from unittest import TestCase + +from mock import patch + +from openlp.core.lib import FormattingTags + +class TestFormattingTags(TestCase): + + def get_html_tags_no_user_tags_test(self): + """ + Test the get_html_tags static method. + """ + with patch(u'openlp.core.lib.translate') as mocked_translate, \ + patch(u'openlp.core.lib.settings') as mocked_settings, \ + patch(u'openlp.core.lib.formattingtags.cPickle') as mocked_cPickle: + # GIVEN: Our mocked modules and functions. + mocked_translate.side_effect = lambda module, string_to_translate: string_to_translate + mocked_settings.value.return_value = u'' + mocked_cPickle.load.return_value = [] + + # WHEN: Get the display tags. + FormattingTags.load_tags() + old_tags_list = FormattingTags.get_html_tags() + FormattingTags.load_tags() + new_tags_list = FormattingTags.get_html_tags() + + # THEN: Lists should be identically. + assert old_tags_list == new_tags_list, u'The formatting tag lists should be identically.' + + def get_html_tags_with_user_tags_test(self): + """ + Add a tag and check if it still exists after reloading the tags list. + """ + # FIXME: not working yet. + with patch(u'openlp.core.lib.translate') as mocked_translate, \ + patch(u'openlp.core.lib.settings') as mocked_settings, \ + patch(u'openlp.core.lib.formattingtags.cPickle') as mocked_cPickle: + # GIVEN: Our mocked modules and functions. + mocked_translate.side_effect = lambda module, string_to_translate: string_to_translate + mocked_settings.value.return_value = u'' + mocked_cPickle.load.return_value = [] + tags = [{ + u'end tag': '{/aa}', + u'start html': '', + u'start tag': '{aa}', + u'protected': False, + u'end html': '', + u'desc': 'name'} + ] + + # WHEN: Get the display tags. + FormattingTags.add_html_tags(tags) + FormattingTags.load_tags() + old_tags_list = FormattingTags.get_html_tags() + FormattingTags.load_tags() + new_tags_list = FormattingTags.get_html_tags() + + # THEN: Lists should be identically. + assert old_tags_list == new_tags_list, u'The formatting tag lists with user tags should be identically.' + + + diff --git a/tests/functional/openlp_core_lib/test_uistrings.py b/tests/functional/openlp_core_lib/test_uistrings.py new file mode 100644 index 000000000..e0d0fc485 --- /dev/null +++ b/tests/functional/openlp_core_lib/test_uistrings.py @@ -0,0 +1,22 @@ +""" +Package to test the openlp.core.lib.uistrings package. +""" + +from unittest import TestCase + +from openlp.core.lib import UiStrings + +class TestUiStrings(TestCase): + + def check_same_instance_test(self): + """ + Test if the always only one instance of the UiStrings is created. + """ + # WHEN: Create two instances of the UiStrings class. + first_instance = UiStrings() + second_instance = UiStrings() + + # THEN: Check if the instances are the same. + assert first_instance is second_instance, "They should be the same instance!" + + From b77ae7a187d180b48ecdf5d17da3daad50e9f382 Mon Sep 17 00:00:00 2001 From: Andreas Preikschat Date: Fri, 15 Feb 2013 12:09:51 +0100 Subject: [PATCH 2/5] fixed test --- .../openlp_core_lib/test_formattingtags.py | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/tests/functional/openlp_core_lib/test_formattingtags.py b/tests/functional/openlp_core_lib/test_formattingtags.py index 5a95e195f..0d3b8b6b6 100644 --- a/tests/functional/openlp_core_lib/test_formattingtags.py +++ b/tests/functional/openlp_core_lib/test_formattingtags.py @@ -10,6 +10,12 @@ from openlp.core.lib import FormattingTags class TestFormattingTags(TestCase): + def tearDown(self): + """ + Clean up the FormattingTags class. + """ + FormattingTags.html_expands = [] + def get_html_tags_no_user_tags_test(self): """ Test the get_html_tags static method. @@ -18,7 +24,7 @@ class TestFormattingTags(TestCase): patch(u'openlp.core.lib.settings') as mocked_settings, \ patch(u'openlp.core.lib.formattingtags.cPickle') as mocked_cPickle: # GIVEN: Our mocked modules and functions. - mocked_translate.side_effect = lambda module, string_to_translate: string_to_translate + mocked_translate.side_effect = lambda module, string_to_translate, comment: string_to_translate mocked_settings.value.return_value = u'' mocked_cPickle.load.return_value = [] @@ -35,32 +41,32 @@ class TestFormattingTags(TestCase): """ Add a tag and check if it still exists after reloading the tags list. """ - # FIXME: not working yet. with patch(u'openlp.core.lib.translate') as mocked_translate, \ patch(u'openlp.core.lib.settings') as mocked_settings, \ patch(u'openlp.core.lib.formattingtags.cPickle') as mocked_cPickle: # GIVEN: Our mocked modules and functions. mocked_translate.side_effect = lambda module, string_to_translate: string_to_translate mocked_settings.value.return_value = u'' - mocked_cPickle.load.return_value = [] tags = [{ u'end tag': '{/aa}', u'start html': '', u'start tag': '{aa}', u'protected': False, u'end html': '', - u'desc': 'name'} - ] + u'desc': 'name' + }] + mocked_cPickle.load.return_value = tags # WHEN: Get the display tags. - FormattingTags.add_html_tags(tags) FormattingTags.load_tags() old_tags_list = FormattingTags.get_html_tags() + + FormattingTags.add_html_tags(tags) FormattingTags.load_tags() new_tags_list = FormattingTags.get_html_tags() - # THEN: Lists should be identically. - assert old_tags_list == new_tags_list, u'The formatting tag lists with user tags should be identically.' + # THEN: Lists should not be identically. + assert len(old_tags_list) - 1 == len(new_tags_list), u'The lists should be different.' From cd83142d774a4dc1571c50bf9561af072df9936c Mon Sep 17 00:00:00 2001 From: Andreas Preikschat Date: Fri, 15 Feb 2013 12:55:47 +0100 Subject: [PATCH 3/5] fixed test --- .../openlp_core_lib/test_formattingtags.py | 20 +++++++++++-------- .../openlp_core_lib/test_serviceitem.py | 3 ++- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/tests/functional/openlp_core_lib/test_formattingtags.py b/tests/functional/openlp_core_lib/test_formattingtags.py index 0d3b8b6b6..db34dd8bd 100644 --- a/tests/functional/openlp_core_lib/test_formattingtags.py +++ b/tests/functional/openlp_core_lib/test_formattingtags.py @@ -1,13 +1,14 @@ """ Package to test the openlp.core.lib.formattingtags package. """ - +import copy from unittest import TestCase from mock import patch from openlp.core.lib import FormattingTags + class TestFormattingTags(TestCase): def tearDown(self): @@ -47,26 +48,29 @@ class TestFormattingTags(TestCase): # GIVEN: Our mocked modules and functions. mocked_translate.side_effect = lambda module, string_to_translate: string_to_translate mocked_settings.value.return_value = u'' - tags = [{ + tag = { u'end tag': '{/aa}', u'start html': '', u'start tag': '{aa}', u'protected': False, u'end html': '', u'desc': 'name' - }] - mocked_cPickle.load.return_value = tags + } + mocked_cPickle.loads.side_effect = [[], [tag]] # WHEN: Get the display tags. FormattingTags.load_tags() - old_tags_list = FormattingTags.get_html_tags() + old_tags_list = copy.deepcopy(FormattingTags.get_html_tags()) - FormattingTags.add_html_tags(tags) + # WHEN: Add our tag and get the tags again. FormattingTags.load_tags() + FormattingTags.add_html_tags([tag]) new_tags_list = FormattingTags.get_html_tags() # THEN: Lists should not be identically. - assert len(old_tags_list) - 1 == len(new_tags_list), u'The lists should be different.' - + assert old_tags_list != new_tags_list, u'The lists should be different.' + # THEN: Added tag and last tag should be the same. + new_tag = new_tags_list.pop() + assert tag == new_tag, u'Tags should be identically.' diff --git a/tests/functional/openlp_core_lib/test_serviceitem.py b/tests/functional/openlp_core_lib/test_serviceitem.py index a50752cce..8085d676a 100644 --- a/tests/functional/openlp_core_lib/test_serviceitem.py +++ b/tests/functional/openlp_core_lib/test_serviceitem.py @@ -210,4 +210,5 @@ class TestServiceItem(TestCase): first_line = items[0] except: first_line = u'' - return first_line \ No newline at end of file + return first_line + From 434349b47d117d1eb0f909a1916da4517b2cc78b Mon Sep 17 00:00:00 2001 From: Andreas Preikschat Date: Sat, 16 Feb 2013 12:46:57 +0100 Subject: [PATCH 4/5] fixed test --- tests/functional/openlp_core_lib/test_formattingtags.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/functional/openlp_core_lib/test_formattingtags.py b/tests/functional/openlp_core_lib/test_formattingtags.py index db34dd8bd..6166d6197 100644 --- a/tests/functional/openlp_core_lib/test_formattingtags.py +++ b/tests/functional/openlp_core_lib/test_formattingtags.py @@ -31,7 +31,7 @@ class TestFormattingTags(TestCase): # WHEN: Get the display tags. FormattingTags.load_tags() - old_tags_list = FormattingTags.get_html_tags() + old_tags_list = copy.deepcopy(FormattingTags.get_html_tags()) FormattingTags.load_tags() new_tags_list = FormattingTags.get_html_tags() From db00a864fcf4aafd0fc94f671cb6703d73e5476a Mon Sep 17 00:00:00 2001 From: Andreas Preikschat Date: Sat, 16 Feb 2013 19:11:22 +0100 Subject: [PATCH 5/5] fixed test method descriptions; created costant --- .../openlp_core_lib/test_formattingtags.py | 28 ++++++++++--------- .../openlp_core_lib/test_uistrings.py | 2 +- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/tests/functional/openlp_core_lib/test_formattingtags.py b/tests/functional/openlp_core_lib/test_formattingtags.py index 6166d6197..335650112 100644 --- a/tests/functional/openlp_core_lib/test_formattingtags.py +++ b/tests/functional/openlp_core_lib/test_formattingtags.py @@ -9,6 +9,16 @@ from mock import patch from openlp.core.lib import FormattingTags +TAG = { + u'end tag': '{/aa}', + u'start html': '', + u'start tag': '{aa}', + u'protected': False, + u'end html': '', + u'desc': 'name' +} + + class TestFormattingTags(TestCase): def tearDown(self): @@ -19,7 +29,7 @@ class TestFormattingTags(TestCase): def get_html_tags_no_user_tags_test(self): """ - Test the get_html_tags static method. + Test the FormattingTags class' get_html_tags static method. """ with patch(u'openlp.core.lib.translate') as mocked_translate, \ patch(u'openlp.core.lib.settings') as mocked_settings, \ @@ -40,7 +50,7 @@ class TestFormattingTags(TestCase): def get_html_tags_with_user_tags_test(self): """ - Add a tag and check if it still exists after reloading the tags list. + Test the FormattingTags class' get_html_tags static method in combination with user tags. """ with patch(u'openlp.core.lib.translate') as mocked_translate, \ patch(u'openlp.core.lib.settings') as mocked_settings, \ @@ -48,15 +58,7 @@ class TestFormattingTags(TestCase): # GIVEN: Our mocked modules and functions. mocked_translate.side_effect = lambda module, string_to_translate: string_to_translate mocked_settings.value.return_value = u'' - tag = { - u'end tag': '{/aa}', - u'start html': '', - u'start tag': '{aa}', - u'protected': False, - u'end html': '', - u'desc': 'name' - } - mocked_cPickle.loads.side_effect = [[], [tag]] + mocked_cPickle.loads.side_effect = [[], [TAG]] # WHEN: Get the display tags. FormattingTags.load_tags() @@ -64,7 +66,7 @@ class TestFormattingTags(TestCase): # WHEN: Add our tag and get the tags again. FormattingTags.load_tags() - FormattingTags.add_html_tags([tag]) + FormattingTags.add_html_tags([TAG]) new_tags_list = FormattingTags.get_html_tags() # THEN: Lists should not be identically. @@ -72,5 +74,5 @@ class TestFormattingTags(TestCase): # THEN: Added tag and last tag should be the same. new_tag = new_tags_list.pop() - assert tag == new_tag, u'Tags should be identically.' + assert TAG == new_tag, u'Tags should be identically.' diff --git a/tests/functional/openlp_core_lib/test_uistrings.py b/tests/functional/openlp_core_lib/test_uistrings.py index e0d0fc485..3351657d1 100644 --- a/tests/functional/openlp_core_lib/test_uistrings.py +++ b/tests/functional/openlp_core_lib/test_uistrings.py @@ -10,7 +10,7 @@ class TestUiStrings(TestCase): def check_same_instance_test(self): """ - Test if the always only one instance of the UiStrings is created. + Test the UiStrings class - we always should have only one instance of the UiStrings class. """ # WHEN: Create two instances of the UiStrings class. first_instance = UiStrings()