diff --git a/stocklake/nasdaqapi/preprocessor.py b/stocklake/nasdaqapi/preprocessor.py index 43ede5b..2f342b0 100644 --- a/stocklake/nasdaqapi/preprocessor.py +++ b/stocklake/nasdaqapi/preprocessor.py @@ -1,11 +1,23 @@ from typing import List from stocklake.core.base_preprocessor import BasePreprocessor -from stocklake.nasdaqapi.entities import RawNasdaqApiSymbolData +from stocklake.nasdaqapi.entities import NasdaqApiSymbolData, RawNasdaqApiSymbolData class NASDAQSymbolsPreprocessor(BasePreprocessor): - def process( - self, data: List[RawNasdaqApiSymbolData] - ) -> List[RawNasdaqApiSymbolData]: - return data + def process(self, data: List[RawNasdaqApiSymbolData]) -> List[NasdaqApiSymbolData]: + processed_data: List[NasdaqApiSymbolData] = [] + for data_dic in data: + _data: NasdaqApiSymbolData = { + "symbol": data_dic["symbol"], + "name": data_dic["name"], + "lastsale": float( + data_dic["lastsale"].replace("$", "").replace(",", "") + ), + "netchange": float(data_dic["netchange"]), + "pctchange": float(data_dic["pctchange"].replace("%", "")), + "marketCap": float(data_dic["marketCap"].replace(",", "")), + "url": data_dic["url"], + } + processed_data.append(_data) + return processed_data diff --git a/tests/nasdaqapi/test_preprocessor.py b/tests/nasdaqapi/test_preprocessor.py index e69de29..fbed8ed 100644 --- a/tests/nasdaqapi/test_preprocessor.py +++ b/tests/nasdaqapi/test_preprocessor.py @@ -0,0 +1,17 @@ +import json + +from stocklake.nasdaqapi.preprocessor import NASDAQSymbolsPreprocessor + +with open("./tests/nasdaqapi/sample_response.json") as f: + mock_raw_data = json.load(f)["data"]["rows"] + + +def test_process(): + preprocessor = NASDAQSymbolsPreprocessor() + data = preprocessor.process(mock_raw_data) + for data_dic in data: + for key, val in data_dic.items(): + if key in ["lastsale", "netchange", "pctchange", "marketCap"]: + assert isinstance(val, float) + else: + assert isinstance(val, str)