Coverage for fio_wrapper/validators.py: 100%

45 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-11-16 11:50 +0100

1from typing import List, Optional 

2from fio_wrapper.exceptions import ( 

3 InvalidAdType, 

4 MaterialTickerInvalid, 

5 ExchangeTickerInvalid, 

6 CompanyCodeInvalid, 

7) 

8 

9 

10def validate_ticker(material_ticker: str) -> None: 

11 """Validates a material ticker 

12 

13 Args: 

14 material_ticker (str): Material ticker 

15 

16 Raises: 

17 MaterialTickerInvalid: Material ticker can't be None type 

18 MaterialTickerInvalid: Material ticker can't be longer than 3 characters 

19 MaterialTickerInvalid: Material ticker can't be shorter than 1 character 

20 MaterialTickerInvalid: Material ticker can't contain spaces 

21 """ 

22 if material_ticker is None: 

23 raise MaterialTickerInvalid("Material ticker can't be None type") 

24 if len(material_ticker) > 3: 

25 raise MaterialTickerInvalid("Material ticker can't be longer than 3 characters") 

26 

27 if len(material_ticker) < 1: 

28 raise MaterialTickerInvalid( 

29 "Material ticker can't be shorter than 1 characters" 

30 ) 

31 

32 if " " in material_ticker: 

33 raise MaterialTickerInvalid("Material ticker can't contain spaces") 

34 

35 

36def validate_exchange_code(exchange_code: str) -> None: 

37 if exchange_code is None: 

38 raise ExchangeTickerInvalid("Exchange code can't be None type") 

39 

40 if len(exchange_code) != 3: 

41 raise ExchangeTickerInvalid("Exchange code too short. Must have 3 characters") 

42 

43 # first 2 characters must be str 

44 if ( 

45 not isinstance(exchange_code[0], str) 

46 or not isinstance(exchange_code[1], str) 

47 or not exchange_code[2].isnumeric() 

48 ): 

49 raise ExchangeTickerInvalid( 

50 "Exchange code must begin with exchange ticker (e.g., AI)" 

51 ) 

52 

53 # last character must be int 

54 

55 

56def validate_company_code(company_code: str) -> None: 

57 if company_code == "" or company_code is None: 

58 raise CompanyCodeInvalid("Invalid company code. Can't be empty or None type") 

59 

60 if len(company_code) > 4: 

61 raise CompanyCodeInvalid("Invalid company code. Must be 1 to 4 characters") 

62 

63 

64def validate_localmarket_adtype(adtype: str) -> None: 

65 accepted_types: List[str] = [ 

66 "BUY", 

67 "BUYS", 

68 "BUYING", 

69 "SELL", 

70 "SELLS", 

71 "SELLING", 

72 "SHIP", 

73 "SHIPPING", 

74 ] 

75 

76 if adtype not in accepted_types: 

77 raise InvalidAdType("Invalid ad type") 

78 

79 

80def validate_planet_search_materials(materials: Optional[List[str]]) -> bool: 

81 if materials is None: 

82 return False 

83 

84 if len(materials) > 4: 

85 return False 

86 

87 for material in materials: 

88 # ensure material is str and length max 3 char 

89 if not isinstance(material, str) or len(material) == 0 or len(material) > 3: 

90 return False 

91 

92 return True 

93 

94 

95def validate_planet_search_distance_checks( 

96 distance_checks: Optional[List[str]], 

97) -> bool: 

98 if distance_checks is None: 

99 return False 

100 

101 if len(distance_checks) > 10: 

102 return False 

103 

104 # ensure all distances are str 

105 for distance in distance_checks: 

106 if not isinstance(distance, str): 

107 return False 

108 

109 return True