generate vCard in utils.py; added Tests

This commit is contained in:
Albert
2025-11-09 19:43:03 +01:00
parent 374c242051
commit 998574f26a
6 changed files with 156 additions and 38 deletions

29
tests/test_utils_vcard.py Normal file
View File

@@ -0,0 +1,29 @@
from types import SimpleNamespace
from pathlib import Path
from utils import generate_vcard
def test_generate_vcard_writes_file(tmp_path):
addr = SimpleNamespace(
vorname='Anna',
nachname='Muster',
strasse='Beispielweg',
hausnummer='5a',
plz='54321',
ort='Beispielstadt',
land='Deutschland',
email='anna@example.com',
telefon_vorwahl='49',
telefon_nummer='7654321',
id=42,
)
path = generate_vcard(addr, str(tmp_path))
assert Path(path).exists()
content = Path(path).read_text(encoding='utf-8')
assert 'BEGIN:VCARD' in content
assert 'FN:Anna Muster' in content
assert 'EMAIL;TYPE=internet:anna@example.com' in content
assert 'TEL;TYPE=voice:+497654321' in content

View File

@@ -0,0 +1,62 @@
import pytest
from pathlib import Path
from app import app, db, Frage
@pytest.fixture
def client(tmp_path, monkeypatch):
# Use a temporary directory for vcards and a temporary sqlite db
# temp DB file
db_file = tmp_path / "test.db"
# Configure app for testing
app.config['TESTING'] = True
app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{db_file}'
# Use temp vcards dir by setting app.BASE_DIR
app.BASE_DIR = str(tmp_path)
# ensure clean DB and create tables
with app.app_context():
db.drop_all()
db.create_all()
# create a sample question so POST processing loops over it
q = Frage(text='Testfrage?')
db.session.add(q)
db.session.commit()
with app.test_client() as test_client:
yield test_client
def test_vcard_created_after_submit(client, tmp_path):
data = {
'vorname': 'Max',
'nachname': 'Mustermann',
'strasse': 'Musterstraße',
'hausnummer': '1',
'plz': '12345',
'ort': 'Musterstadt',
'land': 'Deutschland',
'telefon_vorwahl': '49',
'telefon_nummer': '1234567',
'email': 'max@example.com',
'frage_1': 'Antwort'
}
# Submit the form
res = client.post('/', data=data, follow_redirects=True)
assert res.status_code == 200
# find vcards dir under tmp_path (app.BASE_DIR)
vcards_dir = Path(app.BASE_DIR) / 'vcards'
files = list(vcards_dir.glob('*.vcf'))
assert len(files) == 1
content = files[0].read_text(encoding='utf-8')
assert 'BEGIN:VCARD' in content
assert 'VERSION:4.0' in content
assert 'FN:Max Mustermann' in content
assert 'EMAIL;TYPE=internet:max@example.com' in content
assert 'ADR:' in content