mirror of
https://github.com/yweber/lodel2.git
synced 2025-11-30 08:36:53 +01:00
Changed the _fields properties of LeType child classes in dynamic code + adding complete argument to fieldlist() and fieldtypes() methods
+ tests
This commit is contained in:
parent
5ad9c27a07
commit
bf44d66440
8 changed files with 119 additions and 13 deletions
|
|
@ -20,15 +20,16 @@ class _LeClass(_LeObject):
|
|||
_classtype = None
|
||||
|
||||
@classmethod
|
||||
def fieldtypes(cls):
|
||||
def fieldtypes(cls, complete=True):
|
||||
ret = dict()
|
||||
ret.update(super().fieldtypes())
|
||||
if complete:
|
||||
ret.update(super().fieldtypes())
|
||||
ret.update(cls._fieldtypes)
|
||||
return ret
|
||||
|
||||
@classmethod
|
||||
def fieldlist(cls):
|
||||
return list(cls.fieldtypes().keys())
|
||||
def fieldlist(cls, complete=True):
|
||||
return list(cls.fieldtypes(complete).keys())
|
||||
|
||||
@classmethod
|
||||
def get(cls, query_filters, field_list=None, order=None, group=None, limit=None, offset=0):
|
||||
|
|
|
|||
|
|
@ -240,7 +240,7 @@ class _LeCrud(object):
|
|||
def populate(self, field_list=None):
|
||||
if not self.is_complete():
|
||||
if field_list == None:
|
||||
field_list = [ fname for fname in self._fields if not hasattr(self, fname) ]
|
||||
field_list = [ fname for fname in self.fieldlist() if not hasattr(self, fname) ]
|
||||
filters = [self._id_filter()]
|
||||
rel_filters = []
|
||||
# Getting datas from db
|
||||
|
|
|
|||
|
|
@ -149,7 +149,8 @@ class {classname}(LeRel2Type):
|
|||
type_fields = list()
|
||||
type_superiors = list()
|
||||
for field in emtype.fields(relational=False):
|
||||
type_fields.append(field.name)
|
||||
if not hasattr(field, 'immutable') or not field.immutable:
|
||||
type_fields.append(field.name)
|
||||
|
||||
for nat, sup_l in emtype.superiors().items():
|
||||
type_superiors.append('%s: [%s]' % (
|
||||
|
|
|
|||
|
|
@ -45,8 +45,11 @@ class _LeType(_LeClass):
|
|||
return cls._leclass
|
||||
|
||||
@classmethod
|
||||
def fieldlist(cls):
|
||||
return cls._fields
|
||||
def fieldlist(cls, complete = True):
|
||||
if not complete:
|
||||
return cls._fields
|
||||
else:
|
||||
return list(set(cls._fields + cls.name2class('LeObject').fieldlist()))
|
||||
|
||||
@classmethod
|
||||
def get(cls, query_filters, field_list = None, order = None, group = None, limit = None, offset = 0):
|
||||
|
|
@ -54,9 +57,9 @@ class _LeType(_LeClass):
|
|||
return super().get(query_filters, field_list, order, group, limit, offset)
|
||||
|
||||
@classmethod
|
||||
def fieldtypes(cls):
|
||||
super_fieldtypes = super().fieldtypes()
|
||||
return { fname: super_fieldtypes[fname] for fname in super_fieldtypes if fname in cls._fields }
|
||||
def fieldtypes(cls, complete=True):
|
||||
super_fieldtypes = super().fieldtypes(complete)
|
||||
return { fname: super_fieldtypes[fname] for fname in super_fieldtypes if fname in cls.fieldlist(complete)}
|
||||
|
||||
## @brief Get all the datas for this LeType
|
||||
# @return a dict with fieldname as key and field value as value
|
||||
|
|
|
|||
61
leapi/test/test_leclass.py
Normal file
61
leapi/test/test_leclass.py
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
"""
|
||||
Test for LeClass
|
||||
"""
|
||||
|
||||
import unittest
|
||||
from unittest import TestCase
|
||||
|
||||
import EditorialModel
|
||||
import leapi
|
||||
import DataSource.dummy
|
||||
import leapi.test.utils
|
||||
|
||||
class LeClassTestCase(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_fieldlist(self):
|
||||
""" Testing fieldlist method """
|
||||
from dyncode import Publication, Personnes, Textes, LeObject
|
||||
|
||||
for leclass in [ Publication, Personnes, Textes ]:
|
||||
for fieldname in leclass.fieldlist(complete = False):
|
||||
ftype = leclass.fieldtypes()[fieldname]
|
||||
if hasattr(ftype, 'immutable'):
|
||||
if ftype.immutable:
|
||||
self.assertNotIn(
|
||||
fieldname,
|
||||
LeObject.fieldlist()
|
||||
)
|
||||
else:
|
||||
self.assertIn(
|
||||
fieldname,
|
||||
LeObject.fieldlist()
|
||||
)
|
||||
else:
|
||||
self.assertNotIn(
|
||||
fieldname,
|
||||
LeObject.fieldlist()
|
||||
)
|
||||
for obj_fname in LeObject.fieldlist():
|
||||
self.assertIn(
|
||||
obj_fname,
|
||||
leclass.fieldlist(complete = True)
|
||||
)
|
||||
|
||||
def test_fieldtypes(self):
|
||||
""" Testing the fieldtypes() method """
|
||||
from dyncode import Publication, Personnes, Textes, LeObject
|
||||
for leclass in [ Publication, Personnes, Textes ]:
|
||||
for complete in [ True, False ]:
|
||||
self.assertEqual(
|
||||
sorted(list(leclass.fieldtypes(complete).keys())),
|
||||
sorted(leclass.fieldlist(complete)),
|
||||
)
|
||||
|
|
@ -278,7 +278,7 @@ class LeCrudTestCase(TestCase):
|
|||
[],
|
||||
[],
|
||||
|
||||
Numero._fields,
|
||||
Numero.fieldlist(),
|
||||
[
|
||||
('type_id', '=', Numero._type_id),
|
||||
('class_id', '=', Numero._class_id),
|
||||
|
|
|
|||
|
|
@ -104,8 +104,9 @@ class TestLeFactory(TestCase):
|
|||
)
|
||||
|
||||
#Testing _fields
|
||||
expected_fields = [ f for f in emtype.fields(relational=False) if not(hasattr(f, 'immutable') and f.immutable) ]
|
||||
self.assertEqual(
|
||||
set([ f.name for f in emtype.fields(False) ]),
|
||||
set([ f.name for f in expected_fields ]),
|
||||
set([ f for f in letype._fields])
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -54,6 +54,45 @@ class LeTypeTestCase(TestCase):
|
|||
num.all_datas
|
||||
dsmock.assert_called_once()
|
||||
|
||||
def test_fieldlist(self):
|
||||
""" Test fieldlist method """
|
||||
from dyncode import Numero, Rubrique, Article, Personne, LeObject
|
||||
|
||||
letypes = [Numero, Rubrique, Article, Personne]
|
||||
|
||||
for letype in letypes:
|
||||
self.assertEquals(
|
||||
letype.fieldlist(complete=False),
|
||||
letype._fields
|
||||
)
|
||||
self.assertEquals(
|
||||
sorted(letype.fieldlist(complete = True)),
|
||||
sorted(list(set(letype._fields + LeObject.fieldlist())))
|
||||
)
|
||||
for fname in letype.fieldlist(complete = False):
|
||||
self.assertIn(fname, letype._leclass.fieldlist(False))
|
||||
ftype = letype.fieldtypes()[fname]
|
||||
if hasattr(ftype, 'immutable'):
|
||||
if ftype.immutable:
|
||||
self.assertNotIn(fname, LeObject.fieldlist())
|
||||
else:
|
||||
self.assertIn(fname, LeObject.fieldlist())
|
||||
|
||||
def test_fieldtypes(self):
|
||||
""" Test fieldtypes() method """
|
||||
from dyncode import Numero, Rubrique, Article, Personne, LeObject
|
||||
|
||||
letypes = [Numero, Rubrique, Article, Personne]
|
||||
|
||||
for letype in letypes:
|
||||
for complete in [True, False]:
|
||||
self.assertEquals(
|
||||
sorted(letype.fieldlist(complete = complete)),
|
||||
sorted(list(letype.fieldtypes(complete = complete).keys()))
|
||||
)
|
||||
|
||||
|
||||
|
||||
class LeTypeMockDsTestCase(TestCase):
|
||||
""" Tests that need to mock the datasource """
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue