コード例 #1
0
    def test_replace_variables_simple_case(self):
        dct = {
            "env": "{{env_var}}",
            "env_dict": {
                "env3": "{{ var1 }}"
            },
            "env4": "{{var2 }}",
            "env5": "{{{var2}}",
            "env6": "{{var3}}"
        }

        variables = {
            "env_var": "env value",
            "var1": "value1",
            "var2": "value2"
        }

        expected = {
            "env": "env value",
            "env_dict": {
                "env3": "value1"
            },
            "env4": "value2",
            "env5": "{value2",
            "env6": "{{var3}}"
        }

        self.assertEqual(expected,
                         dict_util.replace_placeholders(dct, variables))
コード例 #2
0
    def test_replace_variables_flat_replace(self, airflow_variable_mock):
        def airflow_variable_values(key, default_var):
            return 'liminal playground' if key == 'playground' else default_var

        airflow_variable_mock.side_effect = airflow_variable_values

        dct = {
            "query": "select * from my_table "
            "where event_type = {{event_type}} and region = {{region}}",
            "env": "{{prod}}, {{stg}}, {{playground}}",
            "optional": "{{optionals}}"
        }

        variables = {
            "region": "us_east_1",
            "event_type": "subscription",
            "prod": "liminal production",
            "stg": "liminal staging"
        }

        expected = {
            'query': 'select * from my_table '
            'where event_type = subscription and region = us_east_1',
            "env": "liminal production, liminal staging, liminal playground",
            "optional": "{{optionals}}"
        }

        self.assertEqual(expected,
                         dict_util.replace_placeholders(dct, variables))
コード例 #3
0
 def test_replace_variables_empty_var(self):
     dct = {
         "env": "{{env_var}}",
         "env_dict": {
             "env3": "{{ var1 }}"
         },
         "env4": "{{var2 }}",
         "env5": "{{{var2}}",
         "env6": "{{var3}}"
     }
     self.assertEqual(dct, dict_util.replace_placeholders(dct, {}))
コード例 #4
0
def apply_variable_substitution(subliminal, superliminal, is_render_variables=False):
    """
     if is_render_variables is True
        Replace all {{variable.key}} in subliminal with variable.value variable in
        subliminal.variables + superliminal.variables
     else
        merge subliminal.variables with superliminal.variables without replace
        placeholders
     """
    keyword = "variables"
    merged_variables = dict_util.merge_dicts(subliminal.get(keyword, {}),
                                             superliminal.get(keyword, {}), True)
    if is_render_variables:
        for k, v in merged_variables.items():
            if isinstance(v, str) or (not isinstance(v, dict) and not isinstance(v, list)):
                merged_variables[k] = dict_util.replace_placholders_in_string(str(v),
                                                                              merged_variables)

        merged_variables = dict_util.replace_placeholders(merged_variables, merged_variables)
        return dict_util.replace_placeholders(subliminal, merged_variables)
    else:
        subliminal[keyword] = merged_variables
        return subliminal
コード例 #5
0
    def test_replace_variables_from_variable_and_not_env(self):
        dct = {
            "query":
            "select * from my_table "
            "where event_type = {{event_type}} and region = {{region}} from {{table}}"
        }

        variables = {"table": "my_variable_table"}

        expected = {
            'query':
            'select * from my_table '
            'where event_type = {{event_type}} '
            'and region = {{region}} from my_variable_table'
        }

        self.assertEqual(expected,
                         dict_util.replace_placeholders(dct, variables))
コード例 #6
0
    def test_replace_variables_with_nested_list(self, airflow_variable_mock):
        def airflow_variable_values(key, default_var):
            return 'liminal playground' if key == 'playground' else default_var

        airflow_variable_mock.side_effect = airflow_variable_values

        dct = {
            "query": "select * from my_table "
            "where event_type = {{event_type}} and region = {{region}}",
            "env": ['{{prod}}', '{{stg}}', '{{playground}}'],
            "tasks": [{
                'id': 'id1',
                'image': '{{image}}'
            }],
            "optional": "{{optionals}}"
        }

        variables = {
            "region": "us_east_1",
            "event_type": "subscription",
            "prod": "liminal production",
            "stg": "liminal staging",
            "image": "my_image_name"
        }

        expected = {
            'env':
            ['liminal production', 'liminal staging', 'liminal playground'],
            'optional':
            '{{optionals}}',
            'query':
            'select * from my_table where event_type = subscription and region = '
            'us_east_1',
            'tasks': [{
                'id': 'id1',
                'image': 'my_image_name'
            }]
        }

        self.assertEqual(expected,
                         dict_util.replace_placeholders(dct, variables))
コード例 #7
0
    def test_replace_variables_from_airflow_and_not_enc(
            self, airflow_variable_mock):
        def airflow_variable_values(key, default_var):
            return 'my_airflow_table' if key == 'table' else default_var

        airflow_variable_mock.side_effect = airflow_variable_values

        dct = {
            "query":
            "select * from my_table "
            "where event_type = {{event_type}} and region = {{region}} from {{table}}"
        }

        variables = {}

        expected = {
            'query':
            'select * from my_table '
            'where event_type = {{event_type}} '
            'and region = {{region}} from my_airflow_table'
        }

        self.assertEqual(expected,
                         dict_util.replace_placeholders(dct, variables))