From 21fbc34f3e8ae0219e57d63667462cbf02d0079d Mon Sep 17 00:00:00 2001 From: Thierry Carrez Date: Wed, 25 Nov 2020 16:03:10 +0100 Subject: [PATCH] Add memory-only database mode Add a DB creation mode that will not persist changes to disk, to be used in unit testing. Change-Id: I3eeac5ddc1a360c1a4ed9dc892720ae92da1de87 --- ptgbot/db.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/ptgbot/db.py b/ptgbot/db.py index 70de342..3b7ecf7 100644 --- a/ptgbot/db.py +++ b/ptgbot/db.py @@ -47,8 +47,9 @@ class PTGDataBase(): 'location': None, 'in': None, 'out': None } - def __init__(self, config): + def __init__(self, config, write_to_disk=True): self.filename = config['db_filename'] + self.write_to_disk = write_to_disk if os.path.isfile(self.filename): with open(self.filename, 'r') as fp: @@ -304,8 +305,9 @@ class PTGDataBase(): timestamp = datetime.datetime.now() self.data['timestamp'] = self.serialise_timestamp(timestamp) self.data['tracks'] = sorted(self.data['tracks']) - with open(self.filename, 'w') as fp: - json.dump(self.data, fp) + if self.write_to_disk: + with open(self.filename, 'w') as fp: + json.dump(self.data, fp) def serialise_timestamp(self, timestamp): return '{:%Y-%m-%d %H:%M:%S}'.format(timestamp)