我正在使用Flask
,我的SQLAlchemy
模型是
class User(UserMixin, db.Model):
__tablename__ = 'users'
# noinspection PyShadowingBuiltins
uuid = Column('uuid', GUID(), default=uuid.uuid4, primary_key=True,
unique=True)
email = Column('email', String, nullable=False, unique=True) # yes unique is True
我编写测试并导入数据,例如
app = Flask(__name__)
app.config['SECRET_KEY'] = dev.SECRET_KEY
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'
db = SQLAlchemy(app)
我的测试看起来像
class TestUser(TestCase):
def setUp(self):
db.create_all()
def test_add_existing_user(self):
user = User('test_add_existing_user', 'welcome')
db.session.add(user)
db.session.commit()
self.assertEquals(1, len(User.query.all()))
db.session.add(user)
db.session.commit()
self.assertEquals(2, len(User.query.all())) # just to verify if it saves the same user twice.
def tearDown(self):
db.drop_all()
db.session.remove()
当我运行这个测试时,我看到
Failure
Traceback (most recent call last):
File "<project path>/tests/test_user.py", line 28, in test_add_existing_user
self.assertEquals(2, len(User.query.all()))
AssertionError: 2 != 1
似乎它没有两次保存同一个用户,但我希望抛出我没有收到的异常,我在这里没有做什么或错过了什么?