Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1""" 

2Standard Cyborg test module 

3""" 

4 

5import unittest 

6from unittest import TestCase, mock 

7 

8import pandas as pd 

9 

10 

11from sdc_etl_libs.api_helpers.apis.StandardCyborg.StandardCyborgDataPipeline import StandardCyborgDataPipeline 

12 

13 

14# pylint: disable=R0201 

15class TestStandarCyborg(TestCase): 

16 """ 

17 Test suite for Module Standard Cyborg 

18 """ 

19 

20 def setUp(self): 

21 """ 

22 Code block executed once on instance creation time 

23 """ 

24 

25 claimant_exchange_mock = mock.Mock() 

26 snowflake_dbhandle_mock = mock.Mock() 

27 origin_file_list_mock = mock.Mock() 

28 destination_file_list_mock = mock.Mock() 

29 

30 self.cyborg_client = StandardCyborgDataPipeline( 

31 claimant_exchange_=claimant_exchange_mock, 

32 case_ids_sql_="case_ids_sql", 

33 snowflake_dbhandle_=snowflake_dbhandle_mock, 

34 file_list_sql_="file_list_sql", 

35 origin_file_list_=origin_file_list_mock, 

36 destination_file_list_=destination_file_list_mock, 

37 include_modification_cases_=True, 

38 only_active_cases_=False) 

39 

40 claiming_data = [["1@test.com", "data/1234567890.0001", "2020-12-31T00:00:00Z"], 

41 ["2@test.com", "data/1234567890.0002", "2020-12-31T00:00:00Z"], 

42 ["3@test.com", "data/1234567890.0003", "2020-12-31T00:00:00Z"]] 

43 df_claiming_data = pd.DataFrame(claiming_data, columns=["metadata_email", "path", "create_dt"]) 

44 pd_read_sql_mock = mock.Mock(return_value=df_claiming_data) 

45 

46 file_mock = mock.Mock() 

47 file_mock.read = mock.Mock(return_value="") 

48 

49 @mock.patch("builtins.open", mock.Mock(return_value=file_mock)) 

50 @mock.patch("sdc_etl_libs.sdc_file_helpers.SDCFileHelpers.SDCFileHelpers.get_file_path", 

51 mock.Mock(return_value=None)) 

52 @mock.patch("pandas.read_sql", pd_read_sql_mock) 

53 def test_get_claimant_input_data_snowflake(self): 

54 result_dict = { 

55 '1@test.com': { 

56 'path': 'data/1234567890.0001', 

57 'timestamp': '2020-12-31T00:00:00Z' 

58 }, 

59 '2@test.com': { 

60 'path': 'data/1234567890.0002', 

61 'timestamp': '2020-12-31T00:00:00Z' 

62 }, 

63 '3@test.com': { 

64 'path': 'data/1234567890.0003', 

65 'timestamp': '2020-12-31T00:00:00Z' 

66 } 

67 } 

68 

69 self.cyborg_client.get_claimant_input_data_snowflake() 

70 self.assertDictEqual(self.cyborg_client.serra_info, result_dict) 

71 

72 @mock.patch("builtins.open", mock.Mock(return_value=file_mock)) 

73 @mock.patch("sdc_etl_libs.sdc_file_helpers.SDCFileHelpers.SDCFileHelpers.get_file_path", 

74 mock.Mock(return_value=None)) 

75 def test_get_cases_ids(self): 

76 case_ids_data = [["12345"], ["67890"]] 

77 self.cyborg_client.snowflake_dbhandle.get_results = mock.Mock(return_value=case_ids_data) 

78 self.cyborg_client.snowflake_dbhandle.execute_query = mock.Mock() 

79 self.cyborg_client.etl_results_log = {"TEST": []} 

80 self.cyborg_client.processing_file_name = "TEST" 

81 

82 self.cyborg_client.get_cases_ids() 

83 self.assertEqual(self.cyborg_client.case_ids, ["12345", "67890"]) 

84 

85 pd_read_sql_mock = mock.Mock(return_value=pd.DataFrame()) 

86 

87 @mock.patch("sdc_etl_libs.sdc_file_helpers.SDCFileHelpers.SDCFileHelpers.get_file_path", 

88 mock.Mock(return_value=None)) 

89 @mock.patch("builtins.open", mock.Mock(return_value=file_mock)) 

90 @mock.patch("pandas.read_sql", pd_read_sql_mock) 

91 def test_load_retry_cases_information(self): 

92 "Test that the result should have the columns expected" 

93 self.cyborg_client.load_retry_cases_information() 

94 columns = ['METADATA_EMAIL', 'RETRIES', "INSERT_DT"] 

95 self.assertListEqual(list(self.cyborg_client.retry_cases.columns), columns) 

96 

97 process_df_mock = mock.Mock() 

98 

99 @mock.patch("sdc_etl_libs.sdc_data_schema.schema_toolbox.SchemaToolbox.get_data_schema_from_file", 

100 mock.Mock(return_value=None)) 

101 @mock.patch("sdc_etl_libs.sdc_dataframe.Dataframe.Dataframe.__init__", mock.Mock(return_value=None)) 

102 @mock.patch("sdc_etl_libs.sdc_dataframe.Dataframe.Dataframe.process_df") 

103 @mock.patch("sdc_etl_libs.sdc_dataframe.Dataframe.Dataframe.write_dataframe_to_database", mock.Mock()) 

104 def test_generate_retries_information(self, process_df_mock): 

105 retry_data = [["1@test.com", 1, "2020-12-31T00:00:00Z"], ["2@test.com", 14, "2020-12-31T00:00:00Z"]] 

106 df_retry_data = pd.DataFrame(retry_data, columns=['METADATA_EMAIL', 'RETRIES', "INSERT_DT"]) 

107 self.cyborg_client.emails_for_retry = ["1@test.com", "2@test.com", "3@test.com"] 

108 self.cyborg_client.retry_cases = df_retry_data 

109 

110 result_data = [["1@test.com", 2], ["2@test.com", 404], ["3@test.com", 1]] 

111 result_df = pd.DataFrame(result_data, columns=["METADATA_EMAIL", "RETRIES"]) 

112 

113 self.cyborg_client.generate_retries_information() 

114 

115 assert process_df_mock.call_args[0][0][["METADATA_EMAIL", "RETRIES"]].equals(result_df) 

116 

117 def test_copy_case_files(self): 

118 pass 

119 

120 

121if __name__ == "__main__": 

122 unittest.main()