def test_save_by_csv_using_kwargs(self): df = pd.DataFrame({ "a": [1, 2], "b": [2, 3], "c": [3, 4] }, index=[9, 10]) name_1 = "test_save_by_csv_using_kwargs_1.csv" content_1 = '''9,1,2,3 10,2,3,4 ''' saver = AutoSaver(self.test_assets_dir, index=True, header=False) saver.save(df, name_1, line_terminator="\n") with open(os.path.join(self.test_assets_dir, name_1), "r", encoding="utf-8") as f: content = f.read() self.assertTrue(content, content_1) name_2 = "test_save_by_csv_using_kwargs_2.csv" content_2 = '''a;b;c 1;2;3 2;3;4 ''' saver = AutoSaver(index=False) saver.save(df, os.path.join(self.test_assets_dir, name_2), sep=";") with open(os.path.join(self.test_assets_dir, name_2), "r", encoding="utf-8") as f: content = f.read() self.assertTrue(content, content_2) saver = AutoSaver(index=True, sep=",") saver.save(df, os.path.join(self.test_assets_dir, name_2), sep=";", index=False) with open(os.path.join(self.test_assets_dir, name_2), "r", encoding="utf-8") as f: content = f.read() self.assertTrue(content, content_2)
def test_save_by_csv_param_error(self): with self.assertRaises(ValueError): AutoSaver(self.test_assets_dir, example_path=r"tests/assets/data1.csv", encoding="utf-8") with self.assertRaises(ValueError): saver = AutoSaver(self.test_assets_dir, example_path=r"tests/assets/data1.csv") saver.save("Nothing", "some_name.csv", encoding="utf-8")
def test_GBK(self): # REMINDER: do not use self.get_csv_content when testing GBK. # It defaultly uses utf-8. saver = AutoSaver(save_dir=self.test_assets_dir, example_path=r"tests/assets/saver-example-GBK.csv") target = '''index,value 0,1.0 1,0.0 2,1.5 3,0.5 ''' saver.save(self.np_1d, self.filename) with open(os.path.join(self.test_assets_dir, self.filename), "r", encoding="GBK") as f: content = f.read() self.assertEqual(content, target) saver.save(self.np_2d_1val, self.filename) with open(os.path.join(self.test_assets_dir, self.filename), "r", encoding="GBK") as f: content = f.read() self.assertEqual(content, target) saver.save(self.pd_df_2d, self.filename) with open(os.path.join(self.test_assets_dir, self.filename), "r", encoding="GBK") as f: content = f.read() self.assertEqual(content, target)
def test_saving_two_value(self): saver = AutoSaver(save_dir=self.test_assets_dir, example_path=r"tests/assets/saver-example-two-value.csv") arr = np.array([ [1, 1.5], [0.5, 0.7], [0.2, 0.3] ]) df = pd.DataFrame(arr) target = '''ix,val1,val2 0,1.0,1.5 1,0.5,0.7 2,0.2,0.3 ''' saver.save(arr, self.filename) with open(os.path.join(self.test_assets_dir, self.filename), "r", encoding="utf-8") as f: content = f.read() self.assertEqual(content, target) saver.save(df, self.filename) with open(os.path.join(self.test_assets_dir, self.filename), "r", encoding="utf-8") as f: content = f.read() self.assertEqual(content, target)
def test_saving_one_header_no_value(self): saver = AutoSaver(save_dir=self.test_assets_dir, example_path=r"tests/assets/saver-example-one-header-no-value.csv") target = ''',val 0,1.0 1,0.0 2,1.5 3,0.5 ''' self.run_all_X_s(saver, target)
def test_saving_without_index(self): saver = AutoSaver(save_dir=self.test_assets_dir, example_path=r"tests/assets/saver-example-without-index.csv") target = '''1.0 0.0 1.5 0.5 ''' self.run_all_X_s(saver, target)
def test_saving_without_head(self): saver = AutoSaver(save_dir=self.test_assets_dir, example_path=r"tests/assets/saver-example-without-head.csv") target = '''0,1.0 1,0.0 2,1.5 3,0.5 ''' self.run_all_X_s(saver, target)
def test_ix_starts_1(self): saver = AutoSaver(save_dir=self.test_assets_dir, example_path=r"tests/assets/saver-example-ix-starts-1.csv") target = '''index,value 1,1.0 2,0.0 3,1.5 4,0.5 ''' self.run_all_X_s(saver, target)
def test_basic(self): saver = AutoSaver(save_dir=self.test_assets_dir, example_path=r"tests/assets/saver-example-basic.csv") target = '''index,value 0,1.0 1,0.0 2,1.5 3,0.5 ''' self.run_all_X_s(saver, target)
def test_no_filename(self): old_files = os.listdir(self.test_assets_dir) saver = AutoSaver(self.test_assets_dir) saver.save(pd.DataFrame()) files = os.listdir(self.test_assets_dir) self.assertEqual(len(files), len(old_files)+1) new_file = [_ for _ in files if _ not in old_files][0] self.assertTrue( bool(re.compile(r"\d{4}-\d{6}\.csv").match(new_file)) )
def test_other_sep(self): saver = AutoSaver(save_dir=self.test_assets_dir, example_path=r"tests/assets/saver-example-other-sep.csv") target = '''index;value 0;1.0 1;0.0 2;1.5 3;0.5 ''' self.run_all_X_s(saver, target)
def test_memo(self): saver = AutoSaver(self.test_assets_dir) saver.save(pd.DataFrame(), "test_memo_df.csv", "hello") with open(os.path.join(self.test_assets_dir, "memo.txt"), "r", encoding="utf-8") as f: content = f.read() self.assertEqual(content.strip(), "test_memo_df.csv: hello") saver.save(pd.DataFrame(), "test_memo_df_2.csv", "hello again") with open(os.path.join(self.test_assets_dir, "memo.txt"), "r", encoding="utf-8") as f: content = f.read() self.assertEqual(content.strip(), "\n".join( ["test_memo_df.csv: hello", "test_memo_df_2.csv: hello again"]))
def test_saving_without_example_path(self): saver = AutoSaver(self.test_assets_dir) with self.assertRaises(TypeError): saver.save(self.np_1d, "wrong.csv") saver.save(self.pd_df_1d, "test_saving_without_example_path_1.csv") self.pd_df_1d.to_csv(os.path.join( self.test_assets_dir, "test_saving_without_example_path_2.csv")) with open(os.path.join(self.test_assets_dir, "test_saving_without_example_path_1.csv"), "r", encoding="utf-8") as f: content_1 = f.read() with open(os.path.join(self.test_assets_dir, "test_saving_without_example_path_2.csv"), "r", encoding="utf-8") as f: content_2 = f.read() self.assertEqual(content_1, content_2)
def test_saving_strange_index(self): saver = AutoSaver(save_dir=self.test_assets_dir, example_path=r"tests/assets/saver-example-strange-index.csv") target = '''strange,val 4,1.0 8,0.0 19,1.5 45,0.5 150,0.2 ''' arr = np.array([1.0, 0.0, 1.5, 0.5, 0.2]) saver.save(arr, self.filename) self.assertEqual(self.get_csv_content(), target) saver.save(arr.reshape((5, 1)), self.filename) self.assertEqual(self.get_csv_content(), target) saver.save(pd.Series(arr), self.filename) self.assertEqual(self.get_csv_content(), target) saver.save(pd.DataFrame(arr, columns=[""]), self.filename) self.assertEqual(self.get_csv_content(), target)
def test_str_as_index(self): saver = AutoSaver(save_dir=self.test_assets_dir, example_path=r"tests/assets/saver-example-str-as-index.csv") str_ix = ["strix1", "strix2", "strix3", "strix4"] with self.assertRaises(SpeculationFailedError): saver.save(self.np_1d, self.filename) with self.assertRaises(SpeculationFailedError): saver.save(self.pd_df_1d, self.filename) saver.save(self.pd_df_2d, self.filename) target = '''ix,val 0,1.0 1,0.0 2,1.5 3,0.5 ''' self.assertEqual(self.get_csv_content(), target) saver.save(pd.Series(self.np_1d, index=str_ix), self.filename) target = '''ix,val strix1,1.0 strix2,0.0 strix3,1.5 strix4,0.5 ''' self.assertEqual(self.get_csv_content(), target) saver.save(pd.DataFrame(self.np_1d, index=str_ix), self.filename) self.assertEqual(self.get_csv_content(), target) saver.save(pd.DataFrame({ "ix": str_ix, "val": self.np_1d, }), self.filename) self.assertEqual(self.get_csv_content(), target)
def test_create_dir(self): AutoSaver(save_dir=self.test_assets_dir) self.assertTrue(os.path.isdir(self.test_assets_dir))