bubble-economy / test_utils.py

import unittest
from bubble_economy import utils 
import numpy
from numpy.testing import TestCase

class TestFract(TestCase):
    wantedValues = ( (-2., 0.),
                     (-1.75, 0.25),
                     (-1.25, 0.75),
                     (-1., 0.),
                     (-0.75, 0.25),
                     (-0.25, 0.75),
                     (-0., 0.),
                     (0., 0.),
                     (0.25, 0.25),
                     (0.75, 0.75),
                     (1., 0.),
                     (1.25, 0.25),
                     (1.75, 0.75),
                    )

    def testScalars(self):                          
        """fract should fit the test data in breaking floats into
        fractional part"""
        for flt, wanted in self.wantedValues:
            got_out_fract = utils.fract(flt)
            self.assertEqual(got_out_fract, wanted)                  

    def testArrays(self):                          
        """fract should work equivalently for arrays"""
        in_flt = numpy.array([f[0] for f in self.wantedValues])
        wanted_out_fract = numpy.array([f[1] for f in self.wantedValues])
        got_out_fract  = utils.fract(in_flt)
        for (wanted, got) in zip(wanted_out_fract, got_out_fract):
            self.assertEquals(wanted, got)

class TestDictCollation(TestCase):
    def testScalars(self):                          
        """"""
        for destructive in (True, False):
            Dicts = [{'A': 1, 'B': 3, 'C':{'A':18}},
                     {'A': 1, 'B': 1, 'C':{'A':19}},
                     {'A': 5, 'B': 2, 'C':{'A':-2}},
                     {'A': 7, 'B': 7, 'C':{'A':17}}]
            collated = utils.addict(Dicts, destructive=destructive)
            self.assertEqual(collated['A'], [1,1,5,7])
            self.assertEqual(collated['B'], [3,1,2,7])
            self.assertEqual(collated['C']['A'], [18,19,-2,17])
    def testLists(self):                          
        """"""
        for destructive in (True, False):
            Dicts = [{'A': [1,2,], 'B': [3,4], 'C':{'A':[18,19]}},
                     {'A': [1,2], 'B': [1,2], 'C':{'A':[19,20]}},
                     {'A': [5,6], 'B': [2,3], 'C':{'A':[-2,-1]}},
                     {'A': [7,8], 'B': [7,8], 'C':{'A':[17,18]}}]
            collated = utils.addict(Dicts, destructive=destructive)
            self.assertEqual(collated['A'], [1,2,1,2,5,6,7,8])
            self.assertEqual(collated['B'], [3,4,1,2,2,3,7,8])
            self.assertEqual(collated['C']['A'], [18,19,19,20,-2,-1,17,18])
    def testMixed(self):                          
        """"""
        for destructive in (True, False):
            Dicts = [{'A': [1,2], 'B': 3, 'C':{'A':[18,19]}},
                     {'A': 1, 'B': [1,2], 'C':{'A':[19,20]}},
                     {'A': [5,6], 'B': 2, 'C':{'A':[-2,-1]}},
                     {'A': 7, 'B': [7,8], 'C':{'A':[17,18]}}]
            collated = utils.addict(Dicts, destructive=destructive)
            self.assertEqual(collated['A'], [1,2,1,5,6,7])
            self.assertEqual(collated['B'], [3,1,2,2,7,8])
            self.assertEqual(collated['C']['A'], [18,19,19,20,-2,-1,17,18])
    def testMyStats(self):
        """actual data, sorta, from my code"""
        for destructive in (True, False):
            Dicts = [
                 {'mi': {'mean_mi': 1.5,
                         'tree_mi': 2.5,
                         'tree_mi_weighted': 3.5},
                  'link_stats': {'mean_degree': 4.5}},
                 {'mi': {'mean_mi': 5.5,
                         'tree_mi': 6.5,
                         'tree_mi_weighted':7.5},
                  'link_stats': {'mean_degree': 8.5}},
                 {'mi': {'mean_mi': 9.5,
                         'tree_mi': 10.5,
                         'tree_mi_weighted': 11.5},
                  'link_stats': {'mean_degree': 12.5}},
                 {'mi': {'mean_mi': 13.5,
                         'tree_mi': 14.5,
                         'tree_mi_weighted': 15.5},
                  'link_stats': {'mean_degree': 16.5}}]
            collated = utils.addict(Dicts, destructive=destructive)
            self.assertEqual(
                collated['mi']['mean_mi'], 
                [1.5, 5.5, 9.5, 13.5]
            )
            self.assertEqual(
                collated['mi']['tree_mi'], 
                [2.5, 6.5, 10.5, 14.5]
            )
            self.assertEqual(
                collated['mi'][
                  'tree_mi_weighted'], 
                [3.5, 7.5, 11.5, 15.5]
            )
            self.assertEqual(
                collated['link_stats']['mean_degree'],
                [4.5, 8.5, 12.5, 16.5]
            )
    # def testListIdempotency(self):
    #     """actual data, sorta, from my code"""
    #     for destructive in (True, False):
    #         Dicts = [1,2,3,5]
    #         collated = utils.addict(Dicts, destructive=destructive)
    #         self.assertArrayEqual(
    #             collated, 
    #             [1,2,3,5]
    #         )

class TestRng(TestCase):
    def test_rng(self):
        """Rng class produces a consistent list of random numbers from a seed"""
        import random
        rng1 = utils.Rng(113)
        rands1 = [rng1.random() for i in xrange(10)]
        _ = random.random()
        _ = random.random()
        rng2 = utils.Rng(113)
        rands2 = [rng2.random() for i in xrange(10)]
        self.assertSequenceEqual(rands1, rands2)
        rng3, rng4 = utils.Rng(113), utils.Rng(113)
        rands34 = [(rng3.random(), rng4.random()) for i in xrange(10)]
        self.assertSequenceEqual(rands1, rands2)
        rands3, rands4 = zip(*rands34) #unzip
        self.assertSequenceEqual(rands1, rands3)
        self.assertSequenceEqual(rands1, rands4)
        rng5 = utils.Rng(113)
        rands5a = [rng5.random() for i in xrange(5)]
        _ = random.random()
        _ = random.random()
        _ = random.random()
        _ = random.random()
        _ = rng1.random()
        _ = rng1.random()
        _ = rng1.random()
        _ = rng2.random()
        _ = rng2.random()
        _ = rng3.random()
        _ = rng4.random()
        rands5b = [rng5.random() for i in xrange(5)]
        self.assertSequenceEqual(rands1, rands5a+rands5b)

class TestRealisedPathDict(TestCase):
    nested_dict = {
      'first_a' : {
        'second_a' : {
          'third_a' : 3
        },
        'second_b' : 2
      },
      'first_b' :1
    }
    path_dict = {
      'first_a.second_a.third_a' : 3,
      'first_a.second_b' : 2,
      'first_b' : 1,
    }

    def testRoundTrip(self):
        """round trip is one easy thing to test..."""
        self.assertEqual(
          self.path_dict,
          utils.nested_to_realised_path_dict(
            utils.realised_path_to_nested_dict(
              self.path_dict
            )
          )
        )
        self.assertEqual(
          self.nested_dict,
          utils.realised_path_to_nested_dict(
            utils.nested_to_realised_path_dict(
              self.nested_dict
            )
          )
        )
    def testOneWay(self):
        """...but let's make sure something happens in the middle"""
        self.assertEqual(
          self.path_dict,
          utils.nested_to_realised_path_dict(
            self.nested_dict
          )
        )
        self.assertEqual(
          self.nested_dict,
          utils.realised_path_to_nested_dict(
            self.path_dict
          )
        )
        

class TestDeepDict(TestCase):
    """Probably needs graceful fail tests for e.g. keys with dots in and
    non-string keys"""
    def testSetCreates(self):
        d = {}
        utils.deep_dict_set(d, ['a', 'b', 'c'], 5)
        self.assertEqual(
           {'a':{'b':{'c':5}}},
           d)
        
    def testSetUpdates(self):
        d = {'a':{'b':{'c':4}}}
        utils.deep_dict_set(d, ['a', 'b', 'c'], 5)
        self.assertEqual(
           {'a':{'b':{'c':5}}},
           d
        )
        
    def testSetDoesNotClobber(self):
        d = {'a':{'b':{'d':4}}}
        utils.deep_dict_set(d, ['a', 'b', 'c'], 5)
        self.assertEqual(
           {'a':{'b':{'c':5, 'd':4}}},
           d)

if __name__ == '__main__':
    unittest.main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.