Skip to content

Commit

Permalink
Add test to test_graph.py
Browse files Browse the repository at this point in the history
  • Loading branch information
max-sixty committed Oct 3, 2022
1 parent fed0e45 commit 7c5919d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 13 deletions.
46 changes: 38 additions & 8 deletions core/dbt/parser/_dbt_prql.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,37 @@
if typing.TYPE_CHECKING:
from dbt.parser.language_provider import references_type

# import prql_python


# Always return the same SQL, mocking the prqlc output for a single case which we
# currently use in tests, so we can test this without configuring dependencies. (Obv
# fix as we expand the tests, way before we merge.)
# import prql_python
# This mocks the prqlc output for two cases which we currently use in tests, so we can
# test this without configuring dependencies. (Obv fix as we expand the tests, way
# before we merge.)
class prql_python: # type: ignore
@staticmethod
def to_sql(prql):
compiled_sql = """
def to_sql(prql) -> str:

query_1 = "from employees"

query_1_compiled = """
SELECT
employees.*
FROM
employees
""".strip()

query_2 = """
from (dbt source.salesforce.in_process)
join (dbt ref.foo.bar) [id]
filter salary > 100
""".strip()

query_2_refs_replaced = """
from (`{{ source('salesforce', 'in_process') }}`)
join (`{{ ref('foo', 'bar') }}`) [id]
filter salary > 100
""".strip()

query_2_compiled = """
SELECT
"{{ source('salesforce', 'in_process') }}".*,
"{{ ref('foo', 'bar') }}".*,
Expand All @@ -33,7 +54,16 @@ def to_sql(prql):
WHERE
salary > 100
""".strip()
return compiled_sql

lookup = dict(
{
query_1: query_1_compiled,
query_2: query_2_compiled,
query_2_refs_replaced: query_2_compiled,
}
)

return lookup[prql]


logger = logging.getLogger(__name__)
Expand Down
24 changes: 23 additions & 1 deletion test/unit/test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ def setUp(self):
# Create file filesystem searcher
self.filesystem_search = patch('dbt.parser.read_files.filesystem_search')
def mock_filesystem_search(project, relative_dirs, extension, ignore_spec):
# Adding in `and "prql" not in extension` will cause a bunch of tests to
# fail; need to understand more on how these are constructed to debug.
# Possibly `sql not in extension` is a way of having it only run once.
if 'sql' not in extension:
return []
if 'models' not in relative_dirs:
Expand Down Expand Up @@ -144,7 +147,7 @@ def use_models(self, models):
path = FilePath(
searched_path='models',
project_root=os.path.normcase(os.getcwd()),
relative_path='{}.sql'.format(k),
relative_path=f'{k}.{lang}',
modification_time=0.0,
)
# FileHash can't be empty or 'search_key' will be None
Expand Down Expand Up @@ -328,3 +331,22 @@ def test__partial_parse(self):
manifest.metadata.dbt_version = '99999.99.99'
is_partial_parsable, _ = loader.is_partial_parsable(manifest)
self.assertFalse(is_partial_parsable)

def test_models_prql(self):
self.use_models({
'model_prql':( 'from employees', 'prql'),
})

config = self.get_config()
manifest = self.load_manifest(config)

compiler = self.get_compiler(config)
linker = compiler.compile(manifest)

self.assertEqual(
list(linker.nodes()),
['model.test_models_compile.model_prql'])

self.assertEqual(
list(linker.edges()),
[])
8 changes: 4 additions & 4 deletions test/unit/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -716,10 +716,10 @@ def test_parse_error(self):

def test_parse_prql_file(self):
prql_code = """
from (dbt source.salesforce.in_process)
join (dbt ref.foo.bar) [id]
filter salary > 100
"""
from (dbt source.salesforce.in_process)
join (dbt ref.foo.bar) [id]
filter salary > 100
""".strip()
block = self.file_block_for(prql_code, 'nested/prql_model.prql')
self.parser.manifest.files[block.file.file_id] = block.file
self.parser.parse_file(block)
Expand Down

0 comments on commit 7c5919d

Please sign in to comment.