예제 #1
0
    def test_environment_variables(self):
        """
        Checks that paths defined in environment variables are added
        
        :return: 
        """
        # -- Check that the Tree Frog is not in the zoo by default
        default_zoo = Zoo()

        self.assertNotIn(
            'Tree Frog',
            default_zoo.factory.identifiers()
        )

        # -- Now add the environment variable
        os.environ['FACTORIES_UNITTEST'] = os.path.join(
            os.path.dirname(factories.examples.zoo.__file__),
            'isolated',
        )

        # -- Instance another zoo - defining our environment variable
        alternate_zoo = Zoo(envvar='FACTORIES_UNITTEST')

        self.assertIn(
            'Tree Frog',
            alternate_zoo.factory.identifiers()
        )
예제 #2
0
    def test_direct_plugin_registration(self):

        # -- Create a zoo factory
        zoo = Zoo()

        # -- Remove all the plugin paths
        zoo.factory.clear()

        # -- Ensure there are no plugins in the zoo
        self.assertEqual(
            len(zoo.factory.plugins()),
            0,
        )

        # -- Now register an animal in the zoo directly
        from factories.examples.zoo.animals import carnivores

        zoo.factory.register(carnivores.ArticFox)

        # -- Test that we have one animal type in the zoo
        self.assertEqual(len(zoo.factory.plugins()), 1)

        # -- Test that we can instance our directly registered animal
        artic_fox = zoo.factory.request('artic fox')()

        self.assertEqual(
            artic_fox.diet(),
            'rabbits!',
        )
예제 #3
0
    def test_registering_from_single_file(self):
        """
        Ensures we can load a plugin from a pyc file

        :return: 
        """
        # -- Define our non_package location
        non_package_location = os.path.join(
            os.path.dirname(__file__),
            'test_plugins',
            'non_package',
        )

        # -- Add the folder to our sys path
        sys.path.append(non_package_location)

        # -- Instance our factory
        try:
            zoo = Zoo()

            zoo.factory.add_path(non_package_location, )

            self.assertIsNotNone(zoo.factory.versions('koala'), )

        finally:

            # -- Make sure we clean up
            if non_package_location in sys.path:
                sys.path.remove(non_package_location)
예제 #4
0
    def test_identifier_as_method(self):
        """
        Checks to ensure that we can correctly call an identifier when
        it is a method rather than a property
        
        :return: 
        """
        zoo = Zoo()

        # -- Add our test animals
        zoo.factory.add_path(
            os.path.join(
                os.path.dirname(__file__),
                'test_plugins',
            ))

        # -- Check that goat is available
        self.assertIn(
            'goat',
            zoo.factory.identifiers(),
        )

        # -- Check that we can access the plugin
        goat = zoo.factory.request('goat')

        # -- Ensure the tiger is valid
        self.assertIsNotNone(goat, )
예제 #5
0
    def test_accessing_versions_for_non_versioned_factory(self):
        """
        Checks to see if we can access the available versions of a plugin
        type.

        :return: 
        """
        zoo = Zoo()

        self.assertEqual(
            zoo.factory.versions('tiger'),
            [],
        )
예제 #6
0
    def test_version_request_on_non_versioned_factory(self):
        """
        When requesting a version on a non-version factory we should
        just get the highest available version
        
        :return: 
        """
        zoo = Zoo()

        # -- Request a tiger of version 2
        tiger = zoo.factory.request('tiger', version=2)

        # -- Ensure the tiger is valid
        self.assertIsNotNone(tiger, )
예제 #7
0
    def test_loading_from_non_package(self):
        """
        Ensures we can load a plugin from a pyc file

        :return: 
        """
        zoo = Zoo()

        zoo.factory.add_path(
            os.path.join(
                os.path.dirname(__file__),
                'test_plugins',
            ))

        self.assertIsNotNone(zoo.factory.versions('koala'), )
예제 #8
0
    def test_direct_load_of_pyc(self):
        """
        Ensures we can load a plugin from a pyc file when registering directly

        :return: 
        """
        zoo = Zoo()

        zoo.factory.add_path(
            os.path.join(
                os.path.dirname(__file__),
                'test_plugins',
            ),
            mechanism=zoo.factory.LOAD_SOURCE,
        )

        self.assertIsNotNone(zoo.factory.versions('koala'), )