1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
# -*- coding: utf-8 -*-
from django.core.management import call_command
from django_extensions.management.commands.export_emails import full_name
import pytest
from tests.testapp.settings import DATABASES
@pytest.fixture(autouse=True)
def custom_djsettings(settings):
"""Custom django settings to avoid warnings in stdout"""
settings.TEMPLATE_DEBUG = False
settings.DEBUG = False
return settings
@pytest.fixture(scope="module")
def django_db_setup():
"""Select default database for testing"""
settings.DATABASES = DATABASES # noqa
@pytest.fixture(scope="module") # noqa
def django_db_setup(django_db_setup, django_db_blocker): # noqa
"""Load to database a set of users, create for export
emails command testing"""
with django_db_blocker.unblock():
call_command("loaddata", "group.json")
call_command("loaddata", "user.json")
@pytest.mark.django_db()
def test_do_export_emails_stdout_start(capsys):
"""Testing export_emails command without args.stdout starts."""
call_command("export_emails")
out, err = capsys.readouterr()
assert out.startswith('"Mijaíl Bulgakóv')
@pytest.mark.django_db()
def test_do_export_emails_stdout_end(capsys):
"""Testing export_emails command without args.stdout end."""
call_command("export_emails")
out, err = capsys.readouterr()
assert '"Frédéric Mistral" <frederic_mistral@gmail.com>;\n\n' in out
@pytest.mark.django_db()
def test_do_export_emails_format_email(capsys):
"""Testing python manage.py export_emails -f emails"""
call_command("export_emails", "--format=emails")
out, err = capsys.readouterr()
assert "frederic_mistral@gmail.com" in out
@pytest.mark.django_db()
def test_do_export_emails_address(capsys):
"""Testing python manage export_emails -f address"""
call_command("export_emails", "--format=address")
out, err = capsys.readouterr()
assert '"Frédéric Mistral"' in out
@pytest.mark.django_db()
def test_do_export_emails_format_google(capsys):
"""Testing python manage export_emails -f google"""
call_command("export_emails", "--format=google")
out, err = capsys.readouterr()
assert out.startswith("Name,Email")
@pytest.mark.django_db()
def test_do_export_emails_format_linkedin(capsys):
"""Testing python manage.py export_emails -f linkedin"""
call_command("export_emails", "--format=linkedin")
out, err = capsys.readouterr()
assert out.startswith("First Name,")
assert "Gabriel Garcia,Marquéz" in out
@pytest.mark.django_db()
def test_do_export_emails_format_outlook(capsys):
"""Testing python manage.py export_emails -f outlook"""
call_command("export_emails", "--format=outlook")
out, err = capsys.readouterr()
assert out.startswith("Name,E-mail Address")
assert "frederic_mistral@gmail.com" in out
@pytest.mark.django_db()
def test_do_export_emails_format_vcard_start(capsys):
"""Testing python manage.py export_emails -f vcard"""
call_command("export_emails", "--format=vcard")
out, err = capsys.readouterr()
assert "N:Bulgakóv;Mijaíl;;;" in out
assert out.startswith("BEGIN:VCARD")
@pytest.mark.django_db()
def test_full_name():
"""Test, getting full name / username"""
fake_user = {"first_name": "Allan", "last_name": "Poe", "username": "allan_poe"}
name = full_name(**fake_user)
assert name == "{fn} {ln}".format(
fn=fake_user["first_name"], ln=fake_user["last_name"]
)
fake_user2 = {"first_name": "", "last_name": "", "username": "niccolas"}
name2 = full_name(**fake_user2)
assert name2 == fake_user2["username"]
|