11import datetime as dt
2+ from numbers import Number
23from typing import Any , List , Optional , Type , Union
34
45import numpy as np
56import pandas as pd
67import pytest
7-
88from databento .common .enums import SType
9- from databento .common .parsing import (
10- optional_date_to_string ,
11- optional_datetime_to_string ,
12- optional_datetime_to_unix_nanoseconds ,
13- optional_symbols_list_to_string ,
14- optional_values_list_to_string ,
15- )
9+ from databento .common .parsing import optional_date_to_string
10+ from databento .common .parsing import optional_datetime_to_string
11+ from databento .common .parsing import optional_datetime_to_unix_nanoseconds
12+ from databento .common .parsing import optional_symbols_list_to_string
13+ from databento .common .parsing import optional_values_list_to_string
14+
1615
1716# Set the type to `Any` to disable mypy type checking. Used to test if functions
1817# will raise a `TypeError` when passed an incorrectly-typed argument.
@@ -47,37 +46,6 @@ def test_maybe_values_list_to_string_given_valid_inputs_returns_expected(
4746 assert result == expected
4847
4948
50- @pytest .mark .parametrize (
51- "symbols, stype, expected" ,
52- [
53- pytest .param ("NVDA" , SType .RAW_SYMBOL , "NVDA" ),
54- pytest .param (" nvda " , SType .RAW_SYMBOL , "NVDA" ),
55- pytest .param ("NVDA,amd" , SType .RAW_SYMBOL , "NVDA,AMD" ),
56- pytest .param ("NVDA,amd,NOC," , SType .RAW_SYMBOL , "NVDA,AMD,NOC" ),
57- pytest .param ("NVDA, amd,NOC, " , SType .RAW_SYMBOL , "NVDA,AMD,NOC" ),
58- pytest .param (["NVDA" , ["NOC" , "AMD" ]], SType .RAW_SYMBOL , "NVDA,NOC,AMD" ),
59- pytest .param (["NVDA" , "NOC,AMD" ], SType .RAW_SYMBOL , "NVDA,NOC,AMD" ),
60- pytest .param ("" , SType .RAW_SYMBOL , ValueError ),
61- pytest .param (["" ], SType .RAW_SYMBOL , ValueError ),
62- pytest .param (["NVDA" , "" ], SType .RAW_SYMBOL , ValueError ),
63- pytest .param (["NVDA" , ["" ]], SType .RAW_SYMBOL , ValueError ),
64- ],
65- )
66- def test_optional_symbols_list_to_string_native (
67- symbols : Optional [Union [List [int ], int ]],
68- stype : SType ,
69- expected : Union [str , Type [Exception ]],
70- ) -> None :
71- """
72- Test that str are allowed for SType.RAW_SYMBOL.
73- """
74- if isinstance (expected , str ):
75- assert optional_symbols_list_to_string (symbols , stype ) == expected
76- else :
77- with pytest .raises (expected ):
78- optional_symbols_list_to_string (symbols , stype )
79-
80-
8149def test_maybe_symbols_list_to_string_given_invalid_input_raises_type_error () -> None :
8250 # Arrange, Act, Assert
8351 with pytest .raises (TypeError ):
@@ -134,7 +102,7 @@ def test_optional_symbols_list_to_string_given_valid_inputs_returns_expected(
134102 ],
135103)
136104def test_optional_symbols_list_to_string_int (
137- symbols : Optional [Union [List [int ], int ]],
105+ symbols : Optional [Union [List [Number ], Number ]],
138106 stype : SType ,
139107 expected : Union [str , Type [Exception ]],
140108) -> None :
@@ -150,6 +118,42 @@ def test_optional_symbols_list_to_string_int(
150118 optional_symbols_list_to_string (symbols , stype )
151119
152120
121+ @pytest .mark .parametrize (
122+ "symbols, stype, expected" ,
123+ [
124+ pytest .param (np .byte (120 ), SType .INSTRUMENT_ID , "120" ),
125+ pytest .param (np .short (32_000 ), SType .INSTRUMENT_ID , "32000" ),
126+ pytest .param (
127+ [np .intc (12345 ), np .intc (67890 )], SType .INSTRUMENT_ID , "12345,67890" ,
128+ ),
129+ pytest .param (
130+ [np .int_ (12345 ), np .longlong (67890 )], SType .INSTRUMENT_ID , "12345,67890" ,
131+ ),
132+ pytest .param (
133+ [np .int_ (12345 ), np .longlong (67890 )], SType .INSTRUMENT_ID , "12345,67890" ,
134+ ),
135+ pytest .param (
136+ [np .int_ (12345 ), np .longlong (67890 )], SType .INSTRUMENT_ID , "12345,67890" ,
137+ ),
138+ ],
139+ )
140+ def test_optional_symbols_list_to_string_numpy (
141+ symbols : Optional [Union [List [Number ], Number ]],
142+ stype : SType ,
143+ expected : Union [str , Type [Exception ]],
144+ ) -> None :
145+ """
146+ Test that weird numpy types are allowed for SType.INSTRUMENT_ID.
147+ If integers are given for a different SType we expect
148+ a ValueError.
149+ """
150+ if isinstance (expected , str ):
151+ assert optional_symbols_list_to_string (symbols , stype ) == expected
152+ else :
153+ with pytest .raises (expected ):
154+ optional_symbols_list_to_string (symbols , stype )
155+
156+
153157@pytest .mark .parametrize (
154158 "symbols, stype, expected" ,
155159 [
@@ -167,7 +171,7 @@ def test_optional_symbols_list_to_string_int(
167171 ],
168172)
169173def test_optional_symbols_list_to_string_raw_symbol (
170- symbols : Optional [Union [List [int ], int ]],
174+ symbols : Optional [Union [List [Number ], Number ]],
171175 stype : SType ,
172176 expected : Union [str , Type [Exception ]],
173177) -> None :
0 commit comments