我正在尝试为Flask应用程序开发一些单元测试,我正在努力解决这个问题。
我想测试我的 create_politician 功能/视图,它需要两个日期(开始和结束日期),我无法弄清楚如何传递它或的格式是什么创建政治家表格正在接受争论。
我的单元测试:
class TestPolitician(BaseTestCase):
#ensures a logged user can create a politician
def test_create_politician(self):
dt = datetime.datetime.strptime("21/11/06", "%d/%m/%y")
with self.client:
self.client.post('/login', data=dict(
email = 'adm@min.com', password='admin'
), follow_redirects=True)
response = self.client.post(
'/create_politician',
data=dict(publicName='Antonio Costa',
completeName='Antonio Cenas Costa',
startDate="21/11/06", endDate="21/11/06"),
follow_redirects=True)
self.assertEqual(response.status_code, 200)
self.assertIn(b'New entry was successfully posted. Thanks.', response.data)
我的表格
class PoliticForm(FlaskForm):
publicName = StringField('Public Name', validators=[DataRequired("Please enter politician public name.")])
completeName = StringField('Complete Name', validators=[DataRequired("Please enter politician complete name.")])
startDate = DateField('Start Date', format='%m-%d-%Y', validators=[DataRequired("Please enter the politician start Date.")])
endDate = DateField('End Date', format='%m-%d-%Y', validators=(validators.Optional(),))
submit = SubmitField('Add Politician', validators=(validators.Optional(),))
和我的 create_politician视图
@politicians_blueprint.route("/create_politician", methods=["GET", "POST"])
@login_required
def create_politician():
form = PoliticForm()
if request.method == "POST":
stDate=datetime.datetime.strptime(request.form.get('date'), '%m/%d/%Y').strftime('%Y-%m-%d')
print stDate
endDate=datetime.datetime.strptime(request.form.get('date2'), '%m/%d/%Y').strftime('%Y-%m-%d')
flash(form.validate())
newpolitician = Politic(form.publicName.data, form.completeName.data, stDate,endDate)
db.session.add(newpolitician)
db.session.commit()
flash('New entry was successfully posted. Thanks.')
return redirect(url_for('home.home'))
elif request.method == "GET":
return render_template("createPolitician.html", form=form)
create_politician.html
{% extends "base.html" %}
{% block content %}
<main class="hero-section">
<div class="container">
<br>
<div class="container">
<h4>Add Entry</h4>
<form method="POST" action="/create_politician">
{{ form.hidden_tag() }}
<div class="form-group">
{{ form.publicName.label }}
{{ form.publicName }}
</div>
<div class="form-group">
{{ form.completeName.label }}
{{ form.completeName }}
</div>
<div class="form-group ">
<label class="control-label " for="date">
Date
</label>
<input class="form-control" id="date" name="date" placeholder="DD/MM/YYYY" type="text"/>
</div>
<div class="form-group ">
<label class="control-label " for="date2">
Date2
</label>
<div class="input-group">
<div class="input-group-addon">
<i class="fa ">
</i>
</div>
<input class="form-control" id="date2" name="date2" placeholder="DD/MM/YYYY" type="text"/>
</div>
</div>
{{ form.submit(class="btn-primary") }}
</form>
</div>
</div>
</main>
{% endblock %}
strptime 期待的字符串格式是什么?
谢谢
此致
答案 0 :(得分:2)
在您看来,您将日期字段称为“日期”和“日期2”
request.form.get('date')
request.form.get('date2')
在您的表单和测试代码中,它们被定义为“startDate”和“endDate”,因此您应该更新视图代码以使用这些名称。
stDate=datetime.datetime.strptime(request.form.get('startDate'), '%m/%d/%Y').strftime('%Y-%m-%d')
endDate=datetime.datetime.strptime(request.form.get('endDate'), '%m/%d/%Y').strftime('%Y-%m-%d'