|
| 1 | +import os |
1 | 2 | import shutil
|
2 | 3 | from decimal import Decimal
|
3 | 4 | from pathlib import Path
|
| 5 | +from unittest.mock import patch |
4 | 6 |
|
5 | 7 | from dbt.include.starter_project import PACKAGE_PATH as starter_project_directory
|
6 | 8 |
|
@@ -58,55 +60,105 @@ def test_execute(self):
|
58 | 60 | Test the Project Interface by reading the original dbt_project.yml
|
59 | 61 | and return the dictionary.
|
60 | 62 | """
|
61 |
| - with DBTAdapter(str(self.project_dir)) as dbt_adapter: |
62 |
| - # create df |
63 |
| - import pandas as pd |
64 |
| - df = pd.DataFrame([[1, 'foo'], [2, 'bar']], columns=['id', 'text']) |
65 |
| - df_dict = df.to_dict(orient='list') |
66 |
| - |
67 |
| - # create agate table from df |
68 |
| - from agate import Table |
69 |
| - table = Table( |
70 |
| - rows=list(map(list, zip(*[v for _, v in df_dict.items()]))), |
71 |
| - column_names=df_dict.keys() |
72 |
| - ) |
73 |
| - |
74 |
| - relation = dbt_adapter.get_relation( |
75 |
| - database='test', |
76 |
| - schema='main', |
77 |
| - identifier='test', |
78 |
| - ) |
79 |
| - relation_context = dict(this=relation) |
80 |
| - |
81 |
| - dbt_adapter.execute_macro( |
82 |
| - 'reset_csv_table', |
83 |
| - context_overide=relation_context, |
84 |
| - model={'config': {}}, |
85 |
| - old_relation=relation, |
86 |
| - full_refresh=True, |
87 |
| - agate_table=table |
88 |
| - ) |
89 |
| - |
90 |
| - dbt_adapter.execute_macro( |
91 |
| - 'load_csv_rows', |
92 |
| - context_overide=relation_context, |
93 |
| - model={'config': {}}, |
94 |
| - agate_table=table |
95 |
| - ) |
| 63 | + value = 'mage' |
96 | 64 |
|
97 |
| - _res, df = dbt_adapter.execute('select * from test.main.test', fetch=True) |
| 65 | + file_path = os.path.join( |
| 66 | + self.repo_path, |
| 67 | + 'dbt_test_project', |
| 68 | + f'.mage_temp_profiles_{value}', |
| 69 | + 'profiles.yml', |
| 70 | + ) |
98 | 71 |
|
99 |
| - self.assertEqual( |
100 |
| - df.to_dict(), |
101 |
| - {0: {'id': Decimal('1'), 'text': Decimal('2')}, 1: {'id': 'foo', 'text': 'bar'}} |
102 |
| - ) |
| 72 | + os.makedirs(os.path.dirname(file_path), exist_ok=True) |
| 73 | + with open(file_path, 'w') as f: |
| 74 | + f.write('') |
| 75 | + |
| 76 | + class MockUUID: |
| 77 | + def __str__(self): |
| 78 | + return value |
| 79 | + |
| 80 | + @property |
| 81 | + def hex(self) -> str: |
| 82 | + return value |
| 83 | + |
| 84 | + with patch( |
| 85 | + 'mage_ai.data_preparation.models.block.dbt.profiles.uuid.uuid4', |
| 86 | + lambda: MockUUID(), |
| 87 | + ): |
| 88 | + with DBTAdapter(str(self.project_dir)) as dbt_adapter: |
| 89 | + # create df |
| 90 | + import pandas as pd |
| 91 | + df = pd.DataFrame([[1, 'foo'], [2, 'bar']], columns=['id', 'text']) |
| 92 | + df_dict = df.to_dict(orient='list') |
| 93 | + |
| 94 | + # create agate table from df |
| 95 | + from agate import Table |
| 96 | + table = Table( |
| 97 | + rows=list(map(list, zip(*[v for _, v in df_dict.items()]))), |
| 98 | + column_names=df_dict.keys() |
| 99 | + ) |
| 100 | + |
| 101 | + relation = dbt_adapter.get_relation( |
| 102 | + database='test', |
| 103 | + schema='main', |
| 104 | + identifier='test', |
| 105 | + ) |
| 106 | + relation_context = dict(this=relation) |
| 107 | + |
| 108 | + dbt_adapter.execute_macro( |
| 109 | + 'reset_csv_table', |
| 110 | + context_overide=relation_context, |
| 111 | + model={'config': {}}, |
| 112 | + old_relation=relation, |
| 113 | + full_refresh=True, |
| 114 | + agate_table=table |
| 115 | + ) |
| 116 | + |
| 117 | + dbt_adapter.execute_macro( |
| 118 | + 'load_csv_rows', |
| 119 | + context_overide=relation_context, |
| 120 | + model={'config': {}}, |
| 121 | + agate_table=table |
| 122 | + ) |
| 123 | + |
| 124 | + _res, df = dbt_adapter.execute('select * from test.main.test', fetch=True) |
| 125 | + |
| 126 | + self.assertEqual( |
| 127 | + df.to_dict(), |
| 128 | + {0: {'id': Decimal('1'), 'text': Decimal('2')}, 1: {'id': 'foo', 'text': 'bar'}} |
| 129 | + ) |
103 | 130 |
|
104 | 131 | def test_credentials(self):
|
105 | 132 | """
|
106 | 133 | Test the Project Interface by reading the original dbt_project.yml
|
107 | 134 | and return the dictionary.
|
108 | 135 | """
|
109 |
| - with DBTAdapter(str(self.project_dir)) as dbt_adapter: |
110 |
| - credentials = dbt_adapter.credentials |
111 |
| - self.assertEqual(credentials.schema, 'main') |
112 |
| - self.assertEqual(credentials.database, 'test') |
| 136 | + value = 'mage' |
| 137 | + |
| 138 | + file_path = os.path.join( |
| 139 | + self.repo_path, |
| 140 | + 'dbt_test_project', |
| 141 | + f'.mage_temp_profiles_{value}', |
| 142 | + 'profiles.yml', |
| 143 | + ) |
| 144 | + |
| 145 | + os.makedirs(os.path.dirname(file_path), exist_ok=True) |
| 146 | + with open(file_path, 'w') as f: |
| 147 | + f.write('') |
| 148 | + |
| 149 | + class MockUUID: |
| 150 | + def __str__(self): |
| 151 | + return value |
| 152 | + |
| 153 | + @property |
| 154 | + def hex(self) -> str: |
| 155 | + return value |
| 156 | + |
| 157 | + with patch( |
| 158 | + 'mage_ai.data_preparation.models.block.dbt.profiles.uuid.uuid4', |
| 159 | + lambda: MockUUID(), |
| 160 | + ): |
| 161 | + with DBTAdapter(str(self.project_dir)) as dbt_adapter: |
| 162 | + credentials = dbt_adapter.credentials |
| 163 | + self.assertEqual(credentials.schema, 'main') |
| 164 | + self.assertEqual(credentials.database, 'test') |
0 commit comments