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 

2import pytest 

3from enum import Enum 

4import sdc_etl_libs 

5from sdc_etl_libs.sdc_data_exchange.SDCDataExchange import SDCDataExchange 

6from testfixtures import log_capture 

7 

8 

9@log_capture() 

10def test_api_source_no_data_returned_path(capture, mocker): 

11 """ 

12 Test that SDCDataExchange goes the 'NO-DATA-RETURNED' path when API call returns no data. 

13 """ 

14 

15 class SourceMock: 

16 def __init__(self): 

17 self.endpoint_items = {"source": None} 

18 self.exchange_type = "api" 

19 

20 def get_data(self): 

21 return None 

22 

23 source_mock = SourceMock() 

24 

25 mocker.patch('sdc_etl_libs.sdc_data_exchange.SDCDataExchange.SDCDataExchange.get_data_schema', return_value="{}") 

26 mocker.patch('sdc_etl_libs.sdc_data_exchange.SDCDataExchange.SDCDataExchange.sink_setup', return_value=None) 

27 mocker.patch('sdc_etl_libs.sdc_data_schema.schema_validation.SchemaValidation.validate_schema', 

28 return_value={"type": "api"}) 

29 mocker.patch('sdc_etl_libs.sdc_data_schema.schema_toolbox.SchemaToolbox.determine_dataframe_type_from_schema', 

30 return_value='pandas') 

31 mocker.patch('sdc_etl_libs.sdc_data_exchange.SDCDataExchangeEndpointFactory.SDCDataExchangeEndpointFactory.get_endpoint', 

32 return_value=source_mock) 

33 mocker.patch('sdc_etl_libs.sdc_file_helpers.SDCFileHelpers.SDCFileHelpers.is_file_endpoint_type', 

34 return_value=False) 

35 

36 exchange = SDCDataExchange("schema_name", "source_endpoint_tag", "sink_endpoint_tag") 

37 exchange.exchange_data() 

38 

39 capture.check_present( 

40 ('root', 'INFO', 'NO-DATA-RETURNED: No data was returned from the API call.') 

41 ) 

42 

43 

44@log_capture() 

45def test_api_source_no_data_returned_path(capture, mocker): 

46 """ 

47 Test that SDCDataExchange raises Exception when API endpoint runs into an issue returning data. 

48 """ 

49 

50 class SourceMock: 

51 def __init__(self): 

52 self.endpoint_items = {"source": None} 

53 self.exchange_type = "api" 

54 

55 def get_data(self): 

56 raise Exception("foo bar") 

57 

58 source_mock = SourceMock() 

59 

60 mocker.patch('sdc_etl_libs.sdc_data_exchange.SDCDataExchange.SDCDataExchange.get_data_schema', return_value="{}") 

61 mocker.patch('sdc_etl_libs.sdc_data_exchange.SDCDataExchange.SDCDataExchange.sink_setup', return_value=None) 

62 mocker.patch('sdc_etl_libs.sdc_data_schema.schema_validation.SchemaValidation.validate_schema', 

63 return_value={"endpoints": [{"type": "source", "tag": "source_endpoint_tag", "info": {"type": "snowflake"}}, 

64 {"type": "sink", "tag": "sink_endpoint_tag", "info": {"type": "api"}}]}) 

65 mocker.patch('sdc_etl_libs.sdc_data_schema.schema_toolbox.SchemaToolbox.get_endpoint_data_from_schema', 

66 return_value={"type": "sink", "tag": "sink_endpoint_tag", "info": {"type": "api", "opts": {"process_by_block": False}}}) 

67 mocker.patch('sdc_etl_libs.sdc_data_schema.schema_toolbox.SchemaToolbox.determine_dataframe_type_from_schema', 

68 return_value='pandas') 

69 mocker.patch( 

70 'sdc_etl_libs.sdc_data_exchange.SDCDataExchangeEndpointFactory.SDCDataExchangeEndpointFactory.get_endpoint', 

71 return_value=source_mock) 

72 mocker.patch('sdc_etl_libs.sdc_file_helpers.SDCFileHelpers.SDCFileHelpers.is_file_endpoint_type', 

73 return_value=False) 

74 

75 exchange = SDCDataExchange("schema_name", "source_endpoint_tag", "sink_endpoint_tag") 

76 

77 with pytest.raises(Exception) as excep_info: 

78 exchange.exchange_data() 

79 assert 'ERROR: Sourcing from api failed.. foo bar' in str(excep_info.value)