123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- """
- Tests for _LeRelation object family
- """
-
- import unittest
- from unittest import TestCase
- from unittest.mock import patch
-
- import EditorialModel
- import DataSource.dummy
- import leapi
- import leapi.test.utils
- import leapi.lecrud as lecrud
- from leapi.lecrud import _LeCrud
-
- class LeRelationTestCase(TestCase):
- @classmethod
- def setUpClass(cls):
- """ Write the generated code in a temporary directory and import it """
- cls.tmpdir = leapi.test.utils.tmp_load_factory_code()
- @classmethod
- def tearDownClass(cls):
- """ Remove the temporary directory created at class setup """
- leapi.test.utils.cleanup(cls.tmpdir)
-
- def test_prepare_filters(self):
- """ Testing the _prepare_filters() method """
- from dyncode import Numero, LeObject, LeRelation
-
- filters = [
- (
- 'rank = 1',
- ('rank', '=', '1')
- ),
- (
- 'nature = "parent"',
- ('nature', '=', '"parent"')
- ),
- (
- 'lesup = 21',
- ('lesup', '=', LeObject(21))
- ),
- (
- 'lesub = 22',
- ('lesub', '=', LeObject(22))
- ),
- (
- ('rank', '=', '1'),
- ('rank', '=', '1'),
- ),
- (
- ('lesup', '=', LeObject(21)),
- ('lesup', '=', LeObject(21)),
- ),
- (
- ('lesub', '=', Numero(42)),
- ('lesub', '=', Numero(42)),
- ),
- ]
-
- for filter_arg, filter_res in filters:
- res, rel_res = LeRelation._prepare_filters([filter_arg])
- self.assertEqual(len(res), 1)
- self.assertEqual(len(rel_res), 0)
- res = res[0]
-
- for i in range(3):
- self.assertEqual(filter_res[i], res[i], "%s != %s"%(filter_res, res))
-
- # @unittest.skip("Wait LeRelation._prepare_filters() and LeRelation.delete() to unskip")
- @patch('DataSource.dummy.leapidatasource.DummyDatasource.delete')
- def test_delete(self, dsmock):
- """ Testing LeHierarch insert method """
- from dyncode import LeCrud, Publication, Numero, Personnes, LeObject, Rubrique, LeHierarch, LeRelation
-
- LeRelation.delete([LeRelation.sup_filter(Numero(42)), 'nature = "parent"'], LeHierarch)
- dsmock.assert_called_once_with(LeHierarch, [('lesup', '=', Numero(42)), ('nature','=','"parent"')])
- dsmock.reset_mock()
-
-
- class LeHierarch(LeRelationTestCase):
-
- @patch('DataSource.dummy.leapidatasource.DummyDatasource.select')
- def test_get(self, dsmock):
- """ Tests the LeHierarch.get() method """
- from dyncode import LeCrud, Publication, Numero, Personnes, LeObject, Rubrique, LeHierarch, LeRelation
-
- queries = [
- (
- ['lesup = 42', 'lesub = 24'], #filters
- ['lesup', 'lesub', 'nature', 'rank'], #field_l
-
- LeHierarch, #target
- ['lesup', 'lesub', 'nature', 'rank'], #field_ds
- [('lesup','=',LeObject(42)), ('lesub', '=', LeObject(24))], #filters_ds
- [], #rfilters_ds
-
- ),
- (
- [ LeRelation.sup_filter(Numero(42)) ],
- [],
-
- LeHierarch,
- [ 'nature', 'rank', 'lesub', 'depth', 'lesup'],
- [('lesup', '=', Numero(42))],
- [],
- ),
- ]
-
- for filters, field_l, target, field_ds, filters_ds, rfilters_ds in queries:
- eargs = (filters, field_l, target, field_ds, filters_ds, rfilters_ds)
-
- LeHierarch.get(filters, field_l)
- cargs = dsmock.call_args
-
- self.assertEqual(len(cargs), 2)
- cargs=cargs[0]
-
- self.assertEqual(cargs[0], target, "%s != %s"%(cargs, eargs))
- self.assertEqual(set(cargs[1]), set(field_ds), "%s != %s"%(cargs, eargs))
- self.assertEqual(cargs[2], filters_ds, "%s != %s"%(cargs, eargs))
- self.assertEqual(cargs[3], rfilters_ds, "%s != %s"%(cargs, eargs))
-
- dsmock.reset_mock()
-
- @patch('DataSource.dummy.leapidatasource.DummyDatasource.insert')
- def test_insert(self, dsmock):
- """ Testing LeHierarch insert method """
- from dyncode import LeCrud, Publication, Numero, Personnes, LeObject, Rubrique, LeHierarch, LeRelation
- queries = [
- (
- {
- 'lesup': Rubrique(7),
- 'lesub': Numero(42),
- 'nature': 'parent',
- },
- {
- 'lesup': Rubrique(7),
- 'lesub': Numero(42),
- 'nature': 'parent',
- }
- ),
- (
- {
- 'lesup': 7,
- 'lesub': 42,
- 'nature': 'parent',
- },
- {
- 'lesup': LeObject(7),
- 'lesub': LeObject(42),
- 'nature': 'parent',
- }
- ),
- (
- {
- 'lesup': LeObject(7),
- 'lesub': LeObject(42),
- 'nature': 'parent',
- },
- {
- 'lesup': LeObject(7),
- 'lesub': LeObject(42),
- 'nature': 'parent',
- }
- ),
- (
- {
- 'lesup': LeObject(7),
- 'lesub': 42,
- 'nature': 'parent',
- },
- {
- 'lesup': LeObject(7),
- 'lesub': LeObject(42),
- 'nature': 'parent',
- }
- )
- ]
- for query, equery in queries:
- LeHierarch.insert(query)
- dsmock.assert_called_once_with(LeHierarch, **equery)
- dsmock.reset_mock()
-
- LeRelation.insert(query, 'LeHierarch')
- dsmock.assert_called_once_with(LeHierarch, **equery)
- dsmock.reset_mock()
-
-
- @unittest.skip("Wait for LeRelation.delete()")
- @patch('DataSource.dummy.leapidatasource.DummyDatasource.delete')
- def test_delete(self, dsmock):
- """ Testing LeHierarch insert method """
- from dyncode import LeCrud, Publication, Numero, Personnes, LeObject, Rubrique, LeHierarch, LeRelation
- rel = LeHierarch(10)
- rel.delete()
- dsmock.assert_called_once_with(LeHierarch, [(LeHierarch.uidname(), '=', 10)], [])
-
-
- @unittest.skip("Wait for LeRelation.update() to unskip")
- @patch('DataSource.dummy.leapidatasource.DummyDatasource.update')
- def test_update(self, dsmock):
- """ test LeHierach update method"""
- from dyncode import LeHierarch
- with self.assertRaises(NotImplementedError):
- LeHierarch.update({})
-
-
-
- class LeRel2TypeTestCase(LeRelationTestCase):
-
- @unittest.skip("Wait implementation to unskip")
- @patch('DataSource.dummy.leapidatasource.DummyDatasource.insert')
- def test_insert(self, dsmock):
- """ test LeHierach update method"""
- from dyncode import LeObject, Article, Textes, Personne, Personnes, LeHierarch, LeRel2Type, Rel_textes2personne
-
- queries = [
- {
- 'lesup': Article(42),
- 'lesub': Personne(24),
- 'adresse': None,
- },
- {
- 'lesup': Textes(42),
- 'lesub': Personne(24),
- 'adresse': None,
- },
- {
- 'lesup': 42,
- 'lesub': Personne(24),
- 'adresse': None,
- },
-
- {
- 'lesup': Article(42),
- 'lesub': Personnes(24),
- 'adresse': "bar",
- },
- {
- 'lesup': Textes(42),
- 'lesub': Personnes(24),
- 'adresse': "foo",
- },
- ]
-
- for query in queries:
- Rel_textes2personne.insert(query)
-
- eres = {'nature': None}
- eres.uopdate(query)
- for fname in ('lesup', 'lesub'):
- if isinstance(eres[fname], int):
- eres[fname] = LeObject(eres[fname])
-
- dsmock.assert_called_once_with(Rel_textes2personne, **eres)
- dsmock.reset_mock()
-
- LeRel2Type.insert(query, "Rel_textes2personne")
-
- dsmock.assert_called_once_with(Rel_textes2personne, **eres)
- dsmock.reset_mock()
-
- @unittest.skip("Wait implementation to unskip")
- @patch('DataSource.dummy.leapidatasource.DummyDatasource.insert')
- def test_insert_fails(self, dsmock):
- """ test LeHierach update method"""
- from dyncode import LeObject, Rubrique, Numero, Article, Textes, Personne, Personnes, LeHierarch, LeRel2Type, Rel_textes2personne
-
- queries = [
- {
- 'lesup': Rubrique(42),
- 'lesub': Personne(24),
- 'adresse': None,
- },
- {
- 'adresse': None,
- },
- {
- 'lesup': Rubrique(42),
- 'lesub': LeObject(24),
- 'adresse': None,
- },
- {
- 'lesup': Article(42),
- 'lesub': Numero(24),
- 'adresse': 'foo',
- },
- {
- 'lesup': 42,
- 'lesub': 24,
- },
- {
- 'lesup': 42,
- 'lesub': 24,
- 'adresse': None,
- 'nature': "parent",
- },
- {
- 'lesup': 42,
- 'lesub': 24,
- 'adresse': None,
- 'nature': None,
- },
- {
- 'lesup': 42,
- 'lesub': 24,
- 'adresse': None,
- 'foofield': None,
- },
- {
- 'lesup': 42,
- 'lesub': 24,
- 'id_relation': 1337,
- 'adresse': None,
- },
- ]
-
- for query in queries:
- with self.assertRaises(lecrud.LeApiQueryError):
- LeRel2Type.insert(query, 'Rel_textes2personne')
-
- with self.assertRaises(lecrud.LeApiQueryError):
- Rel_textes2personne.insert(query)
|