File: test_export_emails.py

package info (click to toggle)
python-django-extensions 2.1.4-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,396 kB
  • sloc: python: 11,858; makefile: 116
file content (131 lines) | stat: -rw-r--r-- 4,285 bytes parent folder | download
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
124
125
126
127
128
129
130
131
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.core.management import call_command

from django_extensions.management.commands.export_emails import Command, full_name

import pytest

from tests.testapp.settings import DATABASES


@pytest.fixture(autouse=True)
def custom_djsettings(settings):  # noqa
    """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():  # noqa
    """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():  # noqa
        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."""
    export_emails = Command()
    export_emails.run_from_argv(['manage.py', '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."""
    export_emails = Command()
    export_emails.run_from_argv(['manage.py', '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"""
    export_emails = Command()
    export_emails.run_from_argv(['manage.py', '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"""
    export_emails = Command()
    export_emails.run_from_argv(['manage.py', '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"""
    export_emails = Command()
    export_emails.run_from_argv(['manage.py', '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"""
    export_emails = Command()
    export_emails.run_from_argv(['manage.py', '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"""
    export_emails = Command()
    export_emails.run_from_argv(['manage.py', 'export_emails', '--format=outlook'])

    out, err = capsys.readouterr()
    assert out.startswith('Name,E-mail Address')
    assert 'frederic_mistral@gmail.com' in out


@pytest.mark.skipif("sys.version_info < (3, 0)", reason='issues with vobject library on PY2x')
@pytest.mark.django_db()
def test_do_export_emails_format_vcard_start(capsys):
    """Testing python manage.py export_emails -f vcard"""
    export_emails = Command()
    export_emails.run_from_argv(['manage.py', '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']