Ejemplo n.º 1
0
    def setUpClass(cls):

        if not hasattr(cls, "engine"):
            os.system("rm tests/test_donkey.sqlite")
            cls.engine = create_engine('sqlite:///tests/test_donkey.sqlite')

        try:
            os.remove("tests/zodb.fs")
            os.remove("tests/zodb.fs.lock")
            os.remove("tests/zodb.fs.index")
            os.remove("tests/zodb.fs.tmp")
        except OSError:
            pass

        meta_to_drop = sa.MetaData()
        meta_to_drop.reflect(bind=cls.engine)
        for table in reversed(meta_to_drop.sorted_tables):
            table.drop(bind=cls.engine)

        super(test_donkey_persist, cls).setUpClass()

#        cls.meta = sa.MetaData()
#        cls.Session = sa.orm.sessionmaker(bind =cls.engine , autoflush = False)
#        cls.Donkey = Database("Donkey",
#                        zodb_store = "tests/zodb.fs",
#                        metadata = cls.meta,
#                        engine = cls.engine,
#                        session = cls.Session)

        cls.p = random.randrange(1,10000)
        cls.Donkey.add_table(tables.Table("moo%s" % cls.p, Text("moo")))
        cls.Donkey.persist()

        cls.session = cls.Donkey.Session()

        cls.jim = cls.Donkey.tables["donkey"].sa_class()
        cls.jim.name = u"jim"
        cls.jim.age = 13
        cls.jim1 = cls.Donkey.tables["donkey"].sa_class()
        cls.jim1.name = u"jim1"
        cls.jim1.age = 131
        jim2 = cls.Donkey.tables["donkey"].sa_class()
        jim2.name = u"jim2"
        jim2.age = 132
        jim3 = cls.Donkey.tables["donkey"].sa_class()
        jim3.name = u"jim3"
        jim3.age = 133
        jim4 = cls.Donkey.tables["donkey"].sa_class()
        jim4.name = u"jim4"
        jim4.age = 142
        jim5 = cls.Donkey.tables["donkey"].sa_class()
        jim5.name = u"jim5"
        jim5.age = 135
        jim6 = cls.Donkey.tables["donkey"].sa_class()
        jim6.name = u"jim6"
        jim6.age = 136
        jim7 = cls.Donkey.tables["donkey"].sa_class()
        jim7.name = u"jim7"
        jim7.age = 137
        jim8 = cls.Donkey.tables["donkey"].sa_class()
        jim8.name = u"jim8"
        jim8.age = 138
        jim9 = cls.Donkey.tables["donkey"].sa_class()
        jim9.name = u"jim9"
        jim9.age = 132
        jim0 = cls.Donkey.tables["donkey"].sa_class()
        jim0.name = u"jim0"
        jim0.age = 102
        cls.david = cls.Donkey.tables["people"].sa_class()
        cls.david.name = u"david"
        cls.david.address_line_1 = u"43 union street"
        cls.david.postcode = u"es388"
        davidsjim = cls.Donkey.tables["donkey_sponsership"].sa_class()
        davidsjim._people = cls.david
        davidsjim._donkey = cls.jim
        davidsjim.amount = 50

        jimpic = file("tests/jim.xcf", mode = "rb").read()

        jimimage = cls.Donkey.tables["donkey_pics"].sa_class()
        jimimage.pic = jimpic

        cls.session.add(cls.david)
        cls.session.add(cls.jim)
        cls.session.add(cls.jim1)
        cls.session.add(jim2)
        cls.session.add(jim3)
        cls.session.add(jim4)
        cls.session.add(jim5)
        cls.session.add(jim6)
        cls.session.add(jim7)
        cls.session.add(jim8)
        cls.session.add(jim9)
        cls.session.add(jim0)
        cls.session.add(davidsjim)
        cls.session.add(jimimage)

        cls.david2 = cls.Donkey.tables["people"].sa_class()
        cls.david2.name = u"david"
        cls.david2.address_line_1 = u""
        cls.david_logged = get_table_from_instance(cls.david, cls.Donkey).logged_instance(cls.david)
        cls.session.add(cls.david_logged)
        cls.session.commit()

        donk = cls.session.query(cls.Donkey.get_class("donkey")).first()
        donk.name = u"jimmii%s" % cls.p
        cls.session.add(donk)
        cls.session.commit()
        cls.jimmi_id = donk.id


        donk2 = cls.session.query(cls.Donkey.get_class("donkey")).filter_by(name=donk.name).first()
        donk2.name = u"jimmii"
        cls.session.add(donk2)
        cls.session.commit()
Ejemplo n.º 2
0
    def test_address_validation(self):

        assert len(get_table_from_instance(self.david, self.Donkey).validate(self.david, self.session)) > 3

        assert_raises(formencode.Invalid,
                      get_table_from_instance(self.david2, self.Donkey).validate,self.david2, self.session)