Files
dtf-system/tests/thumbnail_backfill_test.py
Cauê Faleiros 0319039328
All checks were successful
Build and deploy / Validate source (push) Successful in 4s
Build and deploy / Integration suite on a real stack (push) Successful in 3m21s
Build and deploy / Secret scan and release gate (push) Successful in 5s
Build and deploy / Publish images (push) Successful in 1m1s
feat: every order is born with its origin, shown, filtered and counted (Dropstar's rules D-21 and D-35)
Orders carry site, dropflow or atendimento, required and with no default,
limited to the three. The Site's orders are marked site where they are
created; older orders became site. The Kanban shows the origin on each card
and in the order panel, filters by it, and Numeros has a Por origem table:
orders, metres, money and time from payment to Finalizado per way in.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 11:39:08 -03:00

72 lines
3.5 KiB
Python

"""Old orders get their artwork picture from the stored image, once.
Run inside the API container: python -m tests.thumbnail_backfill_test
"""
import io
from uuid import uuid4
from PIL import Image
from psycopg.types.json import Jsonb
from app.core import db
from ops import backfill_thumbnails
def png(width, height):
out = io.BytesIO()
Image.new('RGBA', (width, height), (255, 0, 0, 128)).save(out, 'PNG')
return out.getvalue()
class Storage:
"""Objects by key, written where the worker asks."""
def __init__(self, objects):
self.objects, self.fetched = objects, []
def fetch(self, key, path):
"""Other uploads in a shared database are not this test's: they fail like unreadable files."""
self.fetched.append(key)
with open(path, 'wb') as handle:
handle.write(self.objects[key])
def run():
owner, quote, order = uuid4(), uuid4(), uuid4()
image, drawing = uuid4(), uuid4()
files = {image: ('folha.png', png(1200, 3000)), drawing: ('arte.cdr', b'CDR')}
storage = Storage({f'backfill/{uid}': content for uid, (_, content) in files.items()})
try:
with db.connect() as c:
for uid, (name, content) in files.items():
c.execute('''INSERT INTO dtf_local.uploads(id,owner,name,size,object_key,multipart_id,complete,
expires_at,scan_state) VALUES(%s,%s,%s,%s,%s,'m',true,now()+interval '1 day','clean')''',
(uid, owner, name, len(content), f'backfill/{uid}'))
items = [{'mode': 'file', 'uploads': [str(image), str(drawing)]}]
c.execute('''INSERT INTO dtf_local.quotes(id,owner,request_key,request_hash,draft)
VALUES(%s,%s,%s,'backfill-fixture',%s)''', (quote, owner, uuid4(), Jsonb({'items': items})))
c.execute("INSERT INTO dtf_local.orders(id,quote_id,owner,snapshot,payment,origin) VALUES(%s,%s,%s,%s,%s,'site')",
(order, quote, owner, Jsonb({'items': items, 'total_cents': 1490}), Jsonb({'provider': 'teste'})))
lines = []
assert backfill_thumbnails.main([], out=lines.append, storage=storage) == 0
with db.connect() as c:
made = c.execute('SELECT mime,data FROM dtf_local.upload_thumbnails WHERE upload_id=%s', (image,)).fetchone()
assert made and made['mime'] == 'image/webp'
with Image.open(io.BytesIO(bytes(made['data']))) as picture:
assert picture.format == 'WEBP' and picture.size[0] <= 480 and picture.size[1] <= 720, picture.size
assert not c.execute('SELECT 1 FROM dtf_local.upload_thumbnails WHERE upload_id=%s', (drawing,)).fetchone()
# The CDR is never fetched; a second run does not fetch the image again.
assert storage.fetched.count(f'backfill/{image}') == 1 and f'backfill/{drawing}' not in storage.fetched
backfill_thumbnails.main([], out=lines.append, storage=storage)
assert storage.fetched.count(f'backfill/{image}') == 1
print('PASS: old orders get a WebP picture from a stored PNG; drawings are skipped; a rerun repeats nothing')
finally:
with db.connect() as c:
c.execute('DELETE FROM dtf_local.upload_thumbnails WHERE upload_id=ANY(%s)', (list(files),))
c.execute('DELETE FROM dtf_local.orders WHERE id=%s', (order,))
c.execute('DELETE FROM dtf_local.quotes WHERE id=%s', (quote,))
c.execute('DELETE FROM dtf_local.uploads WHERE id=ANY(%s)', (list(files),))
if __name__ == '__main__':
run()