예제 #1
0
    def test_experiments(self):
        now = datetime.now
        utcnow = datetime.utcnow
        from_ = datetime(2012, 10, 11) - timedelta(days=10)

        # lambdas is for dates accuracy
        experiments = [
            {
                'machine':  lambda: m(5).days.ago(),
                'expected': lambda: now() - timedelta(days=5),
            },
            {
                'machine':  lambda: m(5).days.later(),
                'expected': lambda: now() + timedelta(days=5),
            },
            {
                'machine':  lambda: m(5).days.ago(in_utc=True),
                'expected': lambda: utcnow() - timedelta(days=5),
                'msg':      'date should be in utc'
            },
            {
                'machine':  lambda: m(5).days.later(in_utc=True),
                'expected': lambda: utcnow() + timedelta(days=5),
                'msg':      'date should be in utc'
            },
            {
                'machine':  lambda: m(5).days.ago(from_=from_),
                'expected': lambda: from_ - timedelta(days=5),
                'msg':      'calculation from "from_" date is wrong'
            },
            {
                'machine':  lambda: m(5).days.and_(10).minutes.ago(),
                'expected': lambda: m(10).minutes.ago(from_=m(5).days.ago()),
                'msg':      'calculation with "and" chain is wrong'
            },
            {
                'machine':  lambda: m(9).days.and_(8).hours.and_(7).seconds.later(),
                'expected': lambda: now() + timedelta(days=9, hours=8, seconds=7),
                'msg':      'calculation with long chain is wrong'
            },
            {
                'machine':  lambda: m(1.5).days.ago(from_=from_),
                'expected': lambda: from_ - timedelta(days=1.5),
                'msg':      'float delta works wrong'
            }            
        ]
        
        for exp in experiments:
            response = exp['machine']()
            self.assert_equals_dates(response, exp['expected'](), msg=exp.get('msg'))
예제 #2
0
    def test_singular_date_component(self):
        experiments = ['day', 'hour', 'minute', 'second', 'microsecond']

        for exp in experiments:
            from_ = datetime(2012, 10, 11)
            kwargs = {exp + 's': 1}
            expected = from_ - timedelta(**kwargs)
            msg_error = 'singular form for "%s" does not supported' % exp
            self.assertEquals(getattr(m(1), exp).ago(from_=from_), expected, msg_error)