testuserfields.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Copyright (c) 2007 Michael Howitz, gocept gmbh & co. kg
  4. #
  5. # This is free software. You may redistribute it under the terms
  6. # of the Apache license and the GNU General Public License Version
  7. # 2 or 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 General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public
  15. # License along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. #
  18. # Contributor(s):
  19. #
  20. import unittest
  21. import os, sys
  22. import os.path
  23. import odf.userfield
  24. import tempfile
  25. import zipfile
  26. from io import BytesIO
  27. if sys.version_info[0]==3:
  28. unicode=str
  29. def get_file_path(file_name):
  30. return os.path.join(os.path.dirname(__file__), "examples", file_name)
  31. def get_user_fields(file_path):
  32. return odf.userfield.UserFields(file_path)
  33. class TestUserFields(unittest.TestCase):
  34. userfields_odt = get_file_path(u"userfields.odt")
  35. userfields_ooo3_odt = get_file_path(u"userfields_ooo3.odt")
  36. no_userfields_odt = get_file_path(u"no_userfields.odt")
  37. def setUp(self):
  38. self.unlink_list = []
  39. def tearDown(self):
  40. # delete created destination files
  41. for filename in self.unlink_list:
  42. os.unlink(filename)
  43. def test_exception(self):
  44. # no zip-file
  45. no_zip = odf.userfield.UserFields(unicode(__file__))
  46. self.assertRaises(TypeError, no_zip.list_fields)
  47. self.assertRaises(TypeError, no_zip.update, {})
  48. def test_list_fields(self):
  49. """ Find the expected fields in the file """
  50. self.assertEqual([],
  51. get_user_fields(self.no_userfields_odt).list_fields())
  52. self.assertEqual([u'username', u'firstname', u'lastname', u'address'],
  53. get_user_fields(self.userfields_odt).list_fields())
  54. def test_list_fields_and_values(self):
  55. """ Find the expected fields and values in the file """
  56. no_user_fields = get_user_fields(self.no_userfields_odt)
  57. self.assertEqual([],
  58. no_user_fields.list_fields_and_values())
  59. self.assertEqual([],
  60. no_user_fields.list_fields_and_values([u'username']))
  61. user_fields = get_user_fields(self.userfields_odt)
  62. self.assertEqual([(u'username', u'string', u''),
  63. (u'lastname', u'string', u'<none>')],
  64. user_fields.list_fields_and_values([u'username',
  65. u'lastname']))
  66. self.assertEqual(4, len(user_fields.list_fields_and_values()))
  67. def test_list_values(self):
  68. self.assertEqual(
  69. [],
  70. get_user_fields(self.no_userfields_odt).list_values([u'username']))
  71. self.assertEqual(
  72. [u'', u'<none>'],
  73. get_user_fields(self.userfields_odt).list_values(
  74. [u'username', u'lastname']))
  75. def test_get(self):
  76. user_fields = get_user_fields(self.userfields_odt)
  77. self.assertEqual(
  78. None,
  79. get_user_fields(self.no_userfields_odt).get(u'username'))
  80. self.assertEqual(u'', user_fields.get(u'username'))
  81. self.assertEqual(u'<none>', user_fields.get(u'lastname'))
  82. self.assertEqual(None, user_fields.get(u'street'))
  83. def test_get_type_and_value(self):
  84. self.assertEqual(
  85. None,
  86. get_user_fields(self.no_userfields_odt).get_type_and_value(
  87. u'username'))
  88. user_fields = get_user_fields(self.userfields_odt)
  89. self.assertEqual(
  90. (u'string', u''), user_fields.get_type_and_value(u'username'))
  91. self.assertEqual(
  92. (u'string', u'<none>'),
  93. user_fields.get_type_and_value(u'lastname'))
  94. self.assertEqual(None, user_fields.get_type_and_value(u'street'))
  95. def test_update(self):
  96. # test for file without user fields
  97. no_user_fields = get_user_fields(self.no_userfields_odt)
  98. no_user_fields.dest_file = self._get_dest_file_name()
  99. no_user_fields.update({u'username': u'mac'})
  100. dest = odf.userfield.UserFields(no_user_fields.dest_file)
  101. self.assertEqual([], dest.list_fields_and_values())
  102. # test for file with user field, including test of encoding
  103. user_fields = get_user_fields(self.userfields_odt)
  104. user_fields.dest_file = self._get_dest_file_name()
  105. user_fields.update({u'username': u'mac',
  106. u'firstname': u'André',
  107. u'street': u'I do not exist'})
  108. dest = odf.userfield.UserFields(user_fields.dest_file)
  109. self.assertEqual([(u'username', u'string', u'mac'),
  110. (u'firstname', u'string', u'André'),
  111. (u'lastname', u'string', u'<none>'),
  112. (u'address', u'string', u'')],
  113. dest.list_fields_and_values())
  114. def test_update_open_office_version_3(self):
  115. """Update fields in OpenOffice.org 3.x version of file."""
  116. user_fields = get_user_fields(self.userfields_ooo3_odt)
  117. user_fields.dest_file = self._get_dest_file_name()
  118. user_fields.update({u'username': u'mari',
  119. u'firstname': u'Lukas',
  120. u'street': u'I might exist.'})
  121. dest = odf.userfield.UserFields(user_fields.dest_file)
  122. self.assertEqual([(u'username', u'string', u'mari'),
  123. (u'firstname', u'string', u'Lukas'),
  124. (u'lastname', u'string', u'<none>'),
  125. (u'address', u'string', u'')],
  126. dest.list_fields_and_values())
  127. def test_stringio(self):
  128. # test wether it is possible to use a StringIO as src and dest
  129. infile=open(self.userfields_odt,'rb')
  130. src = BytesIO(infile.read())
  131. infile.close()
  132. dest = BytesIO()
  133. # update fields
  134. user_fields = odf.userfield.UserFields(src, dest)
  135. user_fields.update({u'username': u'mac',
  136. u'firstname': u'André',
  137. u'street': u'I do not exist'})
  138. # reread dest StringIO to get field values
  139. dest_user_fields = odf.userfield.UserFields(dest)
  140. self.assertEqual([(u'username', u'string', u'mac'),
  141. (u'firstname', u'string', u'André'),
  142. (u'lastname', u'string', u'<none>'),
  143. (u'address', u'string', u'')],
  144. dest_user_fields.list_fields_and_values())
  145. def test_newlines_in_values(self):
  146. # test that newlines in values are encoded correctly so that
  147. # they get read back correctly
  148. user_fields = get_user_fields(self.userfields_odt)
  149. user_fields.dest_file = self._get_dest_file_name()
  150. user_fields.update({'username': 'mac',
  151. 'firstname': 'mac',
  152. 'lastname': 'mac',
  153. 'address': 'Hall-Platz 3\n01234 Testheim'})
  154. dest = odf.userfield.UserFields(user_fields.dest_file)
  155. self.assertEqual([(u'username', u'string', u'mac'),
  156. (u'firstname', u'string', u'mac'),
  157. (u'lastname', u'string', u'mac'),
  158. (u'address', u'string',
  159. u'Hall-Platz 3\n01234 Testheim')],
  160. dest.list_fields_and_values())
  161. def _get_dest_file_name(self):
  162. dummy_fh, dest_file_name = tempfile.mkstemp(u'.odt')
  163. os.close(dummy_fh)
  164. self.unlink_list.append(dest_file_name)
  165. return dest_file_name
  166. if __name__ == '__main__':
  167. if sys.version_info[0]==3:
  168. unittest.main(warnings='ignore') # ignore warnings for unclosed files
  169. else:
  170. unittest.main()