Completed
Push — master ( 58e7a9...dddd97 )
by Chris
45s
created

test_dump_fixtures_empty()   B

Complexity

Conditions 5

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
dl 0
loc 10
rs 8.5454
c 0
b 0
f 0
1
import os
2
import json
3
4
from click.testing import CliRunner
5
6
from flask_jsondash import model_factories
7
from flask_jsondash.settings import CHARTS_CONFIG
8
from conftest import read
9
10
_db = model_factories.adapter
11
12
13
def test_get_random_group():
14
    conf_vals = CHARTS_CONFIG.values()
15
    data = model_factories.get_random_group()
16
    assert isinstance(data, dict)
17
    assert 'charts' in data
18
    assert data in conf_vals
19
20
21
def test_get_random_chart():
22
    chart = model_factories.get_random_group()
23
    data = model_factories.get_random_chart(chart)
24
    assert isinstance(data, tuple)
25
26
27
def test_make_fake_dashboard():
28
    fdash = model_factories.make_fake_dashboard(name='Foo', max_charts=4)
29
    assert isinstance(fdash, dict)
30
    assert fdash.get('name') == 'Foo'
31
32
33
def test_make_fake_chart_data():
34
    chartdata = model_factories.make_fake_chart_data(name='Foo')
35
    chartconfig = json.loads(chartdata[1])
36
    assert isinstance(chartdata, tuple)
37
    assert isinstance(chartconfig, dict)
38
    assert chartconfig.get('name') == 'Foo'
39
40
41
def test_insert_dashboards(monkeypatch):
42
    records = []
43
    runner = CliRunner()
44
    args = ['--max-charts', 5, '--records', 5]
45
    monkeypatch.setattr(_db, 'create', lambda *a, **kw: records.append(a))
46
    result = runner.invoke(model_factories.insert_dashboards, args)
47
    assert result.exit_code == 0
48
    assert len(records) == 5
49
50
51
def test_delete_all(monkeypatch):
52
    monkeypatch.setattr(_db, 'delete_all', lambda *a, **kw: [])
53
    assert model_factories.delete_all() is None
54
55
56
def test_load_fixtures(monkeypatch):
57
    records = []
58
    runner = CliRunner()
59
    args = ['--fixtures', 'example_app/examples/config']
60
    monkeypatch.setattr(_db, 'create', lambda *a, **kw: records.append(a))
61
    result = runner.invoke(model_factories.insert_dashboards, args)
62
    assert result.exit_code == 0
63
    assert len(records) == 16  # Changed as new examples are added.
64
65
66
def test_dump_fixtures_empty(monkeypatch, tmpdir):
67
    records = []
68
    monkeypatch.setattr(_db, 'read', lambda *args, **kwargs: records)
69
    runner = CliRunner()
70
    tmp = tmpdir.mkdir('dumped_fixtures_test')
71
    args = ['--dump', tmp.strpath]
72
    result = runner.invoke(model_factories.insert_dashboards, args)
73
    assert 'Nothing to dump.' in result.output
74
    assert result.exit_code == 0
75
    assert len(os.listdir(tmp.strpath)) == len(records)
76
77
78
def test_dump_fixtures(monkeypatch, tmpdir):
79
    records = [
80
        model_factories.make_fake_dashboard(name=i, max_charts=1)
81
        for i in range(10)]
82
    # Also ensure _id is popped off.
83
    for r in records:
84
        r.update(_id='foo')
85
    monkeypatch.setattr(_db, 'read', lambda *args, **kwargs: records)
86
    runner = CliRunner()
87
    tmp = tmpdir.mkdir('dumped_fixtures_test')
88
    args = ['--dump', tmp.strpath]
89
    result = runner.invoke(model_factories.insert_dashboards, args)
90
    assert 'Saving db as fixtures to:' in result.output
91
    assert result.exit_code == 0
92
    assert len(os.listdir(tmp.strpath)) == len(records)
93
94
95 View Code Duplication
def test_dump_fixtures_delete(monkeypatch, tmpdir):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
96
    records = [
97
        model_factories.make_fake_dashboard(name=i, max_charts=1)
98
        for i in range(10)]
99
100
    def delete_all():
101
        global records
102
        records = []
103
104
    monkeypatch.setattr(_db, 'read', lambda *args, **kwargs: records)
105
    monkeypatch.setattr(_db, 'delete_all', lambda *a, **kw: [])
106
    runner = CliRunner()
107
    tmp = tmpdir.mkdir('dumped_fixtures_test')
108
    args = ['--dump', tmp.strpath, '--delete']
109
    result = runner.invoke(model_factories.insert_dashboards, args)
110
    assert 'Saving db as fixtures to:' in result.output
111
    assert result.exit_code == 0
112
    assert len(os.listdir(tmp.strpath)) == 10
113
    assert len(read()) == 0
114
115
116 View Code Duplication
def test_dump_fixtures_delete_bad_path_show_errors_no_exception(monkeypatch):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
117
    records = [
118
        model_factories.make_fake_dashboard(name=i, max_charts=1)
119
        for i in range(1)]
120
121
    def delete_all():
122
        global records
123
        records = []
124
125
    monkeypatch.setattr(_db, 'read', lambda *args, **kwargs: records)
126
    runner = CliRunner()
127
    args = ['--dump', '/fakepath/', '--delete']
128
    result = runner.invoke(model_factories.insert_dashboards, args)
129
    assert 'Saving db as fixtures to:' in result.output
130
    assert result.exit_code == 0
131
    assert len(read()) == 0
132
    err_msg = "The following records could not be dumped: ['//fakepath/"
133
    assert err_msg in result.output
134