def test_dictify_with_null(self): """ Test SQLAlchemySchemaNode.dictify(obj) with null values and show that result is a valid appstruct for the given schema """ Base = declarative_base() class Sensor(Base): __tablename__ = 'sensor' sensor_id = Column(Integer, primary_key=True) institution_id = Column(Integer, nullable=True) sensor_label = Column(String, nullable=True) sensor = Sensor( sensor_id=3, institution_id=None, sensor_label=None, ) schema = SQLAlchemySchemaNode(Sensor) appstruct = schema.dictify(sensor) cstruct = schema.serialize(appstruct=appstruct) newappstruct = schema.deserialize(cstruct) newobj = schema.objectify(appstruct) self.assertEqual(appstruct, newappstruct) self.assertEqual(sensor.sensor_id, newobj.sensor_id) self.assertEqual(sensor.institution_id, newobj.institution_id) self.assertEqual(sensor.sensor_label, newobj.sensor_label)
def get_doctypes_form_schema(userdatas_model): """ Returns a dynamically built form for doctypes registration """ registrations = userdatas_model.doctypes_registrations node_schema = SQLAlchemySchemaNode(UserDatasSocialDocTypes) appstruct = {} form_schema = colander.Schema() for index, registration in enumerate(registrations): node = node_schema.clone() name = 'node_%s' % index node.name = name node.title = u'' node['status'].title = registration.doctype.label form_schema.add(node) appstruct[name] = node_schema.dictify(registration) return form_schema, appstruct
def test_dictify(self): import datetime overrides = { 'person': { 'includes': ['name', 'surname', 'gender', 'addresses'], 'overrides': { 'addresses': { 'includes': ['street', 'city'], 'overrides': { 'city': { 'exclude': False } } } } }, } includes = ['email', 'enabled', 'created', 'timeout', 'person'] schema = SQLAlchemySchemaNode(Account, includes=includes, overrides=overrides) #Add a non-SQLAlchemy field schema.add(colander.SchemaNode(colander.String, name='non_sql')) args = dict(street='My Street', city='My City') address = Address(**args) kws = dict(name='My Name', surname='My Surname', gender='M', addresses=[address]) person = Person(**kws) params = dict(email='*****@*****.**', enabled=True, created=datetime.datetime.now(), timeout=datetime.time(hour=00, minute=00), person=person) account = Account(**params) dictified = schema.dictify(account) kws['addresses'] = [args] params['person'] = kws self.assertEqual(dictified, params) for key in params: self.assertIn(key, dictified) if key == 'person': for k in kws: self.assertIn(k, dictified[key])