Example #1
0
 def test_validation(self):
     """Test validation of grammar"""
     test = Visualization()
     test.axes.append({'bad axes': 'ShouldRaiseError'})
     with nt.assert_raises(ValidationError) as err:
         test.validate()
     nt.assert_equal(err.exception.args[0],
                     'invalid contents: axes[0] must be Axis')
Example #2
0
 def test_legends(self):
     test_vis = Visualization()
     test_vis.legend(title='Test', text_color='#000')
     nt.assert_equals(test_vis.legends[0].title, 'Test')
     nt.assert_equals(test_vis.legends[0].properties.labels.fill.value,
                      '#000')
     nt.assert_equals(test_vis.legends[0].properties.title.fill.value,
                      '#000')
Example #3
0
 def test_validation(self):
     """Test validation of grammar"""
     test = Visualization()
     test.axes.append({'bad axes': 'ShouldRaiseError'})
     with nt.assert_raises(ValidationError) as err:
         test.validate()
     nt.assert_equal(err.exception.message,
                     'invalid contents: axes[0] must be Axis')
Example #4
0
 def test_legends(self):
     test_vis = Visualization()
     test_vis.legend(title='Test', text_color='#000')
     nt.assert_equals(test_vis.legends[0].title, 'Test')
     nt.assert_equals(test_vis.legends[0].properties.labels.fill.value,
                      '#000')
     nt.assert_equals(test_vis.legends[0].properties.title.fill.value,
                      '#000')
Example #5
0
def test_grammar_dict():
    """Test Vincent Grammar Dict"""

    g_dict = GrammarDict()
    test = Visualization()
    test_dict = {'axes': [], 'data': [], 'marks': [],
                 'scales': [], 'legends': []}
    test_str = ('{"marks": [], "axes": [], "data": [],'
                ' "legends": [], "scales": []}')

    nt.assert_equal(test.grammar(), test_dict)
    nt.assert_equal(str(test.grammar), test_str)
    nt.assert_equal(g_dict.encoder(test), test.grammar)
Example #6
0
def test_grammar_dict():
    """Test Vincent Grammar Dict"""

    g_dict = GrammarDict()
    test = Visualization()
    test_dict = {'axes': [], 'data': [], 'marks': [],
                 'scales': [], 'legends': []}
    test_str = ('{"marks": [], "axes": [], "data": [],'
                ' "legends": [], "scales": []}')

    nt.assert_equal(test.grammar(), test_dict)
    nt.assert_equal(str(test.grammar), test_str)
    nt.assert_equal(g_dict.encoder(test), test.grammar)
Example #7
0
    def test_to_json(self):
        """Test JSON to string"""

        pretty = '''{
          "marks": [],
          "axes": [],
          "data": [],
          "scales": [],
          "legends": []
        }'''

        test = Visualization()
        actual, tested = json.loads(pretty), json.loads(test.to_json())
        nt.assert_dict_equal(actual, tested)
Example #8
0
    def test_to_json(self):
        """Test JSON to string"""

        pretty = '''{
          "marks": [],
          "axes": [],
          "data": [],
          "scales": [],
          "legends": []
        }'''

        test = Visualization()
        actual, tested = json.loads(pretty), json.loads(test.to_json())
        nt.assert_dict_equal(actual, tested)
Example #9
0
def test_grammar_dict():
    """Test Vincent Grammar Dict"""

    g_dict = GrammarDict()
    test = Visualization()
    test_dict = {'axes': [], 'data': [], 'marks': [],
                 'scales': [], 'legends': []}
    test_str = ('{"axes": [], "data": [], "legends": [], '
                '"marks": [], "scales": []}')

    nt.assert_equal(test.grammar(), test_dict)
    print(json.dumps(test.grammar, sort_keys=True))
    nt.assert_equal(json.dumps(test.grammar, sort_keys=True),
                    test_str)
    nt.assert_equal(g_dict.encoder(test), test.grammar)
Example #10
0
    def test_axis_labeling(self):
        """Test convenience method for axis label setting"""

        #With Axes already in place
        test_obj = Visualization()
        test_obj.axes.extend([Axis(type='x'), Axis(type='y')])
        test_obj.axis_titles(x="test1", y="test2")
        nt.assert_equals(test_obj.axes['x'].title, 'test1')
        nt.assert_equals(test_obj.axes['y'].title, 'test2')

        #With no Axes already defined
        del test_obj.axes[0]
        del test_obj.axes[0]
        test_obj.axis_titles(x="test1", y="test2")
        nt.assert_equals(test_obj.axes['x'].title, 'test1')
        nt.assert_equals(test_obj.axes['y'].title, 'test2')
Example #11
0
    def test_manual_typecheck(self):
        """Test manual typechecking for elements like marks"""

        test_attr = [('data', [1]), ('scales', [1]), ('axes', [1]),
                     ('marks', [1]), ('legends', [1])]

        assert_manual_typechecking(test_attr, Visualization())
Example #12
0
def test_grammar_dict():
    """Test Vincent Grammar Dict"""

    g_dict = GrammarDict()
    test = Visualization()
    test_dict = {
        'axes': [],
        'data': [],
        'marks': [],
        'scales': [],
        'legends': []
    }
    test_str = ('{"axes": [], "data": [], "legends": [], '
                '"marks": [], "scales": []}')

    nt.assert_equal(test.grammar(), test_dict)
    print(json.dumps(test.grammar, sort_keys=True))
    nt.assert_equal(json.dumps(test.grammar, sort_keys=True), test_str)
    nt.assert_equal(g_dict.encoder(test), test.grammar)
Example #13
0
    def test_grammar_typechecking(self):
        """Visualization fields are correctly type checked"""

        grammar_types = [('name', [str]), ('width', [int]), ('height', [int]),
                         ('data', [list, KeyedList]),
                         ('scales', [list, KeyedList]),
                         ('axes', [list, KeyedList]),
                         ('marks', [list, KeyedList])]

        assert_grammar_typechecking(grammar_types, Visualization())
Example #14
0
    def test_validation(self):
        """Test Visualization validation"""

        test_obj = Visualization()
        with nt.assert_raises(ValidationError) as err:
            test_obj.validate()
        nt.assert_equal(err.exception.args[0],
                        'data must be defined for valid visualization')

        test_obj.data = [Data(name='test'), Data(name='test')]
        with nt.assert_raises(ValidationError) as err:
            test_obj.validate()
        nt.assert_equal(err.exception.args[0], 'data has duplicate names')
Example #15
0
    def test_validation(self):
        """Test Visualization validation"""

        test_obj = Visualization()
        with nt.assert_raises(ValidationError) as err:
            test_obj.validate()
        nt.assert_equal(err.exception.message,
                        'data must be defined for valid visualization')

        test_obj.data = [Data(name='test'), Data(name='test')]
        with nt.assert_raises(ValidationError) as err:
            test_obj.validate()
        nt.assert_equal(err.exception.message,
                        'data has duplicate names')
Example #16
0
    def test_validation_checking(self):
        """Visualization fields are grammar-checked"""

        grammar_errors = [('width', -1, ValueError,
                           'width cannot be negative'),
                          ('height', -1, ValueError,
                           'height cannot be negative'),
                          ('viewport', [1], ValueError,
                           'viewport must have 2 dimensions'),
                          ('viewport', [-1, -1], ValueError,
                           'viewport dimensions cannot be negative'),
                          ('padding', {'top': 2}, ValueError,
                           ('Padding must have keys "top", "left", "right",'
                            ' "bottom".')),
                          ('padding',
                           {'top': 1, 'left': 1, 'right': 1, 'bottom': -1},
                           ValueError, 'Padding cannot be negative.'),
                          ('padding', -1, ValueError,
                           'Padding cannot be negative.')]

        assert_grammar_validation(grammar_errors, Visualization())
Example #17
0
    def test_axis_labeling(self):
        """Test convenience method for axis label setting"""

        #With Axes already in place
        test_obj = Visualization()
        test_obj.axes.extend([Axis(type='x'), Axis(type='y')])
        test_obj.axis_titles(x="test1", y="test2")
        nt.assert_equals(test_obj.axes['x'].title, 'test1')
        nt.assert_equals(test_obj.axes['y'].title, 'test2')

        #With no Axes already defined
        del test_obj.axes[0]
        del test_obj.axes[0]
        test_obj.axis_titles(x="test1", y="test2")
        nt.assert_equals(test_obj.axes['x'].title, 'test1')
        nt.assert_equals(test_obj.axes['y'].title, 'test2')
Example #18
0
    def test_axis_properties(self):

        test_vis = Visualization()
        with nt.assert_raises(ValueError) as err:
            test_vis.x_axis_properties(title_size=20, label_angle=30)
        nt.assert_equals(err.exception.args[0],
                         'This Visualization has no axes!')
        test_vis.axes = [Axis(scale='x'), Axis(scale='y')]
        test_vis.x_axis_properties(title_size=20,
                                   title_offset=10,
                                   label_angle=30,
                                   color='#000')
        test_vis.y_axis_properties(title_size=20,
                                   title_offset=10,
                                   label_angle=30,
                                   color='#000')

        def check_axis_colors():
            for axis in test_vis.axes:
                props = axis.properties
                for prop in [props.title.fill, props.labels.fill]:
                    nt.assert_equals(getattr(prop, 'value'), '#000')
                for prop in [
                        props.axis.stroke, props.major_ticks.stroke,
                        props.minor_ticks.stroke, props.ticks.stroke
                ]:
                    nt.assert_equals(getattr(prop, 'value'), '#000')

        for axis in test_vis.axes:
            props = axis.properties
            nt.assert_equals(props.labels.angle.value, 30)
            nt.assert_equals(props.title.font_size.value, 20)
            nt.assert_equals(props.title.dy.value, 10)
        check_axis_colors()

        test_vis.axes = [Axis(scale='x'), Axis(scale='y')]
        test_vis.common_axis_properties(color='#000')
        for axis in test_vis.axes:
            check_axis_colors()
Example #19
0
    def test_axis_properties(self):

        test_vis = Visualization()
        with nt.assert_raises(ValueError) as err:
            test_vis.x_axis_properties(title_size=20, label_angle=30)
        nt.assert_equals(err.exception.args[0],
                         'This Visualization has no axes!')
        test_vis.axes = [Axis(scale='x'), Axis(scale='y')]
        test_vis.x_axis_properties(title_size=20, title_offset=10,
                                   label_angle=30, color='#000')
        test_vis.y_axis_properties(title_size=20, title_offset=10,
                                   label_angle=30, color='#000')

        def check_axis_colors():
            for axis in test_vis.axes:
                props = axis.properties
                for prop in [props.title.fill, props.labels.fill]:
                    nt.assert_equals(getattr(prop, 'value'), '#000')
                for prop in [props.axis.stroke, props.major_ticks.stroke,
                             props.minor_ticks.stroke, props.ticks.stroke]:
                    nt.assert_equals(getattr(prop, 'value'), '#000')

        for axis in test_vis.axes:
            props = axis.properties
            nt.assert_equals(props.labels.angle.value, 30)
            nt.assert_equals(props.title.font_size.value, 20)
            nt.assert_equals(props.title.dy.value, 10)
        check_axis_colors()

        test_vis.axes = [Axis(scale='x'), Axis(scale='y')]
        test_vis.common_axis_properties(color='#000')
        for axis in test_vis.axes:
            check_axis_colors()