小编Jam*_*sso的帖子

调用类方法时,Python Nose2测试未完成

当我运行包括调用@classmethod使用setuptools和nose2的测试时,测试套件没有完成它只是继续运行.但是我已经检查过测试确实通过并到达函数的末尾,测试套件只是没有完成运行.如果我使用decode_auth_token它删除测试工作正常.我能够将它缩小到类方法,因为我也测试了其他类方法,它们导致了同样的问题

有谁知道为什么会发生这种情况?以下是相关的代码片段,但没有发布太多的代码

我的用户模型中的代码

  @classmethod
  def decode_auth_token(cls, auth_token):
    try:
      payload = jwt.decode(auth_token, config.SECRET_KEY, algorithms=['HS256'])
      # check the hash of what we expect the token to be and token we got to be the same
      if bcrypt.check_password_hash(User.by_id(payload['sub']).api_token_hash, auth_token):
        return payload['sub']
      else:
        return 'Token does not match Api Token.'
    except jwt.ExpiredSignatureError:
      return 'Signature expired. Please log in again.'
    except jwt.InvalidTokenError:
      return 'Invalid Token. Please log in again.'
Run Code Online (Sandbox Code Playgroud)

以下两个函数在调用时也会导致问题

  @classmethod
  def is_username_taken(cls, username):
    return db.session.query(db.exists().where(User.username==username)).scalar()

  @classmethod
  def is_email_taken(cls, email):
    return …
Run Code Online (Sandbox Code Playgroud)

python unit-testing nose python-3.x nose2

6
推荐指数
1
解决办法
233
查看次数

使用marshmallow序列化sqlalchemy hybrid_property

我在我的RESTful烧瓶应用程序中使用sqlalchemy和marshmallow来序列化我的模型.我有一个hybrid_property来自我在该模型上的一个关系.我现在想使用相关模型中的模式在我的模式中序列化hybrid_property.

有没有人这样做过?这是我的相关代码片段.last_assessment当我检查响应时,它似乎不包括模型的序列化版本

class Person(db.Model):
  daily_rula_average_ranges = db.relationship('DailyRulaAverageRange', order_by="DailyRulaAverageRange.date", back_populates='person')

  @hybrid_property
  def last_assessment(self):
    if self.daily_rula_average_ranges.length > 0:
      return self.daily_rula_average_ranges[-1]

class PersonSchema(ma.Schema):
  last_assessment = ma.Nested(DailyRulaAverageRangeSchema, only=['id', 'date', 'risk'])
  class Meta:
    fields = ('last_assessment')
Run Code Online (Sandbox Code Playgroud)

python sqlalchemy flask flask-sqlalchemy marshmallow

6
推荐指数
1
解决办法
679
查看次数