Beispiel #1
0
 def testOneToMany1(self):
     a = Datum('Schema',
               'id',
               id=1,
               items=one_to_many([
                   Datum('OtherSchema',
                         'id',
                         id=i,
                         value=i,
                         items=one_to_many([
                             Datum('YetOtherSchema', 'id', id=j, value=j)
                             for j in range(0, 10)
                         ], 'other_schema_id')) for i in range(0, 10)
               ], 'schema_id'))
     self.assertEqual(10, len(a.items))
     suite = DataSuite()
     DataWalker(suite)(a)
     for i, item in enumerate(a.items):
         self.assertEqual(i, item.id)
         self.assertEqual(1, item.schema_id)
         self.assertTrue(item in suite['OtherSchema'].data)
         for j, another_item in enumerate(item.items):
             self.assertEqual(j, another_item.id)
             self.assertEqual(item.id, another_item.other_schema_id)
             self.assertTrue(another_item in suite['YetOtherSchema'].data)
     datasets = list(suite)
     self.assertEqual(3, len(datasets))
     self.assertEqual('Schema', datasets[0].schema)
     self.assertEqual('OtherSchema', datasets[1].schema)
     self.assertEqual('YetOtherSchema', datasets[2].schema)
Beispiel #2
0
 def testOneToMany2(self):
     a = Datum(
         'Schema',
         'id',
         id=1,
         items=one_to_many(
             [
                 Datum(
                     'OtherSchema',
                     'id',
                     id=i,
                     value=i,
                     schema_id=many_to_one('Schema')
                     )
                 for i in range(0, 10)
                 ]
             )
         )
     self.assertEqual(10, len(a.items))
     suite = DataSuite()
     DataWalker(suite)(a)
     for i, item in enumerate(a.items):
         self.assertEqual(i, item.id)
         self.assertEqual(1, item.schema_id)
         self.assertTrue(item in suite['OtherSchema'].data)
     datasets = list(suite)
     self.assertEqual(2, len(datasets))
     self.assertEqual('Schema', datasets[0].schema)
     self.assertEqual('OtherSchema', datasets[1].schema)
Beispiel #3
0
        def __setattr__(self, k, v):
            if k.startswith('_'):
                object.__setattr__(self, k, v)
            else:
                if isinstance(v, FunctionType):
                    v = Lazy(v)
                elif isinstance(v, DatumBase):
                    # implicit many_to_one
                    v = many_to_one(v, k, v._tableau_id_fields)
                elif is_iterable_container(v):
                    # implicit one_to_many
                    v = one_to_many(v, k)
                if isinstance(v, DynamicField):
                    v.bind(self, k)

                if isinstance(v, many_to_one):
                    for _k in v.this_side_fields:
                        self.__check_key_is_declared(_k)
                elif isinstance(v, one_to_many):
                    self.__check_key_is_declared(k)
                    if v.referred_fields is not None:
                        for _k in v.referred_fields:
                            self.__check_key_is_declared(_k)
                object.__getattribute__(self, '_tableau_fields')[k] = v
                if self._tableau_managed_class is not None and not isinstance(
                        v, Lazy):
                    object.__setattr__(self, k, self._value_of(k, v))
Beispiel #4
0
        def __setattr__(self, k, v):
            if k.startswith('_'):
                object.__setattr__(self, k, v)
            else:
                if isinstance(v, FunctionType):
                    v = Lazy(v)
                elif isinstance(v, DatumBase):
                    # implicit many_to_one
                    v = many_to_one(v, k, v._tableau_id_fields)
                elif is_iterable_container(v):
                    # implicit one_to_many
                    v = one_to_many(v, k)
                if isinstance(v, DynamicField):
                    v.bind(self, k)

                if isinstance(v, many_to_one):
                    for _k in v.this_side_fields:
                        self.__check_key_is_declared(_k)
                elif isinstance(v, one_to_many):
                    self.__check_key_is_declared(k)
                    if v.referred_fields is not None:
                        for _k in v.referred_fields:
                            self.__check_key_is_declared(_k)
                object.__getattribute__(self, '_tableau_fields')[k] = v
                if self._tableau_managed_class is not None and not isinstance(v, Lazy):
                    object.__setattr__(self, k, self._value_of(k, v))
Beispiel #5
0
 def testOneToMany1(self):
     a = Datum(
         'Schema',
         'id',
         id=1,
         items=one_to_many(
             [
                 Datum(
                     'OtherSchema',
                     'id',
                     id=i,
                     value=i,
                     items=one_to_many([
                         Datum(
                             'YetOtherSchema',
                             'id',
                             id=j,
                             value=j
                             )
                         for j in range(0, 10)
                         ],
                         'other_schema_id'
                         )
                     )
                 for i in range(0, 10)
                 ],
             'schema_id'
             )
         )
     self.assertEqual(10, len(a.items))
     suite = DataSuite()
     DataWalker(suite)(a)
     for i, item in enumerate(a.items):
         self.assertEqual(i, item.id)
         self.assertEqual(1, item.schema_id)
         self.assertTrue(item in suite['OtherSchema'].data)
         for j, another_item in enumerate(item.items):
             self.assertEqual(j, another_item.id)
             self.assertEqual(item.id, another_item.other_schema_id)
             self.assertTrue(another_item in suite['YetOtherSchema'].data)
     datasets = list(suite)
     self.assertEqual(3, len(datasets))
     self.assertEqual('Schema', datasets[0].schema)
     self.assertEqual('OtherSchema', datasets[1].schema)
     self.assertEqual('YetOtherSchema', datasets[2].schema)
Beispiel #6
0
    def testIfDeclarativeIsAddableToSession(self):
        class Foo(self.declarative_base):
            __tablename__ = 'Foo'
            id = Column(Integer, primary_key=True)
            field = Column(String)
            bars = relationship('Bar')
            def some_model_specific_method(self):
              return self.field

        class Bar(self.declarative_base):
            __tablename__ = 'Bar'
            id = Column(Integer, primary_key=True)
            type = Column(String)
            foo_id = Column(Integer, ForeignKey('Foo.id'))
            __mapper_args__ = { 'polymorphic_on': type }

        class Foobar(Bar):
            __tablename__ = 'Foobar'
            __mapper_args__ = {'polymorphic_identity': 'foobar'}
            id = Column(Integer, ForeignKey('Bar.id'), primary_key=True)

        SADatum = newSADatum(self.metadata, self.declarative_base)
        datum = SADatum(
            'Foo',
            auto('id'),
            field='test',
            bars=one_to_many([
                SADatum(
                    'Bar',
                    auto('id')
                    ),
                SADatum(
                    'Foobar',
                    auto('id')
                    )
                ],
                'foo_id'
                )
            )
        self.assertEqual('Foo', datum._tableau_schema)
        self.assertEqual(Foo.__table__, datum._tableau_table)
        self.assertEqual('test', datum.field)
        self.assertIsInstance(datum, Foo)
        self.assertEqual('test', datum.some_model_specific_method())
        self.assertIsInstance(datum.bars[0], Bar)
        self.assertIsInstance(datum.bars[1], Bar)
        engine = create_engine('sqlite+pysqlite:///', echo=True)
        session = sessionmaker(engine)()
        self.metadata.create_all(engine)
        session.add(datum)
        session.flush()
        self.assertEqual(1, datum.id)
        self.assertEqual(1, datum.bars[0].id)
        self.assertEqual(2, datum.bars[1].id)
Beispiel #7
0
 def __setattr__(self, k, v):
     if k.startswith('_'):
         object.__setattr__(self, k, v)
     else:
         if isinstance(v, FunctionType):
             v = Lazy(v)
         elif isinstance(v, DatumBase):
             # implicit many_to_one
             v = many_to_one(v, k, v._tableau_id_fields)
         elif is_iterable_container(v):
             # implicit one_to_many
             v = one_to_many(v, k)
         if isinstance(v, DynamicField):
             v.bind(self, k)
         self._tableau_fields[k] = v
Beispiel #8
0
 def __setattr__(self, k, v):
     if k.startswith('_'):
         object.__setattr__(self, k, v)
     else:
         if isinstance(v, FunctionType):
             v = Lazy(v)
         elif isinstance(v, DatumBase):
             # implicit many_to_one
             v = many_to_one(v, k, v._tableau_id_fields)
         elif is_iterable_container(v):
             # implicit one_to_many
             v = one_to_many(v, k)
         if isinstance(v, DynamicField):
             v.bind(self, k)
         self._tableau_fields[k] = v
Beispiel #9
0
    def testIfDeclarativeIsAddableToSession(self):
        class Foo(self.declarative_base):
            __tablename__ = 'Foo'
            id = Column(Integer, primary_key=True)
            field = Column(String)
            bars = relationship('Bar')

            def some_model_specific_method(self):
                return self.field

        class Bar(self.declarative_base):
            __tablename__ = 'Bar'
            id = Column(Integer, primary_key=True)
            type = Column(String)
            foo_id = Column(Integer, ForeignKey('Foo.id'))
            __mapper_args__ = {'polymorphic_on': type}

        class Foobar(Bar):
            __tablename__ = 'Foobar'
            __mapper_args__ = {'polymorphic_identity': 'foobar'}
            id = Column(Integer, ForeignKey('Bar.id'), primary_key=True)

        SADatum = self.SADatum = newSADatum(self.metadata)
        datum = SADatum(
            'Foo',
            auto('id'),
            field='test',
            bars=one_to_many(
                [SADatum('Bar', auto('id')),
                 SADatum('Foobar', auto('id'))], 'foo_id'))
        self.assertEqual('Foo', datum._tableau_schema)
        self.assertEqual(Foo.__table__, datum._tableau_table)
        self.assertEqual('test', datum.field)
        self.assertEqual('test', datum.some_model_specific_method())
        engine = create_engine('sqlite+pysqlite:///', echo=True)
        session = sessionmaker(engine)()
        self.metadata.create_all(engine)
        session.add(datum)
        session.flush()
        self.assertEqual(1, datum.id)
        self.assertEqual(1, datum.bars[0].id)
        self.assertEqual(2, datum.bars[1].id)
Beispiel #10
0
 def testOneToMany2(self):
     a = Datum('Schema',
               'id',
               id=1,
               items=one_to_many([
                   Datum('OtherSchema',
                         'id',
                         id=i,
                         value=i,
                         schema_id=many_to_one('Schema'))
                   for i in range(0, 10)
               ]))
     self.assertEqual(10, len(a.items))
     suite = DataSuite()
     DataWalker(suite)(a)
     for i, item in enumerate(a.items):
         self.assertEqual(i, item.id)
         self.assertEqual(1, item.schema_id)
         self.assertTrue(item in suite['OtherSchema'].data)
     datasets = list(suite)
     self.assertEqual(2, len(datasets))
     self.assertEqual('Schema', datasets[0].schema)
     self.assertEqual('OtherSchema', datasets[1].schema)