Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pytest-examples and make examples in docs testable #56

Merged
merged 7 commits into from May 17, 2023

Conversation

hramezani
Copy link
Member

Fixes #33

@codecov-commenter
Copy link

codecov-commenter commented May 14, 2023

Codecov Report

Patch coverage: 100.00% and no project coverage change.

Comparison is base (142b9cf) 95.37% compared to head (cbd1d2d) 95.37%.

❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more.

Additional details and impacted files
@@           Coverage Diff           @@
##             main      #56   +/-   ##
=======================================
  Coverage   95.37%   95.37%           
=======================================
  Files           5        5           
  Lines         303      303           
  Branches       69       69           
=======================================
  Hits          289      289           
  Misses         12       12           
  Partials        2        2           
Impacted Files Coverage Δ
pydantic_settings/sources.py 95.34% <100.00%> (ø)

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

Copy link
Member

@samuelcolvin samuelcolvin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see the test code using pytest-examples - I guess you need to add that file?

docs/index.md Outdated
Comment on lines 37 to 38
auth_key: str = Field('', validation_alias='my_auth_key')
api_key: str = Field('', validation_alias='my_api_key')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the idea here was that these fields were required - then set via env vars. Probably best if you can set the appropriate env vars in the test so this works without modifiation.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

docs/index.md Outdated
Comment on lines 175 to 188
# Set environment variables
os.environ['V0'] = '0'
os.environ['SUB_MODEL'] = '{"v1": "json-1", "v2": "json-2"}'
os.environ['SUB_MODEL__V2'] = 'nested-2'
os.environ['SUB_MODEL__V3'] = '3'
os.environ['SUB_MODEL__DEEP__V4'] = 'v4'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe you can see these in the test?

Copy link
Member Author

@hramezani hramezani May 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, didn't get this comment. Do you mean to convert them to a test function here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can move all this logic into test setup.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

e.g. set a bunch of env variables before running all or specific tests, then remove them after.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a fixture

docs/index.md Outdated

print(Settings().model_dump())

pprint(Settings().model_dump())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think you should just use print, pytest-examples should take care of formatting the output with black.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't work with print.

――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― test_docs_examples[docs/index.md:14-72] ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
Print output changed code:
  --- before
  +++ after
  @@ -56,30 +56,21 @@
       model_config = ConfigDict(env_prefix='my_prefix_')  # defaults to no prefix, i.e. ""
 
 
   print(Settings().model_dump())
   """
  -{
  -    'auth_key': '',
  -    'api_key': '',
  -    'redis_dsn': Url('redis://user:pass@localhost:6379/1'),
  -    'pg_dsn': Url('postgres://user:pass@localhost:5432/foobar'),
  -    'amqp_dsn': Url('amqp://user:pass@localhost:5672/'),
  -    'special_function': <built-in function cos>,
  -    'domains': set(),
  -    'more_settings': {'foo': 'bar', 'apple': 1},
  -}
  +{'auth_key': '', 'api_key': '', 'redis_dsn': Url('redis://user:pass@localhost:6379/1'), 'pg_dsn': Url('postgres://user:pass@localhost:5432/foobar'), 'amqp_dsn': Url('amqp://user:pass@localhost:5672/'), 'special_function': <built-in function cos>, 'domains': set(), 'more_settings': {'foo': 'bar', 'apple': 1}}
   """

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I've replaced it with print but it doesn't work with formatted output.
we can ignore it for now.

@hramezani hramezani force-pushed the pytest_examples branch 2 times, most recently from e3b3e50 to 553dce5 Compare May 15, 2023 11:28
Copy link
Member

@samuelcolvin samuelcolvin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

otherwise LGTM, I committed to fix the issue with formatting the first example.

docs/index.md Outdated
Comment on lines 175 to 188
# Set environment variables
os.environ['V0'] = '0'
os.environ['SUB_MODEL'] = '{"v1": "json-1", "v2": "json-2"}'
os.environ['SUB_MODEL__V2'] = 'nested-2'
os.environ['SUB_MODEL__V3'] = '3'
os.environ['SUB_MODEL__DEEP__V4'] = 'v4'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can move all this logic into test setup.

docs/index.md Outdated
Comment on lines 175 to 188
# Set environment variables
os.environ['V0'] = '0'
os.environ['SUB_MODEL'] = '{"v1": "json-1", "v2": "json-2"}'
os.environ['SUB_MODEL__V2'] = 'nested-2'
os.environ['SUB_MODEL__V3'] = '3'
os.environ['SUB_MODEL__DEEP__V4'] = 'v4'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

e.g. set a bunch of env variables before running all or specific tests, then remove them after.

pyproject.toml Outdated Show resolved Hide resolved
@hramezani
Copy link
Member Author

@samuelcolvin It's ready

@samuelcolvin samuelcolvin merged commit 411b88f into main May 17, 2023
18 checks passed
@samuelcolvin samuelcolvin deleted the pytest_examples branch May 17, 2023 08:17
@samuelcolvin
Copy link
Member

Thanks so much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

enable pytest-examples to test the docs examples
3 participants