Exemple #1
0
    def setUp(self):

        builder = CollectionBuilder('TestOutput')
        self.TestOutputCollection = builder.build(
            TestASerializer)  #NOTE integrated with builder here

        self.test_a = self.TestOutputCollection()
        self.test_a.load_data([{'a': 1}, {'a': 2}, {'a': 3}])
Exemple #2
0
    def test_build_dynamically_adds_name_via_build_method(self):
        class TestAutoNameSerializer(BaseSerializer):
            x = fields.Integer()
            y = fields.Integer()
            z = fields.Str()

        class TestOtherAutoNameSchema(BaseSerializer):
            x = fields.Integer()
            y = fields.Integer()
            z = fields.Str()

        builder = CollectionBuilder()
        AliasOne = builder.build(TestAutoNameSerializer)
        self.assertEqual(AliasOne.__name__, 'TestAutoNameCollection')

        AliasTwo = builder.build(TestOtherAutoNameSchema)
        self.assertEqual(AliasTwo.__name__, 'TestOtherAutoNameCollection')
Exemple #3
0
    def setUpClass(cls):
        builder = CollectionBuilder('TestPluggableA')
        cls.TestPluggableACollection = builder.build(TestASerializer)

        builder.name = 'TestPluggableB'
        cls.TestPluggableBCollection = builder.build(TestBSerializer)

        builder.name = 'TestPluggableC'
        cls.TestPluggableCCollection = builder.build(TestCSerializer)

        class PluggableAToBAdapter(PluggableAdapter):
            from_collection_class = cls.TestPluggableACollection
            target_collection_class = cls.TestPluggableBCollection
            calc = adapt_a_to_b

        class PluggableBToCAdapter(PluggableAdapter):
            from_collection_class = cls.TestPluggableBCollection
            target_collection_class = cls.TestPluggableCCollection
            calc = adapt_b_to_c

        cls.PluggableAToBAdapter = PluggableAToBAdapter
        register_adapter(cls.PluggableAToBAdapter)

        cls.PluggableBToCAdapter = PluggableBToCAdapter
        register_adapter(cls.PluggableBToCAdapter)
Exemple #4
0
    def setUpClass(cls):
        builder = CollectionBuilder('TestA')
        cls.TestACollection = builder.build(TestASerializer)

        builder.name = 'TestB'
        cls.TestBCollection = builder.build(TestBSerializer)

        class SimpleAToBAdapter(AbstractAdapter):
            from_collection_class = cls.TestACollection
            target_collection_class = cls.TestBCollection

            def adapt(self, collection, **context):
                df = collection.to_dataframe()
                df['b'] = 42  # add a column with some dataframe here

                return self.render_return(df,
                                          context_var='hep',
                                          other_context_var='tup')

        cls.SimpleAToBAdapter = SimpleAToBAdapter
        register_adapter(cls.SimpleAToBAdapter)
Exemple #5
0
    def setUpClass(cls):
        # These test the creation of the adapter chain

        builder = CollectionBuilder('TestAA')
        cls.TestAACollection = builder.build(TestASerializer)

        builder.name = 'TestBB'
        cls.TestBBCollection = builder.build(TestBSerializer)

        builder.name = 'TestCC'
        cls.TestCCCollection = builder.build(TestCSerializer)

        class SimpleAToBAdapter(AbstractAdapter):
            from_collection_class = cls.TestAACollection
            target_collection_class = cls.TestBBCollection

            def adapt(self, collection, **context):
                df = collection.to_dataframe()
                df['b'] = 42  # add a column b with some dataframe here

                return self.render_return(df,
                                          context_var='hep',
                                          other_context_var='tup')

        class SimpleBToCAdapter(AbstractAdapter):
            from_collection_class = cls.TestBBCollection
            target_collection_class = cls.TestCCCollection

            def adapt(self, collection, **context):
                df = collection.to_dataframe()
                df['c'] = 43  # add a column c with some dataframe here

                return self.render_return(df,
                                          something_else='mups',
                                          some_thing='zups')

        cls.SimpleAToBAdapter = SimpleAToBAdapter
        register_adapter(cls.SimpleAToBAdapter)

        cls.SimpleBToCAdapter = SimpleBToCAdapter
        register_adapter(cls.SimpleBToCAdapter)
Exemple #6
0
 def setUp(self):
     self.coll_builder = CollectionBuilder('Test')
Exemple #7
0
class TestCollectionBuilder(unittest.TestCase):
    def setUp(self):
        self.coll_builder = CollectionBuilder('Test')

    def tearDown(self):
        self.coll_builder.name = 'Test'

    def test_make_dynamic_class(self):
        class A(object):
            pass

        ASubClass = self.coll_builder._make_dynamic_class('ASubClass', (
            'arg1',
            'arg2',
        ),
                                                          base_class=A)
        self.assertEqual(
            str(ASubClass.__bases__[0]),
            "<class 'tests.test_builder.TestCollectionBuilder.test_make_dynamic_class.<locals>.A'>"
        )

        a = ASubClass(arg1='a', arg2='b')
        self.assertEqual(a.__dict__, {'arg2': 'b', 'arg1': 'a'})

        with self.assertRaises(TypeError):
            b = ASubClass(arg3='a')

    def test_make_collection_class(self):

        TestCollection = self.coll_builder._make_collection_class(
            'Test', TestSerializer, TestInternal)

        t = TestCollection()
        self.assertIsInstance(t, BaseCollection)
        self.assertIn('internal_class', TestCollection.__dict__)
        self.assertIn('serializer_class', TestCollection.__dict__)
        self.assertIsInstance(t.serializer, TestSerializer)
        self.assertIsInstance(t.internal_class(1, 2, 3), TestInternal)

    def test_get_declared_fields(self):

        declared = self.coll_builder._get_declared_fields(TestSerializer)
        self.assertEqual(set(['x', 'y', 'z']), set(declared))

    def test_parse_names(self):
        coll_name, internal_name = self.coll_builder._parse_names('Thing')

        self.assertEqual(coll_name, 'ThingCollection')
        self.assertEqual(internal_name, 'ThingInternal')

    def test_build(self):
        class TestAgainSerializer(BaseSerializer):

            x = fields.Integer()
            y = fields.Integer()
            z = fields.Str()

        self.coll_builder.name = 'TestAgain'
        TestCollection = self.coll_builder.build(TestAgainSerializer)

        self.assertIsInstance(TestCollection(), BaseCollection)

    def test_build_internal_only_builds_and_registers_internal(self):
        class TestAgainAgainSerializer(BaseSerializer):

            x = fields.Integer()
            y = fields.Integer()
            z = fields.Str()

        self.coll_builder.name = 'TestAgainAgain'
        InternalOutput = self.coll_builder.build(TestAgainAgainSerializer,
                                                 internal_only=True)

        self.assertIsInstance(InternalOutput(), InternalObject)

    def test_module_name_resolution_using_builder(self):
        # relates to issue 19 -- will always point to

        class TestModuleNameSerializer(BaseSerializer):
            x = fields.Integer()
            y = fields.Integer()
            z = fields.Str()

        self.coll_builder.name = 'TestModuleName'
        TestModuleNameCollection = self.coll_builder.build(
            TestModuleNameSerializer)

        t = TestModuleNameCollection.get_fully_qualified_class_path()
        self.assertEqual(t, 'binx.collection.TestModuleNameCollection')

    def test_build_dynamically_adds_name_via_build_method(self):
        class TestAutoNameSerializer(BaseSerializer):
            x = fields.Integer()
            y = fields.Integer()
            z = fields.Str()

        class TestOtherAutoNameSchema(BaseSerializer):
            x = fields.Integer()
            y = fields.Integer()
            z = fields.Str()

        builder = CollectionBuilder()
        AliasOne = builder.build(TestAutoNameSerializer)
        self.assertEqual(AliasOne.__name__, 'TestAutoNameCollection')

        AliasTwo = builder.build(TestOtherAutoNameSchema)
        self.assertEqual(AliasTwo.__name__, 'TestOtherAutoNameCollection')