Coverage for structured_tutorials / models / tests.py: 100%

16 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2025-12-21 19:08 +0100

1# Copyright (c) 2025 Mathias Ertl 

2# Licensed under the MIT License. See LICENSE file for details. 

3 

4"""Test specifications for commands.""" 

5 

6import re 

7from typing import Annotated, Literal 

8 

9from pydantic import BaseModel, BeforeValidator, ConfigDict, Field 

10 

11from structured_tutorials.models.base import CommandBaseModel, CommandType, TestSpecificationMixin 

12from structured_tutorials.models.validators import validate_regex 

13 

14 

15class TestCommandModel(TestSpecificationMixin, CommandBaseModel): 

16 """Test a command by running another command.""" 

17 

18 model_config = ConfigDict(extra="forbid") 

19 

20 command: CommandType = Field(description="The command to run.") 

21 

22 

23class TestPortModel(TestSpecificationMixin, BaseModel): 

24 """Test a command by checking if a port is open.""" 

25 

26 model_config = ConfigDict(extra="forbid") 

27 

28 host: str = Field(description="The host to connect to.") 

29 port: Annotated[int, Field(ge=0, le=65535)] = Field(description="The port to connect to.") 

30 

31 

32class TestOutputModel(BaseModel): 

33 """Test a command by checking the output of a command.""" 

34 

35 model_config = ConfigDict(extra="forbid") 

36 

37 stream: Literal["stdout", "stderr"] = Field(default="stdout", description="The output stream to use.") 

38 regex: Annotated[re.Pattern[bytes], BeforeValidator(validate_regex)] = Field( 

39 description="A regular expression to test." 

40 )