Completed
Push — master ( 684a71...5716d7 )
by Chris
01:36
created

_schema()   A

Complexity

Conditions 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
1
import json
2
import pytest
3
4
from flask_jsondash import charts_builder as app
5
6
7
def _schema(**vals):
8
    """Default schema."""
9
    data = dict(
10
        date="2016-08-23 15:03:49.178000",
11
        layout="grid",
12
        name="testlayout",
13
        modules=[],
14
    )
15
    data.update(**vals)
16
    return json.dumps(data)
17
18
19
@pytest.mark.schema
20
def test_validate_raw_json_valid_empty_modules():
21
    assert app.validate_raw_json(_schema())
22
23
24
@pytest.mark.schema
25
def test_validate_raw_json_valid_freeform():
26
    d = _schema(
27
        layout='freeform',
28
        modules=[
29
            dict(name='foo', dataSource='foo',
30
                 width=1, height=1, type='line',
31
                 family='C3')]
32
    )
33
    assert app.validate_raw_json(d)
34
35
36
@pytest.mark.schema
37
def test_validate_raw_json_valid_fixed():
38
    d = _schema(
39
        layout='freeform',
40
        modules=[
41
            dict(name='foo', dataSource='foo',
42
                 width=1, height=1, type='line',
43
                 family='C3')]
44
    )
45
    assert app.validate_raw_json(d)
46
47
48 View Code Duplication
@pytest.mark.schema
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
49
@pytest.mark.parametrize('field', [
50
    'type',
51
    'family',
52
    'width',
53
    'height',
54
    'dataSource',
55
])
56
def test_validate_raw_json_missing_required_module_keys(field):
57
    module = dict(
58
        name='foo', dataSource='foo',
59
        width=1, height=1, type='line',
60
        family='C3')
61
    del module[field]
62
    d = _schema(
63
        layout='grid',
64
        modules=[module]
65
    )
66
    with pytest.raises(app.InvalidSchemaError):
67
        app.validate_raw_json(d)
68
69
70 View Code Duplication
@pytest.mark.schema
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
71
@pytest.mark.parametrize('field', [
72
    'row',
73
])
74
def test_validate_raw_json_missing_required_fixedgrid_module_keys(field):
75
    module = dict(
76
        name='foo', dataSource='foo',
77
        width=1, height=1, type='line',
78
        row=1, family='C3')
79
    del module[field]
80
    d = _schema(
81
        layout='grid',
82
        modules=[module]
83
    )
84
    with pytest.raises(app.InvalidSchemaError):
85
        app.validate_raw_json(d)
86
87
88
@pytest.mark.schema
89
@pytest.mark.parametrize('field', [
90
    'row',
91
])
92
def test_validate_raw_json_missing_optional_freeform_module_keys(field):
93
    # Ensure that required fields for fixed grid
94
    # are not required for freeform layouts.
95
    module = dict(
96
        name='foo', dataSource='foo',
97
        width=1, height=1, type='line',
98
        row=1, family='C3')
99
    del module[field]
100
    d = _schema(
101
        layout='freeform',
102
        modules=[module]
103
    )
104
    assert app.validate_raw_json(d)
105