Skip to content

Commit a5e1b86

Browse files
authored
Merge pull request pklaus#19 from DL6ER/tweak/tests
Include {{random}}, {{uuid}} and {{short-uuid}} in tests
2 parents f65fd99 + 538cc03 commit a5e1b86

3 files changed

Lines changed: 26 additions & 7 deletions

File tree

app/labeldesigner/label.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,13 @@ def datetime_replacer(match):
197197

198198
# Replace {{uuid}} with a new UUID
199199
if "{{uuid}}" in line['text']:
200-
line['text'] = line['text'].replace("{{uuid}}", str(uuid.uuid4()))
200+
ui = uuid.UUID(int=random.getrandbits(128))
201+
line['text'] = line['text'].replace("{{uuid}}", str(ui))
201202

202203
# Replace {{short-uuid}} with a shortened UUID
203204
if "{{short-uuid}}" in line['text']:
204-
line['text'] = line['text'].replace("{{short-uuid}}", str(uuid.uuid4())[:8])
205+
ui = uuid.UUID(int=random.getrandbits(128))
206+
line['text'] = line['text'].replace("{{short-uuid}}", str(ui)[:8])
205207

206208
# Replace {{env:var}} with the value of the environment variable var
207209
def env_replacer(match):
@@ -215,7 +217,7 @@ def random_replacer(match):
215217
return ''.join(random.choices(string.ascii_letters + string.digits + string.punctuation, k=length))
216218
line['text'] = re.sub(r"\{\{random(?:\:(\d+))?\}\}", random_replacer, line['text'])
217219

218-
def generate(self, rotate = False):
220+
def generate(self, rotate: bool = False):
219221
# Process possible templates in the text
220222
self.process_templates()
221223

tests/template.png

8.72 KB
Loading

tests/test_labeldesigner_api.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
import sys
66
import os
77
import pytest
8-
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
9-
from app import create_app
10-
from werkzeug.datastructures import FileStorage
118
import io
129
import multiprocessing
13-
from random import choice
10+
import random
11+
from werkzeug.datastructures import FileStorage
12+
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
13+
from app import create_app
1414

1515
UPDATE_IMAGES = False
1616
EXAMPLE_FORMDATA = {
@@ -403,9 +403,26 @@ def test_generate_template(client):
403403
'size': '20',
404404
'align': 'right',
405405
'inverted': True
406+
},
407+
{
408+
'family': 'DejaVu Sans',
409+
'style': 'Book',
410+
'text': '>> {{uuid}} {{short-uuid}} <<',
411+
'size': '20',
412+
'align': 'center'
413+
},
414+
{
415+
'family': 'DejaVu Sans',
416+
'style': 'Book',
417+
'text': '>> {{random:77}} <<',
418+
'size': '10',
419+
'align': 'center'
406420
}
407421
])
408422

423+
# Set random seed to a fixed value for deterministic results of {{random}},
424+
# {{uuid}}, and {{short-uuid}}
425+
random.seed(12)
409426
response = client.post('/labeldesigner/api/preview', data=data)
410427
assert response.status_code == 200
411428
assert response.content_type in ['image/png']

0 commit comments

Comments
 (0)