1. John Paulett
  2. HORNET

Source

HORNET / tests / test_util.py

# -*- coding: utf-8 -*-
#
# Copyright 2009 Vanderbilt University
# 
# Licensed under the Apache License, Version 2.0 (the "License"); 
# you may not use this file except in compliance with the License. 
# You may obtain a copy of the License at 
# 
#     http://www.apache.org/licenses/LICENSE-2.0 
# 
# Unless required by applicable law or agreed to in writing, software 
# distributed under the License is distributed on an "AS IS" BASIS, 
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
# See the License for the specific language governing permissions and 
# limitations under the License. 

from datetime import date, timedelta
from hornet.util import *
from unittest import TestCase
import copy
import hornet.util

def test_period():
    p = Period(date(2006, 1, 8), date(2006, 1, 9))
    assert len(p) == 2
    assert p[0] == date(2006, 1, 8)
    assert p[1] == date(2006, 1, 9)
    
def test_period_str():
    p = Period(date(2006, 1, 8), date(2006, 1, 9))
    assert str(p) == '20060108-20060109'
    assert 'a_%s_b.%s' % (p, 'txt') == 'a_20060108-20060109_b.txt'

def test_period_repr():
    p = Period(date(2008, 1, 8), date(2008, 1, 9))
    assert repr(p) == '<Period(datetime.date(2008, 1, 8), datetime.date(2008, 1, 9))>'

def test_period_deepcopy():
    p = Period(date(2008, 1, 8), date(2008, 1, 9))
    cp = copy.deepcopy(p)
    assert p == cp
    assert repr(cp) == '<Period(datetime.date(2008, 1, 8), datetime.date(2008, 1, 9))>'

def test_make_periods_less_than_a_week():
    start = date(2006, 1, 8)
    end = date(2006, 1, 9)
    delta = timedelta(7)
    result = make_periods(start, end, delta)
    assert len(result) == 1

def test_make_periods_single_week():
    start = date(2008, 9, 28)
    end = date(2008, 10, 5)
    delta = timedelta(7)
    result = make_periods(start, end, delta)
    expected = [(date(2008, 9, 28), date(2008, 10, 5))]
    assert result == expected

def test_make_periods_single_week_plus_some():
    start = date(2008, 9, 28)
    end = date(2008, 10, 8)
    delta = timedelta(7)
    result = make_periods(start, end, delta)
    expected = [(date(2008, 9, 28), date(2008, 10, 5)),
                (date(2008, 10, 5), date(2008, 10, 8))]
    assert result == expected 

def test_make_periods_large():
    start = date(2008, 10, 5)
    end = date(2008, 10, 25)
    delta = timedelta(7)
    result = make_periods(start, end, delta)
    assert len(result) == 3

class DecoratorTest(TestCase):
    def test_memoize_noargs(self):
        seq = iter([1, 2])
        @memoize
        def func():
            return seq.next()
        
        self.assertEqual(1, func())
        self.assertEqual(1, func())
        
    def test_memoize(self):
        seq = iter([1, 2])
        @memoize
        def func(value):
            """My docstring"""
            return seq.next() * value
        
        self.assertEqual(2, func(2))
        self.assertEqual(2, func(2))
        self.assertEqual(6, func(3))
        self.assertEqual(6, func(3))
        
        self.assertEqual('func', func.__name__)
        self.assertEqual('My docstring', func.__doc__)
        
    def test_memoize_unhashable(self):
        seq = iter([1, 2])
        @memoize
        def func(value):
            return seq.next() * value[0]
        
        self.assertEqual(2, func([2]))
        self.assertEqual(4, func([2]))

class CountUniqueTest(TestCase):
    def test_no_uniques(self):
        data = ['a', 'd', 26, 'p t']
        result = count_unique(data)
        self.assertEqual(4, len(result))
        for k, v in result.iteritems():
            self.assertTrue(k in data)
            self.assertEqual(1, v)
            
    def test_dups(self):
        data = ['a', 'd', 26, 'd', 26, 26, 'p t']
        result = count_unique(data)
        self.assertEqual(4, len(result))
        self.assertEqual(1, result['a'])
        self.assertEqual(2, result['d'])
        self.assertEqual(3, result[26])
        self.assertEqual(1, result['p t'])

    def test_single(self):
        data = ['a']
        result = count_unique(data)
        self.assertEqual(1, len(result))
        self.assertEqual(1, result['a'])

    def test_empty(self):
        data = []
        result = count_unique(data)
        self.assertEqual(0, len(result))

    def test_dups_generator(self):
        data = ['a', 'd', 26, 'd', 26, 26, 'p t']
        result = count_unique((x for x in data))
        self.assertEqual(4, len(result))
        self.assertEqual(1, result['a'])
        self.assertEqual(2, result['d'])
        self.assertEqual(3, result[26])
        self.assertEqual(1, result['p t'])
    
    def test_dups_iter(self):
        data = ['a', 'd', 26, 'd', 26, 26, 'p t']
        result = count_unique(iter(data))
        self.assertEqual(4, len(result))
        self.assertEqual(1, result['a'])
        self.assertEqual(2, result['d'])
        self.assertEqual(3, result[26])
        self.assertEqual(1, result['p t'])
    
    def test_dict_dups(self):
        d = {'k1': 40.4,
             'k2': 56,
             'k3': 'thing',
             'k4': 56,
             'k5': 'str',
             'k6': 'thing',
             'k7': 'thing',
             'k8': 'thing'}
        
        result = count_unique(d.values())
        self.assertEqual(4, len(result))
        self.assertEqual(1, result[40.4])
        self.assertEqual(1, result['str'])
        self.assertEqual(2, result[56])
        self.assertEqual(4, result['thing'])

    def test_dict_dups_itervalues(self):
        d = {'k1': 40.4,
             'k2': 56,
             'k3': 'thing',
             'k4': 56,
             'k5': 'str',
             'k6': 'thing',
             'k7': 'thing',
             'k8': 'thing'}
        
        result = count_unique(d.itervalues())
        self.assertEqual(4, len(result))
        self.assertEqual(1, result[40.4])
        self.assertEqual(1, result['str'])
        self.assertEqual(2, result[56])
        self.assertEqual(4, result['thing'])
    
def test_accumulate_unsorted():
    data = {5: 2, 3: 15}
    result = accumulate(data)
    assert result[3] == 15
    assert result[5] == 17

def test_accumulate_float():
    data = {5: 0.1, 3: 0.15}
    result = accumulate(data)
    assert result[3] == 0.15
    assert result[5] == 0.25

def test_accumulate_empty():
    result = accumulate({})
    assert len(result) ==0
    
def test_dict_apply_data():
    result = dict_apply({'a': 0, 'b': 3, 'c': 4},  lambda v: v*2)
    assert result == {'a': 0, 'b': 6, 'c': 8}
    
def test_dict_apply_empty():
    result = dict_apply({},  lambda v: v*2)
    assert result == {}
    
def test_extract_attr_single():
    data = [object()]    
    result = extract_attr(data, '__class__')
    assert 1, len(result)
    assert object == result[0]

def test_extract_attr_multiple():
    data = [object(), list(), dict()]    
    result = extract_attr(data, '__class__')
    assert len(result) == 3
    assert result[0] == object
    assert result[1] == list
    assert result[2] == dict

def test_extract_attr_iterable():
    data = iter([object(), list(), dict()])    
    result = extract_attr(data, '__class__')
    assert len(result) == 3
    assert result[0] == object
    assert result[1] == list
    assert result[2] == dict

def test_extract_attr_generator():
    data = (x for x in [object(), list(), dict()])    
    result = extract_attr(data, '__class__')
    assert len(result) == 3
    assert result[0] == object
    assert result[1] == list
    assert result[2] == dict