Skip to content

Commit

Permalink
Turn off code execution randomness during pytest
Browse files Browse the repository at this point in the history
  • Loading branch information
rlan committed May 29, 2024
1 parent ed03996 commit 1c7fcd0
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 4 deletions.
8 changes: 7 additions & 1 deletion src/beancount_multitool/JABank.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pandas as pd
from pathlib import Path
import uuid
import sys

from .Institution import Institution
from .MappingDatabase import MappingDatabase
Expand Down Expand Up @@ -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)):
Expand All @@ -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):
Expand Down
8 changes: 7 additions & 1 deletion src/beancount_multitool/RakutenBank.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pandas as pd
from pathlib import Path
import uuid
import sys

from .Institution import Institution
from .MappingDatabase import MappingDatabase
Expand Down Expand Up @@ -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)):
Expand All @@ -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):
Expand Down
1 change: 1 addition & 0 deletions src/beancount_multitool/RakutenCard.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
8 changes: 7 additions & 1 deletion src/beancount_multitool/ShinseiBank.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pandas as pd
from pathlib import Path
import uuid
import sys

from .Institution import Institution
from .MappingDatabase import MappingDatabase
Expand Down Expand Up @@ -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)):
Expand All @@ -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):
Expand Down
8 changes: 7 additions & 1 deletion src/beancount_multitool/SumishinNetBank.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from decimal import Decimal
from pathlib import Path
import uuid
import sys

import pandas as pd

Expand Down Expand Up @@ -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)):
Expand All @@ -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):
Expand Down
10 changes: 10 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 1c7fcd0

Please sign in to comment.