Coverage for src/scrilla/util/errors.py: 79%

51 statements  

« prev     ^ index     » next       coverage.py v6.4.2, created at 2022-07-18 18:14 +0000

1# This file is part of scrilla: https://github.com/chinchalinchin/scrilla. 

2 

3# scrilla is free software: you can redistribute it and/or modify 

4# it under the terms of the GNU General Public License version 3 

5# as published by the Free Software Foundation. 

6 

7# scrilla is distributed in the hope that it will be useful, 

8# but WITHOUT ANY WARRANTY; without even the implied warranty of 

9# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

10# GNU General Public License for more details. 

11 

12# You should have received a copy of the GNU General Public License 

13# along with scrilla. If not, see <https://www.gnu.org/licenses/> 

14# or <https://github.com/chinchalinchin/scrilla/blob/develop/main/LICENSE>. 

15 

16from typing import Tuple, Union 

17from datetime import date 

18from scrilla import settings, files 

19from scrilla.static import keys 

20from scrilla.util import dater 

21 

22 

23class InputValidationError(Exception): 

24 pass 

25 

26 

27class APIResponseError(Exception): 

28 pass 

29 

30 

31class APIKeyError(Exception): 

32 pass 

33 

34 

35class ConfigurationError(Exception): 

36 pass 

37 

38 

39class SampleSizeError(Exception): 

40 pass 

41 

42 

43class PriceError(Exception): 

44 pass 

45 

46 

47class ModelError(Exception): 

48 pass 

49 

50 

51class UnboundedIntegral(Exception): 

52 pass 

53 

54 

55def validate_asset_type(ticker: str, asset_type: Union[str, None] = None) -> str: 

56 """ 

57 

58 """ 

59 if asset_type is None: 

60 asset_type = files.get_asset_type(ticker) 

61 if asset_type is None: 61 ↛ 62line 61 didn't jump to line 62, because the condition on line 61 was never true

62 raise InputValidationError( 

63 f'{ticker} cannot be mapped to (crypto, equity) asset classes') 

64 return asset_type 

65 if asset_type in [keys.keys['ASSETS']['CRYPTO'], keys.keys['ASSETS']['EQUITY'], keys.keys['ASSETS']['BOND']]: 65 ↛ 67line 65 didn't jump to line 67, because the condition on line 65 was never false

66 return asset_type 

67 raise InputValidationError( 

68 f'{ticker} cannot be mapped to (crypto, equity) asset classes') 

69 

70 

71def validate_dates(start_date: Union[date, str, None], end_date: Union[date, str, None], asset_type: str) -> Tuple[date, date]: 

72 """ 

73 

74 """ 

75 

76 # if end date exists, make sure it is valid 

77 if end_date is not None: 77 ↛ 84line 77 didn't jump to line 84, because the condition on line 77 was never false

78 end_date = dater.validate_date(end_date) 

79 end_date = dater.truncate_future_from_date(end_date) 

80 if asset_type == keys.keys['ASSETS']['EQUITY']: 

81 end_date = dater.this_date_or_last_trading_date(end_date) 

82 # else create a sensible end date 

83 else: 

84 if asset_type == keys.keys['ASSETS']['CRYPTO']: 

85 end_date = dater.today() 

86 else: 

87 end_date = dater.get_last_trading_date() 

88 

89 # if start date exists, make sure it is valide 

90 if start_date is not None: 90 ↛ 102line 90 didn't jump to line 102, because the condition on line 90 was never false

91 start_date = dater.validate_date(start_date) 

92 if dater.is_future_date(start_date): 92 ↛ 94line 92 didn't jump to line 94, because the condition on line 92 was never true

93 # only invalid user input is if start date doesn't exist yet 

94 raise InputValidationError( 

95 f'Start date of {dater.to_string(start_date)} is greater than today') 

96 

97 if asset_type == keys.keys['ASSETS']['EQUITY']: 

98 start_date = dater.this_date_or_last_trading_date(start_date) 

99 

100 # else create a sensible start date 

101 else: 

102 if asset_type == keys.keys['ASSETS']['CRYPTO']: 

103 start_date = dater.decrement_date_by_days( 

104 end_date, settings.DEFAULT_ANALYSIS_PERIOD) 

105 else: 

106 start_date = dater.decrement_date_by_business_days( 

107 end_date, settings.DEFAULT_ANALYSIS_PERIOD) 

108 

109 # verify dates are in order if they exist after dates are possibly changed 

110 if end_date is not None and start_date is not None: 110 ↛ 114line 110 didn't jump to line 114, because the condition on line 110 was never false

111 start_date, end_date = dater.validate_order_of_dates( 

112 start_date, end_date) 

113 

114 return start_date, end_date