Skip to content

Commit

Permalink
Use temporary directories in test_persistence (#1808)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsnoam committed Mar 4, 2020
1 parent 8d6970a commit f7ec7a7
Showing 1 changed file with 11 additions and 25 deletions.
36 changes: 11 additions & 25 deletions tests/test_persistence.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@
PicklePersistence, CommandHandler, DictPersistence, TypeHandler


@pytest.fixture(autouse=True)
def change_directory(tmp_path):
orig_dir = os.getcwd()
# Switch to a temporary directory so we don't have to worry about cleaning up files
# (str() for py<3.6)
os.chdir(str(tmp_path))
yield
# Go back to original directory
os.chdir(orig_dir)


@pytest.fixture(scope="function")
def base_persistence():
return BasePersistence(store_chat_data=True, store_user_data=True, store_bot_data=True)
Expand Down Expand Up @@ -310,9 +321,6 @@ def bad_pickle_files():
with open(name, 'w') as f:
f.write('(())')
yield True
for name in ['pickletest_user_data', 'pickletest_chat_data', 'pickletest_bot_data',
'pickletest_conversations', 'pickletest']:
os.remove(name)


@pytest.fixture(scope='function')
Expand All @@ -330,9 +338,6 @@ def good_pickle_files(user_data, chat_data, bot_data, conversations):
with open('pickletest', 'wb') as f:
pickle.dump(data, f)
yield True
for name in ['pickletest_user_data', 'pickletest_chat_data', 'pickletest_bot_data',
'pickletest_conversations', 'pickletest']:
os.remove(name)


@pytest.fixture(scope='function')
Expand All @@ -347,9 +352,6 @@ def pickle_files_wo_bot_data(user_data, chat_data, conversations):
with open('pickletest', 'wb') as f:
pickle.dump(data, f)
yield True
for name in ['pickletest_user_data', 'pickletest_chat_data',
'pickletest_conversations', 'pickletest']:
os.remove(name)


@pytest.fixture(scope='function')
Expand Down Expand Up @@ -776,9 +778,6 @@ def test_flush_on_stop(self, bot, update, pickle_persistence):
assert pickle_persistence_2.get_bot_data()['test'] == 'Working3!'

def test_flush_on_stop_only_bot(self, bot, update, pickle_persistence_only_bot):
os.remove('pickletest_user_data')
os.remove('pickletest_chat_data')
os.remove('pickletest_bot_data')
u = Updater(bot=bot, persistence=pickle_persistence_only_bot)
dp = u.dispatcher
u.running = True
Expand All @@ -800,7 +799,6 @@ def test_flush_on_stop_only_bot(self, bot, update, pickle_persistence_only_bot):
assert pickle_persistence_2.get_bot_data()['my_test3'] == 'Working3!'

def test_flush_on_stop_only_chat(self, bot, update, pickle_persistence_only_chat):
os.remove('pickletest_bot_data')
u = Updater(bot=bot, persistence=pickle_persistence_only_chat)
dp = u.dispatcher
u.running = True
Expand All @@ -821,7 +819,6 @@ def test_flush_on_stop_only_chat(self, bot, update, pickle_persistence_only_chat
assert pickle_persistence_2.get_bot_data() == {}

def test_flush_on_stop_only_user(self, bot, update, pickle_persistence_only_user):
os.remove('pickletest_chat_data')
u = Updater(bot=bot, persistence=pickle_persistence_only_user)
dp = u.dispatcher
u.running = True
Expand Down Expand Up @@ -923,17 +920,6 @@ def next2(update, context):
assert nested_ch.conversations[nested_ch._get_key(update)] == 1
assert nested_ch.conversations == pickle_persistence.conversations['name3']

@classmethod
def teardown_class(cls):
try:
for name in ['pickletest_user_data', 'pickletest_chat_data',
'pickletest_bot_data',
'pickletest_conversations',
'pickletest']:
os.remove(name)
except Exception:
pass


@pytest.fixture(scope='function')
def user_data_json(user_data):
Expand Down

0 comments on commit f7ec7a7

Please sign in to comment.