Commits

David Jean Louis committed b20300b

Fixed issue #38: POFile.append() raise a duplicate exception when you try to add a new entry with the same msgid and a different msgctxt (only when check_for_duplicates option is set to True)

Comments (0)

Files changed (3)

         Overriden ``list`` method to implement the membership test (in and
         not in).
         The method considers that an entry is in the file if it finds an entry
-        that has the same msgid (the test is **case sensitive**).
+        that has the same msgid (the test is **case sensitive**) and the same
+        msgctxt (or none for both entries).
 
         Argument:
 
         ``entry``
             an instance of :class:`~polib._BaseEntry`.
         """
-        return self.find(entry.msgid, by='msgid') is not None
+        return self.find(entry.msgid, by='msgid', msgctxt=entry.msgctxt)
 
     def __eq__(self, other):
         return str(self) == str(other)
             entries = [e for e in self if not e.obsolete]
         for e in entries:
             if getattr(e, by) == st:
-                if msgctxt and e.msgctxt != msgctxt:
+                if msgctxt != False and e.msgctxt != msgctxt:
                     continue
                 return e
         return None

tests/test_pofile_helpers.po

 msgid "and"
 msgstr "y"
 
+#: db/models/manipulators.py:310 contrib/admin/views/main.py:342
+#: contrib/admin/views/main.py:344 contrib/admin/views/main.py:346
+#: core/validators.py:275
+msgctxt "some context"
+msgid "and"
+msgstr "y"
+
 #: db/models/fields/__init__.py:49
 #, python-format
 msgid "%(optname)s with this %(fieldname)s already exists."
     def test_append2(self):
         def add_duplicate():
             pofile = polib.pofile('tests/test_pofile_helpers.po', check_for_duplicates=True)
-            pofile.append(polib.POEntry(msgid="and", msgstr="y"))
+            pofile.append(polib.POEntry(msgid="and"))
         self.assertRaises(ValueError, add_duplicate)
 
+    def test_append3(self):
+        def add_duplicate():
+            pofile = polib.pofile('tests/test_pofile_helpers.po', check_for_duplicates=True)
+            pofile.append(polib.POEntry(msgid="and", msgctxt="some context"))
+        self.assertRaises(ValueError, add_duplicate)
+
+    def test_append4(self):
+        pofile = polib.pofile('tests/test_pofile_helpers.po', check_for_duplicates=True)
+        entry = polib.POEntry(msgid="and", msgctxt="some different context")
+        pofile.append(entry)
+        self.assertTrue(entry in pofile)
+
     def test_insert1(self):
         pofile = polib.pofile('tests/test_pofile_helpers.po')
         entry = polib.POEntry(msgid="Foo", msgstr="Bar", msgctxt="Some context")
 
     def test_percent_translated(self):
         po = polib.pofile('tests/test_pofile_helpers.po')
-        self.assertEqual(po.percent_translated(), 50)
+        self.assertEqual(po.percent_translated(), 53)
         po = polib.POFile()
         self.assertEqual(po.percent_translated(), 100)
 
     def test_translated_entries(self):
         po = polib.pofile('tests/test_pofile_helpers.po')
-        self.assertEqual(len(po.translated_entries()), 6)
+        self.assertEqual(len(po.translated_entries()), 7)
 
     def test_untranslated_entries(self):
         po = polib.pofile('tests/test_pofile_helpers.po')