Skip to content

Commit

Permalink
Merge pull request #3 from Gorbush/testing-db-prepare
Browse files Browse the repository at this point in the history
Testing db prepare
  • Loading branch information
hulk66 committed Jul 17, 2023
2 parents ee20bf7 + d9075ba commit 3fd0918
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 59 deletions.
5 changes: 0 additions & 5 deletions backend/tests/test_crud_photo.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ class TestCrudAndFace(unittest.TestCase):

def setUp(self):
self.app = create_app(testing=True, env="../envs/env.test")
with self.app.app_context():
status = Status()
status.last_import_album_id = 1
db.session.add(status)
db.session.commit()

def tearDown(self):

Expand Down
5 changes: 0 additions & 5 deletions backend/tests/test_exif.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@
class Test_Exif(unittest.TestCase):
def setUp(self):
self.app = create_app(testing=True, env="../envs/env.test")
with self.app.app_context():
status = Status()
status.last_import_album_id = 1
db.session.add(status)
db.session.commit()

def tearDown(self):
with self.app.app_context():
Expand Down
5 changes: 0 additions & 5 deletions backend/tests/test_heif.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ class TestHeif(unittest.TestCase):
def setUp(self):
register_heif_opener()
self.app = create_app(testing=True, env="../envs/env.test")
with self.app.app_context():
status = Status()
status.last_import_album_id = 1
db.session.add(status)
db.session.commit()

def tearDown(self):
with self.app.app_context():
Expand Down
3 changes: 0 additions & 3 deletions backend/tests/test_image_assessment.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
class TestImageAssessment(unittest.TestCase):
def setUp(self):
self.app = create_app(testing=True, env="../envs/env.test")
with self.app.app_context():
db.session.add(Status())
db.session.commit()

def test_quali(self):
nima = Nima("MobileNet", weights=None)
Expand Down
58 changes: 26 additions & 32 deletions backend/tests/test_video.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
import context


import os
import unittest
import ffmpeg
from PIL import Image
import exiftool
import shutil

import context
from PIL import Image
from timeline.tasks.crud_tasks import create_asset, create_preview
from timeline.celery_video import create_fullscreen_video, create_preview_video
from timeline.app import create_app
from timeline.extensions import db
from timeline.domain import Asset, Status, TranscodingProcess
import shutil
from timeline.domain import Asset, Status


class Test_VideoConversion(unittest.TestCase):

def setUp(self):
self.app = create_app(testing=True, env="../envs/env.test")
with self.app.app_context():
status = Status()
status.last_import_album_id = 1
db.session.add(status)
db.session.commit()


def test_mov_conversion(self) -> None:
Expand Down Expand Up @@ -81,6 +74,7 @@ def test_create_asset(self):
create_asset("tests/fjord.mov")
video = Asset.query.first()
# create_preview(video.id)
from timeline.celery_video import create_fullscreen_video, create_preview_video
create_preview_video(video.id, 400)
create_fullscreen_video(video.id)
#assert os.path.exists("tests/400/high_res/tests/fjord.mov.jpg")
Expand Down Expand Up @@ -112,26 +106,26 @@ def test_create_asset(self):
#shutil.rmtree("tests/400")
#shutil.rmtree("tests/video")

def test_transcode_process(self):
with self.app.app_context():
create_asset("tests/fjord.mov")
video = Asset.query.first()
tp = TranscodingProcess()
tp.asset = video
tp.progress = 40
db.session.add(tp)
db.session.commit()

tp = TranscodingProcess.query.first()
assert tp.progress == 40
assert tp.id == video.id
db.session.delete(tp)
db.session.commit()

tp = TranscodingProcess.query.first()
assert tp is None
video = Asset.query.first()
assert video
# def test_transcode_process(self):
# with self.app.app_context():
# create_asset("tests/fjord.mov")
# video = Asset.query.first()
# tp = TranscodingProcess()
# tp.asset = video
# tp.progress = 40
# db.session.add(tp)
# db.session.commit()

# tp = TranscodingProcess.query.first()
# assert tp.progress == 40
# assert tp.id == video.id
# db.session.delete(tp)
# db.session.commit()

# tp = TranscodingProcess.query.first()
# assert tp is None
# video = Asset.query.first()
# assert video


if __name__ == '__main__':
Expand Down
3 changes: 2 additions & 1 deletion backend/timeline/api/assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from flask import Blueprint, abort
from timeline.extensions import celery

from timeline.api.util import parse_exif_date
from timeline.tasks.crud_tasks import create_preview
from timeline.util.image_ops import exif_transpose
import logging
Expand Down Expand Up @@ -253,7 +254,7 @@ def upload():

try:
# set asset date
dt = datetime.datetime.strptime(str(date_time), "%Y:%m:%d %H:%M:%S")
dt = parse_exif_date(date_time)
except ValueError:
logger.error("%s can not be parsed as Date for %s",
str(date_time), filename)
Expand Down
19 changes: 19 additions & 0 deletions backend/timeline/api/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
GNU General Public License for more details.
'''

from datetime import datetime
import flask
from timeline.domain import (GPS, Face, Person, Asset, Section, Status, Thing,
asset_thing, Exif, asset_album, Album)
Expand Down Expand Up @@ -70,3 +71,21 @@ def assets_from_smart_album(album: Album, q = None):
camera = album.camera_make, fromDate = album.start_date, toDate = album.end_date,
rating = album.rating)
return q


def parse_exif_date(value:str):
if not value:
return None
# Removing excessive characters from some old exif
dateStr = str(value).strip("\x00\r\n ")
try:
dt = datetime.strptime(str(dateStr), "%Y:%m:%d %H:%M:%S")
except ValueError:
try:
dt = datetime.strptime(str(dateStr), "%Y-%m-%d %H:%M:%S")
except ValueError:
try:
dt = datetime.strptime(str(dateStr), "%Y/%m/%d %H:%M:%S")
except ValueError:
raise
return dt
37 changes: 35 additions & 2 deletions backend/timeline/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
'''

import flask
from timeline.domain import Album, Status
from timeline.extensions import db, celery, cache, migrate
from timeline.api import views, assets, admin, inspect, albums
import logging
Expand All @@ -37,7 +38,11 @@ def create_app(testing=False, cli=False, env=None):
app.config.from_pyfile(env)
if testing is True:
app.config["TESTING"] = True

print(f"Application config for environment {app.config['ENV']}")
print(f"SQLALCHEMY_DATABASE_URI={app.config['SQLALCHEMY_DATABASE_URI']}")
print(f"TESTING={app.config['TESTING']}")
print(f"DEBUG={app.config['DEBUG']}")
print(f"CREATE_DATABASE ={app.config['CREATE_DATABASE']}")
# app.config['SQLALCHEMY_ECHO'] = True
configure_extensions(app, cli)
register_blueprints(app)
Expand All @@ -49,6 +54,33 @@ def index():
return app


def prepare_for_tests(app):
create_for_tests = app.config["TESTING"] or False
if create_for_tests:
db.create_all(app=app)
# from .manage import init
# init()
with app.app_context():
status = Status.query.first()
if not status:
status = Status()
status.next_import_is_new = True
status.sections_dirty = False
status.computing_sections = False
status.num_assets_created = False

album = Album()
album.name = "Last Import"
status.last_import_album = album

db.session.add(status)

status.sections_dirty = False
status.computing_sections = False

db.session.commit()


def configure_extensions(app, cli):
"""configure flask extensions
"""
Expand All @@ -60,7 +92,8 @@ def configure_extensions(app, cli):
create_db(app)
db.init_app(app)
migrate.init_app(app, db)
# db.create_all(app=app)
prepare_for_tests(app)

celery.init_app(app)
cache.init_app(app)

Expand Down
13 changes: 7 additions & 6 deletions backend/timeline/tasks/crud_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from celery import chain
from flask import current_app
from PIL import Image, UnidentifiedImageError
from timeline.api.util import parse_exif_date

from timeline.domain import Album, GPS, Exif, Person, Asset, Section, Status, DateRange, AssetType, TranscodingStatus
from timeline.extensions import celery, db
Expand Down Expand Up @@ -140,7 +141,7 @@ def create_asset(path: str, commit=True):
dateStr = md.get("QuickTime:MediaCreateDate")
if dateStr:
try:
dt = datetime.strptime(str(dateStr), "%Y:%m:%d %H:%M:%S")
dt = parse_exif_date(dateStr)
asset.created = dt
asset.no_creation_date = False
except ValueError:
Expand Down Expand Up @@ -225,14 +226,14 @@ def _extract_exif_data(asset, image=None):
exif.key, exif.value = key, str(value)

except UnicodeDecodeError:
logger.error("%s", img_path)
logger.error("%s", asset.path)

# User either DateTimeOriginal or not available any other DateTime
# or (key.startswith("DateTime") and asset.created is None):
if key == 'DateTimeOriginal':
try:
# set asset date
dt = datetime.strptime(str(value), "%Y:%m:%d %H:%M:%S")
dt = parse_exif_date(value)
asset.created = dt
asset.no_creation_date = False
except ValueError:
Expand Down Expand Up @@ -326,7 +327,7 @@ def delete_asset(asset: Asset):

def _delete_asset_by_path(path):
for p in Asset.query.filter(Asset.path == path):
_delete_asset(p)
delete_asset(p)


@celery.task(mame="Delete asset")
Expand Down Expand Up @@ -443,7 +444,7 @@ def level_date_ranges(start_asset: Asset = None):

status = Status.query.first()

if not status.sections_dirty and Asset.query.filter(asset.section == None).count() == 0:
if not status.sections_dirty and Asset.query.filter(start_asset.section == None).count() == 0:
logger.debug("Level Date Ranges- nothing to do")
status.next_import_is_new = True
db.session.commit()
Expand Down Expand Up @@ -732,7 +733,7 @@ def _resync_asset(asset_id):
# We are out of sync. The database references a asset which does not exist in the filesystem anymore
logger.debug(
"asset %s no longer exists. Remove it from the catalog", asset.path)
_delete_asset(asset)
delete_asset(asset)
db.session.commit()


Expand Down

0 comments on commit 3fd0918

Please sign in to comment.