0

I have the following model and form

class TravelShare(Share):  # indirect derived via Share from models.Model
    source = models.PointField(geography=True, srid=4326)
    destination = models.PointField(geography=True, srid=4326)
    departure = models.DateTimeField(default=timezone.now)
    departure_delta = models.SmallIntegerField(default=60)

    objects = TravelShareManager()

    def __str__(self):
        return self.id

class TravelShareForm(forms.ModelForm):

    class Meta:
        model = TravelShare
        fields = ['source', 'destination', 'departure', 'departure_delta']

When testing the form with

def test_TravelShareCreate(self):

    source = Point(x=48.0, y=11.0, srid=4326)
    destination = Point(x=48.0001, y=11.0001, srid=4326)
    now = timezone.localtime(timezone.now())
    print ("now : %s" % (now))
    now_string = now.strftime('%m.%d.%Y %H:%M:%S')

    form_data = {

        'source': source,
        'destination': destination,
        'departure': now_string,
        'departure_delta': 30,

    }

    form = TravelShareForm (form_data)
    print (form.errors)
    self.assertTrue(form.is_valid())

    form.instance.creator = self.creator
    result = form.save()
    self.assertEqual(result.creator_id, self.creator.id)
    self.assertEqual(result.source, source)
    self.assertEqual(result.destination, destination)
    result_dep = timezone.localtime(result.departure)
    print("result_dep : %s" % (result_dep))
    self.assertEqual(result.departure, now)

I am ending up in

now : 2017-02-10 15:49:21.935894+01:00

result_dep : 2017-10-02 15:49:21+02:00

Failure

Traceback (most recent call last): File "/home/michael/PycharmProjects/sharadar/main/tests/test_forms.py", line 43, in test_TravelShareCreate self.assertEqual(result.departure, now) AssertionError: datet[14 chars]017, 10, 2, 15, 49, 21, tzinfo=DstTzInfo 'Eur[25 chars]DST) != datet[14 chars]017, 2, 10, 15, 49, 21, 935894, tzinfo=DstTzI[32 chars]STD

It seems that the resulted DateTime has a different TZ or at least shows +2 instead of +1

Thanks for any help

Michael

1
  • I have found it. The result from the database is rounded to seconds and therefor 15:49.21.93894+1.00 becomes 15:49:21+02:00 Commented Feb 10, 2017 at 15:11

2 Answers 2

1

The result from the database is rounded to seconds and therefor 15:49.21.93894+1.00 becomes 15:49:21+02:00

It's totally wrong. You have dropped microseconds part during converting datetime object to string now.strftime('%m.%d.%Y %H:%M:%S'). And also, +02:00 is a timezone utc offset of datetime object. It's not a rounded seconds or whatever.

And the real problem was in your's strftime string format. Today, your timezone probably has Daylight Saving Time. But, when you have passed wrong formatted datetime string to form, django form parsed it as 2017-10-02(02 October 2017) that is standart time in your timezone. So, that was the reason why you have got different utc offsets: +01:00 and +02:00.

Sign up to request clarification or add additional context in comments.

1 Comment

Your are right. I have updated my reply with the new test that is now using strftime('%d.%m.%Y %H:%M:%S') in both cases
0

The result from the database is rounded to seconds and therefor 15:49.21.93894+1.00 becomes 15:49:21+02:00

Beside the fact that the strftime format string should be :('%d.%m.%Y %H:%M:%S') instead of ('%m.%d.%Y %H:%M:%S')

My Test is now like this :

class TestTravelShareForm(TestCase):

def setUp(self):
    self.creator = ShrUser.objects.create_user(username="foo", email="[email protected]", password="bla")
    self.source = Point(x=48.0, y=11.0, srid=4326)
    self.destination = Point(x=48.0001, y=11.0001, srid=4326)
    self.now = timezone.now()
    self.now_string = self.now.strftime('%d.%m.%Y %H:%M:%S')

def test_TravelShareCreate(self):

    form_data = {

        'source': self.source,
        'destination': self.destination,
        'departure': self.now_string,

    }

    form = TravelShareForm (form_data)
    self.assertTrue(form.is_valid())

    form.instance.creator = self.creator
    result = form.save()

    self.assertEqual(result.creator_id, self.creator.id)
    self.assertEqual(result.source, self.source)
    self.assertEqual(result.destination, self.destination)

    result_dep = result.departure.strftime('%d.%m.%Y %H:%M:%S')

    self.assertEqual(result_dep, self.now_string)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.