-1

There is my function:

views.py

def save(request):
    data = {'mark':request.POST.get('mark'), 'task':request.POST.get('task')}
    Task.objects.filter(id=request.POST.get('s')).update(mark=data['mark'], task=data['task'])
    return redirect(list)

What is wrong in my test?It doesn't update database.Please help!!!

tests.py

from todo.models import Task
class TaskTest(TestCase):
def test_ok_update_task(self):
    s=1
    Task.objects.create(mark=True, task='task')
    data = {'mark': False, 'task': '1'}
    self.client.post('/save', data)
    task_1 = Task.objects.filter(id=s).get()
    self.assertNotEquals(task_1.mark, True)
    self.assertNotEquals(task_1.task, 'task')
    self.assertEquals(task_1.mark, data['mark'])
    self.assertEquals(task_1.task, data['task'])

models.py

class Task(models.Model):
    mark = models.NullBooleanField()
    task=models.CharField(max_length=200, null=True)
    up_url=models.CharField(max_length=1000, null=True)
    down_url=models.CharField(max_length=1000, null=True)
    update_url=models.CharField(max_length=1000, null=True)
    delete_url=models.CharField(max_length=1000, null=True)
JDxun
  • 1
  • 1

1 Answers1

1

Your view expects request.POST['s'] to contain the id

Task.objects.filter(id=request.POST.get('s'))

but you have forgotten to include it in your data.

data = {'mark': False, 'task': '1'}

An easy way to debug problems like this is to add print statements to your view and tests. If you had added print request.POST and print request.POST.get('s') to your model, you'd probably have spotted the problem.

Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • Try adding print statements to debug the problem as I suggested. – Alasdair Jul 26 '16 at 17:06
  • Views and model are right and app is working on test-server, but my task is write test .I tried to use print, don't see what's wrong – JDxun Jul 26 '16 at 17:21
  • What *do* you see? What print statements have you added? What values did you get? Is the correct view being called by your test? Is the filter working? – Alasdair Jul 26 '16 at 17:27
  • Correct view was call by test,and filter is working, but values in testing database don't change, it's still values that i create in first string of test file – JDxun Jul 26 '16 at 17:59