Skip to content

Commit

Permalink
Restructure Repository (#66)
Browse files Browse the repository at this point in the history
* Switches to a package structure rather than modules.
* Migrates tests to a folder and splits them into several files.
  • Loading branch information
dostuffthatmatters authored Jan 17, 2024
1 parent bca1cc4 commit f21e8af
Show file tree
Hide file tree
Showing 11 changed files with 753 additions and 404 deletions.
13 changes: 7 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ on:

jobs:
build:

runs-on: ubuntu-latest
strategy:
matrix:
Expand All @@ -28,14 +27,16 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 pytest
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
pip install ".[dev]"
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
flake8 jsonref/ tests/ --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
flake8 jsonref/ tests/ --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics --ignore=E731,E741,W503
- name: Test whether code is formatted with black
run: |
black --check jsonref/ tests/
- name: Test with pytest
run: |
pytest tests.py
pytest tests/
10 changes: 5 additions & 5 deletions jsonref.py → jsonref/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from urllib import parse as urlparse
from urllib.parse import unquote
from urllib.request import urlopen
from . import proxytypes # noqa: F401
from .proxytypes import LazyProxy

try:
# If requests >=1.0 is available, we will use it
Expand All @@ -15,8 +17,6 @@
except ImportError:
requests = None

from proxytypes import LazyProxy

__version__ = "1.1.0"


Expand Down Expand Up @@ -350,7 +350,7 @@ def _replace_refs(
merge_props,
store,
path,
recursing
recursing,
):
base_uri, frag = urlparse.urldefrag(base_uri)
store_uri = None # If this does not get set, we won't store the result
Expand Down Expand Up @@ -424,7 +424,7 @@ def load(
merge_props=False,
proxies=True,
lazy_load=True,
**kwargs
**kwargs,
):
"""
Drop in replacement for :func:`json.load`, where JSON references are
Expand Down Expand Up @@ -461,7 +461,7 @@ def loads(
merge_props=False,
proxies=True,
lazy_load=True,
**kwargs
**kwargs,
):
"""
Drop in replacement for :func:`json.loads`, where JSON references are
Expand Down
File renamed without changes.
330 changes: 330 additions & 0 deletions pdm.lock

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
[project]
name = "jsonref"
description = "jsonref is a library for automatic dereferencing of JSON Reference objects for Python."
authors = [
{name = "Chase Sterling", email = "chase.sterling@gmail.com"},
]
license = {text = "MIT"}
authors = [{ name = "Chase Sterling", email = "chase.sterling@gmail.com" }]
license = { text = "MIT" }
readme = "README.md"
dynamic = ["version"]
requires-python = ">=3.7"
Expand All @@ -14,13 +12,15 @@ dependencies = []
repository = "https://github.com/gazpachoking/jsonref"
documentation = "https://jsonref.readthedocs.io/en/latest/"

[tool.pdm.dev-dependencies]
test = ["pytest>=7.1.3"]
[project.optional-dependencies]
dev = ["pytest>=7.1.3", "flake8>=5.0.4", "black>=23.3.0"]

[tool.pdm]
version = { source = "file", path = "jsonref.py" }
version = { source = "file", path = "jsonref/__init__.py" }

[tool.pdm.build]
includes = ["jsonref.py", "proxytypes.py"]
includes = ["jsonref"]
excludes = ["tests", "docs"]

[build-system]
requires = ["pdm-backend"]
Expand Down
Empty file added tests/__init__.py
Empty file.
34 changes: 34 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from jsonref import load, loads, dump, dumps


class TestApi(object):
def test_loads(self):
json = """{"a": 1, "b": {"$ref": "#/a"}}"""
assert loads(json) == {"a": 1, "b": 1}

def test_loads_kwargs(self):
json = """{"a": 5.5, "b": {"$ref": "#/a"}}"""
loaded = loads(json, parse_float=lambda x: int(float(x)))
assert loaded["a"] == loaded["b"] == 5

def test_load(self, tmpdir):
json = """{"a": 1, "b": {"$ref": "#/a"}}"""
tmpdir.join("in.json").write(json)
assert load(tmpdir.join("in.json")) == {"a": 1, "b": 1}

def test_dumps(self):
json = """[1, 2, {"$ref": "#/0"}, 3]"""
loaded = loads(json)
# The string version should load the reference
assert str(loaded) == "[1, 2, 1, 3]"
# Our dump function should write the original reference
assert dumps(loaded) == json

def test_dump(self, tmpdir):
json = """[1, 2, {"$ref": "#/0"}, 3]"""
loaded = loads(json)
# The string version should load the reference
assert str(loaded) == "[1, 2, 1, 3]"
dump(loaded, tmpdir.join("out.json"))
# Our dump function should write the original reference
assert tmpdir.join("out.json").read() == json
28 changes: 28 additions & 0 deletions tests/test_json_loader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import json
from unittest import mock
from jsonref import jsonloader


class TestJsonLoader(object):
def test_it_retrieves_refs_via_requests(self):
ref = "http://bar"
data = {"baz": 12}

with mock.patch("jsonref.requests") as requests:
requests.get.return_value.json.return_value = data
result = jsonloader(ref)
assert result == data
requests.get.assert_called_once_with("http://bar")

def test_it_retrieves_refs_via_urlopen(self):
ref = "http://bar"
data = {"baz": 12}

with mock.patch("jsonref.requests", None):
with mock.patch("jsonref.urlopen") as urlopen:
urlopen.return_value.__enter__.return_value.read.return_value = (
json.dumps(data).encode("utf8")
)
result = jsonloader(ref)
assert result == data
urlopen.assert_called_once_with("http://bar")
Loading

0 comments on commit f21e8af

Please sign in to comment.