我正在使用Flask-Testing进行Flask集成测试.我有一个表格,上面有一个徽标文件,我正在尝试编写测试但是我一直收到错误说:TypeError: 'str' does not support the buffer interface.
我正在使用Python 3.我找到的最接近的答案是这个,但它对我不起作用.
这是我的许多尝试之一:
def test_edit_logo(self):
"""Test can upload logo."""
data = {'name': 'this is a name', 'age': 12}
data['file'] = (io.BytesIO(b"abcdef"), 'test.jpg')
self.login()
response = self.client.post(
url_for('items.save'), data=data, follow_redirects=True)
})
self.assertIn(b'Your item has been saved.', response.data)
advert = Advert.query.get(1)
self.assertIsNotNone(item.logo)
Run Code Online (Sandbox Code Playgroud)
如何在Flask中测试文件上传?
我有三个表(用于与运动相关的应用程序):回合、游戏和联赛。
我想找到最近一轮的联赛。为此,我需要找到最新的游戏并找到它所在的回合。
在我的模型回合中有很多比赛,回合有联赛,但比赛和联赛之间没有直接关系。
这是我的模型简化:
class Round(db.Model):
"""Round Model."""
__tablename__ = 'rounds'
id = db.Column(db.Integer, primary_key=True)
order = db.Column(db.Integer, nullable=False)
league_id = db.Column(db.Integer, db.ForeignKey('leagues.id'))
league = db.relationship('League', backref='rounds')
class Game(db.Model):
"""Game model."""
__tablename__ = "games"
id = db.Column(db.Integer, primary_key=True)
utc_time = db.Column(db.DateTime)
round_id = db.Column(db.Integer, db.ForeignKey('rounds.id'))
round = db.relationship('Round', backref="games")
class League(db.Model):
"""League Model."""
__tablename__ = 'leagues'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String, nullable=False)
def __init__(self, name, abbreviation, other_names):
self.name = name
Run Code Online (Sandbox Code Playgroud)
如何使用 round.league 条件查询游戏?
我在想这样的事情,但它不起作用:
game = Game.query.join(Round).join(League).filter(
Game.utc_time …Run Code Online (Sandbox Code Playgroud) 我正在使用SqlAlchemy在Flask 中构建一个与运动相关的应用程序。我有一个游戏表,它有一个和 ,它们都加入了团队表。home_teamaway_team
我希望能够声明Team表和Game表之间的关系,但我收到一条错误消息
Could not determine join condition between parent/child tables on relationship Team.games - there are multiple foreign key paths linking the tables. Specify the 'foreign_keys' argument, providing a list of those columns which should be counted as containing a foreign key reference to the parent table.
模型声明如下所示:
class Team(db.Model):
"""Team model."""
__tablename__ = "teams"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String)
monkier = …Run Code Online (Sandbox Code Playgroud) 我有一个小 Flask 应用程序 (Python3),它开始变得有点复杂,所以我决定从 SQLite 切换到 Postgresql。我知道这会给我带来一些问题,但我的代码库具有良好的测试覆盖率,因此我有信心在投入生产之前可以消除任何问题。
特别是,在我输入密码的任何地方,我都必须进行编码 ( password.encode('utf-8'))。
(顺便说一句,时区是我遇到一些问题的另一个领域。我最终从我使用的任何日期中删除了时区。)
但还有最后一个错误我无法弄清楚。为了测试密码是否已更新,我进行了以下测试:
self.assertTrue(bcrypt.check_password_hash(
user.password, new_password
))
Run Code Online (Sandbox Code Playgroud)
这应该检查当前密码(当我打印它时,它看起来像一个字节刺)与new_password. 但我收到一条错误消息ValueError: Invalid salt
我很想知道如何解决这个问题,但我也希望有人能解释一下这里发生了什么。