123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
- """
- Tests for _LeRelation object family
- """
-
- import unittest
- from unittest import TestCase
- from unittest.mock import patch
- from collections import OrderedDict
-
- import EditorialModel
- import EditorialModel.classtypes
- 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"')
- ),
- (
- 'superior = 21',
- ('superior', '=', LeObject(21))
- ),
- (
- 'subordinate = 22',
- ('subordinate', '=', LeObject(22))
- ),
- (
- ('rank', '=', '1'),
- ('rank', '=', '1'),
- ),
- (
- ('superior', '=', LeObject(21)),
- ('superior', '=', LeObject(21)),
- ),
- (
- ('subordinate', '=', Numero(42)),
- ('subordinate', '=', 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))
-
- @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
-
- LeHierarch(42).delete()
- dsmock.assert_called_once_with(LeHierarch, 42)
- dsmock.reset_mock()
-
-
- class LeHierarch(LeRelationTestCase):
-
- @patch('DataSource.dummy.leapidatasource.DummyDatasource.select')
- def test_get(self, dsmock):
- """ Tests the LeHierarch.get() method without limit group order etc."""
- from dyncode import LeCrud, Publication, Numero, Personnes, LeObject, Rubrique, LeHierarch, LeRelation
-
- queries = [
- (
- ['superior = 42', 'subordinate = 24'], #filters
- ['superior', 'subordinate', 'nature', 'rank'], #field_l
-
- LeHierarch, #target
- ['superior', 'subordinate', 'nature', 'rank'], #field_ds
- [('superior','=',LeObject(42)), ('subordinate', '=', LeObject(24))], #filters_ds
- [], #rfilters_ds
-
- ),
- (
- [ LeRelation.sup_filter(Numero(42)) ],
- [],
-
- LeHierarch,
- [ 'nature', 'rank', 'subordinate', 'depth', 'superior', 'id_relation', EditorialModel.classtypes.relation_name],
- [('superior', '=', 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[1]
- self.assertEqual(cargs['target_cls'], target, "%s != %s"%(cargs, eargs))
- self.assertEqual(set(cargs['field_list']), set(field_ds), "%s != %s"%(cargs, eargs))
- self.assertEqual(cargs['filters'], filters_ds, "%s != %s"%(cargs, eargs))
- self.assertEqual(cargs['rel_filters'], 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 = [
- (
- {
- 'superior': Rubrique(7, class_id = Rubrique._class_id, type_id = Rubrique._type_id),
- 'subordinate': Numero(42, class_id = Numero._class_id, type_id = Numero._type_id),
- 'nature': 'parent',
- },
- {
- 'superior': Rubrique(7),
- 'subordinate': Numero(42),
- 'nature': 'parent',
- },
- ),
- ]
- """ # Those tests are not good
- (
- {
- 'superior': 7,
- 'subordinate': 42,
- 'nature': 'parent',
- },
- {
- 'superior': LeObject(7),
- 'subordinate': LeObject(42),
- 'nature': 'parent',
- }
- ),
- (
- {
- 'superior': LeObject(7),
- 'subordinate': LeObject(42),
- 'nature': 'parent',
- },
- {
- 'superior': LeObject(7),
- 'subordinate': LeObject(42),
- 'nature': 'parent',
- }
- ),
- (
- {
- 'superior': LeObject(7),
- 'subordinate': 42,
- 'nature': 'parent',
- },
- {
- 'superior': LeObject(7),
- 'subordinate': LeObject(42),
- 'nature': 'parent',
- }
- )
- ]
- """
- for query, equery in queries:
- equery['rank'] = 1
- equery['depth'] = None
- equery[EditorialModel.classtypes.relation_name] = None
-
- 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()
-
-
- @patch('DataSource.dummy.leapidatasource.DummyDatasource.delete')
- def test_delete(self, dsmock):
- """ Testing LeHierarch delete method """
- from dyncode import LeCrud, Publication, Numero, Personnes, LeObject, Rubrique, LeHierarch, LeRelation
- rel = LeHierarch(10)
- rel.delete()
- dsmock.assert_called_once_with(LeHierarch, 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):
-
- @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, RelTextesPersonneAuteur
-
- queries = [
- {
- 'superior': Article(42),
- 'subordinate': Personne(24),
- 'adresse': None,
- },
- {
- 'superior': Textes(42),
- 'subordinate': Personne(24),
- 'adresse': None,
- },
- {
- 'superior': Article(42),
- 'subordinate': Personne(24),
- 'adresse': "bar",
- },
- {
- 'superior': Textes(42),
- 'subordinate': Personne(24),
- 'adresse': "foo",
- },
- ]
-
- for query in queries:
- RelTextesPersonneAuteur.insert(query)
-
- eres = {
- 'nature': None,
- 'depth': None,
- 'rank': 1,
- EditorialModel.classtypes.relation_name: None,
- }
- eres.update(query)
- for fname in ('superior', 'subordinate'):
- if isinstance(eres[fname], int):
- eres[fname] = LeObject(eres[fname])
-
- dsmock.assert_called_once_with(RelTextesPersonneAuteur, **eres)
- dsmock.reset_mock()
-
- query[EditorialModel.classtypes.relation_name] = 'auteur'
- LeRel2Type.insert(query, "RelTextesPersonneAuteur")
-
- dsmock.assert_called_once_with(RelTextesPersonneAuteur, **eres)
- dsmock.reset_mock()
-
- @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, RelTextesPersonneAuteur
-
- queries = [
- {
- 'superior': Rubrique(42),
- 'subordinate': Personne(24),
- 'adresse': None,
- },
- {
- 'adresse': None,
- },
- {
- 'superior': Rubrique(42),
- 'subordinate': Rubrique(24),
- 'adresse': None,
- },
- {
- 'superior': Article(42),
- 'subordinate': Numero(24),
- 'adresse': 'foo',
- },
- {
- 'id_relation': 1337,
- 'superior': Article(42),
- 'subordinate': Numero(24),
- 'adresse': 'foo',
- },
- ]
-
- for query in queries:
- try:
- LeRel2Type.insert(query, 'Rel_textes2personne')
- self.fail("No exception raised")
- except Exception as e:
- if not isinstance(e, lecrud.LeApiErrors) and not isinstance(e, lecrud.LeApiDataCheckError):
- self.fail("Bad exception raised : "+str(e))
-
- try:
- RelTextesPersonneAuteur.insert(query)
- self.fail("No exception raised")
- except Exception as e:
- if not isinstance(e, lecrud.LeApiErrors) and not isinstance(e, lecrud.LeApiDataCheckError):
- self.fail("Bad exception raised : ", e)
|