o
    }oi<                     @   s   d dl Z d dlZd dlmZmZ d dlmZ d dlmZ d dlZd dl	m
Z
mZmZmZmZmZmZmZmZmZ G dd dZdS )    N)datedatetime)Decimal)mock)
CoercionErrorRequiredSettingMissingErrorget_envboolget_envdateget_envdatetimeget_envdecimalget_envdictget_envfloat
get_envintget_envlistc                   @   s|  e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Zd;d< Z d=d> Z!d?d@ Z"dAdB Z#dCdD Z$dEdF Z%dGdH Z&dIdJ Z'dKdL Z(dMdN Z)dOdP Z*dQdR Z+dSdT Z,dUdV Z-dWdX Z.dYdZ Z/d[d\ Z0d]S )^TestEnvironmentVariableParsingc                 C   J   t jtjddi tddksJ W d   dS 1 sw   Y  dS )zITest that get_envint returns the integer value from environment variable.TEST_INT42*   Nr   patchdictosenvironr   self r   T/home/ubuntu/.local/lib/python3.10/site-packages/tests/utils/test_env_var_parsing.py!test_get_envint_returns_int_value'      "z@TestEnvironmentVariableParsing.test_get_envint_returns_int_valuec                 C      t dddks	J dS )zGTest that get_envint returns the default value when env var is missing.NONEXISTENT_INT{   N)r   r   r   r   r   test_get_envint_with_default,      z;TestEnvironmentVariableParsing.test_get_envint_with_defaultc                 C   8   t t td W d   dS 1 sw   Y  dS )zJTest that get_envint raises an exception when required env var is missing.r!   N)pytestraisesr   r   r   r   r   r    test_get_envint_required_missing0      
"z?TestEnvironmentVariableParsing.test_get_envint_required_missingc              	   C   |   t jtjddi* tt td W d   n1 sw   Y  W d   dS W d   dS 1 s7w   Y  dS )zCTest that get_envint raises a CoercionError for non-integer values.r   z
not-an-intN)	r   r   r   r   r   r&   r'   r   r   r   r   r   r   test_get_envint_coercion_error5      
"z=TestEnvironmentVariableParsing.test_get_envint_coercion_errorc                 C   r   )z9Test that get_envint correctly handles negative integers.r   z-42iNr   r   r   r   r   test_get_envint_negative_value;   r   z=TestEnvironmentVariableParsing.test_get_envint_negative_valuec                 C   r   )zITest that get_envfloat returns the float value from environment variable.
TEST_FLOAT3.14Q	@Nr   r   r   r   r   r   r   r   r   r   %test_get_envfloat_returns_float_value@   r   zDTestEnvironmentVariableParsing.test_get_envfloat_returns_float_valuec                 C   s\   t jtjddi td}|dksJ t|tsJ W d   dS 1 s'w   Y  dS )zDTest that get_envfloat correctly converts integer strings to floats.r.   r   g      E@N)r   r   r   r   r   r   
isinstancefloatr   valuer   r   r   %test_get_envfloat_with_integer_stringE   s
   "zDTestEnvironmentVariableParsing.test_get_envfloat_with_integer_stringc                 C   r    )zITest that get_envfloat returns the default value when env var is missing.NONEXISTENT_FLOATr0   N)r   r   r   r   r   test_get_envfloat_with_defaultL   r$   z=TestEnvironmentVariableParsing.test_get_envfloat_with_defaultc                 C   r%   )zLTest that get_envfloat raises an exception when required env var is missing.r8   N)r&   r'   r   r   r   r   r   r   "test_get_envfloat_required_missingP   r)   zATestEnvironmentVariableParsing.test_get_envfloat_required_missingc              	   C   r*   )zCTest that get_envfloat raises a CoercionError for non-float values.r.   znot-a-floatN)	r   r   r   r   r   r&   r'   r   r   r   r   r   r    test_get_envfloat_coercion_errorU   r,   z?TestEnvironmentVariableParsing.test_get_envfloat_coercion_errorc                 C   r   )z=Test that get_envfloat correctly handles scientific notation.r.   z1.23e-4gFn1 ?Nr1   r   r   r   r   %test_get_envfloat_scientific_notation[   r   zDTestEnvironmentVariableParsing.test_get_envfloat_scientific_notationc                 C   r   )z9Test that get_envfloat correctly handles negative values.r.   -3.14gQ	Nr1   r   r   r   r    test_get_envfloat_negative_value`   r   z?TestEnvironmentVariableParsing.test_get_envfloat_negative_valuec              	   C   Z   g d}|D ]$}t jtjd|i tddu sJ W d   n1 s%w   Y  qdS )z=Test that get_envbool returns True for various truthy values.)trueTrueTRUE1yesYesYESyYtT	TEST_BOOLTNr   r   r   r   r   r   )r   true_valuesvalr   r   r   test_get_envbool_true_valuesf      z;TestEnvironmentVariableParsing.test_get_envbool_true_valuesc              	   C   r?   )z=Test that get_envbool returns False for various falsy values.)falseFalseFALSE0noNoNOnNfFnoneNoneNONErK   FNrL   )r   false_valuesrN   r   r   r   test_get_envbool_false_valuesm   rP   z<TestEnvironmentVariableParsing.test_get_envbool_false_valuesc                 C   s(   t dddu s	J t dddu sJ dS )zHTest that get_envbool returns the default value when env var is missing.NONEXISTENT_BOOLTFN)r   r   r   r   r   test_get_envbool_with_defaultt      z<TestEnvironmentVariableParsing.test_get_envbool_with_defaultc                 C   r%   )zKTest that get_envbool raises an exception when required env var is missing.ra   N)r&   r'   r   r   r   r   r   r   !test_get_envbool_required_missingy   r)   z@TestEnvironmentVariableParsing.test_get_envbool_required_missingc                 C   sJ   t jtjddi tddu sJ W d   dS 1 sw   Y  dS )z=Test that get_envbool interprets non-standard values as True.rK   zsomething-elseTNrL   r   r   r   r   "test_get_envbool_non_boolean_value~   r   zATestEnvironmentVariableParsing.test_get_envbool_non_boolean_valuec                 C   `   t jtjddi td}|tdksJ t|tsJ W d   dS 1 s)w   Y  dS )zMTest that get_envdecimal returns the Decimal value from environment variable.TEST_DECIMALr/   Nr   r   r   r   r   r   r   r3   r5   r   r   r   )test_get_envdecimal_returns_decimal_value   
   "zHTestEnvironmentVariableParsing.test_get_envdecimal_returns_decimal_valuec                 C   rf   )zHTest that get_envdecimal correctly converts integer strings to Decimals.rg   r   Nrh   r5   r   r   r   'test_get_envdecimal_with_integer_string   rj   zFTestEnvironmentVariableParsing.test_get_envdecimal_with_integer_stringc                 C   s   t d}td||ksJ dS )zKTest that get_envdecimal returns the default value when env var is missing.r/   NONEXISTENT_DECIMALN)r   r   r   default_valuer   r   r    test_get_envdecimal_with_default      z?TestEnvironmentVariableParsing.test_get_envdecimal_with_defaultc                 C   r%   )zNTest that get_envdecimal raises an exception when required env var is missing.rl   N)r&   r'   r   r   r   r   r   r   $test_get_envdecimal_required_missing   r)   zCTestEnvironmentVariableParsing.test_get_envdecimal_required_missingc              	   C   r*   )zGTest that get_envdecimal raises a CoercionError for non-decimal values.rg   znot-a-decimalN)	r   r   r   r   r   r&   r'   r   r   r   r   r   r   "test_get_envdecimal_coercion_error   r,   zATestEnvironmentVariableParsing.test_get_envdecimal_coercion_errorc                 C   sN   t jtjddi tdtdksJ W d   dS 1 s w   Y  dS )z;Test that get_envdecimal correctly handles negative values.rg   r=   Nr   r   r   r   r   r   r   r   r   r   r   "test_get_envdecimal_negative_value   s   "zATestEnvironmentVariableParsing.test_get_envdecimal_negative_valuec                 C   sR   t jtjddi td}|tdksJ W d   dS 1 s"w   Y  dS )z9Test that get_envdecimal preserves high precision values.rg   z*3.1415926535897932384626433832795028841971Nrs   r5   r   r   r   "test_get_envdecimal_high_precision      "zATestEnvironmentVariableParsing.test_get_envdecimal_high_precisionc                 C   sd   t jtjddi td}|tdddksJ t|ts J W d   dS 1 s+w   Y  dS )zGTest that get_envdate returns the date value from environment variable.	TEST_DATE
2023-05-15        N)r   r   r   r   r   r	   r   r3   r5   r   r   r   #test_get_envdate_returns_date_value   s
   "zBTestEnvironmentVariableParsing.test_get_envdate_returns_date_valuec              	   C   s   t dddt dddt dddt dddt dddd}| D ]&\}}tjtjd|i td|ks7J W d   n1 sAw   Y  q dS )z5Test that get_envdate handles different date formats.ry   rz   r{   )rx   z
15-05-2023z
05/15/2023z15 May 2023zMay 15, 2023rw   N)r   itemsr   r   r   r   r   r	   )r   date_formatsdate_strexpected_dater   r   r   'test_get_envdate_with_different_formats   s   




zFTestEnvironmentVariableParsing.test_get_envdate_with_different_formatsc                 C   s"   t ddd}td||ksJ dS )zHTest that get_envdate returns the default value when env var is missing.ry   rz   r{   NONEXISTENT_DATEN)r   r	   rm   r   r   r   test_get_envdate_with_default   s   z<TestEnvironmentVariableParsing.test_get_envdate_with_defaultc                 C   r%   )zKTest that get_envdate raises an exception when required env var is missing.r   N)r&   r'   r   r	   r   r   r   r   !test_get_envdate_required_missing   r)   z@TestEnvironmentVariableParsing.test_get_envdate_required_missingc              	   C   r*   )zETest that get_envdate raises a CoercionError for invalid date values.rw   z
not-a-dateN)	r   r   r   r   r   r&   r'   r   r	   r   r   r   r   test_get_envdate_coercion_error   r,   z>TestEnvironmentVariableParsing.test_get_envdate_coercion_errorc              	   C   sj   t jtjddi! td}|tddddddksJ t|ts#J W d	   d	S 1 s.w   Y  d	S )
zOTest that get_envdatetime returns the datetime value from environment variable.TEST_DATETIME2023-05-15T14:30:45ry   rz   r{         -   N)r   r   r   r   r   r
   r   r3   r5   r   r   r   +test_get_envdatetime_returns_datetime_value   s
   "zJTestEnvironmentVariableParsing.test_get_envdatetime_returns_datetime_valuec                 C   s   t ddddddt ddddddt ddddddt ddddddt ddddddd}| D ]&\}}tjtjd|i td|ksFJ W d	   n1 sPw   Y  q/d	S )
z=Test that get_envdatetime handles different datetime formats.ry   rz   r{   r   r   r   )r   z2023-05-15 14:30:45z15-05-2023 14:30:45z05/15/2023 14:30:45z15 May 2023 14:30:45r   N)r   r}   r   r   r   r   r   r
   )r   datetime_formatsdatetime_strexpected_datetimer   r   r   +test_get_envdatetime_with_different_formats   s   zJTestEnvironmentVariableParsing.test_get_envdatetime_with_different_formatsc                 C   s(   t dddddd}td||ksJ dS )	zLTest that get_envdatetime returns the default value when env var is missing.ry   rz   r{   r   r   r   NONEXISTENT_DATETIMEN)r   r
   rm   r   r   r   !test_get_envdatetime_with_default   rc   z@TestEnvironmentVariableParsing.test_get_envdatetime_with_defaultc                 C   r%   )zOTest that get_envdatetime raises an exception when required env var is missing.r   N)r&   r'   r   r
   r   r   r   r   %test_get_envdatetime_required_missing   r)   zDTestEnvironmentVariableParsing.test_get_envdatetime_required_missingc              	   C   r*   )zMTest that get_envdatetime raises a CoercionError for invalid datetime values.r   znot-a-datetimeN)	r   r   r   r   r   r&   r'   r   r
   r   r   r   r   #test_get_envdatetime_coercion_error   r,   zBTestEnvironmentVariableParsing.test_get_envdatetime_coercion_errorc                 C   s   t jtjddi7 td}|jdksJ |jdksJ |jdks$J |j	dks+J |j
dks2J |jdks9J W d	   d	S 1 sDw   Y  d	S )
z7Test that get_envdatetime handles timezone information.r   z2023-05-15T14:30:45+0200ry   rz   r{   r   r   r   N)r   r   r   r   r   r
   yearmonthdayhourminutesecond)r   dtr   r   r   "test_get_envdatetime_with_timezone   s   "zATestEnvironmentVariableParsing.test_get_envdatetime_with_timezonec                 C   s`   t jtjddi td}|g dksJ t|tsJ W d   dS 1 s)w   Y  dS )zGTest that get_envlist returns the list value from environment variable.	TEST_LISTzitem1 item2 item3item1item2item3N)r   r   r   r   r   r   r3   listr5   r   r   r   #test_get_envlist_returns_list_value  rj   zBTestEnvironmentVariableParsing.test_get_envlist_returns_list_valuec                 C   sV   t jtjddi tddd}|g dksJ W d   dS 1 s$w   Y  dS )z0Test that get_envlist handles custom separators.r   zitem1,item2,item3,)	separatorr   Nr   r   r   r   r   r   r5   r   r   r   &test_get_envlist_with_custom_separator  s   "zETestEnvironmentVariableParsing.test_get_envlist_with_custom_separatorc                 C   s   ddg}t d||ksJ dS )zHTest that get_envlist returns the default value when env var is missing.default1default2NONEXISTENT_LISTN)r   rm   r   r   r   test_get_envlist_with_default  rp   z<TestEnvironmentVariableParsing.test_get_envlist_with_defaultc                 C   r%   )zKTest that get_envlist raises an exception when required env var is missing.r   N)r&   r'   r   r   r   r   r   r   !test_get_envlist_required_missing  r)   z@TestEnvironmentVariableParsing.test_get_envlist_required_missingc                 C   sP   t jtjddi td}|dgksJ W d   dS 1 s!w   Y  dS )z,Test that get_envlist handles empty strings.r    Nr   r5   r   r   r   test_get_envlist_empty_string  s   "z<TestEnvironmentVariableParsing.test_get_envlist_empty_stringc                 C   sR   t jtjddi td}|g dksJ W d   dS 1 s"w   Y  dS )z9Test that get_envlist correctly splits words with spaces.r   z word1 "phrase with spaces" word3)word1z"phrasewithzspaces"word3Nr   r5   r   r   r   test_get_envlist_multiple_words"  rv   z>TestEnvironmentVariableParsing.test_get_envlist_multiple_wordsc                 C   sn   dddd}t jtjdt|i td}||ksJ t|ts%J W d   dS 1 s0w   Y  dS )zGTest that get_envdict returns the dict value from environment variable.value1r   T)key1key2key3	TEST_DICTN)	r   r   r   r   r   jsondumpsr   r3   )r   	test_dictr6   r   r   r   #test_get_envdict_returns_dict_value)  s   "zBTestEnvironmentVariableParsing.test_get_envdict_returns_dict_valuec                 C   s   ddi}t d||ksJ dS )zHTest that get_envdict returns the default value when env var is missing.default_keyrn   NONEXISTENT_DICTN)r   rm   r   r   r   test_get_envdict_with_default1  rp   z<TestEnvironmentVariableParsing.test_get_envdict_with_defaultc                 C   r%   )zKTest that get_envdict raises an exception when required env var is missing.r   N)r&   r'   r   r   r   r   r   r   !test_get_envdict_required_missing6  r)   z@TestEnvironmentVariableParsing.test_get_envdict_required_missingc              	   C   r*   )z>Test that get_envdict raises a CoercionError for invalid JSON.r   znot-valid-jsonN)	r   r   r   r   r   r&   r'   r   r   r   r   r   r   test_get_envdict_coercion_error;  r,   z>TestEnvironmentVariableParsing.test_get_envdict_coercion_errorc                 C   sr   dddg ddg ddd}t jtjd	t|i td	}||ks'J W d
   d
S 1 s2w   Y  d
S )z:Test that get_envdict handles complex nested dictionaries.r6   r   T)         r   )   rz      )r   r   )stringnumberbooleanr   nestedr   N)r   r   r   r   r   r   r   r   )r   complex_dictr6   r   r   r   test_get_envdict_complex_dictA  s   "z<TestEnvironmentVariableParsing.test_get_envdict_complex_dictN)1__name__
__module____qualname__r   r#   r(   r+   r-   r2   r7   r9   r:   r;   r<   r>   rO   r`   rb   rd   re   ri   rk   ro   rq   rr   rt   ru   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   %   s^    r   )r   r   r   r   decimalr   unittestr   r&   nemo.utils.env_var_parsingr   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   <module>   s   0