Example #1
0
 def test_from_dict(self):
     test_dict = self.config.to_dict(private=True, protected=True)
     # Make sure that the legacy async key works
     test_dict.pop("asynchronous")
     test_dict["async"] = False
     # Make actual call
     type_instance = ConfigurationType.from_dict(test_dict,
                                                 MOCK_CONFIGURATION)
     self.assertEqual(type_instance.test, "public")
     self.assertEqual(type_instance.test2, "protected")
     self.assertEqual(type_instance.test3, "private")
     self.assertEqual(type_instance.test4, "variable")
     self.assertEqual(type_instance.asynchronous, False)
     self.assertEqual(type_instance.global_configuration,
                      "global configuration")
     self.assertEqual(type_instance.namespace_configuration,
                      "namespace configuration")
     self.assertEqual(type_instance._private, self.config._private)
     # Call with malformed dicts
     try:
         ConfigurationType.from_dict({"_namespace": "test"},
                                     MOCK_CONFIGURATION)
         self.fail(
             "from_dict does not fail if _private is missing from configuration."
         )
     except AssertionError:
         pass
     try:
         ConfigurationType.from_dict({"_private": "test"},
                                     MOCK_CONFIGURATION)
         self.fail(
             "from_dict does not fail if _namespace is missing from configuration."
         )
     except AssertionError:
         pass
Example #2
0
class TestRunSerieTask(TestCase):

    fixtures = ["test-shell-resource-mock"]

    def setUp(self):
        super().setUp()
        self.config = ConfigurationType(
            namespace="shell_resource",
            private=["_resource", "_continuation_limit"],
        )
        self.config.update({
            "resource": "resources.ShellResourceMock",
        })
        self.args_list = [["test"], ["success"], ["fail"]]
        self.kwargs_list = [{"context": 5} for _ in range(len(self.args_list))]

    def check_results(self, results, expected_length):
        self.assertEqual(len(results), expected_length)
        for pk in results:
            self.assertIsInstance(pk, int)
            self.assertGreater(pk, 0)

    @patch("datagrowth.resources.shell.tasks.run", wraps=run)
    def test_run_serie(self, run_mock):
        scc, err = run_serie(self.args_list,
                             self.kwargs_list,
                             config=self.config)
        self.check_results(scc, 2)
        self.check_results(err, 1)
        run_mock.assert_has_calls([
            call("test", context=5, config=self.config),
            call("success", context=5, config=self.config),
            call("fail", context=5, config=self.config)
        ])
Example #3
0
 def test_from_dict(self):
     # Call with correct dict
     type_instance = ConfigurationType.from_dict(
         self.config.to_dict(private=True, protected=True),
         MOCK_CONFIGURATION)
     self.assertEqual(type_instance.test, "public")
     self.assertEqual(type_instance.test2, "protected")
     self.assertEqual(type_instance.test3, "private")
     self.assertEqual(type_instance.test4, "variable")
     self.assertEqual(type_instance.global_configuration,
                      "global configuration")
     self.assertEqual(type_instance.namespace_configuration,
                      "namespace configuration")
     self.assertEqual(type_instance._private, self.config._private)
     # Call with malformed dicts
     try:
         ConfigurationType.from_dict({"_namespace": "test"},
                                     MOCK_CONFIGURATION)
         self.fail(
             "from_dict does not fail if _private is missing from configuration."
         )
     except AssertionError:
         pass
     try:
         ConfigurationType.from_dict({"_private": "test"},
                                     MOCK_CONFIGURATION)
         self.fail(
             "from_dict does not fail if _namespace is missing from configuration."
         )
     except AssertionError:
         pass
Example #4
0
 def test_init(self):
     # Implicit init without defaults
     # Notice that apps can manipulate the DATAGROWTH_DEFAULT_CONFIGURATION upon app.ready
     # Therefor defaults are not loaded until first access
     instance = ConfigurationType()
     self.assertEqual(instance._defaults, None)
     self.assertEqual(instance._namespace, ConfigurationType._global_prefix)
     self.assertEqual(instance._private,
                      ConfigurationType._private_defaults)
     purge_immediately = instance.purge_immediately  # this loads the defaults
     self.assertFalse(purge_immediately)
     self.assertEqual(instance._defaults, DATAGROWTH_DEFAULT_CONFIGURATION)
     # Implicit init with defaults
     instance = ConfigurationType(defaults=MOCK_CONFIGURATION)
     self.assertEqual(instance._defaults, MOCK_CONFIGURATION)
     self.assertEqual(instance._namespace, ConfigurationType._global_prefix)
     self.assertEqual(instance._private,
                      ConfigurationType._private_defaults)
     purge_immediately = instance.purge_immediately  # this won't load defaults as defaults got set
     self.assertFalse(purge_immediately)
     self.assertEqual(instance._defaults, MOCK_CONFIGURATION)
     # Explicit init with double private key
     instance = ConfigurationType(namespace="name",
                                  private=["_test", "_test", "oops"],
                                  defaults=MOCK_CONFIGURATION)
     self.assertEqual(instance._defaults, MOCK_CONFIGURATION)
     self.assertEqual(instance._namespace, "name")
     self.assertEqual(
         instance._private,
         ConfigurationType._private_defaults + ["_test", "_oops"])
Example #5
0
 def setUp(self):
     super().setUp()
     self.config = ConfigurationType(
         namespace="shell_resource",
         private=["_resource", "_continuation_limit"],
     )
     self.config.update({
         "resource": "resources.ShellResourceMock",
     })
Example #6
0
 def setUp(self):
     super().setUp()
     self.config = ConfigurationType(
         namespace="http_resource",
         private=["_resource", "_continuation_limit"],
     )
     self.config.update({
         "resource": "resources.HttpResourceMock",
     })
     self.session = MockRequests
Example #7
0
 def setUp(self):
     super().setUp()
     self.config = ConfigurationType(namespace="name",
                                     private=["_test3"],
                                     defaults=MOCK_CONFIGURATION)
     self.config.update({
         "test": "public",
         "_test2": "protected",
         "_test3": "private"
     })
Example #8
0
 def setUp(self):
     super().setUp()
     self.config = ConfigurationType(namespace="name",
                                     private=["_test3"],
                                     defaults=MOCK_CONFIGURATION)
     self.config.update({
         "test": "public",
         "_test2": "protected",
         "_test3": "private",
         "$test4": "variable"  # variable config is not recommended
     })
Example #9
0
 def setUp(self):
     super().setUp()
     self.config = ConfigurationType(
         namespace="shell_resource",
         private=["_resource", "_continuation_limit"],
     )
     self.config.update({
         "resource": "resources.ShellResourceMock",
     })
     self.args_list = [["test"], ["success"], ["fail"]]
     self.kwargs_list = [{"context": 5} for _ in range(len(self.args_list))]
Example #10
0
class TestLoadConfigDecorator(TestCase):

    def setUp(self):
        self.config = ConfigurationType(namespace="name", private=["_test3"], defaults=MOCK_CONFIGURATION)
        self.config.update({
            "test": "public",
            "_test2": "protected",
            "_test3": "private"
        })

    @staticmethod
    @load_config(defaults=MOCK_CONFIGURATION)
    def decorated(config, *args, **kwargs):
        return config, args, kwargs

    def test_decorator(self):
        # Standard call
        test_config, test_args, test_kwargs = self.decorated(
            "test",
            test="test",
            config=self.config.to_dict(protected=True, private=True)
        )
        self.assertIsInstance(test_config, ConfigurationType)
        self.assertEqual(test_config._defaults, MOCK_CONFIGURATION)
        self.assertEqual(test_config._namespace, "name")
        self.assertIn("_test3", test_config._private)
        self.assertEqual(self.config.test, "public")
        self.assertEqual(self.config.test2, "protected")
        self.assertEqual(self.config.test3, "private")
        self.assertEqual(self.config._test2, "protected")
        self.assertEqual(self.config._test3, "private")
        self.assertEqual(test_args, ("test",))
        self.assertEqual(test_kwargs, {"test": "test"})
        # Call with config set to a ConfigurationType
        test_config, test_args, test_kwargs = self.decorated(
            "test",
            test="test",
            config=self.config
        )
        self.assertEqual(test_config, self.config)
        self.assertEqual(test_args, ("test",))
        self.assertEqual(test_kwargs, {"test": "test"})
        # Wrong invocation
        try:
            test_config, test_args, test_kwargs = self.decorated(
                "test",
                test="test",
            )
            self.fail("load_config did not throw an exception when no config kwarg was set.")
        except TypeError:
            pass
Example #11
0
class TestLoadConfigDecorator(TestCase):
    def setUp(self):
        super().setUp()
        self.config = ConfigurationType(namespace="name",
                                        private=["_test3"],
                                        defaults=MOCK_CONFIGURATION)
        self.config.update({
            "test": "public",
            "_test2": "protected",
            "_test3": "private"
        })

    @staticmethod
    @load_config(defaults=MOCK_CONFIGURATION)
    def decorated(config, *args, **kwargs):
        return config, args, kwargs

    def test_decorator(self):
        # Standard call
        test_config, test_args, test_kwargs = self.decorated(
            "test",
            test="test",
            config=self.config.to_dict(protected=True, private=True))
        self.assertIsInstance(test_config, ConfigurationType)
        self.assertEqual(test_config._defaults, MOCK_CONFIGURATION)
        self.assertEqual(test_config._namespace, "name")
        self.assertIn("_test3", test_config._private)
        self.assertEqual(self.config.test, "public")
        self.assertEqual(self.config.test2, "protected")
        self.assertEqual(self.config.test3, "private")
        self.assertEqual(self.config._test2, "protected")
        self.assertEqual(self.config._test3, "private")
        self.assertEqual(test_args, ("test", ))
        self.assertEqual(test_kwargs, {"test": "test"})
        # Call with config set to a ConfigurationType
        test_config, test_args, test_kwargs = self.decorated(
            "test", test="test", config=self.config)
        self.assertEqual(test_config, self.config)
        self.assertEqual(test_args, ("test", ))
        self.assertEqual(test_kwargs, {"test": "test"})
        # Wrong invocation
        try:
            test_config, test_args, test_kwargs = self.decorated(
                "test",
                test="test",
            )
            self.fail(
                "load_config did not throw an exception when no config kwarg was set."
            )
        except TypeError:
            pass
Example #12
0
class TestGetStandardizedConfiguration(TestCase):

    def setUp(self):
        self.config = ConfigurationType(namespace="name", private=["_test3"], defaults=MOCK_CONFIGURATION)
        self.config.update({
            "test": "public",
            "_test2": "protected",
            "_test3": "private"
        })

    def test_standardized_configuration(self):
        out = get_standardized_configuration(self.config)
        self.assertEqual(out, "3601ce2b866f9ccff5e9e49b628e65108abb3d5ada72fce6511645212c0ce520")
        out = get_standardized_configuration(self.config, as_hash=False)
        self.assertEqual(out, "test=public")
Example #13
0
 def setUp(self):
     super().setUp()
     self.empty = ConfigurationType(namespace="name",
                                    private=["_test3"],
                                    defaults=MOCK_CONFIGURATION)
     self.config = ConfigurationType(namespace="name",
                                     private=["_test3"],
                                     defaults=MOCK_CONFIGURATION)
     self.config.update({
         "test": "public",
         "_test2": "protected",
         "_test3": "private",
         "$test4": "variable",  # variable config is not recommended
         "async":
         False  # should map to "asynchronous" property for Python 3.7+
     })
Example #14
0
 def setUp(self):
     self.config = ConfigurationType(namespace="name", private=["_test3"], defaults=MOCK_CONFIGURATION)
     self.config.update({
         "test": "public",
         "_test2": "protected",
         "_test3": "private"
     })
Example #15
0
 def setUp(self):
     self.config = ConfigurationType(namespace="name", private=["_test3"], defaults=MOCK_CONFIGURATION)
     self.config.update({
         "test": "public",
         "_test2": "protected",
         "_test3": "private",
         "$test4": "variable"  # variable config is not recommended
     })
Example #16
0
class TestGetStandardizedConfiguration(TestCase):
    def setUp(self):
        super().setUp()
        self.config = ConfigurationType(namespace="name",
                                        private=["_test3"],
                                        defaults=MOCK_CONFIGURATION)
        self.config.update({
            "test": "public",
            "_test2": "protected",
            "_test3": "private"
        })

    def test_standardized_configuration(self):
        out = get_standardized_configuration(self.config)
        self.assertEqual(
            out,
            "3601ce2b866f9ccff5e9e49b628e65108abb3d5ada72fce6511645212c0ce520")
        out = get_standardized_configuration(self.config, as_hash=False)
        self.assertEqual(out, "test=public")
Example #17
0
class TestHTTPTasksBase(TestCase):

    fixtures = ["test-http-resource-mock"]
    method = ""

    def setUp(self):
        super().setUp()
        self.config = ConfigurationType(
            namespace="http_resource",
            private=["_resource", "_continuation_limit"],
        )
        self.config.update({
            "resource": "resources.HttpResourceMock",
        })
        self.session = MockRequests

    def get_args_list(self, queries):
        if self.method == "get":
            return [[query] for query in queries]
        elif self.method == "post":
            return [[] for query in queries]
        else:
            raise Exception("{} does not have a valid method specified.".format(self.__class__.__name__))

    def get_kwargs_list(self, queries):
        if self.method == "get":
            return [{} for query in queries]
        elif self.method == "post":
            return [{"query": query} for query in queries]
        else:
            raise Exception("{} does not have a valid method specified.".format(self.__class__.__name__))

    def check_results(self, results, expected_length):
        self.assertEqual(len(results), expected_length)
        for pk in results:
            self.assertIsInstance(pk, int)
            self.assertGreater(pk, 0)
Example #18
0
class TestRunTask(TestCase):

    fixtures = ["test-shell-resource-mock"]

    def setUp(self):
        super().setUp()
        self.config = ConfigurationType(
            namespace="shell_resource",
            private=["_resource", "_continuation_limit"],
        )
        self.config.update({
            "resource": "resources.ShellResourceMock",
        })

    def check_results(self, results, expected_length):
        self.assertEqual(len(results), expected_length)
        for pk in results:
            self.assertIsInstance(pk, int)
            self.assertGreater(pk, 0)

    def test_run(self):
        scc, err = run("test", context=5, config=self.config)
        self.check_results(scc, 1)
        self.check_results(err, 0)
        # Similar but with a cached result
        scc, err = run("success", context=5, config=self.config)
        self.check_results(scc, 1)
        self.check_results(err, 0)
        # And with an error result
        scc, err = run("fail", context=5, config=self.config)
        self.check_results(scc, 0)
        self.check_results(err, 1)
        # In total there should be three Resources (two old, one new)
        self.assertEqual(
            ShellResourceMock.objects.count(), 3,
            "Expected three ShellResourceMock instances. Two from cache and one new"
        )
Example #19
0
 def test_from_dict(self):
     # Call with correct dict
     type_instance = ConfigurationType.from_dict(
         self.config.to_dict(private=True, protected=True),
         MOCK_CONFIGURATION
     )
     self.assertEqual(type_instance.test, "public")
     self.assertEqual(type_instance.test2, "protected")
     self.assertEqual(type_instance.test3, "private")
     self.assertEqual(type_instance.test4, "variable")
     self.assertEqual(type_instance.global_configuration, "global configuration")
     self.assertEqual(type_instance.namespace_configuration, "namespace configuration")
     self.assertEqual(type_instance._private, self.config._private)
     # Call with malformed dicts
     try:
         ConfigurationType.from_dict({"_namespace": "test"}, MOCK_CONFIGURATION)
         self.fail("from_dict does not fail if _private is missing from configuration.")
     except AssertionError:
         pass
     try:
         ConfigurationType.from_dict({"_private": "test"}, MOCK_CONFIGURATION)
         self.fail("from_dict does not fail if _namespace is missing from configuration.")
     except AssertionError:
         pass
Example #20
0
 def setUp(self):
     super().setUp()
     self.config = ConfigurationType(namespace="test",)
Example #21
0
class TestConfigurationType(TestCase):

    def setUp(self):
        self.config = ConfigurationType(namespace="name", private=["_test3"], defaults=MOCK_CONFIGURATION)
        self.config.update({
            "test": "public",
            "_test2": "protected",
            "_test3": "private",
            "$test4": "variable"  # variable config is not recommended
        })

    def test_init(self):
        # Implicit init without defaults
        # Notice that apps can manipulate the DEFAULT_CONFIGURATION upon app.ready
        # Therefor defaults are not loaded until first access
        instance = ConfigurationType()
        self.assertEqual(instance._defaults, None)
        self.assertEqual(instance._namespace, ConfigurationType._global_prefix)
        self.assertEqual(instance._private, ConfigurationType._private_defaults)
        purge_immediately = instance.purge_immediately  # this loads the defaults
        self.assertFalse(purge_immediately)
        self.assertEqual(instance._defaults, DEFAULT_CONFIGURATION)
        # Implicit init with defaults
        instance = ConfigurationType(defaults=MOCK_CONFIGURATION)
        self.assertEqual(instance._defaults, MOCK_CONFIGURATION)
        self.assertEqual(instance._namespace, ConfigurationType._global_prefix)
        self.assertEqual(instance._private, ConfigurationType._private_defaults)
        purge_immediately = instance.purge_immediately  # this won't load defaults as defaults got set
        self.assertFalse(purge_immediately)
        self.assertEqual(instance._defaults, MOCK_CONFIGURATION)
        # Explicit init with double private key
        instance = ConfigurationType(namespace="name", private=["_test", "_test", "oops"], defaults=MOCK_CONFIGURATION)
        self.assertEqual(instance._defaults, MOCK_CONFIGURATION)
        self.assertEqual(instance._namespace, "name")
        self.assertEqual(instance._private, ConfigurationType._private_defaults + ["_test", "_oops"])

    def test_attribute_access(self):
        self.assertEqual(self.config.test, "public")
        self.assertEqual(self.config.test2, "protected")
        self.assertEqual(self.config.test3, "private")
        self.assertEqual(self.config._test2, "protected")
        self.assertEqual(self.config._test3, "private")
        self.assertEqual(self.config.test4, "variable")
        # Not found error
        try:
            self.test = self.config.test5
            self.fail("ConfigurationType should raise an exception when configuration is not available")
        except ConfigurationNotFoundError:
            pass
        # Namespace configuration
        self.assertNotIn("namespace_configuration", self.config.__dict__)
        self.assertEqual(self.config.namespace_configuration, "namespace configuration")
        try:
            self.test = self.config.namespace_missing
            self.fail("ConfigurationType should raise an exception when namespace configuration is not available")
        except ConfigurationNotFoundError:
            pass
        # Global configuration
        self.assertNotIn("global_configuration", self.config.__dict__)
        self.assertEqual(self.config.global_configuration, "global configuration")
        try:
            self.test = self.config.namespace_missing
            self.fail("ConfigurationType should raise an exception when global configuration is not available")
        except ConfigurationNotFoundError:
            pass

    def test_update_using_update(self):
        # Test (partial) updating
        self.config.update({"test": "public 2"})
        self.assertEqual(self.config.test, "public 2")
        self.assertEqual(self.config.test2, "protected")
        self.assertEqual(self.config.test3, "private")
        self.config.update({"_test2": "protected 2", "_test3": "private 2", "$test4": "variable 2"})
        self.assertEqual(self.config.test, "public 2")
        self.assertEqual(self.config.test2, "protected 2")
        self.assertEqual(self.config.test3, "private 2")
        self.assertEqual(self.config.test4, "variable 2")
        # Test private configuration detection
        self.config._private.append("_private_also")
        self.config.update({"private_also": "private"})
        self.assertEqual(self.config.private_also, "private")
        self.assertEqual(self.config._private_also, "private")

    def test_to_dict(self):
        # Get all different possibilities
        private = self.config.to_dict(private=True)
        protected = self.config.to_dict(protected=True)
        public = self.config.to_dict()
        everything = self.config.to_dict(private=True, protected=True)
        # Check properties
        self.assertIn("_test3", private)
        self.assertIn("test", private)
        self.assertIn("$test4", private)
        self.assertNotIn("_test2", private)
        self.assertIn("_test2", protected)
        self.assertIn("test", protected)
        self.assertIn("$test4", protected)
        self.assertNotIn("_test3", protected)
        self.assertIn("test", public)
        self.assertIn("$test4", public)
        self.assertNotIn("_test2", public)
        self.assertNotIn("_test3", public)
        self.assertIn("test", everything)
        self.assertIn("_test2", everything)
        self.assertIn("_test3", everything)
        self.assertIn("$test4", everything)
        # Make sure that all private keys are there
        # But the defaults key should not be passed down
        self.assertEqual(len(self.config._private) - 1 + len(public), len(private))

    def test_from_dict(self):
        # Call with correct dict
        type_instance = ConfigurationType.from_dict(
            self.config.to_dict(private=True, protected=True),
            MOCK_CONFIGURATION
        )
        self.assertEqual(type_instance.test, "public")
        self.assertEqual(type_instance.test2, "protected")
        self.assertEqual(type_instance.test3, "private")
        self.assertEqual(type_instance.test4, "variable")
        self.assertEqual(type_instance.global_configuration, "global configuration")
        self.assertEqual(type_instance.namespace_configuration, "namespace configuration")
        self.assertEqual(type_instance._private, self.config._private)
        # Call with malformed dicts
        try:
            ConfigurationType.from_dict({"_namespace": "test"}, MOCK_CONFIGURATION)
            self.fail("from_dict does not fail if _private is missing from configuration.")
        except AssertionError:
            pass
        try:
            ConfigurationType.from_dict({"_private": "test"}, MOCK_CONFIGURATION)
            self.fail("from_dict does not fail if _namespace is missing from configuration.")
        except AssertionError:
            pass

    def test_contains(self):
        self.assertTrue("test" in self.config)
        self.assertTrue("test2" in self.config)
        self.assertTrue("test3" in self.config)
        self.assertTrue("_test2" in self.config)
        self.assertTrue("_test3" in self.config)
        self.assertTrue("test4" in self.config)
        self.assertFalse("test5" in self.config)

    @patch("datagrowth.configuration.types.ConfigurationType.update")
    def test_supplement(self, update_method):
        self.config.supplement({"test": "public 2"})
        self.assertEqual(self.config.test, "public")
        update_method.assert_not_called()
        self.config.supplement({"_test2": "protected 2", "_test3": "private 2", "$test4": "variable 2"})
        self.assertEqual(self.config.test, "public")
        self.assertEqual(self.config.test2, "protected")
        self.assertEqual(self.config.test3, "private")
        self.assertEqual(self.config.test4, "variable")
        update_method.assert_not_called()
        self.config.supplement({"new": "new", "_new2": "new 2", "$new3": "new 3"})
        update_method.assert_called_once_with({"new": "new", "_new2": "new 2", "$new3": "new 3"})

    def test_items(self):
        # Get all different possibilities
        private = self.config.items(private=True)
        protected = self.config.items(protected=True)
        public = self.config.items()
        everything = self.config.items(private=True, protected=True)
        # Check types
        self.assertIsInstance(private, Iterator)
        self.assertIsInstance(protected, Iterator)
        self.assertIsInstance(public, Iterator)
        self.assertIsInstance(everything, Iterator)
        # Convert to more predictable dicts
        private = dict(private)
        protected = dict(protected)
        public = dict(public)
        everything = dict(everything)
        # Check properties
        self.assertIn("_test3", private)
        self.assertIn("test", private)
        self.assertIn("$test4", private)
        self.assertNotIn("_test2", private)
        self.assertIn("_test2", protected)
        self.assertIn("test", protected)
        self.assertIn("$test4", protected)
        self.assertNotIn("_test3", protected)
        self.assertIn("test", public)
        self.assertIn("$test4", public)
        self.assertNotIn("_test2", public)
        self.assertNotIn("_test3", public)
        self.assertIn("test", everything)
        self.assertIn("_test2", everything)
        self.assertIn("_test3", everything)
        self.assertIn("$test4", everything)
        # Make sure that all private keys are there
        # But the defaults key should not be passed down
        self.assertEqual(len(self.config._private) - 1 + len(public), len(private))

    def test_clean_key(self):
        variable_key = "$variable"
        protected_key = "_protected"
        weird_key = ",weird"
        self.assertEqual(self.config.clean_key(variable_key), "variable")
        self.assertEqual(self.config.clean_key(protected_key), "protected")
        self.assertEqual(self.config.clean_key(weird_key), ",weird")

    def test_get(self):
        self.assertEqual(self.config.get("test", None), "public")
        self.assertEqual(self.config.get("test2", None), "protected")
        self.assertEqual(self.config.get("test3", None), "private")
        self.assertEqual(self.config.get("_test2", None), "protected")
        self.assertEqual(self.config.get("_test3", None), "private")
        self.assertEqual(self.config.get("test4", None), "variable")
        # Default fallback
        self.assertEqual(self.config.get("test5", "does-not-exist"), "does-not-exist")
        self.assertEqual(self.config.get("test5", 0), 0)
        self.assertEqual(self.config.get("test5", None), None)
        # Namespace configuration (with a list default)
        self.assertNotIn("namespace_configuration", self.config.__dict__)
        self.assertEqual(self.config.get("namespace_configuration", None), "namespace configuration")
        self.assertEqual(self.config.get("namespace_missing", ["missing namespace"]), ["missing namespace"])
        # Global configuration (with a dict default)
        self.assertNotIn("global_configuration", self.config.__dict__)
        self.assertEqual(self.config.get("global_configuration", None), "global configuration")
        self.assertEqual(self.config.get("global_missing", {"global missing": True}), {"global missing": True})
        try:
            self.test = self.config.get("test5")
            self.fail(
                "ConfigurationType.get should raise an exception "
                "when configuration is not available and no default is set"
            )
        except ConfigurationNotFoundError:
            pass
Example #22
0
class TestConfigurationType(TestCase):
    def setUp(self):
        super().setUp()
        self.config = ConfigurationType(namespace="name",
                                        private=["_test3"],
                                        defaults=MOCK_CONFIGURATION)
        self.config.update({
            "test": "public",
            "_test2": "protected",
            "_test3": "private",
            "$test4": "variable"  # variable config is not recommended
        })

    def test_init(self):
        # Implicit init without defaults
        # Notice that apps can manipulate the DATAGROWTH_DEFAULT_CONFIGURATION upon app.ready
        # Therefor defaults are not loaded until first access
        instance = ConfigurationType()
        self.assertEqual(instance._defaults, None)
        self.assertEqual(instance._namespace, ConfigurationType._global_prefix)
        self.assertEqual(instance._private,
                         ConfigurationType._private_defaults)
        purge_immediately = instance.purge_immediately  # this loads the defaults
        self.assertFalse(purge_immediately)
        self.assertEqual(instance._defaults, DATAGROWTH_DEFAULT_CONFIGURATION)
        # Implicit init with defaults
        instance = ConfigurationType(defaults=MOCK_CONFIGURATION)
        self.assertEqual(instance._defaults, MOCK_CONFIGURATION)
        self.assertEqual(instance._namespace, ConfigurationType._global_prefix)
        self.assertEqual(instance._private,
                         ConfigurationType._private_defaults)
        purge_immediately = instance.purge_immediately  # this won't load defaults as defaults got set
        self.assertFalse(purge_immediately)
        self.assertEqual(instance._defaults, MOCK_CONFIGURATION)
        # Explicit init with double private key
        instance = ConfigurationType(namespace="name",
                                     private=["_test", "_test", "oops"],
                                     defaults=MOCK_CONFIGURATION)
        self.assertEqual(instance._defaults, MOCK_CONFIGURATION)
        self.assertEqual(instance._namespace, "name")
        self.assertEqual(
            instance._private,
            ConfigurationType._private_defaults + ["_test", "_oops"])

    def test_attribute_access(self):
        self.assertEqual(self.config.test, "public")
        self.assertEqual(self.config.test2, "protected")
        self.assertEqual(self.config.test3, "private")
        self.assertEqual(self.config._test2, "protected")
        self.assertEqual(self.config._test3, "private")
        self.assertEqual(self.config.test4, "variable")
        # Not found error
        try:
            self.test = self.config.test5
            self.fail(
                "ConfigurationType should raise an exception when configuration is not available"
            )
        except ConfigurationNotFoundError:
            pass
        # Namespace configuration
        self.assertNotIn("namespace_configuration", self.config.__dict__)
        self.assertEqual(self.config.namespace_configuration,
                         "namespace configuration")
        try:
            self.test = self.config.namespace_missing
            self.fail(
                "ConfigurationType should raise an exception when namespace configuration is not available"
            )
        except ConfigurationNotFoundError:
            pass
        # Global configuration
        self.assertNotIn("global_configuration", self.config.__dict__)
        self.assertEqual(self.config.global_configuration,
                         "global configuration")
        try:
            self.test = self.config.namespace_missing
            self.fail(
                "ConfigurationType should raise an exception when global configuration is not available"
            )
        except ConfigurationNotFoundError:
            pass

    def test_update_using_update(self):
        # Test (partial) updating
        self.config.update({"test": "public 2"})
        self.assertEqual(self.config.test, "public 2")
        self.assertEqual(self.config.test2, "protected")
        self.assertEqual(self.config.test3, "private")
        self.config.update({
            "_test2": "protected 2",
            "_test3": "private 2",
            "$test4": "variable 2"
        })
        self.assertEqual(self.config.test, "public 2")
        self.assertEqual(self.config.test2, "protected 2")
        self.assertEqual(self.config.test3, "private 2")
        self.assertEqual(self.config.test4, "variable 2")
        # Test private configuration detection
        self.config._private.append("_private_also")
        self.config.update({"private_also": "private"})
        self.assertEqual(self.config.private_also, "private")
        self.assertEqual(self.config._private_also, "private")

    def test_to_dict(self):
        # Get all different possibilities
        private = self.config.to_dict(private=True)
        protected = self.config.to_dict(protected=True)
        public = self.config.to_dict()
        everything = self.config.to_dict(private=True, protected=True)
        # Check properties
        self.assertIn("_test3", private)
        self.assertIn("test", private)
        self.assertIn("$test4", private)
        self.assertNotIn("_test2", private)
        self.assertIn("_test2", protected)
        self.assertIn("test", protected)
        self.assertIn("$test4", protected)
        self.assertNotIn("_test3", protected)
        self.assertIn("test", public)
        self.assertIn("$test4", public)
        self.assertNotIn("_test2", public)
        self.assertNotIn("_test3", public)
        self.assertIn("test", everything)
        self.assertIn("_test2", everything)
        self.assertIn("_test3", everything)
        self.assertIn("$test4", everything)
        # Make sure that all private keys are there
        # But the defaults key should not be passed down
        self.assertEqual(
            len(self.config._private) - 1 + len(public), len(private))

    def test_from_dict(self):
        # Call with correct dict
        type_instance = ConfigurationType.from_dict(
            self.config.to_dict(private=True, protected=True),
            MOCK_CONFIGURATION)
        self.assertEqual(type_instance.test, "public")
        self.assertEqual(type_instance.test2, "protected")
        self.assertEqual(type_instance.test3, "private")
        self.assertEqual(type_instance.test4, "variable")
        self.assertEqual(type_instance.global_configuration,
                         "global configuration")
        self.assertEqual(type_instance.namespace_configuration,
                         "namespace configuration")
        self.assertEqual(type_instance._private, self.config._private)
        # Call with malformed dicts
        try:
            ConfigurationType.from_dict({"_namespace": "test"},
                                        MOCK_CONFIGURATION)
            self.fail(
                "from_dict does not fail if _private is missing from configuration."
            )
        except AssertionError:
            pass
        try:
            ConfigurationType.from_dict({"_private": "test"},
                                        MOCK_CONFIGURATION)
            self.fail(
                "from_dict does not fail if _namespace is missing from configuration."
            )
        except AssertionError:
            pass

    def test_contains(self):
        self.assertTrue("test" in self.config)
        self.assertTrue("test2" in self.config)
        self.assertTrue("test3" in self.config)
        self.assertTrue("_test2" in self.config)
        self.assertTrue("_test3" in self.config)
        self.assertTrue("test4" in self.config)
        self.assertFalse("test5" in self.config)

    @patch("datagrowth.configuration.types.ConfigurationType.update")
    def test_supplement(self, update_method):
        self.config.supplement({"test": "public 2"})
        self.assertEqual(self.config.test, "public")
        update_method.assert_not_called()
        self.config.supplement({
            "_test2": "protected 2",
            "_test3": "private 2",
            "$test4": "variable 2"
        })
        self.assertEqual(self.config.test, "public")
        self.assertEqual(self.config.test2, "protected")
        self.assertEqual(self.config.test3, "private")
        self.assertEqual(self.config.test4, "variable")
        update_method.assert_not_called()
        self.config.supplement({
            "new": "new",
            "_new2": "new 2",
            "$new3": "new 3"
        })
        update_method.assert_called_once_with({
            "new": "new",
            "_new2": "new 2",
            "$new3": "new 3"
        })

    def test_items(self):
        # Get all different possibilities
        private = self.config.items(private=True)
        protected = self.config.items(protected=True)
        public = self.config.items()
        everything = self.config.items(private=True, protected=True)
        # Check types
        self.assertIsInstance(private, Iterator)
        self.assertIsInstance(protected, Iterator)
        self.assertIsInstance(public, Iterator)
        self.assertIsInstance(everything, Iterator)
        # Convert to more predictable dicts
        private = dict(private)
        protected = dict(protected)
        public = dict(public)
        everything = dict(everything)
        # Check properties
        self.assertIn("_test3", private)
        self.assertIn("test", private)
        self.assertIn("$test4", private)
        self.assertNotIn("_test2", private)
        self.assertIn("_test2", protected)
        self.assertIn("test", protected)
        self.assertIn("$test4", protected)
        self.assertNotIn("_test3", protected)
        self.assertIn("test", public)
        self.assertIn("$test4", public)
        self.assertNotIn("_test2", public)
        self.assertNotIn("_test3", public)
        self.assertIn("test", everything)
        self.assertIn("_test2", everything)
        self.assertIn("_test3", everything)
        self.assertIn("$test4", everything)
        # Make sure that all private keys are there
        # But the defaults key should not be passed down
        self.assertEqual(
            len(self.config._private) - 1 + len(public), len(private))

    def test_clean_key(self):
        variable_key = "$variable"
        protected_key = "_protected"
        weird_key = ",weird"
        self.assertEqual(self.config.clean_key(variable_key), "variable")
        self.assertEqual(self.config.clean_key(protected_key), "protected")
        self.assertEqual(self.config.clean_key(weird_key), ",weird")

    def test_get(self):
        self.assertEqual(self.config.get("test", None), "public")
        self.assertEqual(self.config.get("test2", None), "protected")
        self.assertEqual(self.config.get("test3", None), "private")
        self.assertEqual(self.config.get("_test2", None), "protected")
        self.assertEqual(self.config.get("_test3", None), "private")
        self.assertEqual(self.config.get("test4", None), "variable")
        # Default fallback
        self.assertEqual(self.config.get("test5", "does-not-exist"),
                         "does-not-exist")
        self.assertEqual(self.config.get("test5", 0), 0)
        self.assertEqual(self.config.get("test5", None), None)
        # Namespace configuration (with a list default)
        self.assertNotIn("namespace_configuration", self.config.__dict__)
        self.assertEqual(self.config.get("namespace_configuration", None),
                         "namespace configuration")
        self.assertEqual(
            self.config.get("namespace_missing", ["missing namespace"]),
            ["missing namespace"])
        # Global configuration (with a dict default)
        self.assertNotIn("global_configuration", self.config.__dict__)
        self.assertEqual(self.config.get("global_configuration", None),
                         "global configuration")
        self.assertEqual(
            self.config.get("global_missing", {"global missing": True}),
            {"global missing": True})
        try:
            self.test = self.config.get("test5")
            self.fail(
                "ConfigurationType.get should raise an exception "
                "when configuration is not available and no default is set")
        except ConfigurationNotFoundError:
            pass