@@ -125,25 +125,59 @@ def test_prepare_args_with_positional():
125125
126126@no_s3_hook
127127def test_dbt_base_dbt_directory ():
128- """Test dbt_directory yields a temporary directory"""
128+ """Test dbt_directory yields a temporary directory. """
129129 op = DbtBaseOperator (
130130 task_id = "dbt_task" ,
131131 project_dir = "/path/to/project/" ,
132132 profiles_dir = "/path/to/profiles/" ,
133133 )
134+ op .state = "target/"
135+
134136 with op .dbt_directory () as tmp_dir :
135137 assert Path (tmp_dir ).exists ()
136138 assert op .project_dir == "/path/to/project/"
137139 assert op .profiles_dir == "/path/to/profiles/"
140+ assert op .state == f"{ tmp_dir } /target"
141+
142+
143+ @no_s3_hook
144+ def test_dbt_base_dbt_directory_with_absolute_state ():
145+ """Test dbt_directory does not alter state when not needed."""
146+ op = DbtBaseOperator (
147+ task_id = "dbt_task" ,
148+ project_dir = "/path/to/project/" ,
149+ profiles_dir = "/path/to/profiles/" ,
150+ )
151+ op .state = "/absolute/path/to/target/"
152+
153+ with op .dbt_directory () as tmp_dir :
154+ assert Path (tmp_dir ).exists ()
155+ assert op .state == "/absolute/path/to/target/"
156+
157+
158+ @no_s3_hook
159+ def test_dbt_base_dbt_directory_with_no_state ():
160+ """Test dbt_directory does not alter state when not needed."""
161+ op = DbtBaseOperator (
162+ task_id = "dbt_task" ,
163+ project_dir = "/path/to/project/" ,
164+ profiles_dir = "/path/to/profiles/" ,
165+ )
166+
167+ with op .dbt_directory () as tmp_dir :
168+ assert Path (tmp_dir ).exists ()
169+ assert getattr (op , "state" , None ) is None
138170
139171
140172@no_s3_hook
141173def test_dbt_base_dbt_directory_changed_to_s3 (
142174 dbt_project_file , profiles_file , s3_bucket
143175):
144- """
145- Test dbt_directory yields a temporary directory and updates profile_dir and
146- profiles_dir when files have been pulled from s3
176+ """Test dbt_directory yields a temporary directory and updates attributes.
177+
178+ Certain attributes, like project_dir, profiles_dir, and state, need to be updated to
179+ work once a temporary directory has been created, in particular, when pulling from
180+ S3.
147181 """
148182 hook = DbtS3Hook ()
149183 bucket = hook .get_bucket (s3_bucket )
@@ -161,11 +195,16 @@ def test_dbt_base_dbt_directory_changed_to_s3(
161195 project_dir = f"s3://{ s3_bucket } /dbt/project/" ,
162196 profiles_dir = f"s3://{ s3_bucket } /dbt/profiles/" ,
163197 )
198+ op .state = "target/"
199+
164200 with op .dbt_directory () as tmp_dir :
165201 assert Path (tmp_dir ).exists ()
166202 assert Path (tmp_dir ).is_dir ()
203+
167204 assert op .project_dir == f"{ tmp_dir } /"
168205 assert op .profiles_dir == f"{ tmp_dir } /"
206+ assert op .state == f"{ tmp_dir } /target"
207+
169208 assert Path (f"{ tmp_dir } /profiles.yml" ).exists ()
170209 assert Path (f"{ tmp_dir } /profiles.yml" ).is_file ()
171210 assert Path (f"{ tmp_dir } /dbt_project.yml" ).exists ()
0 commit comments