Skip to content

Commit

Permalink
test: check hypothesis that it works for JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
jirikuncar committed Feb 16, 2018
1 parent a5d95f5 commit 12d663a
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 10 deletions.
53 changes: 53 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# -*- coding: utf-8 -*-

notifications:
email: false

language: python

matrix:
fast_finish: true

cache:
- pip

env:
- REQUIREMENTS=lowest
- REQUIREMENTS=release DEPLOY=true
# - REQUIREMENTS=devel

python:
- "2.7"
- "3.5"
- "3.6"

before_install:
- "travis_retry pip install --upgrade pip setuptools py"
- "travis_retry pip install twine wheel coveralls requirements-builder"
- "requirements-builder -e all --level=min setup.py > .travis-lowest-requirements.txt"
- "requirements-builder -e all --level=pypi setup.py > .travis-release-requirements.txt"
# - "requirements-builder -e all --level=dev --req requirements-devel.txt setup.py > .travis-devel-requirements.txt"

install:
- "travis_retry pip install -r .travis-${REQUIREMENTS}-requirements.txt"
- "travis_retry pip install -e .[all]"

script:
- check-manifest
- python setup.py test
- sphinx-build -qnNW docs docs/_build/html

after_success:
- coveralls

deploy:
- provider: pypi
user: jirikuncar
password:
secure: TODO
distributions: "sdist bdist_wheel"
on:
tags: true
python: "3.6"
repo: jirikuncar/git-json-tree
condition: $DEPLOY = true
4 changes: 4 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
include *.py
include LICENSE
recursive-include docs *.css
recursive-include docs *.html
recursive-include docs *.png
recursive-include docs *.py
recursive-include docs *.rst
1 change: 1 addition & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import os
import re
import sys

sys.path.insert(0, os.path.abspath('..'))

# -- General configuration ------------------------------------------------
Expand Down
26 changes: 18 additions & 8 deletions git_json_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,16 @@ def _from_obj(obj):
while stack:
path, item = stack.pop()
if isinstance(item, dict):
if not item:
yield path + ['.object'], None

for key, value in item.items():
stack.insert(0, (path + ['"{0}"'.format(key)], value))
assert isinstance(key, str)
stack.insert(0, (path + [json.dumps(key)], value))
elif isinstance(item, (tuple, list)):
if not item:
yield path + ['.array'], None

for key, value in enumerate(item):
stack.insert(0, (path + [str(key)], value))
else:
Expand Down Expand Up @@ -101,19 +108,22 @@ def build_tree(path):
return build_tree(b'')


def _path_defaults(path):
"""Yield path defaults."""
for key in path:
yield json.loads(key)


def decode(repo, tree_id):
"""Decode index to a Python structure."""
tree = repo[tree_id]

if tree.type_name == b'tree':
items = [(json.loads(item.path, encoding='utf-8'), item.sha)
for item in tree.items()]
for item in tree.items()
if item.path not in {b'.object', b'.array'}]

if not items:
if b'.array' in tree:
return []
elif b'.object' in tree:
return {}
raise TypeError('Unknown tree type.')

if all((isinstance(key[0], str) for key in items)):
return {key: decode(repo, sha) for key, sha in items}
elif all((isinstance(key[0], int) for key in items)):
Expand Down
13 changes: 13 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,16 @@ universal = 1

[pydocstyle]
add_ignore = D401

[tool:pytest]
addopts =
--pep8
--isort
--cov=git_json_tree
--cov-report=term-missing
--hypothesis-show-statistics
# --doctest-glob="*.rst"
# --doctest-modules
doctest_optionflags = ALLOW_UNICODE ALLOW_BYTES DONT_ACCEPT_TRUE_FOR_1 ELLIPSIS IGNORE_EXCEPTION_DETAIL
pep8ignore = docs/conf.py ALL
testpaths = docs test_git_json_tree.py git_json_tree.py
6 changes: 4 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
'coverage>=4.0',
'coverage>=4.0.0',
'isort>=4.2.2',
'hypothesis>=3.45.0',
'mock>=1.0.0',
'pydocstyle>=1.0.0',
'pytest-cache>=1.0',
'pytest-cov>=2.1.0',
'pytest-cov>=2.5.1',
'pytest-isort>=0.1.0',
'pytest-pep8>=1.0.6',
'pytest>=2.8.0',
'pytest>=3.4.0',
]

extras_require = {
Expand Down
44 changes: 44 additions & 0 deletions test_git_json_tree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""Test Git-JSON-Tree library."""

from string import printable

import hypothesis.strategies as st
import pytest
from dulwich.repo import Repo
from hypothesis import assume, given, settings

from git_json_tree import decode, encode


@pytest.fixture()
def repo(tmpdir):
"""Create a repo."""
yield Repo.init_bare(str(tmpdir))


def json_data():
"""Generate JSON data."""
return st.recursive(
st.none() |
st.booleans() |
st.integers() |
st.floats(allow_nan=False) |
st.text(printable),
lambda children:
st.lists(children) |
st.dictionaries(
st.text(
set(printable) - set('/')
),
children,
),
max_leaves=500,
)


@given(data=json_data())
@settings(max_examples=1000, deadline=500)
def test_encode_decode(data, repo):
"""Test (d)encoding."""
assume(isinstance(data, (dict, list)))
assert decode(repo, encode(repo, data)) == data

0 comments on commit 12d663a

Please sign in to comment.