예제 #1
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'
            })
예제 #2
0
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 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)
예제 #4
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'}
     )
예제 #5
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'}
     )
예제 #6
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",
    }
예제 #7
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'
            }
        )
예제 #8
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",
    }
예제 #9
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'
         }
     )
예제 #10
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'
         }
     )
예제 #11
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",
    }
예제 #12
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'}
     )
예제 #13
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'}
     )