-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
96 additions
and
184 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
|
||
# Python | ||
__pycache__ | ||
.hypothesis | ||
.venv | ||
|
||
# Coverage | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,29 @@ | ||
import pytest | ||
from pydantic import BaseModel | ||
|
||
from persil import regex | ||
from persil.stream import Stream, from_stream | ||
from datetime import datetime | ||
|
||
from hypothesis import given | ||
from hypothesis import strategies as st | ||
|
||
class Flight(BaseModel): | ||
carrier: str | ||
flight_number: int | ||
|
||
from persil import regex, string | ||
from persil.stream import Stream, from_stream | ||
|
||
@from_stream("Flight parser") | ||
def flight_parser(stream: Stream[str]) -> Flight: | ||
carrier = stream.apply(regex(r"[A-Z]{2}")) | ||
flight_number = stream.apply(regex(r"\d{2,4}").map(int)) | ||
year_parser = regex(r"\d{4}").map(int) | ||
month_parser = regex(r"(?:0\d|1[012])").map(int) | ||
day_parser = regex(r"(?:[012]\d|3[01])").map(int) | ||
|
||
return Flight(carrier=carrier, flight_number=flight_number) | ||
|
||
@from_stream | ||
def datetime_parser(stream: Stream[str]) -> datetime: | ||
year = stream.apply(year_parser) | ||
stream.apply(string("-")) | ||
month = stream.apply(month_parser) | ||
stream.apply(string("-")) | ||
day = stream.apply(day_parser) | ||
|
||
EXAMPLES = [ | ||
("AF071", Flight(carrier="AF", flight_number=71)), | ||
("LY180", Flight(carrier="LY", flight_number=180)), | ||
] | ||
return datetime(year, month, day) | ||
|
||
|
||
@pytest.mark.parametrize("message,expected", EXAMPLES) | ||
def test_generate(message: str, expected: Flight): | ||
flight = flight_parser.parse(message) | ||
assert flight == expected | ||
@given(st.datetimes()) | ||
def test_generate(dt: datetime): | ||
dt = datetime(dt.year, dt.month, dt.day) | ||
text = dt.strftime("%Y-%m-%d") | ||
assert datetime_parser.parse(text) == dt |
This file was deleted.
Oops, something went wrong.