def test_dashboard_tags(self):
        strategy = DashboardTagsStrategy(['tag1'])

        result = sorted(strategy.get_urls())
        expected = []
        self.assertEqual(result, expected)

        # tag dashboard 3 with `tag1`
        tag1 = get_tag('tag1', db.session, TagTypes.custom)
        object_id = 3
        tagged_object = TaggedObject(
            tag_id=tag1.id,
            object_id=object_id,
            object_type=ObjectTypes.dashboard,
        )
        db.session.add(tagged_object)
        db.session.commit()

        result = sorted(strategy.get_urls())
        expected = [
            f'{TEST_URL}/?form_data=%7B%27slice_id%27%3A+31%7D',
        ]
        self.assertEqual(result, expected)

        strategy = DashboardTagsStrategy(['tag2'])

        result = sorted(strategy.get_urls())
        expected = []
        self.assertEqual(result, expected)

        # tag chart 30 with `tag2`
        tag2 = get_tag('tag2', db.session, TagTypes.custom)
        object_id = 30
        tagged_object = TaggedObject(
            tag_id=tag2.id,
            object_id=object_id,
            object_type=ObjectTypes.chart,
        )
        db.session.add(tagged_object)
        db.session.commit()

        result = sorted(strategy.get_urls())
        expected = [
            f'{TEST_URL}/?form_data=%7B%27slice_id%27%3A+30%7D',
        ]
        self.assertEqual(result, expected)

        strategy = DashboardTagsStrategy(['tag1', 'tag2'])

        result = sorted(strategy.get_urls())
        expected = [
            f'{TEST_URL}/?form_data=%7B%27slice_id%27%3A+30%7D',
            f'{TEST_URL}/?form_data=%7B%27slice_id%27%3A+31%7D',
        ]
        self.assertEqual(result, expected)
    def test_dashboard_tags(self):
        tag1 = get_tag("tag1", db.session, TagTypes.custom)
        # delete first to make test idempotent
        self.reset_tag(tag1)

        strategy = DashboardTagsStrategy(["tag1"])
        result = sorted(strategy.get_urls())
        expected = []
        self.assertEqual(result, expected)

        # tag dashboard 'births' with `tag1`
        tag1 = get_tag("tag1", db.session, TagTypes.custom)
        dash = self.get_dash_by_slug("births")
        tag1_urls = sorted([f"{URL_PREFIX}{slc.url}" for slc in dash.slices])
        tagged_object = TaggedObject(tag_id=tag1.id,
                                     object_id=dash.id,
                                     object_type=ObjectTypes.dashboard)
        db.session.add(tagged_object)
        db.session.commit()

        self.assertEqual(sorted(strategy.get_urls()), tag1_urls)

        strategy = DashboardTagsStrategy(["tag2"])
        tag2 = get_tag("tag2", db.session, TagTypes.custom)
        self.reset_tag(tag2)

        result = sorted(strategy.get_urls())
        expected = []
        self.assertEqual(result, expected)

        # tag first slice
        dash = self.get_dash_by_slug("unicode-test")
        slc = dash.slices[0]
        tag2_urls = [f"{URL_PREFIX}{slc.url}"]
        object_id = slc.id
        tagged_object = TaggedObject(tag_id=tag2.id,
                                     object_id=object_id,
                                     object_type=ObjectTypes.chart)
        db.session.add(tagged_object)
        db.session.commit()

        result = sorted(strategy.get_urls())
        self.assertEqual(result, tag2_urls)

        strategy = DashboardTagsStrategy(["tag1", "tag2"])

        result = sorted(strategy.get_urls())
        expected = sorted(tag1_urls + tag2_urls)
        self.assertEqual(result, expected)
Ejemplo n.º 3
0
    def post(self, object_type, object_id):  # pylint: disable=no-self-use
        """Add new tags to an object."""
        if object_id == 0:
            return Response(status=404)

        tagged_objects = []
        for name in request.get_json(force=True):
            if ":" in name:
                type_name = name.split(":", 1)[0]
                type_ = TagTypes[type_name]
            else:
                type_ = TagTypes.custom

            tag = db.session.query(Tag).filter_by(name=name,
                                                  type=type_).first()
            if not tag:
                tag = Tag(name=name, type=type_)

            tagged_objects.append(
                TaggedObject(object_id=object_id,
                             object_type=object_type,
                             tag=tag))

        db.session.add_all(tagged_objects)
        db.session.commit()

        return Response(status=201)  # 201 CREATED