Feature report writer #309 (#690)

* Initial implementation of ReportWriter for #309

* Initial implementation of ReportWriterJSON

* Added sqlite report format

* Added auto-flush to report writer, fixed exportdb --info to output json

* Added exportdb --report
This commit is contained in:
Rhet Turnbull
2022-05-15 09:30:17 -07:00
committed by GitHub
parent 391815dd94
commit 470839ba0d
8 changed files with 873 additions and 224 deletions

View File

@@ -1,4 +1,5 @@
""" Test the command line interface (CLI) """
import csv
import datetime
import glob
import json
@@ -9,7 +10,6 @@ import pathlib
import re
import shutil
import sqlite3
import sys
import tempfile
import time
from tempfile import TemporaryDirectory
@@ -19,8 +19,8 @@ from click.testing import CliRunner
from osxmetadata import OSXMetaData, Tag
import osxphotos
from osxphotos._version import __version__
from osxphotos._constants import OSXPHOTOS_EXPORT_DB
from osxphotos._version import __version__
from osxphotos.cli import (
about,
albums,
@@ -960,6 +960,18 @@ EXPORT_UNICODE_TITLE_FILENAMES = [
"Frítest (3).jpg",
]
# data for --report
UUID_REPORT = [
{
"uuid": "4D521201-92AC-43E5-8F7C-59BC41C37A96",
"filenames": ["IMG_1997.JPG", "IMG_1997.cr2"],
},
{
"uuid": "7783E8E6-9CAC-40F3-BE22-81FB7051C266",
"filenames": ["IMG_3092.heic", "IMG_3092_edited.jpeg"],
},
]
# data for --exif
QUERY_EXIF_DATA = [("EXIF:Make", "FUJIFILM", ["6191423D-8DB8-4D4C-92BE-9BBBA308AAC4"])]
QUERY_EXIF_DATA_CASE_INSENSITIVE = [
@@ -5473,13 +5485,217 @@ def test_export_report():
cwd = os.getcwd()
# pylint: disable=not-context-manager
with runner.isolated_filesystem():
# test report creation
result = runner.invoke(
export,
[os.path.join(cwd, CLI_PHOTOS_DB), ".", "-V", "--report", "report.csv"],
[
os.path.join(cwd, CLI_PHOTOS_DB),
".",
"-V",
"--uuid",
UUID_REPORT[0]["uuid"],
"--report",
"report.csv",
],
)
assert result.exit_code == 0
assert "Writing export report" in result.output
assert "Wrote export report" in result.output
assert os.path.exists("report.csv")
with open("report.csv", "r") as f:
reader = csv.DictReader(f)
rows = list(reader)
filenames = [str(pathlib.Path(row["filename"]).name) for row in rows]
assert sorted(filenames) == sorted(UUID_REPORT[0]["filenames"])
# test report gets overwritten
result = runner.invoke(
export,
[
os.path.join(cwd, CLI_PHOTOS_DB),
".",
"-V",
"--uuid",
UUID_REPORT[1]["uuid"],
"--report",
"report.csv",
],
)
assert result.exit_code == 0
with open("report.csv", "r") as f:
reader = csv.DictReader(f)
rows = list(reader)
filenames = [str(pathlib.Path(row["filename"]).name) for row in rows]
assert sorted(filenames) == sorted(UUID_REPORT[1]["filenames"])
# test report with --append
result = runner.invoke(
export,
[
os.path.join(cwd, CLI_PHOTOS_DB),
".",
"-V",
"--uuid",
UUID_REPORT[0]["uuid"],
"--report",
"report.csv",
"--overwrite",
"--append",
],
)
assert result.exit_code == 0
with open("report.csv", "r") as f:
reader = csv.DictReader(f)
rows = list(reader)
filenames = [str(pathlib.Path(row["filename"]).name) for row in rows]
assert sorted(filenames) == sorted(
UUID_REPORT[0]["filenames"] + UUID_REPORT[1]["filenames"]
)
def test_export_report_json():
"""test export with --report option for JSON report"""
runner = CliRunner()
cwd = os.getcwd()
# pylint: disable=not-context-manager
with runner.isolated_filesystem():
# test report creation
result = runner.invoke(
export,
[
os.path.join(cwd, CLI_PHOTOS_DB),
".",
"-V",
"--uuid",
UUID_REPORT[0]["uuid"],
"--report",
"report.json",
],
)
assert result.exit_code == 0
assert "Wrote export report" in result.output
assert os.path.exists("report.json")
with open("report.json", "r") as f:
rows = json.load(f)
filenames = [str(pathlib.Path(row["filename"]).name) for row in rows]
assert sorted(filenames) == sorted(UUID_REPORT[0]["filenames"])
# test report gets overwritten
result = runner.invoke(
export,
[
os.path.join(cwd, CLI_PHOTOS_DB),
".",
"-V",
"--uuid",
UUID_REPORT[1]["uuid"],
"--report",
"report.json",
],
)
assert result.exit_code == 0
with open("report.json", "r") as f:
rows = json.load(f)
filenames = [str(pathlib.Path(row["filename"]).name) for row in rows]
assert sorted(filenames) == sorted(UUID_REPORT[1]["filenames"])
# test report with --append
result = runner.invoke(
export,
[
os.path.join(cwd, CLI_PHOTOS_DB),
".",
"-V",
"--uuid",
UUID_REPORT[0]["uuid"],
"--report",
"report.json",
"--overwrite",
"--append",
],
)
assert result.exit_code == 0
with open("report.json", "r") as f:
rows = json.load(f)
filenames = [str(pathlib.Path(row["filename"]).name) for row in rows]
assert sorted(filenames) == sorted(
UUID_REPORT[0]["filenames"] + UUID_REPORT[1]["filenames"]
)
@pytest.mark.parametrize("report_file", ["report.db", "report.sqlite"])
def test_export_report_sqlite(report_file):
"""test export with --report option with sqlite report"""
runner = CliRunner()
cwd = os.getcwd()
# pylint: disable=not-context-manager
with runner.isolated_filesystem():
# test report creation
result = runner.invoke(
export,
[
os.path.join(cwd, CLI_PHOTOS_DB),
".",
"-V",
"--uuid",
UUID_REPORT[0]["uuid"],
"--report",
report_file,
],
)
assert result.exit_code == 0
assert "Wrote export report" in result.output
assert os.path.exists(report_file)
conn = sqlite3.connect(report_file)
c = conn.cursor()
c.execute("SELECT filename FROM report")
filenames = [str(pathlib.Path(row[0]).name) for row in c.fetchall()]
assert sorted(filenames) == sorted(UUID_REPORT[0]["filenames"])
# test report gets overwritten
result = runner.invoke(
export,
[
os.path.join(cwd, CLI_PHOTOS_DB),
".",
"-V",
"--uuid",
UUID_REPORT[1]["uuid"],
"--report",
report_file,
],
)
assert result.exit_code == 0
conn = sqlite3.connect(report_file)
c = conn.cursor()
c.execute("SELECT filename FROM report")
filenames = [str(pathlib.Path(row[0]).name) for row in c.fetchall()]
assert sorted(filenames) == sorted(UUID_REPORT[1]["filenames"])
# test report with --append
result = runner.invoke(
export,
[
os.path.join(cwd, CLI_PHOTOS_DB),
".",
"-V",
"--uuid",
UUID_REPORT[0]["uuid"],
"--report",
report_file,
"--overwrite",
"--append",
],
)
assert result.exit_code == 0
conn = sqlite3.connect(report_file)
c = conn.cursor()
c.execute("SELECT filename FROM report")
filenames = [str(pathlib.Path(row[0]).name) for row in c.fetchall()]
assert sorted(filenames) == sorted(
UUID_REPORT[0]["filenames"] + UUID_REPORT[1]["filenames"]
)
def test_export_report_template():
@@ -5500,7 +5716,7 @@ def test_export_report_template():
],
)
assert result.exit_code == 0
assert "Writing export report" in result.output
assert "Wrote export report" in result.output
assert os.path.exists(f"report_{__version__}.csv")