Skip to content

Commit 1c7fcd0

Browse files
committed
Turn off code execution randomness during pytest
1 parent ed03996 commit 1c7fcd0

File tree

6 files changed

+39
-4
lines changed

6 files changed

+39
-4
lines changed

src/beancount_multitool/JABank.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import pandas as pd
33
from pathlib import Path
44
import uuid
5+
import sys
56

67
from .Institution import Institution
78
from .MappingDatabase import MappingDatabase
@@ -108,7 +109,11 @@ def write_bean(self, df: pd.DataFrame, file_name: str) -> None:
108109
if amount > 0: # a credit
109110
metadata["uuid"] = ""
110111
else: # a debit
111-
metadata["uuid"] = str(uuid.uuid4())
112+
if hasattr(sys, "_called_from_pytest"):
113+
# remove randomness during pytest
114+
metadata["uuid"] = "_called_from_pytest"
115+
else:
116+
metadata["uuid"] = str(uuid.uuid4())
112117

113118
account_metadata = {}
114119
for x in range(1, len(accounts)):
@@ -126,6 +131,7 @@ def write_bean(self, df: pd.DataFrame, file_name: str) -> None:
126131
f.write(output)
127132
print(f"Written {file_name}")
128133
except IOError as e:
134+
print(f"Error encountered while writing to: {file_name}")
129135
print(e)
130136

131137
def convert(self, csv_file: str, bean_file: str):

src/beancount_multitool/RakutenBank.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pandas as pd
22
from pathlib import Path
33
import uuid
4+
import sys
45

56
from .Institution import Institution
67
from .MappingDatabase import MappingDatabase
@@ -93,7 +94,11 @@ def write_bean(self, df: pd.DataFrame, file_name: str) -> None:
9394
if amount > 0: # a credit
9495
metadata["uuid"] = ""
9596
else: # a debit
96-
metadata["uuid"] = str(uuid.uuid4())
97+
if hasattr(sys, "_called_from_pytest"):
98+
# remove randomness during pytest
99+
metadata["uuid"] = "_called_from_pytest"
100+
else:
101+
metadata["uuid"] = str(uuid.uuid4())
97102

98103
account_metadata = {}
99104
for x in range(1, len(accounts)):
@@ -111,6 +116,7 @@ def write_bean(self, df: pd.DataFrame, file_name: str) -> None:
111116
f.write(output)
112117
print(f"Written {file_name}")
113118
except IOError as e:
119+
print(f"Error encountered while writing to: {file_name}")
114120
print(e)
115121

116122
def convert(self, csv_file: str, bean_file: str):

src/beancount_multitool/RakutenCard.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ def write_bean(self, df: pd.DataFrame, file_name: str) -> None:
121121
f.write(output)
122122
print(f"Written {file_name}")
123123
except IOError as e:
124+
print(f"Error encountered while writing to: {file_name}")
124125
print(e)
125126

126127
def convert(self, csv_file: str, bean_file: str):

src/beancount_multitool/ShinseiBank.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pandas as pd
22
from pathlib import Path
33
import uuid
4+
import sys
45

56
from .Institution import Institution
67
from .MappingDatabase import MappingDatabase
@@ -112,7 +113,11 @@ def write_bean(self, df: pd.DataFrame, file_name: str) -> None:
112113
if amount > 0: # a credit
113114
metadata["uuid"] = ""
114115
else: # a debit
115-
metadata["uuid"] = str(uuid.uuid4())
116+
if hasattr(sys, "_called_from_pytest"):
117+
# remove randomness during pytest
118+
metadata["uuid"] = "_called_from_pytest"
119+
else:
120+
metadata["uuid"] = str(uuid.uuid4())
116121

117122
account_metadata = {}
118123
for x in range(1, len(accounts)):
@@ -130,6 +135,7 @@ def write_bean(self, df: pd.DataFrame, file_name: str) -> None:
130135
f.write(output)
131136
print(f"Written {file_name}")
132137
except IOError as e:
138+
print(f"Error encountered while writing to: {file_name}")
133139
print(e)
134140

135141
def convert(self, csv_file: str, bean_file: str):

src/beancount_multitool/SumishinNetBank.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from decimal import Decimal
22
from pathlib import Path
33
import uuid
4+
import sys
45

56
import pandas as pd
67

@@ -113,7 +114,11 @@ def write_bean(self, df: pd.DataFrame, file_name: str) -> None:
113114
if amount > 0: # a credit
114115
metadata["uuid"] = ""
115116
else: # a debit
116-
metadata["uuid"] = str(uuid.uuid4())
117+
if hasattr(sys, "_called_from_pytest"):
118+
# remove randomness during pytest
119+
metadata["uuid"] = "_called_from_pytest"
120+
else:
121+
metadata["uuid"] = str(uuid.uuid4())
117122

118123
account_metadata = {}
119124
for x in range(1, len(accounts)):
@@ -131,6 +136,7 @@ def write_bean(self, df: pd.DataFrame, file_name: str) -> None:
131136
f.write(output)
132137
print(f"Written {file_name}")
133138
except IOError as e:
139+
print(f"Error encountered while writing to: {file_name}")
134140
print(e)
135141

136142
def convert(self, csv_file: str, bean_file: str):

tests/conftest.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# content of conftest.py
2+
3+
# Ref: https://stackoverflow.com/a/25188424
4+
def pytest_configure(config):
5+
import sys
6+
sys._called_from_pytest = True
7+
8+
def pytest_unconfigure(config):
9+
import sys
10+
del sys._called_from_pytest

0 commit comments

Comments
 (0)