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.

test_utils_mlstrings.py 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #-*- coding: utf-8 -*-
  2. import unittest
  3. import copy
  4. from lodel.utils.mlstring import MlString
  5. class MlStringTestCase(unittest.TestCase):
  6. examples = {
  7. 'eng': 'Hello world !',
  8. 'fre': 'Bonjour monde !',
  9. 'ger': 'Hallo welt !',
  10. }
  11. def test_init_str(self):
  12. """ Test MlString instanciation with string as argument """
  13. testlangs = ['eng', 'fre']
  14. teststrs = ['hello', 'Hello world !', '{helloworld !!!]}']
  15. for lang in testlangs:
  16. for teststr in teststrs:
  17. MlString.default_lang(lang)
  18. mls_test = MlString(teststr)
  19. self.assertEqual(teststr, str(mls_test))
  20. self.assertEqual(teststr, mls_test.get(lang))
  21. def test_init_dict(self):
  22. """ Test MlString instanciation with dict as argument """
  23. mls = MlString(self.examples)
  24. for lang, val in self.examples.items():
  25. self.assertEqual(mls.get(lang), val)
  26. def test_init_json(self):
  27. """ Test MlString instanciation using a json string """
  28. testval = '{"eng":"Hello world !", "fre":"Bonjour monde !", "ger":"Hallo welt !"}'
  29. mls = MlString.from_json(testval)
  30. for lang in self.examples:
  31. self.assertEqual(mls.get(lang), self.examples[lang])
  32. def test_get(self):
  33. """ Test MlString get method """
  34. mls = MlString(self.examples)
  35. for lang, val in self.examples.items():
  36. self.assertEqual(mls.get(lang), val)
  37. # A lang that is not set
  38. self.assertEqual(mls.get('esp'), str(mls))
  39. # A deleted lang
  40. mls.set('fre', None)
  41. self.assertEqual(mls.get('fre'), str(mls))
  42. def test_eq(self):
  43. """ Test MlString comparison functions """
  44. for val in ['hello world', self.examples]:
  45. mls1 = MlString(val)
  46. mls2 = MlString(val)
  47. self.assertTrue(mls1 == mls2)
  48. self.assertEqual(hash(mls1), hash(mls2))
  49. mls1 = MlString('Hello world !')
  50. mls2 = MlString('hello world !')
  51. self.assertTrue(mls1 != mls2)
  52. self.assertNotEqual(hash(mls1), hash(mls2))
  53. modexamples = copy.copy(self.examples)
  54. modexamples['eng'] = 'hello world !'
  55. mls1 = MlString(modexamples)
  56. mls2 = MlString(self.examples)
  57. self.assertTrue(mls1 != mls2)
  58. self.assertNotEqual(hash(mls1), hash(mls2))
  59. mls1 = MlString(self.examples)
  60. mls2 = MlString(self.examples)
  61. mls1.set('eng', None)
  62. self.assertTrue(mls1 != mls2)
  63. self.assertNotEqual(hash(mls1), hash(mls2))
  64. mls1 = MlString(self.examples)
  65. mls2 = MlString(self.examples)
  66. mls1.set('eng', 'hello world !')
  67. self.assertTrue(mls1 != mls2)
  68. self.assertNotEqual(hash(mls1), hash(mls2))
  69. def test_d_hash(self):
  70. """ Test if the hash method is deterministic """
  71. mls1 = MlString('Hello world !')
  72. self.assertEqual(mls1.d_hash(),305033383450738439650269714534939972534)