class UnitConversionContextAdapterTestCase(unittest.TestCase):
    """ Other tests for UnitConversionContextAdapater
    """

    ############################################################################
    # TestCase interface
    ############################################################################

    def setUp(self):
        self.context = AdaptedDataContext(context=DataContext())

    ############################################################################
    # UnitConversionContextAdapterTestCase interface
    ############################################################################

    def test_getitem_converts_correctly(self):
        """ Does getitem convert units correctly?
        """
        getitem_units = {'depth': feet}
        adapter = UnitConversionAdapter(getitem_units=getitem_units)

        old_log = UnitArray((1, 2, 3), units=meters)

        self.context['depth'] = old_log
        self.context.push_adapter(adapter)

        new_log = self.context['depth']

        # Did the values get converted correctly?
        self.assert_(all(new_log == old_log.as_units(feet)))

        # Are the units assigned correctly?
        self.assert_(new_log.units == feet)

        return

    def test_setitem_converts_correctly(self):
        """ Does setitem convert units correctly?
        """

        old_log = UnitArray((1, 2, 3), units=meters)
        getitem_units = {'depth': feet}
        adapter = UnitConversionAdapter(getitem_units=getitem_units)

        self.context.push_adapter(adapter)

        # pass the log into the conversion adapter as meters
        self.context['depth'] = old_log

        # Now retreive the log from the underlying context.
        new_log = self.context['depth']

        # Did the values get converted correctly?
        self.assert_(all(new_log == old_log.as_units(feet)))

        # Are the units assigned correctly?
        self.assert_(new_log.units == feet)

        return
class UnitConversionContextAdapterTestCase(unittest.TestCase):
    """ Other tests for UnitConversionContextAdapater
    """

    ############################################################################
    # TestCase interface
    ############################################################################

    def setUp(self):
        self.context = AdaptedDataContext(context=DataContext())

    ############################################################################
    # UnitConversionContextAdapterTestCase interface
    ############################################################################

    def test_getitem_converts_correctly(self):
        """ Does getitem convert units correctly?
        """
        getitem_units = {'depth':feet}
        adapter = UnitConversionAdapter(getitem_units=getitem_units)

        old_log = UnitArray((1,2,3),units=meters)

        self.context['depth'] = old_log
        self.context.push_adapter(adapter)

        new_log = self.context['depth']

        # Did the values get converted correctly?
        self.assert_(all(new_log==old_log.as_units(feet)))

        # Are the units assigned correctly?
        self.assert_(new_log.units==feet)

        return

    def test_setitem_converts_correctly(self):
        """ Does setitem convert units correctly?
        """

        old_log = UnitArray((1,2,3),units=meters)
        getitem_units = {'depth':feet}
        adapter = UnitConversionAdapter(getitem_units=getitem_units)

        self.context.push_adapter(adapter)

        # pass the log into the conversion adapter as meters
        self.context['depth'] = old_log

        # Now retreive the log from the underlying context.
        new_log = self.context['depth']

        # Did the values get converted correctly?
        self.assert_(all(new_log==old_log.as_units(feet)))

        # Are the units assigned correctly?
        self.assert_(new_log.units==feet)

        return
 def context_factory(self):
     """ Return the type of context we are testing.
     """
     data_context = DataContext()
     context = AdaptedDataContext(context=data_context)
     name = self.key_name()
     getitem_units = {name: meters / second}
     adapter = UnitConversionAdapter(getitem_units=getitem_units)
     context.push_adapter(adapter)
     return context
 def context_factory(self):
     """ Return the type of context we are testing.
     """
     data_context=DataContext()
     context = AdaptedDataContext(context=data_context)
     name = self.key_name()
     getitem_units = {name:meters/second}
     adapter = UnitConversionAdapter(getitem_units=getitem_units)
     context.push_adapter(adapter)
     return context
Exemple #5
0
    def setUp(self):
        unittest.TestCase.setUp(self)

        # Create the contexts
        self.context = AdaptedDataContext(subcontext=DataContext())
        self.raw_context = self.context.subcontext

        # Add data (before creating the adapter)
        self.context.update(fun=1, bar=2, baz=3, not_mapped=4)

        # Add an adapter
        self.adapter = NameAdapter(map={"foo": "fun", "bar": "baz"})
        self.context.push_adapter(self.adapter)
class NameAdapterTestCase(unittest.TestCase):

    ###########################################################################
    # TestCase interface
    ###########################################################################

    def setUp(self):
        unittest.TestCase.setUp(self)

        # Create the contexts
        self.context = AdaptedDataContext(subcontext=DataContext())
        self.raw_context = self.context.subcontext

        # Add data (before creating the adapter)
        self.context.update(fun=1, bar=2, baz=3, not_mapped=4)

        # Add an adapter
        self.adapter = NameAdapter(map={"foo": "fun", "bar": "baz"})
        self.context.push_adapter(self.adapter)

    def tearDown(self):
        unittest.TestCase.tearDown(self)

    ###########################################################################
    # MaskingAdapterTestCase interface
    ###########################################################################

    def test_adapt_name(self):
        """ Does adapter map values correctly?
        """

        name = self.adapter.adapt_name(self.raw_context, "foo")
        self.assertEqual(name, "fun")
        self.assertEqual(self.raw_context[name], 1)

    def test_adapt_name_existing_value(self):
        """ Does adapter not map values that are in the context?
        """

        name = self.adapter.adapt_name(self.raw_context, "bar")
        self.assertEqual(name, "bar")
        self.assertEqual(self.raw_context[name], 2)

    def test_adapt_name_not_in_map(self):
        """ Does adapter work for values not in the map?
        """

        name = self.adapter.adapt_name(self.raw_context, "not_mapped")
        self.assertEqual(name, "not_mapped")
        self.assertEqual(self.raw_context[name], 4)
Exemple #7
0
class NameAdapterTestCase(unittest.TestCase):

    ###########################################################################
    # TestCase interface
    ###########################################################################

    def setUp(self):
        unittest.TestCase.setUp(self)

        # Create the contexts
        self.context = AdaptedDataContext(subcontext=DataContext())
        self.raw_context = self.context.subcontext

        # Add data (before creating the adapter)
        self.context.update(fun=1, bar=2, baz=3, not_mapped=4)

        # Add an adapter
        self.adapter = NameAdapter(map={"foo": "fun", "bar": "baz"})
        self.context.push_adapter(self.adapter)

    def tearDown(self):
        unittest.TestCase.tearDown(self)

    ###########################################################################
    # MaskingAdapterTestCase interface
    ###########################################################################

    def test_adapt_name(self):
        """ Does adapter map values correctly?
        """

        name = self.adapter.adapt_name(self.raw_context, 'foo')
        self.assertEqual(name, 'fun')
        self.assertEqual(self.raw_context[name], 1)

    def test_adapt_name_existing_value(self):
        """ Does adapter not map values that are in the context?
        """

        name = self.adapter.adapt_name(self.raw_context, 'bar')
        self.assertEqual(name, 'bar')
        self.assertEqual(self.raw_context[name], 2)

    def test_adapt_name_not_in_map(self):
        """ Does adapter work for values not in the map?
        """

        name = self.adapter.adapt_name(self.raw_context, 'not_mapped')
        self.assertEqual(name, 'not_mapped')
        self.assertEqual(self.raw_context[name], 4)
    def setUp(self):

        unittest.TestCase.setUp(self)

        # Set up data for the contexts
        depth = linspace(0.0, 100.0, 11)
        lith = array(['sand'] * len(depth), dtype=object)

        # Create the contexts
        self.context = AdaptedDataContext(subcontext=DataContext())
        self.raw_context = self.context.subcontext

        # Add data (before creating the adapter)
        self.context.update(depth=depth, lith=lith)

        # Add an adapter
        self.mask = (20.0 <= depth) & (depth <= 50.0)
        self.adapter = MaskingAdapter(mask=self.mask)
        self.context.push_adapter(self.adapter)
    def setUp(self):
        unittest.TestCase.setUp(self)

        # Create the contexts
        self.context = AdaptedDataContext(subcontext=DataContext())
        self.raw_context = self.context.subcontext

        # Add data (before creating the adapter)
        self.context.update(fun=1, bar=2, baz=3, not_mapped=4)

        # Add an adapter
        self.adapter = NameAdapter(map={"foo": "fun", "bar": "baz"})
        self.context.push_adapter(self.adapter)
    def setUp(self):

        unittest.TestCase.setUp(self)

        # Set up data for the contexts
        depth = linspace(0.0,100.0, 11)
        lith = array(['sand']*len(depth), dtype=object)

        # Create the contexts
        self.context = AdaptedDataContext(subcontext=DataContext())
        self.raw_context = self.context.subcontext

        # Add data (before creating the adapter)
        self.context.update(depth=depth, lith=lith)

        # Add an adapter
        self.mask = (20.0<=depth) & (depth<=50.0)
        self.adapter = MaskingAdapter(mask=self.mask)
        self.context.push_adapter(self.adapter)
Exemple #11
0
def test_name_adapter_get_set():

    obj1 = object()
    obj2 = object()
    obj3 = object()

    subcx = DataContext(subcontext=dict(a1=obj1, a2=obj2))

    name_map1 = {
        'b1':'a1',
        'b2':'a2',
        'b3':'a3'  # nonexistent
       }
    name_map2 = {
        'c1':'b1',
        'c2':'b2',
        'c3':'b3', # eventually nonexistent
       }

    name_adapter1 = NameAdapter(map=name_map1)
    name_adapter2 = NameAdapter(map=name_map2)
    # unit adapter won't do anything, just ensure doesn't block flow or choke.
    unit_adapter = UnitConversionAdapter()
    context = AdaptedDataContext(subcontext=subcx)

    # Note that the adapters are pushed with those closest to the context first.
    # b->a, then c->b. (FIFO, not LIFO)
    context.push_adapter(name_adapter1)
    context.push_adapter(unit_adapter)
    context.push_adapter(name_adapter2)

    # getitem
    assert_equal( context['a1'], obj1)
    assert_equal( context['b1'], obj1)
    assert_equal( context['c1'], obj1)

    assert_equal( context['a2'], obj2)
    assert_equal( context['b2'], obj2)
    assert_equal( context['c2'], obj2)

    # setitem
    context['c2'] = obj3
    assert_equal( context['c2'], obj3)
    assert_equal( context['b2'], obj3)
    assert_equal( context['a2'], obj3)

    assert_equal( context['a1'], obj1)
    assert_equal( context['b1'], obj1)
    assert_equal( context['c1'], obj1)

    # IterableAdaptedDataContext
    context2 = IterableAdaptedDataContext(subcontext=subcx)
    context2.push_adapter(name_adapter1)
    context2.push_adapter(unit_adapter)
    context2.push_adapter(name_adapter2)

    assert_equal(set(context2.keys()), set('a1 a2 b1 b2 c1 c2'.split()))
    assert_equal(set([k for k in context2]), set(context2.keys()))
    for key in context2:
        assert_(key in context2)
    assert_('b3' not in context2)
    assert_('c3' not in context2)
def test_stacked_hybrid_adapters():
    """ Test the stacked application of multiple adapters which adapt both
    names and values. This tests a bug in AdapterManagerMixin which applied
    name adapters in backwards order. Without value transformation as well as
    name mapping, it's not as obvious what the correct order of name
    adaptation should be. But the essence is that the first adapter applied is
    the one closest to the underlying context.
    """
    # Converts freezing & boiling temps of water between F, C, and approx K,
    # to test adapters whose composition is not commutative.
    FF = 32.
    BF = 212.
    FC = 0.
    BC = 100.
    FK = 273.
    BK = 373.

    # These adapters are named with the units closer to context on the left,
    # and units closer to the user on the right. Context is F, user is C or K.
    # Note that any changes made by adapt_getitem, to name, are only for the
    # use of other adapters -- such name changes do not otherwise affect the
    # value ultimately returned by the original top level __getitem__.

    class AdaptCtoK(NameAdapter):
        map = Dict({'frK': 'frC', 'boK': 'boC'})

        def adapt_getitem(self, context, name, value):
            if name[-1] == 'C':
                return name[:-1] + 'K', value + FK
            else:
                assert_(False, 'AdaptCtoK called unexpectedly on %s' % name)

    class AdaptFtoC(NameAdapter):
        map = Dict({'frC': 'frF', 'boC': 'boF'})

        def adapt_getitem(self, context, name, value):
            if name[-1] == 'F':
                return name[:-1] + 'C', (value - FF) * (BC - FC) / (BF - FF)
            else:
                assert_(False, 'AdaptFtoC called unexpectedly on %s' % name)

    farenheit_context = DataContext(subcontext=dict(frF=FF, boF=BF))

    # no adapter
    context = AdaptedDataContext(subcontext=farenheit_context)
    assert_equal(context['frF'], FF)
    assert_equal(context['boF'], BF)

    # one adapter: to Celsius
    # Note that the data are adapted to Celsius even if we retrieve them
    # with a Farenheit name, because getitem adapters are not told the
    # original retrieval key.
    context.push_adapter(AdaptFtoC())
    assert_equal(context['frF'], FC)
    assert_equal(context['frC'], FC)
    assert_equal(context['boF'], BC)
    assert_equal(context['boC'], BC)

    # two adapters: to Kelvin (no matter which name is used to retrieve).
    # unit adapter won't do anything, just ensure doesn't block flow or choke.
    context.push_adapter(UnitConversionAdapter())
    context.push_adapter(AdaptCtoK())
    assert_equal(context['frF'], FK)
    assert_equal(context['frC'], FK)
    assert_equal(context['frK'], FK)
    assert_equal(context['boF'], BK)
    assert_equal(context['boC'], BK)
    assert_equal(context['boK'], BK)
 def setUp(self):
     self.context = AdaptedDataContext(context=DataContext())
 def setUp(self):
     self.context = AdaptedDataContext(context=DataContext())
def test_name_adapter_get_set():

    obj1 = object()
    obj2 = object()
    obj3 = object()

    subcx = DataContext(subcontext=dict(a1=obj1, a2=obj2))

    name_map1 = {
        'b1': 'a1',
        'b2': 'a2',
        'b3': 'a3'  # nonexistent
    }
    name_map2 = {
        'c1': 'b1',
        'c2': 'b2',
        'c3': 'b3',  # eventually nonexistent
    }

    name_adapter1 = NameAdapter(map=name_map1)
    name_adapter2 = NameAdapter(map=name_map2)
    # unit adapter won't do anything, just ensure doesn't block flow or choke.
    unit_adapter = UnitConversionAdapter()
    context = AdaptedDataContext(subcontext=subcx)

    # Note that the adapters are pushed with those closest to the context first.
    # b->a, then c->b. (FIFO, not LIFO)
    context.push_adapter(name_adapter1)
    context.push_adapter(unit_adapter)
    context.push_adapter(name_adapter2)

    # getitem
    assert_equal(context['a1'], obj1)
    assert_equal(context['b1'], obj1)
    assert_equal(context['c1'], obj1)

    assert_equal(context['a2'], obj2)
    assert_equal(context['b2'], obj2)
    assert_equal(context['c2'], obj2)

    # setitem
    context['c2'] = obj3
    assert_equal(context['c2'], obj3)
    assert_equal(context['b2'], obj3)
    assert_equal(context['a2'], obj3)

    assert_equal(context['a1'], obj1)
    assert_equal(context['b1'], obj1)
    assert_equal(context['c1'], obj1)

    # IterableAdaptedDataContext
    context2 = IterableAdaptedDataContext(subcontext=subcx)
    context2.push_adapter(name_adapter1)
    context2.push_adapter(unit_adapter)
    context2.push_adapter(name_adapter2)

    assert_equal(set(context2.keys()), set('a1 a2 b1 b2 c1 c2'.split()))
    assert_equal(set([k for k in context2]), set(context2.keys()))
    for key in context2:
        assert_(key in context2)
    assert_('b3' not in context2)
    assert_('c3' not in context2)
class MaskingAdapterTestCase(unittest.TestCase):

    ###########################################################################
    # TestCase interface
    ###########################################################################

    def setUp(self):

        unittest.TestCase.setUp(self)

        # Set up data for the contexts
        depth = linspace(0.0, 100.0, 11)
        lith = array(['sand'] * len(depth), dtype=object)

        # Create the contexts
        self.context = AdaptedDataContext(subcontext=DataContext())
        self.raw_context = self.context.subcontext

        # Add data (before creating the adapter)
        self.context.update(depth=depth, lith=lith)

        # Add an adapter
        self.mask = (20.0 <= depth) & (depth <= 50.0)
        self.adapter = MaskingAdapter(mask=self.mask)
        self.context.push_adapter(self.adapter)

    def tearDown(self):
        unittest.TestCase.tearDown(self)

    ###########################################################################
    # MaskingAdapterTestCase interface
    ###########################################################################

    def test_getitem(self):
        """ Does adapter mask values correctly?
        """
        name, value = self.adapter.adapt_getitem(self.raw_context, 'depth',
                                                 self.raw_context['depth'])
        self.assertEqual(len(value), 4)
        self.assertTrue(all(value == (20.0, 30.0, 40.0, 50.0)))

    def test_setitem_existing_value(self):
        """ Does setitem on existing data only change the masked values?
        """
        new_values = (30.0, 40.0, 50.0, 60.0)

        desired = self.raw_context['depth'].copy()
        desired[self.mask] = new_values

        name, value = self.adapter.adapt_setitem(self.raw_context, 'depth',
                                                 new_values)
        self.assertEqual(len(value), 11)
        self.assertTrue(all(value == desired))

    def test_setitem_non_existing_value(self):
        """ Does setitem on non-existing data expand to depth's shape?
        """
        new_values = (30.0, 40.0, 50.0, 60.0)

        desired = self.raw_context['depth'].copy()
        desired[self.mask] = new_values
        desired[~self.mask] = nan

        name, value = self.adapter.adapt_setitem(self.raw_context, 'foo',
                                                 new_values)

        self.assertEqual(len(value), len(desired))
        self.assertTrue(
            all((value == desired) | (isnan(value) == isnan(desired))))

    def test_context_getitem(self):
        """ Are the returned values from context masked correctly?
        """
        depth = self.context['depth']
        self.assertEqual(len(depth), 4)
        self.assertTrue(all(depth == (20.0, 30.0, 40.0, 50.0)))

    def test_context_setitem_existing(self):
        """ Are the returned values from context masked correctly?
        """
        new_values = (30.0, 40.0, 50.0, 60.0)
        self.context['depth'] = new_values

        # Grab the values from the underlying context, skipping the mask.
        depth = self.raw_context['depth']

        desired = self.raw_context['depth'].copy()
        desired[self.mask] = new_values
        self.assertTrue(all(depth == desired))
class MaskingAdapterTestCase(unittest.TestCase):

    ###########################################################################
    # TestCase interface
    ###########################################################################

    def setUp(self):

        unittest.TestCase.setUp(self)

        # Set up data for the contexts
        depth = linspace(0.0,100.0, 11)
        lith = array(['sand']*len(depth), dtype=object)

        # Create the contexts
        self.context = AdaptedDataContext(subcontext=DataContext())
        self.raw_context = self.context.subcontext

        # Add data (before creating the adapter)
        self.context.update(depth=depth, lith=lith)

        # Add an adapter
        self.mask = (20.0<=depth) & (depth<=50.0)
        self.adapter = MaskingAdapter(mask=self.mask)
        self.context.push_adapter(self.adapter)

    def tearDown(self):
        unittest.TestCase.tearDown(self)

    ###########################################################################
    # MaskingAdapterTestCase interface
    ###########################################################################

    def test_getitem(self):
        """ Does adapter mask values correctly?
        """
        name, value = self.adapter.adapt_getitem(self.raw_context, 'depth',
                                                 self.raw_context['depth'])
        self.assertEqual(len(value), 4)
        self.assertTrue(all(value==(20.0, 30.0, 40.0, 50.0)))

    def test_setitem_existing_value(self):
        """ Does setitem on existing data only change the masked values?
        """
        new_values = (30.0, 40.0, 50.0, 60.0)

        desired = self.raw_context['depth'].copy()
        desired[self.mask] = new_values

        name, value = self.adapter.adapt_setitem(self.raw_context, 'depth',
                                                 new_values)
        self.assertEqual(len(value), 11)
        self.assertTrue(all(value==desired))

    def test_setitem_non_existing_value(self):
        """ Does setitem on non-existing data expand to depth's shape?
        """
        new_values = (30.0, 40.0, 50.0, 60.0)

        desired = self.raw_context['depth'].copy()
        desired[self.mask] = new_values
        desired[~self.mask] = nan

        name, value = self.adapter.adapt_setitem(self.raw_context, 'foo',
                                                 new_values)

        self.assertEqual(len(value), len(desired))
        self.assertTrue(all((value==desired) | (isnan(value)==isnan(desired))))

    def test_context_getitem(self):
        """ Are the returned values from context masked correctly?
        """
        depth = self.context['depth']
        self.assertEqual(len(depth), 4)
        self.assertTrue(all(depth==(20.0, 30.0, 40.0, 50.0)))

    def test_context_setitem_existing(self):
        """ Are the returned values from context masked correctly?
        """
        new_values = (30.0, 40.0, 50.0, 60.0)
        self.context['depth'] = new_values

        # Grab the values from the underlying context, skipping the mask.
        depth = self.raw_context['depth']

        desired = self.raw_context['depth'].copy()
        desired[self.mask] = new_values
        self.assertTrue(all(depth==desired))
Exemple #18
0
def test_stacked_hybrid_adapters():
    """ Test the stacked application of multiple adapters which adapt both
    names and values. This tests a bug in AdapterManagerMixin which applied
    name adapters in backwards order. Without value transformation as well as
    name mapping, it's not as obvious what the correct order of name
    adaptation should be. But the essence is that the first adapter applied is
    the one closest to the underlying context.
    """
    # Converts freezing & boiling temps of water between F, C, and approx K,
    # to test adapters whose composition is not commutative.
    FF = 32.
    BF = 212.
    FC = 0.
    BC = 100.
    FK = 273.
    BK = 373.

    # These adapters are named with the units closer to context on the left,
    # and units closer to the user on the right. Context is F, user is C or K.
    # Note that any changes made by adapt_getitem, to name, are only for the
    # use of other adapters -- such name changes do not otherwise affect the
    # value ultimately returned by the original top level __getitem__.

    class AdaptCtoK(NameAdapter):
        map = Dict({'frK':'frC', 'boK':'boC'})

        def adapt_getitem(self, context, name, value):
            if name[-1] == 'C':
                return name[:-1]+'K', value+FK
            else:
                assert_(False, 'AdaptCtoK called unexpectedly on %s' % name)


    class AdaptFtoC(NameAdapter):
        map = Dict({'frC':'frF', 'boC':'boF'})

        def adapt_getitem(self, context, name, value):
            if name[-1] == 'F':
                return name[:-1]+'C', (value-FF)*(BC-FC)/(BF-FF)
            else:
                assert_(False, 'AdaptFtoC called unexpectedly on %s' % name)


    farenheit_context = DataContext(subcontext=dict(frF=FF, boF=BF))

    # no adapter
    context = AdaptedDataContext(subcontext=farenheit_context)
    assert_equal(context['frF'], FF)
    assert_equal(context['boF'], BF)

    # one adapter: to Celsius
    # Note that the data are adapted to Celsius even if we retrieve them
    # with a Farenheit name, because getitem adapters are not told the
    # original retrieval key.
    context.push_adapter(AdaptFtoC())
    assert_equal(context['frF'], FC)
    assert_equal(context['frC'], FC)
    assert_equal(context['boF'], BC)
    assert_equal(context['boC'], BC)

    # two adapters: to Kelvin (no matter which name is used to retrieve).
    # unit adapter won't do anything, just ensure doesn't block flow or choke.
    context.push_adapter(UnitConversionAdapter())
    context.push_adapter(AdaptCtoK())
    assert_equal(context['frF'], FK)
    assert_equal(context['frC'], FK)
    assert_equal(context['frK'], FK)
    assert_equal(context['boF'], BK)
    assert_equal(context['boC'], BK)
    assert_equal(context['boK'], BK)