def test_widget_with_default_settings(self):
        """
            Test widget with default settings which is defined in django settings file
        """
        zoom = 15
        default_map_center = [51.5073509, -0.12775829999]
        widget_settings = {
            "GooglePointFieldWidget": (
                ("zoom", zoom),
                ("mapCenterLocation", default_map_center),
            )
        }

        with override_settings(MAP_WIDGETS=widget_settings):
            reload_module(mw_widgets)
            widget = mw_widgets.GooglePointFieldInlineWidget()
            self.assertEqual(hasattr(widget, "settings"), True)
            self.assertEqual(hasattr(widget, "settings_namespace"), True)
            self.assertEqual(isinstance(widget.media, django_forms.Media), True)

            # test `map_options` method
            options_str = widget.map_options()
            options = json.loads(options_str)
            self.assertEqual(options.get("zoom"), zoom)
            self.assertEqual(options.get("mapCenterLocation"), default_map_center)

            # test render with Point object value
            point = Point(-104.9903, 39.73922, srid=DJANGO_DEFAULT_SRID_VALUE)
            widget_html_elem_id = "id_location"
            widget_html_elem_name = "location"
            widget_html_elem_class = "coordinates-input"
            result = widget.render(name=widget_html_elem_name, value=point, attrs={'id': widget_html_elem_id})
            self.assertIn(widget.serialize(point), result)
            self.assertIn(get_textarea_html(widget_html_elem_id, widget_html_elem_name, point, widget_html_elem_class), result)

            # test render with serialized data value
            result = widget.render(name=widget_html_elem_name, value=widget.serialize(point))
            self.assertIn(widget.serialize(point), result)

            # test widget as a formset empty form
            result = widget.render(name=widget_html_elem_name, value=point, attrs={'id': widget_html_elem_id})
            self.assertIn(widget.serialize(point), result)
            inline_widget_data = widget.get_js_widget_data(widget_html_elem_name, widget_html_elem_id)
            self.assertIn(escapejs(json.dumps(inline_widget_data)), result)

            # test widget `attrs` param
            w = mw_widgets.GooglePointFieldInlineWidget(attrs={"max-height": 600})
            self.assertIn("max-height", w.attrs)

            # test widget render `attrs` param with `None` value
            self.assertIn(widget_html_elem_name, w.render(name=widget_html_elem_name, value=None, attrs=None))
示例#2
0
    def test_widget_with_custom_settings(self):
        """
            Test the widget with custom settings which is updated by `settings` parameter
        """
        zoom = 11
        default_map_center = [52.5073509, -0.23775829999]
        widget_settings = {
            "GooglePointFieldWidget": (
                ("zoom", zoom),
                ("mapCenterLocation", default_map_center),
            )
        }

        widget = mw_widgets.GooglePointFieldInlineWidget(
            settings=widget_settings)
        self.assertEqual(hasattr(widget, "settings"), True)
        self.assertEqual(hasattr(widget, "settings_namespace"), True)
        self.assertEqual(isinstance(widget.media, django_forms.Media), True)

        # test `map_options` method
        options_str = widget.map_options()
        options = json.loads(options_str)
        self.assertEqual(options.get("zoom"), zoom)
        self.assertEqual(options.get("mapCenterLocation"), default_map_center)

        # test render with Point object value
        point = Point(-105.9903, 38.73922, srid=DJANGO_DEFAULT_SRID_VALUE)
        widget_html_elem_id = "id_location"
        widget_html_elem_name = "location"
        result = widget.render(name=widget_html_elem_name,
                               value=point,
                               attrs={'id': widget_html_elem_id})
        self.assertIn(widget.serialize(point), result)
        self.assertIn(
            get_textarea_html(widget_html_elem_id, widget_html_elem_name,
                              point), result)

        # test render with serialized data value
        result = widget.render(name=widget_html_elem_name,
                               value=widget.serialize(point))
        self.assertIn(widget.serialize(point), result)

        # test widget as a formset empty form
        widget_html_elem_id = "__prefix__id_location"
        result = widget.render(name=widget_html_elem_name,
                               value=point,
                               attrs={'id': widget_html_elem_id})
        self.assertIn(widget.serialize(point), result)
        inline_widget_data = widget.get_js_widget_data(widget_html_elem_name,
                                                       widget_html_elem_id)
        self.assertIn(escapejs(json.dumps(inline_widget_data)), result)