def test_write_to_csv(): # SETUP example_rows = [ {"timestamp": "2019-06-08", "open": "101.0924", "high": "101.9500", "low": "100.5400", "close": "101.6300", "volume": "22165128"}, {"timestamp": "2019-06-07", "open": "102.6500", "high": "102.6900", "low": "100.3800", "close": "100.8800", "volume": "28232197"}, {"timestamp": "2019-06-06", "open": "102.4800", "high": "102.6000", "low": "101.9000", "close": "102.4900", "volume": "21122917"}, {"timestamp": "2019-06-05", "open": "102.0000", "high": "102.3300", "low": "101.5300", "close": "102.1900", "volume": "23514402"}, {"timestamp": "2019-06-04", "open": "101.2600", "high": "101.8600", "low": "100.8510", "close": "101.6700", "volume": "27281623"}, {"timestamp": "2019-06-01", "open": '99.2798', "high": "100.8600", "low": "99.1700", "close": "100.7900", "volume": "28655624"} ] csv_filepath = os.path.join(os.path.dirname(__file__), "example_reports", "temp_prices1.csv") if os.path.isfile(csv_filepath): os.remove(csv_filepath) assert os.path.isfile(csv_filepath) == False # just making sure the test was setup properly # INVOCATION result = write_to_csv(example_rows, csv_filepath) # EXPECTATIONS assert result == True assert os.path.isfile(csv_filepath) == True with open(csv_filepath, "r") as csv_file: reader = csv.DictReader(csv_file) assert [row for row in reader] == example_rows
def test_write_csv(): example_data = [{ 'timestamp': '2019-05-02', 'open': '209.8400', 'high': '212.6500', 'low': '208.1300', 'close': '209.1500', 'volume': '29368219' }, { 'timestamp': '2019-05-01', 'open': '209.8800', 'high': '215.3100', 'low': '209.2300', 'close': '210.5200', 'volume': '63420533' }, { 'timestamp': '2019-04-30', 'open': '203.0600', 'high': '203.4000', 'low': '199.1100', 'close': '200.6700', 'volume': '46534923' }, { 'timestamp': '2019-04-29', 'open': '204.4000', 'high': '205.9700', 'low': '203.8600', 'close': '204.6100', 'volume': '22204716' }] csv_file_path = os.path.join(os.path.dirname(__file__), "sample_data", "sample.csv") result = write_to_csv(csv_file_path, example_data) assert result == True assert os.path.isfile(csv_file_path) == True
def test_write_to_csv(): # SETUP example_rows = [{ "timestamp": "2019-06-08", "open": "101.0924", "high": "101.9500", "low": "100.5400", "close": "101.6300", "volume": "22165128" }, { "timestamp": "2019-06-07", "open": "102.6500", "high": "102.6900", "low": "100.3800", "close": "100.8800", "volume": "28232197" }, { "timestamp": "2019-06-06", "open": "102.4800", "high": "102.6000", "low": "101.9000", "close": "102.4900", "volume": "21122917" }, { "timestamp": "2019-06-05", "open": "102.0000", "high": "102.3300", "low": "101.5300", "close": "102.1900", "volume": "23514402" }, { "timestamp": "2019-06-04", "open": "101.2600", "high": "101.8600", "low": "100.8510", "close": "101.6700", "volume": "27281623" }, { "timestamp": "2019-06-01", "open": '99.2798', "high": "100.8600", "low": "99.1700", "close": "100.7900", "volume": "28655624" }] csv_filepath = os.path.join(os.path.dirname(__file__), "example_reports", "temp_prices.csv") if os.path.isfile(csv_filepath): os.remove(csv_filepath) assert os.path.isfile(csv_filepath) == False result = write_to_csv(example_rows, csv_filepath) assert result == True assert os.path.isfile(csv_filepath) == True
def test_write_to_csv(): test_rows = [{ "timestamp": "2019-06-08", "open": "101.0924", "high": "101.9500", "low": "100.5400", "close": "101.6300", "volume": "22165128" }, { "timestamp": "2019-06-07", "open": "102.6500", "high": "102.6900", "low": "100.3800", "close": "100.8800", "volume": "28232197" }, { "timestamp": "2019-06-06", "open": "102.4800", "high": "102.6000", "low": "101.9000", "close": "102.4900", "volume": "21122917" }, { "timestamp": "2019-06-05", "open": "102.0000", "high": "102.3300", "low": "101.5300", "close": "102.1900", "volume": "23514402" }, { "timestamp": "2019-06-04", "open": "101.2600", "high": "101.8600", "low": "100.8510", "close": "101.6700", "volume": "27281623" }, { "timestamp": "2019-06-01", "open": '99.2798', "high": "100.8600", "low": "99.1700", "close": "100.7900", "volume": "28655624" }] #referenced and adapted row data from Prof. Rossetti's solution csv_file_path = os.path.join(os.path.dirname(__file__), "test_prices.csv") #TODO: add test_reports and test_prices csv files to test on if os.path.isfile(csv_file_path): os.remove(csv_file_path) result = write_to_csv(test_rows, csv_file_path) assert result == True assert os.path.isfile(csv_file_path) == True
def test_write_to_csv(): #adapted from: https://github.com/s2t2/robo-advisor-screencast/blob/v3-testing/test/advisor_test.py # SETUP example_rows = [{ "timestamp": "2019-06-08", "open": "101.0924", "high": "101.9500", "low": "100.5400", "close": "101.6300", "volume": "22165128" }, { "timestamp": "2019-06-07", "open": "102.6500", "high": "102.6900", "low": "100.3800", "close": "100.8800", "volume": "28232197" }, { "timestamp": "2019-06-06", "open": "102.4800", "high": "102.6000", "low": "101.9000", "close": "102.4900", "volume": "21122917" }, { "timestamp": "2019-06-05", "open": "102.0000", "high": "102.3300", "low": "101.5300", "close": "102.1900", "volume": "23514402" }, { "timestamp": "2019-06-04", "open": "101.2600", "high": "101.8600", "low": "100.8510", "close": "101.6700", "volume": "27281623" }, { "timestamp": "2019-06-01", "open": '99.2798', "high": "100.8600", "low": "99.1700", "close": "100.7900", "volume": "28655624" }] csv_filepath = os.path.join(os.path.dirname(__file__), "example_reports", "prices2.csv") if os.path.isfile(csv_filepath): os.remove(csv_filepath) assert os.path.isfile( csv_filepath) == False # just making sure the test was setup properly # INVOCATION result = write_to_csv(example_rows, csv_filepath) # EXPECTATIONS assert result == True assert os.path.isfile(csv_filepath) == True
def test_write_to_csv(): """ Tests whether write_to_csv effectively writes daily stock pricing data into "prices.csv" """ # SETUP example_rows = [{ "timestamp": "2019-06-08", "open": "101.0924", "high": "101.9500", "low": "100.5400", "close": "101.6300", "volume": "22165128" }, { "timestamp": "2019-06-07", "open": "102.6500", "high": "102.6900", "low": "100.3800", "close": "100.8800", "volume": "28232197" }, { "timestamp": "2019-06-06", "open": "102.4800", "high": "102.6000", "low": "101.9000", "close": "102.4900", "volume": "21122917" }, { "timestamp": "2019-06-05", "open": "102.0000", "high": "102.3300", "low": "101.5300", "close": "102.1900", "volume": "23514402" }, { "timestamp": "2019-06-04", "open": "101.2600", "high": "101.8600", "low": "100.8510", "close": "101.6700", "volume": "27281623" }, { "timestamp": "2019-06-01", "open": '99.2798', "high": "100.8600", "low": "99.1700", "close": "100.7900", "volume": "28655624" }] csv_filepath = os.path.join(os.path.dirname(__file__), "example_reports", "temp_prices.csv") if os.path.isfile(csv_filepath): os.remove(csv_filepath) assert os.path.isfile( csv_filepath) == False # just making sure the test was setup properly # INVOKE result = write_to_csv(example_rows, csv_filepath) # RESULT assert result == True assert os.path.isfile(csv_filepath) == True
def test_write_to_csv(): example_rows = [{ "timestamp": "2019-06-08", "open": "101.0924", "high": "101.9500", "low": "100.5400", "close": "101.6300", "volume": "22165128" }, { "timestamp": "2019-06-07", "open": "102.6500", "high": "102.6900", "low": "100.3800", "close": "100.8800", "volume": "28232197" }, { "timestamp": "2019-06-06", "open": "102.4800", "high": "102.6000", "low": "101.9000", "close": "102.4900", "volume": "21122917" }, { "timestamp": "2019-06-05", "open": "102.0000", "high": "102.3300", "low": "101.5300", "close": "102.1900", "volume": "23514402" }, { "timestamp": "2019-06-04", "open": "101.2600", "high": "101.8600", "low": "100.8510", "close": "101.6700", "volume": "27281623" }, { "timestamp": "2019-06-01", "open": '99.2798', "high": "100.8600", "low": "99.1700", "close": "100.7900", "volume": "28655624" }] csv_file_path = os.path.join(os.path.dirname(__file__), "example_reports", "temp_prices.csv") if os.path.isfile(csv_file_path): os.remove(csv_file_path) assert os.path.isfile( csv_file_path ) == False # just making sure the test was setup properly(AKA removes filepath and deletes file temp_prices.csv) # INVOCATION result = write_to_csv(example_rows, csv_file_path) # EXPECTATIONS assert result == True #If the function invokes correctly, it should return "True" assert os.path.isfile( csv_file_path ) == True #Checks if a .csv file was created at the location of the csv_file_path csv_headers = ["timestamp", "open", "high", "low", "close", "volume"] with open(csv_file_path, "r") as csv_file: # "r" means "open the file for reading" reader = csv.DictReader(csv_file) # assuming your CSV has headers for row in reader: assert row[ "timestamp"] == "2019-06-08" #Checks to see if the value of first timestamp is correct assert row[ "open"] == "101.0924" #Checks to see if the value of first open price is correct break with open(csv_file_path, "r") as csv_file: reader = csv.DictReader(csv_file) x = 0 for row in reader: x += 1 row_number = len(example_rows) assert x == row_number #Checks to see if the .csv file has all the rows
def test_write_to_csv(): f_name = "test.csv" cols = ["this", 'is', 'a', 'test'] t_series = {} write_to_csv(f_name, cols, t_series) assert os.path.exists("test.csv")