Completed
Push — master ( b17e58...6f8c55 )
by Chris
37s
created

test_dump_fixtures_delete_bad_path_show_errors_no_exception()   C

Complexity

Conditions 8

Size

Total Lines 18

Duplication

Lines 18
Ratio 100 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 8
c 2
b 0
f 0
dl 18
loc 18
rs 6.6666
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 View Code Duplication
def test_dump_fixtures(monkeypatch, tmpdir):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
67
    records = [
68
        model_factories.make_fake_dashboard(name=i, max_charts=1)
69
        for i in range(10)]
70
71
    monkeypatch.setattr(_db, 'read', lambda *args, **kwargs: records)
72
    runner = CliRunner()
73
    tmp = tmpdir.mkdir('dumped_fixtures_test')
74
    args = ['--dump', tmp.strpath]
75
    result = runner.invoke(model_factories.insert_dashboards, args)
76
    assert 'Saving db as fixtures to:' in result.output
77
    assert result.exit_code == 0
78
    assert len(os.listdir(tmp.strpath)) == len(records)
79
80
81
def test_dump_fixtures_delete(monkeypatch, tmpdir):
82
    records = [
83
        model_factories.make_fake_dashboard(name=i, max_charts=1)
84
        for i in range(10)]
85
86
    def delete_all():
87
        global records
88
        records = []
89
90
    monkeypatch.setattr(_db, 'read', lambda *args, **kwargs: records)
91
    monkeypatch.setattr(_db, 'delete_all', lambda *a, **kw: [])
92
    runner = CliRunner()
93
    tmp = tmpdir.mkdir('dumped_fixtures_test')
94
    args = ['--dump', tmp.strpath, '--delete']
95
    result = runner.invoke(model_factories.insert_dashboards, args)
96
    assert 'Saving db as fixtures to:' in result.output
97
    assert result.exit_code == 0
98
    assert len(os.listdir(tmp.strpath)) == 10
99
    assert len(read()) == 0
100
101
102 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...
103
    records = [
104
        model_factories.make_fake_dashboard(name=i, max_charts=1)
105
        for i in range(1)]
106
107
    def delete_all():
108
        global records
109
        records = []
110
111
    monkeypatch.setattr(_db, 'read', lambda *args, **kwargs: records)
112
    runner = CliRunner()
113
    args = ['--dump', '/fakepath/', '--delete']
114
    result = runner.invoke(model_factories.insert_dashboards, args)
115
    assert 'Saving db as fixtures to:' in result.output
116
    assert result.exit_code == 0
117
    assert len(read()) == 0
118
    err_msg = "The following records could not be dumped: ['/fakepath/"
119
    assert err_msg in result.output
120