Skip to content

Commit

Permalink
fix unittest
Browse files Browse the repository at this point in the history
  • Loading branch information
yutaro-oguri committed Feb 29, 2024
1 parent 1d48e74 commit 8f79375
Showing 1 changed file with 28 additions and 25 deletions.
53 changes: 28 additions & 25 deletions test/test_file_processor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import tempfile
from typing import Callable
import unittest

import pandas as pd
Expand All @@ -13,79 +14,79 @@ def test_dump_csv_with_utf8(self):
processor = CsvFileProcessor()

with tempfile.TemporaryDirectory() as temp_dir:
temp_path = f"{temp_dir}/temp.csv"
temp_path = f'{temp_dir}/temp.csv'

local_target = LocalTarget(path=temp_path, format=processor.format())
with local_target.open("w") as f:
with local_target.open('w') as f:
processor.dump(df, f)

# read with utf-8 to check if the file is dumped with utf8
loaded_df = pd.read_csv(temp_path, encoding="utf-8")
loaded_df = pd.read_csv(temp_path, encoding='utf-8')
pd.testing.assert_frame_equal(df, loaded_df)

def test_dump_csv_with_cp932(self):
df = pd.DataFrame({"あ": [1, 2, 3], "い": [4, 5, 6]})
processor = CsvFileProcessor(encoding="cp932")
df = pd.DataFrame({'あ': [1, 2, 3], 'い': [4, 5, 6]})
processor = CsvFileProcessor(encoding='cp932')

with tempfile.TemporaryDirectory() as temp_dir:
temp_path = f"{temp_dir}/temp.csv"
temp_path = f'{temp_dir}/temp.csv'

local_target = LocalTarget(path=temp_path, format=processor.format())
with local_target.open("w") as f:
with local_target.open('w') as f:
processor.dump(df, f)

# read with cp932 to check if the file is dumped with cp932
loaded_df = pd.read_csv(temp_path, encoding="cp932")
loaded_df = pd.read_csv(temp_path, encoding='cp932')
pd.testing.assert_frame_equal(df, loaded_df)

def test_load_csv_with_utf8(self):
df = pd.DataFrame({"あ": [1, 2, 3], "い": [4, 5, 6]})
df = pd.DataFrame({'あ': [1, 2, 3], 'い': [4, 5, 6]})
processor = CsvFileProcessor()

with tempfile.TemporaryDirectory() as temp_dir:
temp_path = f"{temp_dir}/temp.csv"
df.to_csv(temp_path, encoding="utf-8", index=False)
temp_path = f'{temp_dir}/temp.csv'
df.to_csv(temp_path, encoding='utf-8', index=False)

local_target = LocalTarget(path=temp_path, format=processor.format())
with local_target.open("r") as f:
with local_target.open('r') as f:
# read with utf-8 to check if the file is dumped with utf8
loaded_df = processor.load(f)
pd.testing.assert_frame_equal(df, loaded_df)

def test_load_csv_with_cp932(self):
df = pd.DataFrame({"あ": [1, 2, 3], "い": [4, 5, 6]})
processor = CsvFileProcessor(encoding="cp932")
df = pd.DataFrame({'あ': [1, 2, 3], 'い': [4, 5, 6]})
processor = CsvFileProcessor(encoding='cp932')

with tempfile.TemporaryDirectory() as temp_dir:
temp_path = f"{temp_dir}/temp.csv"
df.to_csv(temp_path, encoding="cp932", index=False)
temp_path = f'{temp_dir}/temp.csv'
df.to_csv(temp_path, encoding='cp932', index=False)

local_target = LocalTarget(path=temp_path, format=processor.format())
with local_target.open("r") as f:
with local_target.open('r') as f:
# read with cp932 to check if the file is dumped with cp932
loaded_df = processor.load(f)
pd.testing.assert_frame_equal(df, loaded_df)


class TestPickleFileProcessor(unittest.TestCase):
def test_dump_and_load_normal_obj(self):
var = "abc"
var = 'abc'
processor = PickleFileProcessor()

with tempfile.TemporaryDirectory() as temp_dir:
temp_path = f"{temp_dir}/temp.pkl"
temp_path = f'{temp_dir}/temp.pkl'
local_target = LocalTarget(path=temp_path, format=processor.format())
with local_target.open("w") as f:
with local_target.open('w') as f:
processor.dump(var, f)
with local_target.open("r") as f:
with local_target.open('r') as f:
loaded = processor.load(f)

self.assertEqual(loaded, var)

def test_dump_and_load_class(self):
import functools

def plus1(func):
def plus1(func: Callable[[], int]) -> Callable[[], int]:
@functools.wraps(func)
def wrapped() -> int:
ret = func()
Expand All @@ -94,6 +95,8 @@ def wrapped() -> int:
return wrapped

class A:
run: Callable[[], int]

def __init__(self) -> None:
self.run = plus1(self.run)

Expand All @@ -103,11 +106,11 @@ def run(self) -> int:
obj = A()
processor = PickleFileProcessor()
with tempfile.TemporaryDirectory() as temp_dir:
temp_path = f"{temp_dir}/temp.pkl"
temp_path = f'{temp_dir}/temp.pkl'
local_target = LocalTarget(path=temp_path, format=processor.format())
with local_target.open("w") as f:
with local_target.open('w') as f:
processor.dump(obj, f)
with local_target.open("r") as f:
with local_target.open('r') as f:
loaded = processor.load(f)

self.assertEqual(loaded.run(), obj.run())

0 comments on commit 8f79375

Please sign in to comment.