Без опису
Ви не можете вибрати більше 25 тем 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

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