From 1c7fcd0148ca56ec6dfbc547bb1e837002435184 Mon Sep 17 00:00:00 2001 From: Rick Lan Date: Wed, 29 May 2024 20:37:38 +0900 Subject: [PATCH] Turn off code execution randomness during pytest --- src/beancount_multitool/JABank.py | 8 +++++++- src/beancount_multitool/RakutenBank.py | 8 +++++++- src/beancount_multitool/RakutenCard.py | 1 + src/beancount_multitool/ShinseiBank.py | 8 +++++++- src/beancount_multitool/SumishinNetBank.py | 8 +++++++- tests/conftest.py | 10 ++++++++++ 6 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 tests/conftest.py diff --git a/src/beancount_multitool/JABank.py b/src/beancount_multitool/JABank.py index f1714ae..16c3219 100644 --- a/src/beancount_multitool/JABank.py +++ b/src/beancount_multitool/JABank.py @@ -2,6 +2,7 @@ import pandas as pd from pathlib import Path import uuid +import sys from .Institution import Institution from .MappingDatabase import MappingDatabase @@ -108,7 +109,11 @@ def write_bean(self, df: pd.DataFrame, file_name: str) -> None: if amount > 0: # a credit metadata["uuid"] = "" else: # a debit - metadata["uuid"] = str(uuid.uuid4()) + if hasattr(sys, "_called_from_pytest"): + # remove randomness during pytest + metadata["uuid"] = "_called_from_pytest" + else: + metadata["uuid"] = str(uuid.uuid4()) account_metadata = {} for x in range(1, len(accounts)): @@ -126,6 +131,7 @@ def write_bean(self, df: pd.DataFrame, file_name: str) -> None: f.write(output) print(f"Written {file_name}") except IOError as e: + print(f"Error encountered while writing to: {file_name}") print(e) def convert(self, csv_file: str, bean_file: str): diff --git a/src/beancount_multitool/RakutenBank.py b/src/beancount_multitool/RakutenBank.py index 803a2a2..5060170 100644 --- a/src/beancount_multitool/RakutenBank.py +++ b/src/beancount_multitool/RakutenBank.py @@ -1,6 +1,7 @@ import pandas as pd from pathlib import Path import uuid +import sys from .Institution import Institution from .MappingDatabase import MappingDatabase @@ -93,7 +94,11 @@ def write_bean(self, df: pd.DataFrame, file_name: str) -> None: if amount > 0: # a credit metadata["uuid"] = "" else: # a debit - metadata["uuid"] = str(uuid.uuid4()) + if hasattr(sys, "_called_from_pytest"): + # remove randomness during pytest + metadata["uuid"] = "_called_from_pytest" + else: + metadata["uuid"] = str(uuid.uuid4()) account_metadata = {} for x in range(1, len(accounts)): @@ -111,6 +116,7 @@ def write_bean(self, df: pd.DataFrame, file_name: str) -> None: f.write(output) print(f"Written {file_name}") except IOError as e: + print(f"Error encountered while writing to: {file_name}") print(e) def convert(self, csv_file: str, bean_file: str): diff --git a/src/beancount_multitool/RakutenCard.py b/src/beancount_multitool/RakutenCard.py index f0135a3..5fe24a6 100644 --- a/src/beancount_multitool/RakutenCard.py +++ b/src/beancount_multitool/RakutenCard.py @@ -121,6 +121,7 @@ def write_bean(self, df: pd.DataFrame, file_name: str) -> None: f.write(output) print(f"Written {file_name}") except IOError as e: + print(f"Error encountered while writing to: {file_name}") print(e) def convert(self, csv_file: str, bean_file: str): diff --git a/src/beancount_multitool/ShinseiBank.py b/src/beancount_multitool/ShinseiBank.py index f60dfb7..03f73e6 100644 --- a/src/beancount_multitool/ShinseiBank.py +++ b/src/beancount_multitool/ShinseiBank.py @@ -1,6 +1,7 @@ import pandas as pd from pathlib import Path import uuid +import sys from .Institution import Institution from .MappingDatabase import MappingDatabase @@ -112,7 +113,11 @@ def write_bean(self, df: pd.DataFrame, file_name: str) -> None: if amount > 0: # a credit metadata["uuid"] = "" else: # a debit - metadata["uuid"] = str(uuid.uuid4()) + if hasattr(sys, "_called_from_pytest"): + # remove randomness during pytest + metadata["uuid"] = "_called_from_pytest" + else: + metadata["uuid"] = str(uuid.uuid4()) account_metadata = {} for x in range(1, len(accounts)): @@ -130,6 +135,7 @@ def write_bean(self, df: pd.DataFrame, file_name: str) -> None: f.write(output) print(f"Written {file_name}") except IOError as e: + print(f"Error encountered while writing to: {file_name}") print(e) def convert(self, csv_file: str, bean_file: str): diff --git a/src/beancount_multitool/SumishinNetBank.py b/src/beancount_multitool/SumishinNetBank.py index b542a30..20998e0 100644 --- a/src/beancount_multitool/SumishinNetBank.py +++ b/src/beancount_multitool/SumishinNetBank.py @@ -1,6 +1,7 @@ from decimal import Decimal from pathlib import Path import uuid +import sys import pandas as pd @@ -113,7 +114,11 @@ def write_bean(self, df: pd.DataFrame, file_name: str) -> None: if amount > 0: # a credit metadata["uuid"] = "" else: # a debit - metadata["uuid"] = str(uuid.uuid4()) + if hasattr(sys, "_called_from_pytest"): + # remove randomness during pytest + metadata["uuid"] = "_called_from_pytest" + else: + metadata["uuid"] = str(uuid.uuid4()) account_metadata = {} for x in range(1, len(accounts)): @@ -131,6 +136,7 @@ def write_bean(self, df: pd.DataFrame, file_name: str) -> None: f.write(output) print(f"Written {file_name}") except IOError as e: + print(f"Error encountered while writing to: {file_name}") print(e) def convert(self, csv_file: str, bean_file: str): diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..b03069c --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,10 @@ +# content of conftest.py + +# Ref: https://stackoverflow.com/a/25188424 +def pytest_configure(config): + import sys + sys._called_from_pytest = True + +def pytest_unconfigure(config): + import sys + del sys._called_from_pytest