ordigi/tests/conftest.py

77 lines
2.1 KiB
Python
Raw Normal View History

2021-08-08 15:33:47 +02:00
""" pytest test configuration """
2021-08-08 21:43:37 +02:00
from configparser import RawConfigParser
2021-08-27 12:45:25 +02:00
import os
from pathlib import Path, PurePath
import random
2021-08-08 21:43:37 +02:00
import shutil
import tempfile
2021-08-08 15:33:47 +02:00
2021-12-05 18:27:04 +01:00
import pytest
2021-08-13 21:11:24 +02:00
from ordigi.exiftool import _ExifToolProc
2021-08-08 15:33:47 +02:00
2021-08-13 21:11:24 +02:00
ORDIGI_PATH = Path(__file__).parent.parent
2021-08-08 15:33:47 +02:00
2021-12-05 18:27:04 +01:00
2021-08-08 15:33:47 +02:00
@pytest.fixture(autouse=True)
def reset_singletons():
""" Need to clean up any ExifTool singletons between tests """
_ExifToolProc.instance = None
@pytest.fixture(scope="module")
2021-08-27 12:45:25 +02:00
def sample_files_paths(tmpdir_factory):
2021-09-18 22:06:34 +02:00
tmp_path = Path(tmpdir_factory.mktemp("ordigi-src-"))
path = Path(ORDIGI_PATH, 'samples/test_exif')
shutil.copytree(path, tmp_path / path.name)
paths = Path(tmp_path).glob('**/*')
2021-08-08 21:43:37 +02:00
file_paths = [x for x in paths if x.is_file()]
2021-08-27 12:45:25 +02:00
return tmp_path, file_paths
def randomize_files(dest_dir):
# Get files randomly
for path, subdirs, files in os.walk(dest_dir):
2021-08-31 16:18:41 +02:00
if '.ordigi' in path:
continue
2021-08-27 12:45:25 +02:00
for name in files:
file_path = PurePath(path, name)
if bool(random.getrandbits(1)):
with open(file_path, 'wb') as fout:
fout.write(os.urandom(random.randrange(128, 2048)))
if bool(random.getrandbits(1)):
dest_path = PurePath(path, file_path.stem + '_1'+ file_path.suffix)
shutil.copyfile(file_path, dest_path)
2021-08-08 21:43:37 +02:00
2021-08-31 16:18:41 +02:00
def randomize_db(dest_dir):
# alterate database
2021-12-05 18:27:04 +01:00
file_path = Path(str(dest_dir), '.ordigi', 'collection.db')
2021-08-31 16:18:41 +02:00
with open(file_path, 'wb') as fout:
fout.write(os.urandom(random.randrange(128, 2048)))
2021-08-08 21:43:37 +02:00
@pytest.fixture(scope="module")
def conf_path():
2021-08-27 12:45:25 +02:00
conf_dir = tempfile.mkdtemp(prefix='ordigi-')
2021-08-08 21:43:37 +02:00
conf = RawConfigParser()
conf['Path'] = {
'day_begins': '4',
2022-04-18 08:57:47 +02:00
'dirs_path':'%u<%Y-%m>/<city>|<city>-<%Y>/<folders[:1]>/<folder>',
'name':'<%Y-%m-%b-%H-%M-%S>-<basename>.%l<ext>'
2021-08-08 21:43:37 +02:00
}
conf['Geolocation'] = {
'geocoder': 'Nominatium'
}
2021-08-27 12:45:25 +02:00
conf_path = Path(conf_dir, "ordigi.conf")
2021-10-17 20:04:39 +02:00
with open(conf_path, 'w') as conf_file:
conf.write(conf_file)
2021-08-08 21:43:37 +02:00
yield conf_path
2021-08-27 12:45:25 +02:00
shutil.rmtree(conf_dir)
2021-08-08 21:43:37 +02:00