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_letype.py 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. """
  2. Test for LeType and childs
  3. """
  4. import unittest
  5. from unittest import TestCase
  6. from unittest.mock import patch
  7. import EditorialModel
  8. import leapi
  9. import DataSource.dummy
  10. import leapi.test.utils
  11. from leapi.lecrud import _LeCrud
  12. class LeTypeTestCase(TestCase):
  13. @classmethod
  14. def setUpClass(cls):
  15. """ Write the generated code in a temporary directory and import it """
  16. cls.tmpdir = leapi.test.utils.tmp_load_factory_code()
  17. @classmethod
  18. def tearDownClass(cls):
  19. """ Remove the temporary directory created at class setup """
  20. leapi.test.utils.cleanup(cls.tmpdir)
  21. def test_init(self):
  22. """ testing the constructor """
  23. from dyncode import Publication, Numero, LeObject, LeType
  24. with self.assertRaises(NotImplementedError):
  25. LeType(42)
  26. badargs = [
  27. {'class_id':Numero._class_id + 1},
  28. {'type_id':Numero._type_id + 1},
  29. ]
  30. for badarg in badargs:
  31. with self.assertRaises(RuntimeError):
  32. Numero(42, **badarg)
  33. badargs = [
  34. ({'lodel_id':'foobar'}, TypeError),
  35. ({'lodel_id': 42, 'titre_mais_qui_existe_pas':'hello'}, leapi.lecrud.LeApiDataCheckError),
  36. ]
  37. for badarg, expect_e in badargs:
  38. with self.assertRaises(expect_e, msg="Invalid argument given %s"%badarg):
  39. Numero(**badarg)
  40. @patch('leapi.letype._LeType.populate')
  41. def test_datas(self, dsmock):
  42. """ Testing the datas \@property method """
  43. from dyncode import Publication, Numero, LeObject
  44. num = Numero(42, titre = 'foofoo')
  45. self.assertEqual({'lodel_id' : 42, 'titre': 'foofoo'}, num.datas())
  46. num.all_datas
  47. dsmock.assert_called_once()
  48. def test_fieldlist(self):
  49. """ Test fieldlist method """
  50. from dyncode import Numero, Rubrique, Article, Personne, LeObject
  51. letypes = [Numero, Rubrique, Article, Personne]
  52. for letype in letypes:
  53. self.assertEqual(
  54. letype.fieldlist(complete=False),
  55. letype._fields
  56. )
  57. self.assertEquals(
  58. sorted(letype.fieldlist(complete = True)),
  59. sorted(list(set(letype._fields + LeObject.fieldlist())))
  60. )
  61. for fname in letype.fieldlist(complete = False):
  62. self.assertIn(fname, letype._leclass.fieldlist(False))
  63. ftype = letype.fieldtypes()[fname]
  64. self.assertNotIn(fname, LeObject.fieldlist())
  65. def test_fieldtypes(self):
  66. """ Test fieldtypes() method """
  67. from dyncode import Numero, Rubrique, Article, Personne, LeObject
  68. letypes = [Numero, Rubrique, Article, Personne]
  69. for letype in letypes:
  70. for complete in [True, False]:
  71. self.assertEquals(
  72. sorted(letype.fieldlist(complete = complete)),
  73. sorted(list(letype.fieldtypes(complete = complete).keys()))
  74. )
  75. class LeTypeMockDsTestCase(TestCase):
  76. """ Tests that need to mock the datasource """
  77. @classmethod
  78. def setUpClass(cls):
  79. """ Write the generated code in a temporary directory and import it """
  80. cls.tmpdir = leapi.test.utils.tmp_load_factory_code()
  81. @classmethod
  82. def tearDownClass(cls):
  83. """ Remove the temporary directory created at class setup """
  84. leapi.test.utils.cleanup(cls.tmpdir)
  85. @patch('DataSource.dummy.leapidatasource.DummyDatasource.select')
  86. def test_populate(self, dsmock):
  87. from dyncode import Publication, Numero, LeObject
  88. num = Numero(1, type_id = Numero._type_id)
  89. missing_fields = [f for f in Numero._fields if not (f in ['lodel_id', 'type_id'])]
  90. with self.assertRaises(leapi.lecrud.LeApiQueryError):
  91. num.populate()
  92. dsmock.assert_called_once_with(Numero, missing_fields, [('lodel_id','=',1)],[])
  93. @patch('DataSource.dummy.leapidatasource.DummyDatasource.update')
  94. def test_update(self, dsmock):
  95. from dyncode import Publication, Numero, LeObject
  96. datas = { 'titre' : 'foobar' }
  97. #Testing as instance method
  98. num = Numero(lodel_id = 1)
  99. with patch.object(_LeCrud, 'populate', return_value=None) as mock_populate:
  100. num.update(datas)
  101. mock_populate.assert_called_once_with()
  102. dsmock.assert_called_once_with(Numero, num.uidget(), **datas)
  103. @patch('DataSource.dummy.leapidatasource.DummyDatasource.delete')
  104. def test_delete(self, dsmock):
  105. from dyncode import Publication, Numero, LeObject
  106. num = Numero(lodel_id = 1)
  107. num.delete()
  108. dsmock.assert_called_once_with(Numero, 1)