當(dāng)我在 python unittest 的 setUp 中保存一個(gè)實(shí)例時(shí),我彈出了 2 個(gè)錯(cuò)誤:sqlite3.IntegrityError:NOT NULL約束失?。?registro_c170.reg_c100_id和peewee.OperationalError:連接已打開。(這是為我的測試類中的每個(gè) test_method 引發(fā)的。)語境:我對面向?qū)ο蟮木幊毯蛦卧獪y試非常陌生。我正在使用python構(gòu)建寄存器層次結(jié)構(gòu)的模型。該注冊簿包含有關(guān)稅收和問責(zé)制的信息。模型:from peewee import *db = SqliteDatabase('pis_cofins.db')class BaseModel(Model): class Meta: database = dbclass Registro_C100(BaseModel): """ """ # Atributos x = CharField() y = CharField() @property def dados(self): # some property @dados.setter def dados(self, novos_dados): #... @property def formato_linha(self): # some methodclass Registro_C170(BaseModel): """ """ # Atributos a = CharField() b = CharField() reg_c100 = ForeignKeyField(Registro_C100, backref='registros_c170') @property def dados(self): # some property @dados.setter def dados(self, novos_dados): # ... @property def formato_linha(self): # some method老實(shí)說,我知道我唯一可以嘗試的是在每個(gè)測試方法中創(chuàng)建每個(gè)實(shí)例,但這正是我想要避免的。我搜索了“peewee testing”和“peewee unittest”,但沒有找到任何有用的東西。Peewee文檔只有你展示了如何寫的安裝和拆卸方法: http://docs.peewee-orm.com/en/latest/peewee/database.html#testing-peewee-applications 或如何使用他們的test_utils: HTTP: //docs.peewee-orm.com/en/latest/peewee/playhouse.html#test-utils
1 回答

慕森卡
TA貢獻(xiàn)1806條經(jīng)驗(yàn) 獲得超8個(gè)贊
好吧,這很尷尬。我意識到我正在保存一個(gè)沒有值的實(shí)例ForeignKeyField
(默認(rèn)為null=False
)。我需要做的就是允許該字段接受null
值(null=True
)。
添加回答
舉報(bào)
0/150
提交
取消