示例#1
0
def test_debc004_get_item_with_nested_children_one_branch():
    c1 = Component(id="1")
    c2 = Component(id="2", children=[c1])
    c3 = Component(children=[c2])
    assert c2["1"] == c1
    assert c3["2"] == c2
    assert c3["1"] == c1
示例#2
0
 def test_set_item_with_children_as_list(self):
     c1 = Component(id='1')
     c2 = Component(id='2', children=[c1])
     self.assertEqual(c2['1'], c1)
     c3 = Component(id='3')
     c2['1'] = c3
     self.assertEqual(c2['3'], c3)
示例#3
0
def test_debc027_component_error_message():
    with pytest.raises(TypeError) as e:
        Component(asdf=True)
    assert str(e.value) == (
        "The `TestComponent` component received an unexpected " +
        "keyword argument: `asdf`\nAllowed arguments: a, children, " +
        "id, style")

    with pytest.raises(TypeError) as e:
        Component(asdf=True, id="my-component")
    assert str(e.value) == (
        "The `TestComponent` component " +
        'with the ID "my-component" received an unexpected ' +
        "keyword argument: `asdf`\nAllowed arguments: a, children, " +
        "id, style")

    with pytest.raises(TypeError) as e:
        html.Div(asdf=True)
    assert str(e.value) == (
        "The `html.Div` component (version {}) ".format(__version__) +
        "received an unexpected " + "keyword argument: `asdf`\n" +
        "Allowed arguments: {}".format(", ".join(sorted(
            html.Div()._prop_names))))

    with pytest.raises(TypeError) as e:
        html.Div(asdf=True, id="my-component")
    assert str(e.value) == (
        "The `html.Div` component (version {}) ".format(__version__) +
        'with the ID "my-component" received an unexpected ' +
        "keyword argument: `asdf`\n" + "Allowed arguments: {}".format(
            ", ".join(sorted(html.Div()._prop_names))))
示例#4
0
 def test_set_item(self):
     c1a = Component(id='1', children='Hello world')
     c2 = Component(id='2', children=c1a)
     self.assertEqual(c2['1'], c1a)
     c1b = Component(id='1', children='Brave new world')
     c2['1'] = c1b
     self.assertEqual(c2['1'], c1b)
示例#5
0
def test_debc015_set_item_with_children_as_list():
    c1 = Component(id="1")
    c2 = Component(id="2", children=[c1])
    assert c2["1"] == c1
    c3 = Component(id="3")
    c2["1"] = c3
    assert c2["3"] == c3
示例#6
0
 def test_get_item_with_nested_children_one_branch(self):
     c1 = Component(id='1')
     c2 = Component(id='2', children=[c1])
     c3 = Component(children=[c2])
     self.assertEqual(c2['1'], c1)
     self.assertEqual(c3['2'], c2)
     self.assertEqual(c3['1'], c1)
示例#7
0
def test_debc014_set_item():
    c1a = Component(id="1", children="Hello world")
    c2 = Component(id="2", children=c1a)
    assert c2["1"] == c1a

    c1b = Component(id="1", children="Brave new world")
    c2["1"] = c1b
    assert c2["1"] == c1b
示例#8
0
 def test_to_plotly_json_without_children(self):
     c = Component(id='a')
     c._prop_names = ('id',)
     c._type = 'MyComponent'
     c._namespace = 'basic'
     self.assertEqual(
         c.to_plotly_json(),
         {'namespace': 'basic', 'props': {'id': 'a'}, 'type': 'MyComponent'}
     )
示例#9
0
def test_debc019_del_item_from_class():
    c1 = Component(id="1")
    c2 = Component(id="2", children=c1)
    assert c2["1"] == c1
    del c2["1"]
    with pytest.raises(KeyError):
        c2["1"]

    assert c2.children is None
示例#10
0
    def test_del_item_from_class(self):
        c1 = Component(id='1')
        c2 = Component(id='2', children=c1)
        self.assertEqual(c2['1'], c1)
        del c2['1']
        with self.assertRaises(KeyError):
            c2['1']

        self.assertEqual(c2.children, None)
示例#11
0
def test_debc005_get_item_with_nested_children_two_branches():
    c1 = Component(id="1")
    c2 = Component(id="2", children=[c1])
    c3 = Component(id="3")
    c4 = Component(id="4", children=[c3])
    c5 = Component(children=[c2, c4])
    assert c2["1"] == c1
    assert c4["3"] == c3
    assert c5["2"] == c2
    assert c5["4"] == c4
    assert c5["1"] == c1
    assert c5["3"] == c3
示例#12
0
 def test_get_item_with_nested_children_two_branches(self):
     c1 = Component(id='1')
     c2 = Component(id='2', children=[c1])
     c3 = Component(id='3')
     c4 = Component(id='4', children=[c3])
     c5 = Component(children=[c2, c4])
     self.assertEqual(c2['1'], c1)
     self.assertEqual(c4['3'], c3)
     self.assertEqual(c5['2'], c2)
     self.assertEqual(c5['4'], c4)
     self.assertEqual(c5['1'], c1)
     self.assertEqual(c5['3'], c3)
示例#13
0
def test_debc018_del_item_from_list():
    c1 = Component(id="1")
    c2 = Component(id="2")
    c3 = Component(id="3", children=[c1, c2])
    assert c3["1"] == c1
    assert c3["2"] == c2
    del c3["2"]
    with pytest.raises(KeyError):
        c3["2"]
    assert c3.children == [c1]

    del c3["1"]
    with pytest.raises(KeyError):
        c3["1"]
    assert c3.children == []
示例#14
0
    def test_del_item_from_list(self):
        c1 = Component(id='1')
        c2 = Component(id='2')
        c3 = Component(id='3', children=[c1, c2])
        self.assertEqual(c3['1'], c1)
        self.assertEqual(c3['2'], c2)
        del c3['2']
        with self.assertRaises(KeyError):
            c3['2']
        self.assertEqual(c3.children, [c1])

        del c3['1']
        with self.assertRaises(KeyError):
            c3['1']
        self.assertEqual(c3.children, [])
示例#15
0
def test_debc013_get_item_raises_key_if_id_doesnt_exist():
    c = Component()
    with pytest.raises(KeyError):
        c["1"]

    c1 = Component(id="1")
    with pytest.raises(KeyError):
        c1["1"]

    c2 = Component(id="2", children=[c1])
    with pytest.raises(KeyError):
        c2["0"]

    c3 = Component(children="string with no id")
    with pytest.raises(KeyError):
        c3["0"]
示例#16
0
    def test_get_item_raises_key_if_id_doesnt_exist(self):
        c = Component()
        with self.assertRaises(KeyError):
            c['1']

        c1 = Component(id='1')
        with self.assertRaises(KeyError):
            c1['1']

        c2 = Component(id='2', children=[c1])
        with self.assertRaises(KeyError):
            c2['0']

        c3 = Component(children='string with no id')
        with self.assertRaises(KeyError):
            c3['0']
示例#17
0
 def test_to_plotly_json_with_children(self):
     c = Component(id='a', children='Hello World')
     c._prop_names = ('id', 'children',)
     c._type = 'MyComponent'
     c._namespace = 'basic'
     self.assertEqual(
         c.to_plotly_json(),
         {
             'namespace': 'basic',
             'props': {
                 'id': 'a',
                 # TODO - Rename 'children' to 'children'
                 'children': 'Hello World'
             },
             'type': 'MyComponent'
         }
     )
    def assertSnapshotEqual(self, component: Component, file_id: str) -> None:
        """
        Tests the supplied component against the specified JSON file snapshot, if it exists.
        If the component and the snapshot match, the test passes. If the specified file is not
        found, it is created and the test passes. This test will only fail if the file already
        exists, and the component-as-JSON does not match the contents of the file. A Dash user can
        set the environment variable "UPDATE_DASH_SNAPSHOTS" to True to replace all existing
        snapshots.

        Parameters
        ----------
        component: Component
            The output of a Dash component that will be rendered to the page
        file_id: str
            A string ID used to distinguish the multiple JSON files that may be used as
            part of a single component's test cases

        Returns
        -------
        None
        """
        assert isinstance(
            component, Component), 'Component passed in must be Dash Component'
        assert isinstance(
            file_id, str), 'must pass in a file id to use as unique file ID'

        filename = self.__get_filename(file_id=file_id)

        component_json = component.to_plotly_json()

        if os.path.exists(filename):
            # Check the env variable to see whether snapshots should be replaced
            if os.environ.get('UPDATE_DASH_SNAPSHOTS') == 'TRUE':
                with open(filename, 'w') as file:
                    json.dump(component_json,
                              file,
                              cls=plotly.utils.PlotlyJSONEncoder)
            else:
                # Load a dumped JSON for the passed-in component, to ensure matches standard format
                expected_dict = json.loads(
                    json.dumps(component_json,
                               cls=plotly.utils.PlotlyJSONEncoder))
                existing_snapshot = self.__load_snapshot(filename=filename)
                try:
                    self.__check_object_equality(expected_dict,
                                                 existing_snapshot,
                                                 context1=expected_dict,
                                                 context2=existing_snapshot)
                except DashSnapshotMismatch as e:
                    self.assertEqual(existing_snapshot, expected_dict,
                                     '\n\nDETAILS:\n\n{}'.format(e))
                self.assertEqual(existing_snapshot, expected_dict)
        else:
            # Component did not already exist, so we'll write to the file
            with open(filename, 'w') as file:
                json.dump(component_json,
                          file,
                          cls=plotly.utils.PlotlyJSONEncoder)
示例#19
0
 def test_to_plotly_json_with_wildcards(self):
     c = Component(id='a', **{'aria-expanded': 'true',
                              'data-toggle': 'toggled',
                              'data-none': None})
     c._prop_names = ('id',)
     c._type = 'MyComponent'
     c._namespace = 'basic'
     self.assertEqual(
         c.to_plotly_json(),
         {'namespace': 'basic',
          'props': {
              'aria-expanded': 'true',
              'data-toggle': 'toggled',
              'data-none': None,
              'id': 'a',
          },
          'type': 'MyComponent'}
     )
示例#20
0
def test_debc008_set_item_anywhere_in_tree():
    keys = ["0.0", "0.1", "0.1.x", "0.1.x.x", "0.1.x.x.0"]
    c = nested_tree()[0]

    # Test setting items starting from the innermost item
    for key in reversed(keys):
        new_id = "new {}".format(key)
        new_component = Component(id=new_id, children="new string")
        c[key] = new_component
        assert c[new_id] == new_component
示例#21
0
def test_set_item_with_nested_children_with_mixed_strings_and_without_lists():
    keys = ["0.0", "0.1", "0.1.x", "0.1.x.x", "0.1.x.x.0"]
    c = nested_tree()[0]

    # Test setting items starting from the innermost item
    for key in reversed(keys):
        new_id = "new {}".format(key)
        new_component = Component(id=new_id, children="new string")
        c[key] = new_component
        assert c[new_id] == new_component
示例#22
0
    def test_set_item_with_nested_children_with_mixed_strings_and_without_lists(
            self):  # noqa: E501
        keys = ['0.0', '0.1', '0.1.x', '0.1.x.x', '0.1.x.x.0']
        c = nested_tree()[0]

        # Test setting items starting from the innermost item
        for i, key in enumerate(reversed(keys)):
            new_id = 'new {}'.format(key)
            new_component = Component(id=new_id, children='new string')
            c[key] = new_component
            self.assertEqual(c[new_id], new_component)
示例#23
0
    def test_to_plotly_json_with_null_arguments(self):
        c = Component(id='a')
        c._prop_names = ('id', 'style',)
        c._type = 'MyComponent'
        c._namespace = 'basic'
        self.assertEqual(
            c.to_plotly_json(),
            {'namespace': 'basic', 'props': {'id': 'a'}, 'type': 'MyComponent'}
        )

        c = Component(id='a', style=None)
        c._prop_names = ('id', 'style',)
        c._type = 'MyComponent'
        c._namespace = 'basic'
        self.assertEqual(
            c.to_plotly_json(),
            {
                'namespace': 'basic', 'props': {'id': 'a', 'style': None},
                'type': 'MyComponent'
            }
        )
示例#24
0
    def test_equality(self):
        # TODO - Why is this the case? How is == being performed?
        # __eq__ only needs __getitem__, __iter__, and __len__
        self.assertTrue(Component() == Component())
        self.assertTrue(Component() is not Component())

        c1 = Component(id='1')
        c2 = Component(id='2', children=[Component()])
        self.assertTrue(c1 == c2)
        self.assertTrue(c1 is not c2)
示例#25
0
def nested_tree():
    """This tree has a few unique properties:
    - children is mixed strings and components (as in c2)
    - children is just components (as in c)
    - children is just strings (as in c1)
    - children is just a single component (as in c3, c4)
    - children contains numbers (as in c2)
    - children contains "None" items (as in c2)
    """
    c1 = Component(
        id='0.1.x.x.0',
        children='string'
    )
    c2 = Component(
        id='0.1.x.x',
        children=[10, None, 'wrap string', c1, 'another string', 4.51]
    )
    c3 = Component(
        id='0.1.x',
        # children is just a component
        children=c2
    )
    c4 = Component(
        id='0.1',
        children=c3
    )
    c5 = Component(id='0.0')
    c = Component(id='0', children=[c5, c4])
    return c, c1, c2, c3, c4, c5
示例#26
0
def test_debc026_component_not_children():
    children = [Component(id="a"), html.Div(id="b"), "c", 1]
    for i in range(len(children)):
        # cycle through each component in each position
        children = children[1:] + [children[0]]

        # use html.Div because only real components accept positional args
        html.Div(children)
        # the first arg is children, and a single component works there
        html.Div(children[0], id="x")

        with pytest.raises(TypeError):
            # If you forget the `[]` around children you get this:
            html.Div(children[0], children[1], children[2], children[3])
示例#27
0
 def test_to_plotly_json_without_children(self):
     c = Component(id='a')
     c._prop_names = ('id',)
     c._type = 'MyComponent'
     c._namespace = 'basic'
     self.assertEqual(
         c.to_plotly_json(),
         {'namespace': 'basic', 'props': {'id': 'a'}, 'type': 'MyComponent'}
     )
示例#28
0
    def test_iter(self):
        # keys, __contains__, items, values, and more are all mixin methods
        # that we get for free by inheriting from the MutableMapping
        # and behave as according to our implementation of __iter__

        c = Component(
            id='1',
            children=[
                Component(id='2', children=[
                    Component(id='3', children=Component(id='4'))
                ]),
                Component(id='5', children=[
                    Component(id='6', children='Hello World')
                ]),
                Component(),
                Component(children='Hello World'),
                Component(children=Component(id='7')),
                Component(children=[Component(id='8')]),
            ]
        )
        # test keys()
        keys = [k for k in list(c.keys())]
        self.assertEqual(keys, ['2', '3', '4', '5', '6', '7', '8'])
        self.assertEqual([i for i in c], keys)

        # test values()
        components = [i for i in list(c.values())]
        self.assertEqual(components, [c[k] for k in keys])

        # test __iter__()
        for k in keys:
            # test __contains__()
            self.assertTrue(k in c)

        # test __items__
        items = [i for i in list(c.items())]
        self.assertEqual(list(zip(keys, components)), items)
示例#29
0
def test_debc020_to_plotly_json_without_children():
    c = Component(id="a")
    c._prop_names = ("id",)
    c._type = "MyComponent"
    c._namespace = "basic"
    assert c.to_plotly_json() == {
        "namespace": "basic",
        "props": {"id": "a"},
        "type": "MyComponent",
    }
示例#30
0
def test_debc022_to_plotly_json_with_children():
    c = Component(id="a", children="Hello World")
    c._prop_names = ("id", "children")
    c._type = "MyComponent"
    c._namespace = "basic"
    assert c.to_plotly_json() == {
        "namespace": "basic",
        "props": {
            "id": "a",
            # TODO - Rename 'children' to 'children'
            "children": "Hello World",
        },
        "type": "MyComponent",
    }
示例#31
0
 def test_to_plotly_json_with_children(self):
     c = Component(id='a', children='Hello World')
     c._prop_names = ('id', 'children',)
     c._type = 'MyComponent'
     c._namespace = 'basic'
     self.assertEqual(
         c.to_plotly_json(),
         {
             'namespace': 'basic',
             'props': {
                 'id': 'a',
                 # TODO - Rename 'children' to 'children'
                 'children': 'Hello World'
             },
             'type': 'MyComponent'
         }
     )
示例#32
0
def test_debc023_to_plotly_json_with_wildcards():
    c = Component(
        id="a", **{"aria-expanded": "true", "data-toggle": "toggled", "data-none": None}
    )
    c._prop_names = ("id",)
    c._type = "MyComponent"
    c._namespace = "basic"
    assert c.to_plotly_json() == {
        "namespace": "basic",
        "props": {
            "aria-expanded": "true",
            "data-toggle": "toggled",
            "data-none": None,
            "id": "a",
        },
        "type": "MyComponent",
    }
示例#33
0
 def test_to_plotly_json_with_wildcards(self):
     c = Component(id='a', **{'aria-expanded': 'true',
                              'data-toggle': 'toggled',
                              'data-none': None})
     c._prop_names = ('id',)
     c._type = 'MyComponent'
     c._namespace = 'basic'
     self.assertEqual(
         c.to_plotly_json(),
         {'namespace': 'basic',
          'props': {
              'aria-expanded': 'true',
              'data-toggle': 'toggled',
              'data-none': None,
              'id': 'a',
          },
          'type': 'MyComponent'}
     )
示例#34
0
def test_debc016_set_item_with_nested_children():
    c1 = Component(id="1")
    c2 = Component(id="2", children=[c1])
    c3 = Component(id="3")
    c4 = Component(id="4", children=[c3])
    c5 = Component(id="5", children=[c2, c4])

    c3b = Component(id="3")
    assert c5["3"] == c3
    assert c5["3"] != "3"
    assert c5["3"] is not c3b

    c5["3"] = c3b
    assert c5["3"] is c3b
    assert c5["3"] is not c3

    c2b = Component(id="2")
    c5["2"] = c2b
    assert c5["4"] is c4
    assert c5["2"] is not c2
    assert c5["2"] is c2b
    with pytest.raises(KeyError):
        c5["1"]
示例#35
0
    def test_set_item_with_nested_children(self):
        c1 = Component(id='1')
        c2 = Component(id='2', children=[c1])
        c3 = Component(id='3')
        c4 = Component(id='4', children=[c3])
        c5 = Component(id='5', children=[c2, c4])

        c3b = Component(id='3')
        self.assertEqual(c5['3'], c3)
        self.assertTrue(c5['3'] is not '3')
        self.assertTrue(c5['3'] is not c3b)

        c5['3'] = c3b
        self.assertTrue(c5['3'] is c3b)
        self.assertTrue(c5['3'] is not c3)

        c2b = Component(id='2')
        c5['2'] = c2b
        self.assertTrue(c5['4'] is c4)
        self.assertTrue(c5['2'] is not c2)
        self.assertTrue(c5['2'] is c2b)
        with self.assertRaises(KeyError):
            c5['1']
示例#36
0
 def test_pop(self):
     c2 = Component(id='2')
     c = Component(id='1', children=c2)
     c2_popped = c.pop('2')
     self.assertTrue('2' not in c)
     self.assertTrue(c2_popped is c2)
示例#37
0
    def test_to_plotly_json_with_nested_children(self):
        c1 = Component(id='1', children='Hello World')
        c1._prop_names = ('id', 'children',)
        c1._type = 'MyComponent'
        c1._namespace = 'basic'

        c2 = Component(id='2', children=c1)
        c2._prop_names = ('id', 'children',)
        c2._type = 'MyComponent'
        c2._namespace = 'basic'

        c3 = Component(id='3', children='Hello World')
        c3._prop_names = ('id', 'children',)
        c3._type = 'MyComponent'
        c3._namespace = 'basic'

        c4 = Component(id='4', children=[c2, c3])
        c4._prop_names = ('id', 'children',)
        c4._type = 'MyComponent'
        c4._namespace = 'basic'

        def to_dict(id, children):
            return {
                'namespace': 'basic',
                'props': {
                    'id': id,
                    'children': children
                },
                'type': 'MyComponent'
            }

        """