|
11 | 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | | - |
| 14 | +import schemathesis |
| 15 | +from hypothesis import given, settings, HealthCheck, Phase, Verbosity |
15 | 16 | from DataDriver import DataDriver # type: ignore |
| 17 | +from robot.api.deco import keyword |
16 | 18 | from robot.running.model import TestCase, TestSuite # type: ignore |
17 | 19 | from robot.result.model import TestSuite as ResultTestSuite # type: ignore |
18 | 20 | from robot.result.model import TestCase as ResultTestCase # type: ignore |
| 21 | +from robotlibcore import DynamicCore # type: ignore |
| 22 | +from DataDriver.AbstractReaderClass import AbstractReaderClass # type: ignore |
| 23 | +from DataDriver.ReaderConfig import TestCaseData # type: ignore |
19 | 24 |
|
20 | 25 | __version__ = "0.1.0" |
21 | 26 |
|
22 | 27 |
|
23 | | -class SchemathesisLibrary: |
| 28 | +class SchemathesisReader(AbstractReaderClass): |
| 29 | + def __init__(self, *, url: "str|None" = None): |
| 30 | + self.url = url |
| 31 | + |
| 32 | + def get_data_from_source( |
| 33 | + self, |
| 34 | + ): # This method will be called from DataDriver to get the TestCaseData list. |
| 35 | + schema = schemathesis.from_uri(self.url) |
| 36 | + all_cases = [] |
| 37 | + for op in schema.get_all_operations(): |
| 38 | + op_as_strategy = op.ok().as_strategy() |
| 39 | + for case in generate_examples(op_as_strategy, 10): |
| 40 | + args = { |
| 41 | + "${case}": case, |
| 42 | + } |
| 43 | + all_cases.append( |
| 44 | + TestCaseData(test_case_name=str(case.id), arguments=args) |
| 45 | + ) |
| 46 | + return all_cases |
| 47 | + |
| 48 | + |
| 49 | +class SchemathesisLibrary(DynamicCore): |
24 | 50 | ROBOT_LIBRARY_VERSION = __version__ |
25 | 51 | ROBOT_LISTENER_API_VERSION = 3 |
26 | 52 | ROBOT_LIBRARY_SCOPE = "TEST SUITE" |
27 | 53 |
|
28 | | - def __init__(self): |
| 54 | + def __init__(self, *, url: "str|None" = None): |
29 | 55 | self.ROBOT_LIBRARY_LISTENER = self |
30 | | - self.data_driver = DataDriver() |
| 56 | + self.data_driver = DataDriver( |
| 57 | + reader_class=SchemathesisReader(url=url), file_regex="" |
| 58 | + ) |
| 59 | + DynamicCore.__init__(self, []) |
31 | 60 |
|
32 | 61 | def _start_suite(self, data: TestSuite, result: ResultTestSuite): |
33 | 62 | self.data_driver._start_suite(data, result) |
34 | 63 |
|
35 | 64 | def _start_test(self, data: TestCase, result: ResultTestCase): |
36 | 65 | self.data_driver._start_test(data, result) |
| 66 | + |
| 67 | + @keyword |
| 68 | + def call_and_validate(self, case: schemathesis.Case) -> None: |
| 69 | + """Validate a Schemathesis case.""" |
| 70 | + case.call_and_validate() |
| 71 | + |
| 72 | + |
| 73 | +def generate_examples(strategy, number) -> list[schemathesis.Case]: |
| 74 | + examples = [] |
| 75 | + |
| 76 | + @given(strategy) |
| 77 | + @settings( |
| 78 | + database=None, |
| 79 | + max_examples=number, |
| 80 | + deadline=None, |
| 81 | + verbosity=Verbosity.quiet, |
| 82 | + phases=(Phase.generate,), |
| 83 | + suppress_health_check=list(HealthCheck), |
| 84 | + ) |
| 85 | + def example_generating_inner_function(ex): |
| 86 | + examples.append(ex) |
| 87 | + |
| 88 | + example_generating_inner_function() |
| 89 | + return examples |
0 commit comments