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

1import ast 

2import json 

3import os 

4 

5from sdc_etl_libs.sdc_data_schema.schema_toolbox import SchemaToolbox 

6from sdc_etl_libs.sdc_file_helpers.SDCFileFactory import SDCFileFactory 

7from sdc_etl_libs.sdc_file_helpers.TechnicalStandards.EDI.SDCEDIObject import \ 

8 SDCEDIObject 

9 

10data_schema_1 = json.loads( 

11 open(os.path.dirname(os.path.abspath(__file__)) + "/test-schema-edi-motor-carrier-freight-invoice.json").read()) 

12ep_schema_1 = SchemaToolbox.get_endpoint_data_from_schema(data_schema_1, "main_source", validate_=True) 

13 

14 

15def test_get_edi_file_as_dataframe(): 

16 """ 

17 Test loading an EDI file 

18 Check dataframe length to ensure that Dataframe contains data 

19 """ 

20 with open(os.path.dirname(os.path.abspath(__file__)) + "/edi/UPS-EDI-210.DAT", "rb") as fp: 

21 sdc_file = SDCFileFactory.get_file(data_schema_1, ep_schema_1, "UPS-EDI-210.DAT", "", fp) 

22 df = sdc_file.get_file_as_dataframe() 

23 assert len(df.df) == 101 

24 

25 

26def test_extract_values_from_dataframe(): 

27 """ 

28 Test extraction of specific values 

29 extract currency of second invoice 

30 """ 

31 with open(os.path.dirname(os.path.abspath(__file__)) + "/edi/UPS-EDI-210.DAT", "rb") as fp: 

32 sdc_file = SDCFileFactory.get_file(data_schema_1, ep_schema_1, "UPS-EDI-210.DAT", "", fp) 

33 sdc_df = sdc_file.get_file_as_dataframe() 

34 df = sdc_df.df 

35 # invoice_id = 2 

36 # currency is the first field in the C3 segment 

37 string_json = (df[(df.SEGMENT_ID == 'C3') & (df.INVOICE_ID == 2)]['DETAIL'].values[0]) 

38 currency = ast.literal_eval(string_json)['C301'] 

39 assert currency == 'USD' 

40 

41 

42def test_failure(): 

43 """ 

44 Notify an error and response a empty dataframe when is not posible read the EDI file 

45 """ 

46 with open(os.path.dirname(os.path.abspath(__file__)) + "/edi/WRONGFILE.DAT", "rb") as fp: 

47 sdc_file = SDCFileFactory.get_file(data_schema_1, ep_schema_1, "UPS-EDI-210.DAT", "", fp) 

48 sdc_df = sdc_file.get_file_as_dataframe() 

49 df = sdc_df.df 

50 assert df.empty 

51 

52 

53def test_EDI_parser_is_generic(): 

54 """ 

55 Verify SDCEDIObject class is able to process any EDI File. Not only UPS's files 

56 """ 

57 """ 

58 Test processing Volkswagen EDI File 

59 source: http://www.iconnect-corp.com/specs/vendors/volkswagen/edi%20specification%20desadv.pdf 

60 See section 2.5 

61 """ 

62 with open(os.path.dirname(os.path.abspath(__file__)) + "/edi/volkswagen.edi.sample.DAT", "rb") as fp: 

63 edi_file_obj = SDCEDIObject(fp, field_separator_="+", segment_separator_="'") 

64 unb = edi_file_obj._find_segment('UNB')[0] 

65 assert unb.DETAIL['UNB07'] == 'NA40' 

66 """ 

67 Test processing Fedex EDI File 

68 source: http://www.fedex.com/us/account/invhome/Air_Ground_X12-4060_New_Customer.pdf 

69 See section Sample Invoice File 

70 """ 

71 with open(os.path.dirname(os.path.abspath(__file__)) + "/edi/fedex.edi.sample.DAT", "rb") as fp: 

72 edi_file_obj = SDCEDIObject(fp, field_separator_="*", segment_separator_="\\n") 

73 b3 = edi_file_obj._find_segment('B3')[0] 

74 # Billing date must be 20050701 

75 assert b3.DETAIL['B306'] == '20050701'