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
This commit is contained in:
Thierry Carrez 2020-11-25 16:03:10 +01:00
parent 6bf2598db6
commit 21fbc34f3e

View File

@ -47,8 +47,9 @@ class PTGDataBase():
'location': None, 'in': None, 'out': None 'location': None, 'in': None, 'out': None
} }
def __init__(self, config): def __init__(self, config, write_to_disk=True):
self.filename = config['db_filename'] self.filename = config['db_filename']
self.write_to_disk = write_to_disk
if os.path.isfile(self.filename): if os.path.isfile(self.filename):
with open(self.filename, 'r') as fp: with open(self.filename, 'r') as fp:
@ -304,6 +305,7 @@ class PTGDataBase():
timestamp = datetime.datetime.now() timestamp = datetime.datetime.now()
self.data['timestamp'] = self.serialise_timestamp(timestamp) self.data['timestamp'] = self.serialise_timestamp(timestamp)
self.data['tracks'] = sorted(self.data['tracks']) self.data['tracks'] = sorted(self.data['tracks'])
if self.write_to_disk:
with open(self.filename, 'w') as fp: with open(self.filename, 'w') as fp:
json.dump(self.data, fp) json.dump(self.data, fp)