Skip to content

Commit

Permalink
modernize (#4)
Browse files Browse the repository at this point in the history
* drop py up to 3.8, remove future dep

* fix setup.cfg

* fix copypasta

* poetry lock

* update tests

* format
  • Loading branch information
grizz authored Apr 29, 2024
1 parent f7b0d40 commit f401c64
Show file tree
Hide file tree
Showing 13 changed files with 1,491 additions and 1,005 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: tests

on: [push, pull_request]

jobs:

linting:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install virtualenv from poetry
uses: 20c/workflows/poetry@v1
- name: Run linters
run: |
poetry run pre-commit run --all-files
test:
needs: linting
strategy:
fail-fast: false
matrix:
os: [ "ubuntu-latest" ]
python-version: [ "3.8", "3.9", "3.10", "3.11", "3.12" ]
runs-on: ${{ matrix.os }}
steps:
- name: Check out repository
uses: actions/checkout@v4
- name: Install virtualenv from poetry
uses: 20c/workflows/poetry@v1
with:
python-version: ${{ matrix.python-version }}
- name: Run tests
run: poetry run tox -e py
- name: Upload coverage
uses: codecov/codecov-action@v4
with:
fail_ci_if_error: false
token: ${{ secrets.CODECOV_TOKEN }}
31 changes: 31 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
fail_fast: false
exclude: |
(?x)^(
tests/data/.*
)$
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
hooks:
- id: check-toml
- id: check-yaml
- id: trailing-whitespace
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.1.14
hooks:
# Run the linter.
- id: ruff
# removes imports for tests
exclude: tests/
args: [--fix]
# Run the formatter.
- id: ruff-format
- repo: local
hooks:
- id: pyupgrade
name: pyupgrade
entry: poetry run pyupgrade --py38-plus
language: python
types: [python]
pass_filenames: true
7 changes: 1 addition & 6 deletions Ctl/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,4 @@ ctl:
repo_url: [email protected]:20c/pytest-filedata.git

- name: version
type: version
config:
branch_dev: master
branch_release: master
repositories:
- this_repo
type: semver2
5 changes: 2 additions & 3 deletions Ctl/requirements-test.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
codecov>=2.0.5
coverage>=4.1
pytest-cov>=2.3, <3
tox>=2.3.1, <4
pytest-cov>=2.3
tox>=4
5 changes: 2 additions & 3 deletions Ctl/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
decorator>=4, <5
future>=0.16.0, <1
requests-mock>=1, <2
decorator>=4
requests-mock>=1
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
[![Travis CI](https://img.shields.io/travis/20c/pytest-filedata.svg?maxAge=3600)](https://travis-ci.org/20c/pytest-filedata)
[![Code Health](https://landscape.io/github/20c/pytest-filedata/master/landscape.svg?style=flat)](https://landscape.io/github/20c/pytest-filedata/master)
[![Codecov](https://img.shields.io/codecov/c/github/20c/pytest-filedata/master.svg?maxAge=3600)](https://codecov.io/github/20c/pytest-filedata)
[![Requires.io](https://img.shields.io/requires/github/20c/pytest-filedata.svg?maxAge=3600)](https://requires.io/github/20c/pytest-filedata/requirements)

Easily load test data from files

Expand Down Expand Up @@ -41,10 +40,10 @@ def pytest_generate_tests(metafunc):

### License

Copyright 2016 20C, LLC
Copyright 2016-2024 20C, LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this softare except in compliance with the License.
you may not use this software except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0
Expand Down
2,236 changes: 1,329 additions & 907 deletions poetry.lock

Large diffs are not rendered by default.

82 changes: 39 additions & 43 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,61 +1,57 @@
[tool.poetry]
name = "pytest_filedata"
repository = "[email protected]:20c/pytest-filedata.git"
name = "pytest-filedata"
version = "0.4.0"
description = "easily load data from files"
authors = [ "20C <[email protected]>",]
description = "easily load test data from files"
readme = 'README.md'
license = "LICENSE.txt"
repository = "https://github.com/20c/pytest-filedata"
authors = [ "20C <[email protected]>",]
license = "Apache-2.0"

classifiers = [
"Development Status :: 4 - Beta",
"Development Status :: 5 - Production/Stable",
"Framework :: Pytest",
"License :: OSI Approved :: Apache Software License",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Topic :: Software Development :: Testing",
]

[tool.poetry.dependencies]
python = ">=3.6.1,<4.0"
pre-commit = "^2.13.0"
python = ">=3.8,<4.0"
decorator = ">=4"
requests-mock = ">=1"

[tool.poetry.dev-dependencies]
# test
pytest = ">=6.0.1"
pytest-django = ">=3.8.0"
pytest-cov = "*"
pytest-pythonpath = "*"

# lint
bandit = "^1.6.2"
black = "^20.8b1"
isort = "^5.7.0"
flake8 = "^3.8.4"

# docs
markdown = "*"
markdown-include = ">=0.5,<1"
mkdocs = ">=1.0.0,<2.0.0"
# Package docutils (0.21.post1) not found.
docutils = "<=0.21"

# tests
coverage = ">=5.3"
pytest = ">=6.1.2"
pytest-cov = ">=2.10.1"
tox = ">=4"
tox-gh-actions = ">2"

# linting
ruff = ">=0.1"
mypy = ">=0.950"
pre-commit = ">=2.13"
pyupgrade = ">=2.19"

# ctl
ctl = "^1.0.0"
jinja2 = "^2.11.2"
tmpl = "^0.3.0"
ctl = ">=1"
jinja2 = ">=3"
tmpl = ">=1"
twine = ">=3.3.0"

decorator = ">=4, <5"
future = ">=0.16.0, <1"
requests-mock = ">=1, <2"


[tool.poetry.plugins."markdown.extensions"]
pymdgen = "pymdgen.md:Extension"
# docs
markdown-include = ">=0.5,<1"
mkdocs = ">=1.2.3"
pymdgen = ">=1"

[build-system]
requires = [ "poetry>=0.12",]
build-backend = "poetry.masonry.api"

[tool.isort]
profile = "black"
multi_line_output = 3
requires = ["poetry-core>=1"]
build-backend = "poetry.core.masonry.api"
42 changes: 19 additions & 23 deletions pytest_filedata/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@

from future import standard_library
standard_library.install_aliases()
from builtins import map
from builtins import object
import collections
from datetime import datetime
import json
Expand All @@ -26,17 +21,16 @@ def setup(base_dir, fixture_prefixes=None):

def json_hook(data):
date_keys = (
'last_change',
'last_reboot',
'last_reconfiguration',
)
"last_change",
"last_reboot",
"last_reconfiguration",
)
for key in date_keys:
if key in data:
data[key] = datetime.strptime(data[key], "%Y-%m-%dT%H:%M:%S")
return data



class JSONEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, datetime):
Expand All @@ -57,20 +51,21 @@ def loads(data):
return json.loads(data, object_hook=json_hook)


class FileTestData(object):
""" object to hold file test data """
class FileTestData:
"""object to hold file test data"""

def __init__(self, inp=None, exp=None, name=None, path=None):
self.input = inp
self.expected = exp
self.name = name
self.path = path

def dumps(self, data):
""" dump data in configured output method """
"""dump data in configured output method"""
return dumps(data)

def loads(self, data):
""" load data in configured output method """
"""load data in configured output method"""
return loads(data)


Expand All @@ -89,25 +84,25 @@ def get_data(name):
Gets data from fixture name.
"""
data = collections.OrderedDict()
dirname = os.path.join(test_dir, *name.split('_'))
dirname = os.path.join(test_dir, *name.split("_"))

if not os.path.isdir(dirname):
raise ValueError("data directory '{}' does not exist".format(dirname))
raise ValueError(f"data directory '{dirname}' does not exist")

for each in get_test_files(dirname):
if os.path.isdir(each):
continue

fname = os.path.basename(each)
if fname.startswith('.'):
if fname.startswith("."):
continue

test_name, ext = os.path.splitext(fname)
data.setdefault(test_name, FileTestData(name=test_name, path=dirname))

# could setattr
attr = ext[1:]
if ext == '.expected':
if ext == ".expected":
with open(each) as fobj:
data[test_name].expected = json.load(fobj, object_hook=json_hook)
else:
Expand All @@ -117,10 +112,11 @@ def get_data(name):
return data


class RequestsData(object):
class RequestsData:
"""
class to use test data from requests
"""

def __init__(self, prefix, real_http=False):
self.prefix = prefix
adapter = requests_mock.Adapter()
Expand All @@ -129,7 +125,7 @@ def __init__(self, prefix, real_http=False):

def callback(self, request, context):
path = urllib.parse.urlparse(request.url).path
path = os.path.join(test_dir, 'data', self.prefix, path.lstrip('/'))
path = os.path.join(test_dir, "data", self.prefix, path.lstrip("/"))

files = get_test_files(path)

Expand All @@ -138,16 +134,16 @@ def callback(self, request, context):

if len(files) == 0:
# dir not found, check for file.input
fname = path + '.input'
fname = path + ".input"
if not os.path.exists(fname):
raise ValueError("failed to find data for {}".format(path))
raise ValueError(f"failed to find data for {path}")

else:
fname = files[0]

try:
# file extension is status code
context.status_code = int(os.path.splitext(fname)[1].lstrip('.'))
context.status_code = int(os.path.splitext(fname)[1].lstrip("."))

except ValueError:
context.status_code = 200
Expand Down
3 changes: 1 addition & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import os
import pytest
import pytest_filedata
Expand All @@ -11,6 +10,6 @@

def pytest_generate_tests(metafunc):
for fixture in metafunc.fixturenames:
if fixture.startswith('data_'):
if fixture.startswith("data_"):
data = pytest_filedata.get_data(fixture)
metafunc.parametrize(fixture, list(data.values()), ids=list(data.keys()))
11 changes: 6 additions & 5 deletions tests/test_filedata.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# -*- coding: utf-8 -*-


import pytest


def assert_json(data, parsed):
# dump in json format for easily adding expected
print("echo \\\n'{}'\\\n > {}/{}.expected".format(data.dumps(parsed), data.path, data.name))
print(
"echo \\\n'{}'\\\n > {}/{}.expected".format(
data.dumps(parsed), data.path, data.name
)
)
assert data.expected == parsed
assert data.name
assert data.path
Expand All @@ -17,6 +18,6 @@ def test_json_data(data_json):
assert_json(data_json, parsed)


#with pytest.raises(ValueError):
# with pytest.raises(ValueError):
# def test_nonexistant(data_nonexistant):
# pass
Loading

0 comments on commit f401c64

Please sign in to comment.