Exemplo n.º 1
0
def basic_compiled_model():
    return CompiledModelNode(
        package_name='test',
        root_path='/root/',
        path='/root/models/foo.sql',
        original_file_path='models/foo.sql',
        raw_sql='select * from {{ ref("other") }}',
        name='foo',
        resource_type=NodeType.Model,
        unique_id='model.test.foo',
        fqn=['test', 'models', 'foo'],
        refs=[],
        sources=[],
        depends_on=DependsOn(),
        deferred=True,
        description='',
        database='test_db',
        schema='test_schema',
        alias='bar',
        tags=[],
        config=NodeConfig(),
        meta={},
        compiled=True,
        extra_ctes=[InjectedCTE('whatever', 'select * from other')],
        extra_ctes_injected=True,
        compiled_sql='with whatever as (select * from other) select * from whatever',
        checksum=FileHash.from_contents(''),
        unrendered_config={}
    )
Exemplo n.º 2
0
 def test_basic(self):
     raw_sql = 'select 1 as id'
     block = self.file_block_for(raw_sql, 'nested/analysis_1.sql')
     self.parser.parse_file(block)
     self.assert_has_results_length(self.parser.results, nodes=1)
     node = list(self.parser.results.nodes.values())[0]
     expected = ParsedAnalysisNode(
         alias='analysis_1',
         name='analysis_1',
         database='test',
         schema='analytics',
         resource_type=NodeType.Analysis,
         unique_id='analysis.snowplow.analysis_1',
         fqn=['snowplow', 'analysis', 'nested', 'analysis_1'],
         package_name='snowplow',
         original_file_path=normalize('analyses/nested/analysis_1.sql'),
         root_path=get_abs_os_path('./dbt_modules/snowplow'),
         depends_on=DependsOn(),
         config=NodeConfig(),
         path=normalize('analysis/nested/analysis_1.sql'),
         raw_sql=raw_sql,
     )
     self.assertEqual(node, expected)
     path = get_abs_os_path('./dbt_modules/snowplow/analyses/nested/analysis_1.sql')
     self.assertIn(path, self.parser.results.files)
     self.assertEqual(self.parser.results.files[path].nodes, ['analysis.snowplow.analysis_1'])
Exemplo n.º 3
0
 def test_basic(self):
     raw_sql = '{{ config(materialized="table") }}select 1 as id'
     block = self.file_block_for(raw_sql, 'nested/model_1.sql')
     self.parser.parse_file(block)
     self.assert_has_results_length(self.parser.results, nodes=1)
     node = list(self.parser.results.nodes.values())[0]
     expected = ParsedModelNode(
         alias='model_1',
         name='model_1',
         database='test',
         schema='analytics',
         resource_type=NodeType.Model,
         unique_id='model.snowplow.model_1',
         fqn=['snowplow', 'nested', 'model_1'],
         package_name='snowplow',
         original_file_path=normalize('models/nested/model_1.sql'),
         root_path=get_abs_os_path('./dbt_modules/snowplow'),
         config=NodeConfig(materialized='table'),
         path=normalize('nested/model_1.sql'),
         raw_sql=raw_sql,
     )
     self.assertEqual(node, expected)
     path = get_abs_os_path('./dbt_modules/snowplow/models/nested/model_1.sql')
     self.assertIn(path, self.parser.results.files)
     self.assertEqual(self.parser.results.files[path].nodes, ['model.snowplow.model_1'])
Exemplo n.º 4
0
def make_model(pkg,
               name,
               sql,
               refs=None,
               sources=None,
               tags=None,
               path=None,
               alias=None,
               config_kwargs=None,
               fqn_extras=None):
    if refs is None:
        refs = []
    if sources is None:
        sources = []
    if tags is None:
        tags = []
    if path is None:
        path = f'{name}.sql'
    if alias is None:
        alias = name
    if config_kwargs is None:
        config_kwargs = {}

    if fqn_extras is None:
        fqn_extras = []

    fqn = [pkg] + fqn_extras + [name]

    depends_on_nodes = []
    source_values = []
    ref_values = []
    for ref in refs:
        ref_values.append([ref.name])
        depends_on_nodes.append(ref.unique_id)
    for src in sources:
        source_values.append([src.source_name, src.name])
        depends_on_nodes.append(src.unique_id)

    return ParsedModelNode(
        raw_sql=sql,
        database='dbt',
        schema='dbt_schema',
        alias=alias,
        name=name,
        fqn=fqn,
        unique_id=f'model.{pkg}.{name}',
        package_name=pkg,
        root_path='/usr/dbt/some-project',
        path=path,
        original_file_path=f'models/{path}',
        config=NodeConfig(**config_kwargs),
        tags=tags,
        refs=ref_values,
        sources=source_values,
        depends_on=DependsOn(nodes=depends_on_nodes),
        resource_type=NodeType.Model,
        checksum=FileHash.from_contents(''),
    )
Exemplo n.º 5
0
    def test_basic_uncompiled(self):
        node_dict = {
            'name': 'foo',
            'root_path': '/root/',
            'resource_type': str(NodeType.Model),
            'path': '/root/x/path.sql',
            'original_file_path': '/root/path.sql',
            'package_name': 'test',
            'raw_sql': 'select * from wherever',
            'unique_id': 'model.test.foo',
            'fqn': ['test', 'models', 'foo'],
            'refs': [],
            'sources': [],
            'depends_on': {'macros': [], 'nodes': []},
            'database': 'test_db',
            'description': '',
            'schema': 'test_schema',
            'alias': 'bar',
            'tags': [],
            'config': {
                'column_types': {},
                'enabled': True,
                'materialized': 'view',
                'persist_docs': {},
                'post-hook': [],
                'pre-hook': [],
                'quoting': {},
                'tags': [],
                'vars': {},
            },
            'docs': {'show': True},
            'columns': {},
            'meta': {},
            'compiled': False,
            'extra_ctes': [],
            'extra_ctes_injected': False,
        }
        node = self.ContractType(
            package_name='test',
            root_path='/root/',
            path='/root/x/path.sql',
            original_file_path='/root/path.sql',
            raw_sql='select * from wherever',
            name='foo',
            resource_type=NodeType.Model,
            unique_id='model.test.foo',
            fqn=['test', 'models', 'foo'],
            refs=[],
            sources=[],
            depends_on=DependsOn(),
            description='',
            database='test_db',
            schema='test_schema',
            alias='bar',
            tags=[],
            config=NodeConfig(),
            meta={},
            compiled=False,
            extra_ctes=[],
            extra_ctes_injected=False,
        )
        self.assert_symmetric(node, node_dict)
        self.assertFalse(node.empty)
        self.assertTrue(node.is_refable)
        self.assertFalse(node.is_ephemeral)
        self.assertEqual(node.local_vars(), {})

        minimum = self._minimum()
        self.assert_from_dict(node, minimum)
        pickle.loads(pickle.dumps(node))
Exemplo n.º 6
0
 def test_basic_compiled(self):
     node_dict = {
         'name': 'foo',
         'root_path': '/root/',
         'resource_type': str(NodeType.Model),
         'path': '/root/x/path.sql',
         'original_file_path': '/root/path.sql',
         'package_name': 'test',
         'raw_sql': 'select * from {{ ref("other") }}',
         'unique_id': 'model.test.foo',
         'fqn': ['test', 'models', 'foo'],
         'refs': [],
         'sources': [],
         'depends_on': {
             'macros': [],
             'nodes': []
         },
         'database': 'test_db',
         'description': '',
         'schema': 'test_schema',
         'alias': 'bar',
         'tags': [],
         'config': {
             'column_types': {},
             'enabled': True,
             'materialized': 'view',
             'persist_docs': {},
             'post-hook': [],
             'pre-hook': [],
             'quoting': {},
             'tags': [],
             'vars': {},
         },
         'docrefs': [],
         'columns': {},
         'compiled': True,
         'compiled_sql': 'select * from whatever',
         'extra_ctes': [{
             'id': 'whatever',
             'sql': 'select * from other'
         }],
         'extra_ctes_injected': True,
         'injected_sql':
         'with whatever as (select * from other) select * from whatever',
         'wrapped_sql': 'None',
     }
     node = self.ContractType(
         package_name='test',
         root_path='/root/',
         path='/root/x/path.sql',
         original_file_path='/root/path.sql',
         raw_sql='select * from {{ ref("other") }}',
         name='foo',
         resource_type=NodeType.Model,
         unique_id='model.test.foo',
         fqn=['test', 'models', 'foo'],
         refs=[],
         sources=[],
         depends_on=DependsOn(),
         description='',
         database='test_db',
         schema='test_schema',
         alias='bar',
         tags=[],
         config=NodeConfig(),
         compiled=True,
         compiled_sql='select * from whatever',
         extra_ctes=[InjectedCTE('whatever', 'select * from other')],
         extra_ctes_injected=True,
         injected_sql=
         'with whatever as (select * from other) select * from whatever',
         wrapped_sql='None',
     )
     self.assert_symmetric(node, node_dict)
     self.assertFalse(node.empty)
     self.assertTrue(node.is_refable)
     self.assertFalse(node.is_ephemeral)
     self.assertEqual(node.local_vars(), {})