ordigi/tests/conftest.py

78 lines
2.2 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
2021-08-08 15:33:47 +02:00
import pytest
2021-08-27 12:45:25 +02:00
from pathlib import Path, PurePath
import random
2021-08-08 21:43:37 +02:00
import shutil
2021-08-27 12:45:25 +02:00
import string
2021-08-08 21:43:37 +02:00
import tempfile
2021-08-08 15:33:47 +02:00
from ordigi.config import Config
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
@pytest.fixture(autouse=True)
def reset_singletons():
""" Need to clean up any ExifTool singletons between tests """
_ExifToolProc.instance = None
2021-08-27 12:45:25 +02:00
@pytest.fixture(scope="session")
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
paths = Path(dest_dir).glob('*')
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
file_path = Path(str(dest_dir), '.ordigi', str(dest_dir.name) + '.db')
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',
'dirs_path':'%u{%Y-%m}/{city}|{city}-{%Y}/{folders[:1]}/{folder}',
'name':'{%Y-%m-%b-%H-%M-%S}-{basename}.%l{ext}'
}
conf['Geolocation'] = {
'geocoder': 'Nominatium'
}
2021-08-27 12:45:25 +02:00
conf_path = Path(conf_dir, "ordigi.conf")
config = Config(conf_path)
config.write(conf)
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