No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

nc_test_utils_mlstrings.py 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #
  2. # This file is part of Lodel 2 (https://github.com/OpenEdition)
  3. #
  4. # Copyright (C) 2015-2017 Cléo UMS-3287
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU Affero General Public License as published
  8. # by the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU Affero General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Affero General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. import unittest
  20. import copy
  21. from lodel.utils.mlstring import MlString
  22. class MlStringTestCase(unittest.TestCase):
  23. examples = {
  24. 'eng': 'Hello world !',
  25. 'fre': 'Bonjour monde !',
  26. 'ger': 'Hallo welt !',
  27. }
  28. def test_init_str(self):
  29. """ Test MlString instanciation with string as argument """
  30. testlangs = ['eng', 'fre']
  31. teststrs = ['hello', 'Hello world !', '{helloworld !!!]}']
  32. for lang in testlangs:
  33. for teststr in teststrs:
  34. MlString.default_lang(lang)
  35. mls_test = MlString(teststr)
  36. self.assertEqual(teststr, str(mls_test))
  37. self.assertEqual(teststr, mls_test.get(lang))
  38. def test_init_dict(self):
  39. """ Test MlString instanciation with dict as argument """
  40. mls = MlString(self.examples)
  41. for lang, val in self.examples.items():
  42. self.assertEqual(mls.get(lang), val)
  43. def test_init_json(self):
  44. """ Test MlString instanciation using a json string """
  45. testval = '{"eng":"Hello world !", "fre":"Bonjour monde !", "ger":"Hallo welt !"}'
  46. mls = MlString.from_json(testval)
  47. for lang in self.examples:
  48. self.assertEqual(mls.get(lang), self.examples[lang])
  49. def test_get(self):
  50. """ Test MlString get method """
  51. mls = MlString(self.examples)
  52. for lang, val in self.examples.items():
  53. self.assertEqual(mls.get(lang), val)
  54. # A lang that is not set
  55. self.assertEqual(mls.get('esp'), str(mls))
  56. # A deleted lang
  57. mls.set('fre', None)
  58. self.assertEqual(mls.get('fre'), str(mls))
  59. def test_eq(self):
  60. """ Test MlString comparison functions """
  61. for val in ['hello world', self.examples]:
  62. mls1 = MlString(val)
  63. mls2 = MlString(val)
  64. self.assertTrue(mls1 == mls2)
  65. self.assertEqual(hash(mls1), hash(mls2))
  66. mls1 = MlString('Hello world !')
  67. mls2 = MlString('hello world !')
  68. self.assertTrue(mls1 != mls2)
  69. self.assertNotEqual(hash(mls1), hash(mls2))
  70. modexamples = copy.copy(self.examples)
  71. modexamples['eng'] = 'hello world !'
  72. mls1 = MlString(modexamples)
  73. mls2 = MlString(self.examples)
  74. self.assertTrue(mls1 != mls2)
  75. self.assertNotEqual(hash(mls1), hash(mls2))
  76. mls1 = MlString(self.examples)
  77. mls2 = MlString(self.examples)
  78. mls1.set('eng', None)
  79. self.assertTrue(mls1 != mls2)
  80. self.assertNotEqual(hash(mls1), hash(mls2))
  81. mls1 = MlString(self.examples)
  82. mls2 = MlString(self.examples)
  83. mls1.set('eng', 'hello world !')
  84. self.assertTrue(mls1 != mls2)
  85. self.assertNotEqual(hash(mls1), hash(mls2))
  86. def test_d_hash(self):
  87. """ Test if the hash method is deterministic """
  88. mls1 = MlString('Hello world !')
  89. self.assertEqual(mls1.d_hash(),305033383450738439650269714534939972534)