|
1 | 1 | import os |
2 | 2 | import pytest |
| 3 | +import time |
3 | 4 | from dbt.tests.util import run_dbt, run_dbt_and_capture, write_file |
4 | 5 | import dbt.tests.adapter.python_model.test_python_model as dbt_tests |
5 | 6 |
|
@@ -64,6 +65,93 @@ def model(dbt, spark): |
64 | 65 | return spark.createDataFrame(data, schema=['test1', 'test3']) |
65 | 66 | """ |
66 | 67 |
|
| 68 | +models__python_array_batch_id_python = """ |
| 69 | +import pandas |
| 70 | +
|
| 71 | +def model(dbt, spark): |
| 72 | + random_array = [ |
| 73 | + [9001.3985362160208, -157.9871329592354], |
| 74 | + [-817.8786101352823, -528.9769041860632], |
| 75 | + [-886.6488625065194, 941.0504221837489], |
| 76 | + [6.69525238666165, 919.5903586746183], |
| 77 | + [754.3718741592056, -121.25678519054622], |
| 78 | + [-352.3158889341157, 254.9985130814921], |
| 79 | + [563.0633042715097, 833.2963094260072], |
| 80 | + ] |
| 81 | +
|
| 82 | + df = pd.DataFrame(random_array, columns=["A", "B"]) |
| 83 | +
|
| 84 | + df["C"] = df["A"] * df["B"] |
| 85 | +
|
| 86 | + final_df = df[["A", "B", "C"]] |
| 87 | +
|
| 88 | + return final_df |
| 89 | +""" |
| 90 | + |
| 91 | +models__python_array_batch_id_yaml = """ |
| 92 | +models: |
| 93 | + - name: python_array_batch_id |
| 94 | + description: A random table with a calculated column defined in python. |
| 95 | + config: |
| 96 | + batch_id: '{{ run_started_at.strftime("%Y-%m-%d-%H-%M-%S") }}-python-array' |
| 97 | + columns: |
| 98 | + - name: A |
| 99 | + description: Column A |
| 100 | + - name: B |
| 101 | + description: Column B |
| 102 | + - name: C |
| 103 | + description: Column C |
| 104 | +""" |
| 105 | + |
| 106 | +custom_ts_id = str("custom-" + str(time.time()).replace(".", "-")) |
| 107 | + |
| 108 | +models__bad_python_array_batch_id_yaml = f""" |
| 109 | +models: |
| 110 | + - name: python_array_batch_id |
| 111 | + description: A random table with a calculated column defined in python. |
| 112 | + config: |
| 113 | + batch_id: {custom_ts_id}-python-array |
| 114 | + columns: |
| 115 | + - name: A |
| 116 | + description: Column A |
| 117 | + - name: B |
| 118 | + description: Column B |
| 119 | + - name: C |
| 120 | + description: Column C |
| 121 | +""" |
| 122 | + |
| 123 | + |
| 124 | +class TestPythonBatchIdModels: |
| 125 | + @pytest.fixture(scope="class") |
| 126 | + def models(self): |
| 127 | + return { |
| 128 | + "python_array_batch_id.py": models__python_array_batch_id_python, |
| 129 | + "python_array_batch_id.yml": models__python_array_batch_id_yaml, |
| 130 | + } |
| 131 | + |
| 132 | + def test_multiple_named_python_models(self, project): |
| 133 | + result, output = run_dbt_and_capture(["run"], expect_pass=True) |
| 134 | + time.sleep(5) # In case both runs are submitted simultaneously |
| 135 | + result_two, output_two = run_dbt_and_capture(["run"], expect_pass=True) |
| 136 | + assert len(result) == 1 |
| 137 | + assert len(result_two) == 1 |
| 138 | + |
| 139 | + |
| 140 | +class TestPythonDuplicateBatchIdModels: |
| 141 | + @pytest.fixture(scope="class") |
| 142 | + def models(self): |
| 143 | + return { |
| 144 | + "python_array_batch_id.py": models__python_array_batch_id_python, |
| 145 | + "python_array_batch_id.yml": models__bad_python_array_batch_id_yaml, |
| 146 | + } |
| 147 | + |
| 148 | + def test_multiple_python_models_fixed_id(self, project): |
| 149 | + result, output = run_dbt_and_capture(["run"], expect_pass=True) |
| 150 | + result_two, output_two = run_dbt_and_capture(["run"], expect_pass=False) |
| 151 | + assert result_two[0].message.startswith("409 Already exists: Failed to create batch:") |
| 152 | + assert len(result) == 1 |
| 153 | + assert len(result_two) == 1 |
| 154 | + |
67 | 155 |
|
68 | 156 | @pytest.mark.skip(reason=TEST_SKIP_MESSAGE) |
69 | 157 | class TestChangingSchemaDataproc: |
|
0 commit comments