Added some locale determination tools
[mediagoblin.git] / mediagoblin / tests / test_util.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 Free Software Foundation, Inc
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
13 #
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17 import email
18
19 from mediagoblin import util
20
21
22 util._activate_testing()
23
24
25 def _import_component_testing_method(silly_string):
26 # Just for the sake of testing that our component importer works.
27 return u"'%s' is the silliest string I've ever seen" % silly_string
28
29
30 def test_import_component():
31 imported_func = util.import_component(
32 'mediagoblin.tests.test_util:_import_component_testing_method')
33 result = imported_func('hooobaladoobala')
34 expected = u"'hooobaladoobala' is the silliest string I've ever seen"
35 assert result == expected
36
37
38 def test_send_email():
39 util._clear_test_inboxes()
40
41 # send the email
42 util.send_email(
43 "sender@mediagoblin.example.org",
44 ["amanda@example.org", "akila@example.org"],
45 "Testing is so much fun!",
46 """HAYYY GUYS!
47
48 I hope you like unit tests JUST AS MUCH AS I DO!""")
49
50 # check the main inbox
51 assert len(util.EMAIL_TEST_INBOX) == 1
52 message = util.EMAIL_TEST_INBOX.pop()
53 assert message['From'] == "sender@mediagoblin.example.org"
54 assert message['To'] == "amanda@example.org, akila@example.org"
55 assert message['Subject'] == "Testing is so much fun!"
56 assert message.get_payload(decode=True) == """HAYYY GUYS!
57
58 I hope you like unit tests JUST AS MUCH AS I DO!"""
59
60 # Check everything that the FakeMhost.sendmail() method got is correct
61 assert len(util.EMAIL_TEST_MBOX_INBOX) == 1
62 mbox_dict = util.EMAIL_TEST_MBOX_INBOX.pop()
63 assert mbox_dict['from'] == "sender@mediagoblin.example.org"
64 assert mbox_dict['to'] == ["amanda@example.org", "akila@example.org"]
65 mbox_message = email.message_from_string(mbox_dict['message'])
66 assert mbox_message['From'] == "sender@mediagoblin.example.org"
67 assert mbox_message['To'] == "amanda@example.org, akila@example.org"
68 assert mbox_message['Subject'] == "Testing is so much fun!"
69 assert mbox_message.get_payload(decode=True) == """HAYYY GUYS!
70
71 I hope you like unit tests JUST AS MUCH AS I DO!"""
72
73
74 def test_locale_to_lower_upper():
75 """
76 Test cc.i18n.util.locale_to_lower_upper()
77 """
78 assert util.locale_to_lower_upper('en') == 'en'
79 assert util.locale_to_lower_upper('en_US') == 'en_US'
80 assert util.locale_to_lower_upper('en-us') == 'en_US'
81
82 # crazy renditions. Useful?
83 assert util.locale_to_lower_upper('en-US') == 'en_US'
84 assert util.locale_to_lower_upper('en_us') == 'en_US'
85
86
87 def test_locale_to_lower_lower():
88 """
89 Test cc.i18n.util.locale_to_lower_lower()
90 """
91 assert util.locale_to_lower_lower('en') == 'en'
92 assert util.locale_to_lower_lower('en_US') == 'en-us'
93 assert util.locale_to_lower_lower('en-us') == 'en-us'
94
95 # crazy renditions. Useful?
96 assert util.locale_to_lower_lower('en-US') == 'en-us'
97 assert util.locale_to_lower_lower('en_us') == 'en-us'