Skip to content

Commit

Permalink
Merge pull request #875 from akx/asserts
Browse files Browse the repository at this point in the history
tests: Use bare asserts and pytest.raises
  • Loading branch information
akx authored May 10, 2022
2 parents d0ec73d + eeb01c7 commit 5e37cea
Show file tree
Hide file tree
Showing 10 changed files with 656 additions and 898 deletions.
123 changes: 57 additions & 66 deletions tests/messages/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,17 @@ def test_python_format(self):

def test_translator_comments(self):
mess = catalog.Message('foo', user_comments=['Comment About `foo`'])
self.assertEqual(mess.user_comments, ['Comment About `foo`'])
assert mess.user_comments == ['Comment About `foo`']
mess = catalog.Message('foo',
auto_comments=['Comment 1 About `foo`',
'Comment 2 About `foo`'])
self.assertEqual(mess.auto_comments, ['Comment 1 About `foo`',
'Comment 2 About `foo`'])
assert mess.auto_comments == ['Comment 1 About `foo`', 'Comment 2 About `foo`']

def test_clone_message_object(self):
msg = catalog.Message('foo', locations=[('foo.py', 42)])
clone = msg.clone()
clone.locations.append(('bar.py', 42))
self.assertEqual(msg.locations, [('foo.py', 42)])
assert msg.locations == [('foo.py', 42)]
msg.flags.add('fuzzy')
assert not clone.fuzzy and msg.fuzzy

Expand All @@ -62,41 +61,39 @@ class CatalogTestCase(unittest.TestCase):
def test_add_returns_message_instance(self):
cat = catalog.Catalog()
message = cat.add('foo')
self.assertEqual('foo', message.id)
assert message.id == 'foo'

def test_two_messages_with_same_singular(self):
cat = catalog.Catalog()
cat.add('foo')
cat.add(('foo', 'foos'))
self.assertEqual(1, len(cat))
assert len(cat) == 1

def test_duplicate_auto_comment(self):
cat = catalog.Catalog()
cat.add('foo', auto_comments=['A comment'])
cat.add('foo', auto_comments=['A comment', 'Another comment'])
self.assertEqual(['A comment', 'Another comment'],
cat['foo'].auto_comments)
assert cat['foo'].auto_comments == ['A comment', 'Another comment']

def test_duplicate_user_comment(self):
cat = catalog.Catalog()
cat.add('foo', user_comments=['A comment'])
cat.add('foo', user_comments=['A comment', 'Another comment'])
self.assertEqual(['A comment', 'Another comment'],
cat['foo'].user_comments)
assert cat['foo'].user_comments == ['A comment', 'Another comment']

def test_duplicate_location(self):
cat = catalog.Catalog()
cat.add('foo', locations=[('foo.py', 1)])
cat.add('foo', locations=[('foo.py', 1)])
self.assertEqual([('foo.py', 1)], cat['foo'].locations)
assert cat['foo'].locations == [('foo.py', 1)]

def test_update_message_changed_to_plural(self):
cat = catalog.Catalog()
cat.add(u'foo', u'Voh')
tmpl = catalog.Catalog()
tmpl.add((u'foo', u'foos'))
cat.update(tmpl)
self.assertEqual((u'Voh', ''), cat['foo'].string)
assert cat['foo'].string == ('Voh', '')
assert cat['foo'].fuzzy

def test_update_message_changed_to_simple(self):
Expand All @@ -105,22 +102,22 @@ def test_update_message_changed_to_simple(self):
tmpl = catalog.Catalog()
tmpl.add(u'foo')
cat.update(tmpl)
self.assertEqual(u'Voh', cat['foo'].string)
assert cat['foo'].string == 'Voh'
assert cat['foo'].fuzzy

def test_update_message_updates_comments(self):
cat = catalog.Catalog()
cat[u'foo'] = catalog.Message('foo', locations=[('main.py', 5)])
self.assertEqual(cat[u'foo'].auto_comments, [])
self.assertEqual(cat[u'foo'].user_comments, [])
assert cat['foo'].auto_comments == []
assert cat['foo'].user_comments == []
# Update cat[u'foo'] with a new location and a comment
cat[u'foo'] = catalog.Message('foo', locations=[('main.py', 7)],
user_comments=['Foo Bar comment 1'])
self.assertEqual(cat[u'foo'].user_comments, ['Foo Bar comment 1'])
assert cat['foo'].user_comments == ['Foo Bar comment 1']
# now add yet another location with another comment
cat[u'foo'] = catalog.Message('foo', locations=[('main.py', 9)],
auto_comments=['Foo Bar comment 2'])
self.assertEqual(cat[u'foo'].auto_comments, ['Foo Bar comment 2'])
assert cat['foo'].auto_comments == ['Foo Bar comment 2']

def test_update_fuzzy_matching_with_case_change(self):
cat = catalog.Catalog()
Expand All @@ -129,11 +126,11 @@ def test_update_fuzzy_matching_with_case_change(self):
tmpl = catalog.Catalog()
tmpl.add('Foo')
cat.update(tmpl)
self.assertEqual(1, len(cat.obsolete))
assert len(cat.obsolete) == 1
assert 'foo' not in cat

self.assertEqual('Voh', cat['Foo'].string)
self.assertEqual(True, cat['Foo'].fuzzy)
assert cat['Foo'].string == 'Voh'
assert cat['Foo'].fuzzy is True

def test_update_fuzzy_matching_with_char_change(self):
cat = catalog.Catalog()
Expand All @@ -142,11 +139,11 @@ def test_update_fuzzy_matching_with_char_change(self):
tmpl = catalog.Catalog()
tmpl.add('foo')
cat.update(tmpl)
self.assertEqual(1, len(cat.obsolete))
assert len(cat.obsolete) == 1
assert 'fo' not in cat

self.assertEqual('Voh', cat['foo'].string)
self.assertEqual(True, cat['foo'].fuzzy)
assert cat['foo'].string == 'Voh'
assert cat['foo'].fuzzy is True

def test_update_fuzzy_matching_no_msgstr(self):
cat = catalog.Catalog()
Expand All @@ -158,10 +155,10 @@ def test_update_fuzzy_matching_no_msgstr(self):
assert 'fo' in cat
assert 'foo' in cat

self.assertEqual('', cat['fo'].string)
self.assertEqual(False, cat['fo'].fuzzy)
self.assertEqual(None, cat['foo'].string)
self.assertEqual(False, cat['foo'].fuzzy)
assert cat['fo'].string == ''
assert cat['fo'].fuzzy is False
assert cat['foo'].string is None
assert cat['foo'].fuzzy is False

def test_update_fuzzy_matching_with_new_context(self):
cat = catalog.Catalog()
Expand All @@ -170,13 +167,13 @@ def test_update_fuzzy_matching_with_new_context(self):
tmpl = catalog.Catalog()
tmpl.add('Foo', context='Menu')
cat.update(tmpl)
self.assertEqual(1, len(cat.obsolete))
assert len(cat.obsolete) == 1
assert 'foo' not in cat

message = cat.get('Foo', 'Menu')
self.assertEqual('Voh', message.string)
self.assertEqual(True, message.fuzzy)
self.assertEqual('Menu', message.context)
assert message.string == 'Voh'
assert message.fuzzy is True
assert message.context == 'Menu'

def test_update_fuzzy_matching_with_changed_context(self):
cat = catalog.Catalog()
Expand All @@ -185,13 +182,13 @@ def test_update_fuzzy_matching_with_changed_context(self):
tmpl = catalog.Catalog()
tmpl.add('Foo', context='Menu|Edit')
cat.update(tmpl)
self.assertEqual(1, len(cat.obsolete))
assert len(cat.obsolete) == 1
assert cat.get('Foo', 'Menu|File') is None

message = cat.get('Foo', 'Menu|Edit')
self.assertEqual('Voh', message.string)
self.assertEqual(True, message.fuzzy)
self.assertEqual('Menu|Edit', message.context)
assert message.string == 'Voh'
assert message.fuzzy is True
assert message.context == 'Menu|Edit'

def test_update_fuzzy_matching_no_cascading(self):
cat = catalog.Catalog()
Expand All @@ -205,12 +202,12 @@ def test_update_fuzzy_matching_no_cascading(self):
assert 'fo' in cat
assert 'foo' in cat

self.assertEqual('Voh', cat['fo'].string)
self.assertEqual(False, cat['fo'].fuzzy)
self.assertEqual('Vohe', cat['foo'].string)
self.assertEqual(False, cat['foo'].fuzzy)
self.assertEqual('Vohe', cat['fooo'].string)
self.assertEqual(True, cat['fooo'].fuzzy)
assert cat['fo'].string == 'Voh'
assert cat['fo'].fuzzy is False
assert cat['foo'].string == 'Vohe'
assert cat['foo'].fuzzy is False
assert cat['fooo'].string == 'Vohe'
assert cat['fooo'].fuzzy is True

def test_update_without_fuzzy_matching(self):
cat = catalog.Catalog()
Expand All @@ -219,20 +216,20 @@ def test_update_without_fuzzy_matching(self):
tmpl = catalog.Catalog()
tmpl.add('foo')
cat.update(tmpl, no_fuzzy_matching=True)
self.assertEqual(2, len(cat.obsolete))
assert len(cat.obsolete) == 2

def test_fuzzy_matching_regarding_plurals(self):
cat = catalog.Catalog()
cat.add(('foo', 'foh'), ('foo', 'foh'))
ru = copy.copy(cat)
ru.locale = 'ru_RU'
ru.update(cat)
self.assertEqual(True, ru['foo'].fuzzy)
assert ru['foo'].fuzzy is True
ru = copy.copy(cat)
ru.locale = 'ru_RU'
ru['foo'].string = ('foh', 'fohh', 'fohhh')
ru.update(cat)
self.assertEqual(False, ru['foo'].fuzzy)
assert ru['foo'].fuzzy is False

def test_update_no_template_mutation(self):
tmpl = catalog.Catalog()
Expand All @@ -243,37 +240,32 @@ def test_update_no_template_mutation(self):
cat2 = catalog.Catalog()
cat2.update(tmpl)

self.assertEqual(None, cat2['foo'].string)
self.assertEqual(False, cat2['foo'].fuzzy)
assert cat2['foo'].string is None
assert cat2['foo'].fuzzy is False

def test_update_po_updates_pot_creation_date(self):
template = catalog.Catalog()
localized_catalog = copy.deepcopy(template)
localized_catalog.locale = 'de_DE'
self.assertNotEqual(template.mime_headers,
localized_catalog.mime_headers)
self.assertEqual(template.creation_date,
localized_catalog.creation_date)
assert template.mime_headers != localized_catalog.mime_headers
assert template.creation_date == localized_catalog.creation_date
template.creation_date = datetime.datetime.now() - \
datetime.timedelta(minutes=5)
localized_catalog.update(template)
self.assertEqual(template.creation_date,
localized_catalog.creation_date)
assert template.creation_date == localized_catalog.creation_date

def test_update_po_keeps_po_revision_date(self):
template = catalog.Catalog()
localized_catalog = copy.deepcopy(template)
localized_catalog.locale = 'de_DE'
fake_rev_date = datetime.datetime.now() - datetime.timedelta(days=5)
localized_catalog.revision_date = fake_rev_date
self.assertNotEqual(template.mime_headers,
localized_catalog.mime_headers)
self.assertEqual(template.creation_date,
localized_catalog.creation_date)
assert template.mime_headers != localized_catalog.mime_headers
assert template.creation_date == localized_catalog.creation_date
template.creation_date = datetime.datetime.now() - \
datetime.timedelta(minutes=5)
localized_catalog.update(template)
self.assertEqual(localized_catalog.revision_date, fake_rev_date)
assert localized_catalog.revision_date == fake_rev_date

def test_stores_datetime_correctly(self):
localized = catalog.Catalog()
Expand All @@ -283,7 +275,7 @@ def test_stores_datetime_correctly(self):
"PO-Revision-Date: 2009-03-09 15:47-0700\n")
for key, value in localized.mime_headers:
if key in ('POT-Creation-Date', 'PO-Revision-Date'):
self.assertEqual(value, '2009-03-09 15:47-0700')
assert value == '2009-03-09 15:47-0700'

def test_mime_headers_contain_same_information_as_attributes(self):
cat = catalog.Catalog()
Expand All @@ -292,20 +284,19 @@ def test_mime_headers_contain_same_information_as_attributes(self):
"Language-Team: de <[email protected]>\n" +
"POT-Creation-Date: 2009-03-01 11:20+0200\n" +
"PO-Revision-Date: 2009-03-09 15:47-0700\n")
self.assertEqual(None, cat.locale)
assert cat.locale is None
mime_headers = dict(cat.mime_headers)

self.assertEqual('Foo Bar <[email protected]>', cat.last_translator)
self.assertEqual('Foo Bar <[email protected]>',
mime_headers['Last-Translator'])
assert cat.last_translator == 'Foo Bar <[email protected]>'
assert mime_headers['Last-Translator'] == 'Foo Bar <[email protected]>'

self.assertEqual('de <[email protected]>', cat.language_team)
self.assertEqual('de <[email protected]>', mime_headers['Language-Team'])
assert cat.language_team == 'de <[email protected]>'
assert mime_headers['Language-Team'] == 'de <[email protected]>'

dt = datetime.datetime(2009, 3, 9, 15, 47, tzinfo=FixedOffsetTimezone(-7 * 60))
self.assertEqual(dt, cat.revision_date)
assert cat.revision_date == dt
formatted_dt = format_datetime(dt, 'yyyy-MM-dd HH:mmZ', locale='en')
self.assertEqual(formatted_dt, mime_headers['PO-Revision-Date'])
assert mime_headers['PO-Revision-Date'] == formatted_dt


def test_message_fuzzy():
Expand Down
Loading

0 comments on commit 5e37cea

Please sign in to comment.