@@ -703,6 +703,157 @@ def test_one_load_job_failed_after_waiting(self, sleep_mock):
703703
704704 sleep_mock .assert_called_once ()
705705
706+ def test_temporary_table_load_inherits_destination_time_partitioning (self ):
707+ destination = 'project1:dataset1.table1'
708+ partition = (destination , (0 , ['gs://bucket/file1' ]))
709+ job_reference = bigquery_api .JobReference (
710+ projectId = 'project1' , jobId = 'job_name1' )
711+ destination_table = bigquery_api .Table (
712+ timePartitioning = bigquery_api .TimePartitioning (type = 'DAY' ))
713+
714+ dofn = bqfl .TriggerLoadJobs (
715+ schema = _ELEMENTS_SCHEMA , test_client = mock .Mock (), temporary_tables = True )
716+ dofn .start_bundle ()
717+ dofn .bq_wrapper .get_table = mock .Mock (return_value = destination_table )
718+ dofn .bq_wrapper .perform_load_job = mock .Mock (return_value = job_reference )
719+
720+ list (dofn .process (partition , 'test_job' , pane_info = mock .Mock (index = 0 )))
721+
722+ load_call = dofn .bq_wrapper .perform_load_job .call_args .kwargs
723+ self .assertEqual (
724+ load_call ['additional_load_parameters' ]['timePartitioning' ],
725+ destination_table .timePartitioning )
726+ dofn .bq_wrapper .get_table .assert_called_once_with (
727+ project_id = 'project1' , dataset_id = 'dataset1' , table_id = 'table1' )
728+
729+ def test_temporary_table_load_inherits_destination_range_partitioning (self ):
730+ destination = 'project1:dataset1.table1'
731+ partition = (destination , (0 , ['gs://bucket/file1' ]))
732+ job_reference = bigquery_api .JobReference (
733+ projectId = 'project1' , jobId = 'job_name1' )
734+ destination_table = bigquery_api .Table (
735+ rangePartitioning = bigquery_api .RangePartitioning ())
736+
737+ dofn = bqfl .TriggerLoadJobs (
738+ schema = _ELEMENTS_SCHEMA , test_client = mock .Mock (), temporary_tables = True )
739+ dofn .start_bundle ()
740+ dofn .bq_wrapper .get_table = mock .Mock (return_value = destination_table )
741+ dofn .bq_wrapper .perform_load_job = mock .Mock (return_value = job_reference )
742+
743+ list (dofn .process (partition , 'test_job' , pane_info = mock .Mock (index = 0 )))
744+
745+ load_call = dofn .bq_wrapper .perform_load_job .call_args .kwargs
746+ self .assertEqual (
747+ load_call ['additional_load_parameters' ]['rangePartitioning' ],
748+ destination_table .rangePartitioning )
749+ dofn .bq_wrapper .get_table .assert_called_once_with (
750+ project_id = 'project1' , dataset_id = 'dataset1' , table_id = 'table1' )
751+
752+ def test_temporary_table_load_keeps_explicit_partitioning_parameters (self ):
753+ destination = 'project1:dataset1.table1'
754+ partition = (destination , (0 , ['gs://bucket/file1' ]))
755+ explicit_partitioning = {'timePartitioning' : {'type' : 'DAY' }}
756+ job_reference = bigquery_api .JobReference (
757+ projectId = 'project1' , jobId = 'job_name1' )
758+
759+ dofn = bqfl .TriggerLoadJobs (
760+ schema = _ELEMENTS_SCHEMA ,
761+ test_client = mock .Mock (),
762+ temporary_tables = True ,
763+ additional_bq_parameters = explicit_partitioning )
764+ dofn .start_bundle ()
765+ dofn .bq_wrapper .get_table = mock .Mock ()
766+ dofn .bq_wrapper .perform_load_job = mock .Mock (return_value = job_reference )
767+
768+ list (dofn .process (partition , 'test_job' , pane_info = mock .Mock (index = 0 )))
769+
770+ load_call = dofn .bq_wrapper .perform_load_job .call_args .kwargs
771+ self .assertEqual (
772+ load_call ['additional_load_parameters' ], explicit_partitioning )
773+ dofn .bq_wrapper .get_table .assert_not_called ()
774+
775+ def test_temporary_table_load_uses_cached_schema_with_explicit_partitioning (
776+ self ):
777+ destination = 'project1:dataset1.table1'
778+ partition = (destination , (0 , ['gs://bucket/file1' ]))
779+ explicit_partitioning = {'timePartitioning' : {'type' : 'DAY' }}
780+ job_reference = bigquery_api .JobReference (
781+ projectId = 'project1' , jobId = 'job_name1' )
782+ table_reference = bigquery_tools .parse_table_reference (destination )
783+ hashed_dest = bigquery_tools .get_hashable_destination (table_reference )
784+
785+ dofn = bqfl .TriggerLoadJobs (
786+ schema = None ,
787+ test_client = mock .Mock (),
788+ temporary_tables = True ,
789+ additional_bq_parameters = explicit_partitioning )
790+ dofn .start_bundle ()
791+ dofn .schema_cache [hashed_dest ] = _ELEMENTS_SCHEMA
792+ dofn .bq_wrapper .get_table = mock .Mock ()
793+ dofn .bq_wrapper .perform_load_job = mock .Mock (return_value = job_reference )
794+
795+ list (dofn .process (partition , 'test_job' , pane_info = mock .Mock (index = 0 )))
796+
797+ load_call = dofn .bq_wrapper .perform_load_job .call_args .kwargs
798+ self .assertEqual (load_call ['schema' ], _ELEMENTS_SCHEMA )
799+ self .assertEqual (
800+ load_call ['additional_load_parameters' ], explicit_partitioning )
801+ dofn .bq_wrapper .get_table .assert_not_called ()
802+
803+ def test_temporary_table_load_caches_destination_table_per_bundle (self ):
804+ destination = 'project1:dataset1.table1'
805+ first_partition = (destination , (0 , ['gs://bucket/file1' ]))
806+ second_partition = (destination , (1 , ['gs://bucket/file2' ]))
807+ job_reference = bigquery_api .JobReference (
808+ projectId = 'project1' , jobId = 'job_name1' )
809+ destination_table = bigquery_api .Table (
810+ timePartitioning = bigquery_api .TimePartitioning (type = 'DAY' ))
811+
812+ dofn = bqfl .TriggerLoadJobs (
813+ schema = _ELEMENTS_SCHEMA , test_client = mock .Mock (), temporary_tables = True )
814+ dofn .start_bundle ()
815+ dofn .bq_wrapper .get_table = mock .Mock (return_value = destination_table )
816+ dofn .bq_wrapper .perform_load_job = mock .Mock (return_value = job_reference )
817+
818+ list (
819+ dofn .process (first_partition , 'test_job' , pane_info = mock .Mock (index = 0 )))
820+ list (
821+ dofn .process (
822+ second_partition , 'test_job' , pane_info = mock .Mock (index = 1 )))
823+
824+ dofn .bq_wrapper .get_table .assert_called_once_with (
825+ project_id = 'project1' , dataset_id = 'dataset1' , table_id = 'table1' )
826+ load_call = dofn .bq_wrapper .perform_load_job .call_args .kwargs
827+ self .assertEqual (
828+ load_call ['additional_load_parameters' ]['timePartitioning' ],
829+ destination_table .timePartitioning )
830+
831+ def test_temporary_table_load_ignores_invalid_mock_partitioning_metadata (
832+ self ):
833+ destination = 'project1:dataset1.table1'
834+ partition = (destination , (0 , ['gs://bucket/file1' ]))
835+ job_reference = bigquery_api .JobReference (
836+ projectId = 'project1' , jobId = 'job_name1' )
837+ destination_table = mock .Mock ()
838+ destination_table .timePartitioning = mock .Mock ()
839+ destination_table .rangePartitioning = mock .Mock ()
840+
841+ dofn = bqfl .TriggerLoadJobs (
842+ schema = _ELEMENTS_SCHEMA , test_client = mock .Mock (), temporary_tables = True )
843+ dofn .start_bundle ()
844+ dofn .bq_wrapper .get_table = mock .Mock (return_value = destination_table )
845+ dofn .bq_wrapper .perform_load_job = mock .Mock (return_value = job_reference )
846+
847+ list (dofn .process (partition , 'test_job' , pane_info = mock .Mock (index = 0 )))
848+
849+ load_call = dofn .bq_wrapper .perform_load_job .call_args .kwargs
850+ self .assertNotIn (
851+ 'timePartitioning' , load_call ['additional_load_parameters' ])
852+ self .assertNotIn (
853+ 'rangePartitioning' , load_call ['additional_load_parameters' ])
854+ dofn .bq_wrapper .get_table .assert_called_once_with (
855+ project_id = 'project1' , dataset_id = 'dataset1' , table_id = 'table1' )
856+
706857 def test_multiple_partition_files (self ):
707858 destination = 'project1:dataset1.table1'
708859
0 commit comments