Files @ 1641e0ea14d6
Branch filter:

Location: rattail-project/rattail/tests/test_time.py - annotation

lance
fix: add startup workaround for trainwreck query bug

not at all clear why, but c621e21cd4b4a2a408d8a32ace0b34afe6fdf526 has
introduced some strange bug where the "first" trainwreck query will
raise an error and yet subsequent queries work fine.

so now in the startup sequence we simply force a query, which for
unknown reasons does *not* raise any error, and will somehow magically
prevent the error when a "normal" trainwreck query happens later.

AFAIK this is a "benign" operation and causes no real side effects
other than preventing the strange error. fingers crossed, this is
fine and will not cause further problems...??!
# -*- coding: utf-8; -*-

import datetime
from unittest import TestCase

import pytz
from wuttjamaican.exc import ConfigurationError

from rattail import time
from rattail.config import RattailConfig


class TestLocaltime(TestCase):

    def setUp(self):
        self.config = RattailConfig()
        self.config.setdefault('rattail', 'timezone.default', 'America/Los_Angeles')
        self.config.setdefault('rattail', 'timezone.missouri', 'America/Chicago')

    def test_default_returns_current_time_in_default_timezone(self):
        zone = pytz.timezone('America/Los_Angeles')
        before = zone.normalize(pytz.utc.localize(datetime.datetime.utcnow()).astimezone(zone))
        local = time.localtime(self.config)
        after = zone.normalize(pytz.utc.localize(datetime.datetime.utcnow()).astimezone(zone))
        self.assertTrue(before <= local)
        self.assertTrue(local <= after)
        self.assertEqual(local.tzinfo.zone, 'America/Los_Angeles')

    def test_provided_time_is_localized_to_default_timezone(self):
        t = datetime.datetime(2014, 7, 26, 8, 28, 41)
        t = time.localtime(self.config, t)
        self.assertEqual(t.year, 2014)
        self.assertEqual(t.month, 7)
        self.assertEqual(t.day, 26)
        self.assertEqual(t.hour, 8)
        self.assertEqual(t.minute, 28)
        self.assertEqual(t.second, 41)
        self.assertEqual(t.tzinfo.zone, 'America/Los_Angeles')

    def test_provided_time_is_localized_to_custom_timezone(self):
        t = datetime.datetime(2014, 7, 26, 8, 28, 41)
        t = time.localtime(self.config, t, 'missouri')
        self.assertEqual(t.year, 2014)
        self.assertEqual(t.month, 7)
        self.assertEqual(t.day, 26)
        self.assertEqual(t.hour, 8)
        self.assertEqual(t.minute, 28)
        self.assertEqual(t.second, 41)
        self.assertEqual(t.tzinfo.zone, 'America/Chicago')


class TestTimezone(TestCase):

    def setUp(self):
        self.config = RattailConfig()

    def test_default_timezone_returned_by_default(self):
        self.config.setdefault('rattail', 'timezone.default', 'Antarctica/Rothera')
        zone = time.timezone(self.config)
        self.assertEqual(zone.zone, 'Antarctica/Rothera')

    def test_local_timezone_returned_if_no_default(self):
        self.config.setdefault('rattail', 'timezone.local', 'Africa/Lagos')
        zone = time.timezone(self.config)
        self.assertEqual(zone.zone, 'Africa/Lagos')

    def test_custom_timezone_returned_instead_of_default(self):
        self.config.setdefault('rattail', 'timezone.another', 'Canada/Yukon')
        zone = time.timezone(self.config, key='another')
        self.assertEqual(zone.zone, 'Canada/Yukon')

    def test_config_error_if_no_timezone_defined(self):
        self.assertRaises(ConfigurationError, time.timezone, self.config)