Skip to content

Commit

Permalink
fix bulk schema and api tests
Browse files Browse the repository at this point in the history
  • Loading branch information
villebro committed Dec 12, 2024
1 parent a62349b commit 353b236
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 41 deletions.
31 changes: 8 additions & 23 deletions superset/tags/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
openapi_spec_methods_override,
TaggedObjectEntityResponseSchema,
TagGetResponseSchema,
TagPostBulkResponseSchema,
TagPostBulkSchema,
TagPostSchema,
TagPutSchema,
Expand Down Expand Up @@ -132,6 +133,8 @@ class TagRestApi(BaseSupersetModelRestApi):
openapi_spec_component_schemas = (
TagGetResponseSchema,
TaggedObjectEntityResponseSchema,
TagPostBulkResponseSchema,
TagPostBulkSchema,
)
apispec_parameter_schemas = {
"delete_tags_schema": delete_tags_schema,
Expand Down Expand Up @@ -211,40 +214,21 @@ def bulk_create(self) -> Response:
"""Bulk create tags and tagged objects
---
post:
summary: Get all objects associated with a tag
parameters:
- in: path
schema:
type: integer
name: tag_id
summary: Bulk create tags and tagged objects
requestBody:
description: Tag schema
required: true
content:
application/json:
schema:
type: object
properties:
tags:
description: list of tag names to add to object
type: array
items:
type: string
objects_to_tag:
description: list of object names to add to object
type: array
items:
type: array
$ref: '#/components/schemas/TagPostBulkSchema'
responses:
200:
description: Tag added to favorites
description: Bulk created tags and tagged objects
content:
application/json:
schema:
type: object
properties:
result:
type: object
$ref: '#/components/schemas/TagPostBulkResponseSchema'
302:
description: Redirects to the current digest
400:
Expand All @@ -267,6 +251,7 @@ def bulk_create(self) -> Response:
tagged_item: dict[str, Any] = self.add_model_schema.load(
{
"name": tag.get("name"),
"description": tag.get("description"),
"objects_to_tag": tag.get("objects_to_tag"),
}
)
Expand Down
30 changes: 23 additions & 7 deletions superset/tags/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,37 @@ class TaggedObjectEntityResponseSchema(Schema):
changed_on = fields.DateTime()
created_by = fields.Nested(UserSchema(exclude=["username"]))
creator = fields.String()
tags = fields.List(fields.Nested(TagGetResponseSchema))
owners = fields.List(fields.Nested(UserSchema))
tags = fields.List(fields.Nested(TagGetResponseSchema()))
owners = fields.List(fields.Nested(UserSchema()))


objects_to_tag_field = fields.List(
fields.Tuple(
(
fields.String(metadata={"description": "type of resource"}),
fields.Int(validate=Range(min=1), metadata={"description": "resource id"}),
),
),
metadata={
"description": "Objects to tag",
},
required=False,
)


class TagObjectSchema(Schema):
name = fields.String(validate=Length(min=1))
description = fields.String(required=False, allow_none=True)
objects_to_tag = fields.List(
fields.Tuple((fields.String(), fields.Int(validate=Range(min=1)))),
required=False,
)
objects_to_tag = objects_to_tag_field


class TagPostBulkSchema(Schema):
tags = fields.List(fields.Nested(TagObjectSchema))
tags = fields.List(fields.Nested(TagObjectSchema()))


class TagPostBulkResponseSchema(Schema):
objects_tagged = objects_to_tag_field
objects_skipped = objects_to_tag_field


class TagPostSchema(TagObjectSchema):
Expand Down
22 changes: 11 additions & 11 deletions tests/integration_tests/tags/api_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ def test_post_tag(self):
)
rv = self.client.post(
uri,
json={"name": "my_tag", "objects_to_tag": [["dashboard", dashboard.id]]},
json={"name": "my_tag", "objects_to_tag": [["DASHBOARD", dashboard.id]]},
)

assert rv.status_code == 201
Expand All @@ -547,7 +547,7 @@ def test_post_tag_no_name_400(self):
)
rv = self.client.post(
uri,
json={"name": "", "objects_to_tag": [["dashboard", dashboard.id]]},
json={"name": "", "objects_to_tag": [["DASHBOARD", dashboard.id]]},
)

assert rv.status_code == 400
Expand Down Expand Up @@ -601,28 +601,28 @@ def test_post_bulk_tag(self):
{
"name": "tag1",
"objects_to_tag": [
["dashboard", dashboard.id],
["chart", chart.id],
["DASHBOARD", dashboard.id],
["CHART", chart.id],
],
},
{
"name": "tag2",
"objects_to_tag": [["dashboard", dashboard.id]],
"objects_to_tag": [["DASHBOARD", dashboard.id]],
},
{
"name": "tag3",
"objects_to_tag": [["chart", chart.id]],
"objects_to_tag": [["CHART", chart.id]],
},
]
},
)

assert rv.status_code == 200

result = TagDAO.get_tagged_objects_for_tags(tags, ["dashboard"])
result = TagDAO.get_tagged_objects_for_tags(tags, ["DASHBOARD"])
assert len(result) == 1

result = TagDAO.get_tagged_objects_for_tags(tags, ["chart"])
result = TagDAO.get_tagged_objects_for_tags(tags, ["CHART"])
assert len(result) == 1

tagged_objects = (
Expand Down Expand Up @@ -671,16 +671,16 @@ def test_post_bulk_tag_skipped_tags_perm(self):
{
"name": "tag1",
"objects_to_tag": [
["dashboard", alpha_dash.id],
["DASHBOARD", alpha_dash.id],
],
},
{
"name": "tag2",
"objects_to_tag": [["dashboard", dashboard.id]],
"objects_to_tag": [["DASHBOARD", dashboard.id]],
},
{
"name": "tag3",
"objects_to_tag": [["chart", chart.id]],
"objects_to_tag": [["CHART", chart.id]],
},
]
},
Expand Down

0 comments on commit 353b236

Please sign in to comment.