update() on queryset not functional.

15 views
Skip to first unread message

Biola Oyeniyi

unread,
Nov 10, 2014, 5:15:04 AM11/10/14
to django...@googlegroups.com
based on the docs, update() Performs an SQL update query for the specified fields, and returns the number of rows matched.
I have tried updating a model using it on django 1.7.1 with both sqlite and postgres but my test suite keeps failing and indicating no update.

Here is what i have tried.

    

    def create_session(self, email='devito@gmailcom', date=datetime.datetime.now(),
                       duration=datetime.timedelta(hours=1)):
        new_user = UserFactory(email=email)
        sf = TutorSessionFactory(tutor_with_skill=self.ts, buyer=new_user,
                            date=date,duration=duration)
        return sf

    def test_can_update_ehen_udated(self):
        result = self.create_session()
        Session.objects.filter(id=result.id).update(price=Decimal('700'))
        self.assertEqual(result.price,Decimal('700'))

and below is the result from the test suite

    Failure
Traceback (most recent call last):
  File "C:\Python27\Lib\unittest\case.py", line 329, in run
    testMethod()
  File "F:\work-projects\python\django-projects\Tuteria\tuteria\tutor_sessions\tests\test_model.py", line 24, in test_can_update_ehen_udated
    self.assertEqual(result.price,Decimal('700'))
  File "C:\Python27\Lib\unittest\case.py", line 513, in assertEqual
    assertion_func(first, second, msg=msg)
  File "C:\Python27\Lib\unittest\case.py", line 506, in _baseAssertEqual
    raise self.failureException(msg)
AssertionError: 500 != Decimal('700')

Pls can someone confirm is my suspicions are valid. thanks

Tom Evans

unread,
Nov 10, 2014, 7:52:46 AM11/10/14
to django...@googlegroups.com
On Mon, Nov 10, 2014 at 10:15 AM, Biola Oyeniyi <gbo...@gmail.com> wrote:
> based on the docs, update() Performs an SQL update query for the specified
> fields, and returns the number of rows matched.
> I have tried updating a model using it on django 1.7.1 with both sqlite and
> postgres but my test suite keeps failing and indicating no update.
>
> Here is what i have tried.
>
>
>
> def create_session(self, email='devito@gmailcom',
> date=datetime.datetime.now(),
> duration=datetime.timedelta(hours=1)):
> new_user = UserFactory(email=email)
> sf = TutorSessionFactory(tutor_with_skill=self.ts, buyer=new_user,
> date=date,duration=duration)
> return sf
>
> def test_can_update_ehen_udated(self):
> result = self.create_session()

This creates data from the database, and returns an object with the
values just stored in the database.

> Session.objects.filter(id=result.id).update(price=Decimal('700'))

This updates data in the database

> self.assertEqual(result.price,Decimal('700'))

This asserts that the data you fetched from the database before you
updated the database has the value which you just updated it to in the
database.

I hope you can see that is unlikely to ever work. If you want to check
that the data in the database has been updated, you will need to
re-fetch the data from the database after updating it.

Bear in mind that this test is effectively then just testing that
django can store stuff in a database and fetch it out afterwards.
Django itself has lots of tests that verify this happens.

Cheers

Tom

Biola Oyeniyi

unread,
Nov 10, 2014, 12:24:20 PM11/10/14
to django...@googlegroups.com
Thanks for the clarification. Just confirmed your suggestions. Learning something new everyday. 
Reply all
Reply to author
Forward
0 new messages