コード例 #1
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)
コード例 #2
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)
コード例 #3
0
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))