Skip to content

Commit

Permalink
[NEW] Get Object Fixture
Browse files Browse the repository at this point in the history
Pytest Object Getter v0.1.0 Release
  • Loading branch information
boromir674 committed Jun 2, 2022
2 parents 351956a + 5230ad4 commit e8aaee8
Show file tree
Hide file tree
Showing 14 changed files with 395 additions and 181 deletions.
23 changes: 23 additions & 0 deletions CHANGELOG.rst
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,29 @@
Changelog
=========

0.1.0 (2022-06-02)
==================

First `Pytest Object Getter` release!

We provide the *get_object* pytest fixture!

Changes
^^^^^^^

feature
"""""""
- provide fixtures featuring mocking capabilities

documentation
"""""""""""""
- document features and usage

build
"""""
- add mypy dependency in 'typing' extras


0.0.1 (2022-06-01)
=======================================

Expand Down
55 changes: 50 additions & 5 deletions README.rst
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ Features

1. **pytest_object_getter** `python package`

a. TODO Document a **Great Feature**
b. TODO Document another **Nice Feature**
a. Dynamically import an object from a module
b. Optionally mock any object that is present in the module's namespace
c. Construct the mock object at runtime
2. Tested against multiple `platforms` and `python` versions


Expand Down Expand Up @@ -61,8 +62,52 @@ Using `pip` is the approved way for installing `pytest_object_getter`.
python3 -m pip install pytest_object_getter
Let's see how to write a test and use the 'get_object' fixture to mock
the `requests.get` method to avoid actual network communication:

TODO Document a use case
.. code-block:: shell
python3 -m pip install ask-pypi
.. code-block:: python
import pytest
@pytest.fixture
def mock_response():
def init(self, package_name: str):
self.status_code = 200 if package_name == 'existing-package' else 404
return type('MockResponse', (), {
'__init__': init
})
@pytest.fixture
def create_mock_requests(mock_response):
def _create_mock_requests():
def mock_get(*args, **kwargs):
package_name = args[0].split('/')[-1]
return mock_response(package_name)
return type('MockRequests', (), {
'get': mock_get,
})
return _create_mock_requests
def test_fixture(get_object, create_mock_requests):
from ask_pypi import is_pypi_project
assert is_pypi_project('numpy') == True
assert is_pypi_project('pandas') == True
assert is_pypi_project('existing-package') == False
get_object('is_project', 'ask_pypi.pypi_project',
overrides={'requests': lambda: create_mock_requests()})
assert is_pypi_project('existing-package') == True
assert is_pypi_project('numpy') == False
assert is_pypi_project('pandas') == False
assert is_pypi_project('so-magic') == False
License
Expand Down Expand Up @@ -141,9 +186,9 @@ License

.. Github Releases & Tags
.. |commits_since_specific_tag_on_master| image:: https://img.shields.io/github/commits-since/boromir674/pytest-object-getter/v0.0.1/master?color=blue&logo=github
.. |commits_since_specific_tag_on_master| image:: https://img.shields.io/github/commits-since/boromir674/pytest-object-getter/v0.1.0/master?color=blue&logo=github
:alt: GitHub commits since tagged version (branch)
:target: https://github.com/boromir674/pytest-object-getter/compare/v0.0.1..master
:target: https://github.com/boromir674/pytest-object-getter/compare/v0.1.0..master

.. |commits_since_latest_github_release| image:: https://img.shields.io/github/commits-since/boromir674/pytest-object-getter/latest?color=blue&logo=semver&sort=semver
:alt: GitHub commits since latest release (by SemVer)
Expand Down
7 changes: 4 additions & 3 deletions docs/contents/10_introduction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ Introduction

| This is **Pytest Object Getter**, a *Python Package* desinged to ...
| Goal of this project is to TODO Document
| Additionally, TODO Document
| Goal of this project is to facilitate easier mocking when writing test cases.
It provides with infrastructure (as pytest fixtures) to simplify writing a test case
that requires one or more (python) objects to be mocked

| This documentation aims to help people understand what are the package's features and to demonstrate
| how to leverage them for their use cases.
| It also presents the overall package design.
3 changes: 2 additions & 1 deletion docs/contents/20_why_this_package.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ So, why would one opt for this Package?

It is **easy** to *install* (using pip) and intuitive to *use*.

**Pytest Object Getter** features TODO Document
**Pytest Object Getter** features the 'get_object' pytest fixture
that arguably simplifies the process of mocking an object present in a namespace.

Well-tested against multiple Python Interpreter versions (3.6 - 3.10),
tested on both *Linux* (Ubuntu) and *Darwin* (Macos) platforms.
Expand Down
54 changes: 52 additions & 2 deletions docs/contents/30_usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,56 @@ Assuming you have 'activated' a `python virtual environment`:
Simple Use Case
---------------

| Common Use Case for the pytest_object_getter is to TODO Document
| Common Use Case for the pytest_object_getter is to use the 'get_obejct' fixture
| to mock an object in your python test case (using pytest).
TODO Document
Let's see a test that mocks the `requests.get` method to avoid
actual network communication:

Install Python dependencies:

.. code-block:: shell
python3 -m pip install ask-pypi
Test case:

.. code-block:: python
import pytest
@pytest.fixture
def mock_response():
def init(self, package_name: str):
self.status_code = 200 if package_name == 'existing-package' else 404
return type('MockResponse', (), {
'__init__': init
})
@pytest.fixture
def create_mock_requests(mock_response):
def _create_mock_requests():
def mock_get(*args, **kwargs):
package_name = args[0].split('/')[-1]
return mock_response(package_name)
return type('MockRequests', (), {
'get': mock_get,
})
return _create_mock_requests
def test_fixture(get_object, create_mock_requests):
from ask_pypi import is_pypi_project
assert is_pypi_project('numpy') == True
assert is_pypi_project('pandas') == True
assert is_pypi_project('existing-package') == False
get_object('is_project', 'ask_pypi.pypi_project',
overrides={'requests': lambda: create_mock_requests()})
assert is_pypi_project('existing-package') == True
assert is_pypi_project('numpy') == False
assert is_pypi_project('pandas') == False
assert is_pypi_project('so-magic') == False
133 changes: 3 additions & 130 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,132 +1,9 @@
# BUILD

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

# Poetry

# Information required for building (sdist/wheel)
## Also renders on pypi as 'subtitle'
[tool.poetry]
name = "pytest_object_getter"
version = "0.0.1"
description = "Project entirely generated using Generator https://github.com/boromir674/cookiecutter-python-package/"
authors = ["Konstantinos Lampridis <k.lampridis@hotmail.com>"]
maintainers = ["Konstantinos Lampridis <k.lampridis@hotmail.com>"]
license = "AGPL-3.0-only"
readme = "README.rst"

homepage = "https://github.com/boromir674/pytest-object-getter"
repository = "https://github.com/boromir674/pytest-object-getter"
documentation = "https://pytest-object-getter.readthedocs.io/"

keywords = [
"python package",
]
classifiers = [
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Developers",
"Intended Audience :: End Users/Desktop",
"Intended Audience :: Science/Research",
"License :: OSI Approved :: GNU Affero General Public License v3",
"Natural Language :: English",
"Operating System :: Unix",
"Operating System :: POSIX :: Linux",
"Operating System :: MacOS",
"Programming Language :: Python",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Topic :: Software Development",
"Topic :: Software Development :: Libraries :: Python Modules",
"Topic :: System :: Software Distribution",
"Typing :: Typed"
]

packages = [
{ include = "pytest_object_getter", from = "src" },
]

include = [
{ path = "tests", format = "sdist" },
{ path = "docs/**/*.rst", format = "sdist" },
{ path = "docs/conf.py", format = "sdist" },
{ path = "src/**/*.typed", format = "sdist" },
{ path = "src/stubs/*.pyi", format = "sdist" },
"pyproject.toml",
"LICENSE",
"README.rst",
"CONTRIBUTING.md",
"CHANGELOG.rst",
]

exclude = [
"docs/*",
"requirements/*",
"scripts/*.py",
"tox.ini",
".bettercodehub.yml",
".circleci/config.yml",
".coveragerc",
".DS_Store",
".gitignore",
".prospector.yml",
".pylintrc",
".readthedocs.yml",
".scrutinizer.yml",
".travis.yml"
]

[tool.poetry.dependencies]
python = "^3.6"

# A list of all of the optional dependencies, some of which are included in the
# below `extras`. They can be opted into by apps.

# Test: packages imported in test code and packages required for the "test runner"
pytest = { version = ">= 6.2.4", optional = true }
pytest-cov = { version = ">= 2.12", optional = true }
pytest-explicit = { version = "~= 1.0.1", optional = true }
pytest-xdist = { version = ">= 1.34", optional = true }

# Docs: development and build dependencies
sphinx = { version = "~= 4.0", optional = true }
sphinx-autodoc-typehints = { version = ">= 1.10", optional = true }
sphinx-rtd-theme = { version = "== 0.5.0", optional = true }
sphinxcontrib-spelling = { version = "~= 7.3.3", optional = true }

# Type Checking: packages required for the type check (ie mypy) to pass
mypy = { version = "~= 0.950", optional = true }

[tool.poetry.extras]
test = [
"pytest",
"pytest-cov",
"pytest-explicit",
"pytest-xdist",
]
docs = [
"sphinx",
"sphinx-autodoc-typehints",
"sphinx-rtd-theme",
"sphinxcontrib-spelling",
]
typing = [
"mypy",
]


# PyPi url links, that appear in 'Project Links' section
[tool.poetry.urls]
"Bug Tracker" = "https://github.com/pytest-object-getter/issues"
"CI: Github Actions" = "https://github.com/boromir674/pytest-object-getter/actions"
"Documentation" = "https://pytest-object-getter.readthedocs.io/"
"Source Code" = "https://github.com/boromir674/pytest-object-getter"
"Changelog" = "https://github.com/boromir674/pytest-object-getter/blob/master/CHANGELOG.rst"
"Code of Conduct" = "https://github.com/boromir674/pytest-object-getter/blob/master/CONTRIBUTING.rst"
[build-system]
requires = ["setuptools", "wheel"]
build-backend = "setuptools.build_meta"


# TOOLS
Expand All @@ -144,10 +21,6 @@ markers = [
testpaths = [
"tests",
]
explicit-only = [
"integration",
"network_bound",
]


## Black formatting/linting
Expand Down
2 changes: 0 additions & 2 deletions scripts/parse_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import sys
import typing as t

# TODO Improve: try using the semantic_version_checker package for semver regex

ExceptionFactory = t.Callable[[str, str, str], Exception]
ClientCallback = t.Callable[[str, str], t.Tuple]

Expand Down
Loading

0 comments on commit e8aaee8

Please sign in to comment.