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)
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' } """
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)
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)
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' })
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
def test_debc021_to_plotly_json_with_null_arguments(): c = Component(id="a") c._prop_names = ("id", "style") c._type = "MyComponent" c._namespace = "basic" assert 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" assert c.to_plotly_json() == { "namespace": "basic", "props": { "id": "a", "style": None }, "type": "MyComponent", }
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
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))))
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
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
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)
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)
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
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, [])
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 == []
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']
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"]
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'} )
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
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", }
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
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)
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)
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
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", }
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])
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", }
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 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'} )