Skip to content

Commit

Permalink
Add test and make pretty
Browse files Browse the repository at this point in the history
  • Loading branch information
ahopkins committed Jun 29, 2022
1 parent 924ce77 commit e732953
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions tests/extra/test_validation_dataclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
from typing import List, Optional

import pytest
from sanic import json
from sanic.views import HTTPMethodView

from sanic_ext import validate
from sanic_ext.extras.validation.check import check_data
from sanic_ext.extras.validation.schema import make_schema, parse_hint

Expand Down Expand Up @@ -239,3 +242,28 @@ def test_modeling_union_type():
check_data(models.ModelUnionTypeStrNone, {"foo": None}, schema)
with pytest.raises(TypeError):
check_data(models.ModelUnionTypeStrNone, {"foo": 0}, schema)


def test_validate_decorator(app):
@dataclass
class Pet:
name: str

@app.post("/function")
@validate(json=Pet)
async def handler(_, body: Pet):
return json({"is_pet": isinstance(body, Pet)})

class MethodView(HTTPMethodView, attach=app, uri="/method"):
decorators = [validate(json=Pet)]

async def post(self, _, body: Pet):
return json({"is_pet": isinstance(body, Pet)})

_, response = app.test_client.post("/function", json={"name": "Snoopy"})
assert response.status == 200
assert response.json["is_pet"]

_, response = app.test_client.post("/method", json={"name": "Snoopy"})
assert response.status == 200
assert response.json["is_pet"]

0 comments on commit e732953

Please sign in to comment.