-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathtest_validation.py
160 lines (139 loc) · 5.78 KB
/
test_validation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# --------------------------------------------------------------------------
#
# Copyright (c) Microsoft Corporation. All rights reserved.
#
# The MIT License (MIT)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the ""Software""), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
#
# --------------------------------------------------------------------------
import unittest
import subprocess
import sys
import isodate
import tempfile
import json
from datetime import date, datetime, timedelta
import os
from os.path import dirname, pardir, join, realpath
from msrest.exceptions import DeserializationError, ValidationError
from validation import AutoRestValidationTest
from validation.models import (
Product,
ConstantProduct,
ChildProduct)
import pytest
@pytest.fixture
def client():
with AutoRestValidationTest("abc123", base_url="http://localhost:3000") as client:
client.api_version = "12-34-5678"
yield client
@pytest.fixture
def constant_body():
"""This is NOT considering the constant body, this should work with
the commented line.
See https://github.com/Azure/autorest.modelerfour/issues/83
"""
#return Product(child=ChildProduct())
return Product(
child=ChildProduct(),
const_child=ConstantProduct(),
)
class TestValidation(object):
def test_with_constant_in_path(self, client):
client.get_with_constant_in_path()
def test_post_with_constant_in_body(self, client, constant_body):
product = client.post_with_constant_in_body(body=constant_body)
assert product is not None
def test_min_length_validation(self, client):
try:
client.validation_of_method_parameters("1", 100)
except ValidationError as err:
assert err.rule == "min_length"
assert err.target == "resource_group_name"
def test_max_length_validation(self, client):
try:
client.validation_of_method_parameters("1234567890A", 100)
except ValidationError as err:
assert err.rule == "max_length"
assert err.target == "resource_group_name"
def test_pattern_validation(self, client):
try:
client.validation_of_method_parameters("!@#$", 100)
except ValidationError as err:
assert err.rule == "pattern"
assert err.target == "resource_group_name"
def test_multiple_validation(self, client):
try:
client.validation_of_method_parameters("123", 105)
except ValidationError as err:
assert err.rule == "multiple"
assert err.target == "id"
def test_minimum_validation(self, client):
try:
client.validation_of_method_parameters("123", 0)
except ValidationError as err:
assert err.rule == "minimum"
assert err.target == "id"
def test_maximum_validation(self, client):
try:
client.validation_of_method_parameters("123", 2000)
except ValidationError as err:
assert err.rule == "maximum"
assert err.target == "id"
def test_minimum_ex_validation(self, client, constant_body):
try:
constant_body.capacity = 0
client.validation_of_body("123", 150, constant_body)
except ValidationError as err:
assert err.rule == "minimum_ex"
assert "capacity" in err.target
def test_maximum_ex_validation(self, client, constant_body):
try:
constant_body.capacity = 100
client.validation_of_body("123", 150, constant_body)
except ValidationError as err:
assert err.rule == "maximum_ex"
assert "capacity" in err.target
def test_max_items_validation(self, client, constant_body):
try:
constant_body.display_names = ["item1","item2","item3","item4","item5","item6","item7"]
client.validation_of_body("123", 150, constant_body)
except ValidationError as err:
assert err.rule == "max_items"
assert "display_names" in err.target
@pytest.mark.xfail(reason="https://github.com/Azure/autorest.modelerfour/issues/90")
def test_api_version_validation(self, client):
client = AutoRestValidationTest(
"abc123",
base_url="http://localhost:3000")
client.api_version = "abc"
try:
client.validation_of_method_parameters("123", 150)
except ValidationError as err:
assert err.rule == "pattern"
assert err.target == "self.api_version"
def test_models(self):
from validation.models import Error
if sys.version_info >= (3,5):
from validation.models._models_py3 import Error as ErrorPy3
assert Error == ErrorPy3
else:
from validation.models._models import Error as ErrorPy2
assert Error == ErrorPy2