2016-12-03 162 views
0

我正在尝试开发一些单元测试到我的Flask应用程序,并且我正在为此努力。strptime()参数1必须是字符串,而不是无

我想测试我的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 = '[email protected]', 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

假设这个问题的标题是错误讯息,比我会说,在首位,这必须是一个字符串,而不是NoneType - 从文字似乎有点明显。我不知道Flask,但我的猜测是,request.form.get'在失败时返回'None',并检查它。 |查看[docs](http://werkzeug.pocoo.org/docs/0.11/datastructures/#werkzeug.datastructures.MultiDict),我们看到“如果请求的数据不存在,则返回默认值”。而'None'是默认的默认值。 –

回答

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' 
+0

我不这么认为,因为在我的** create_politician.html **上,我为startDate和endDate创建了两个数据输入字段(date和date2)。发布更新。 – Perseverance

相关问题