Skip to content

Commit

Permalink
Added testing CI & pyi
Browse files Browse the repository at this point in the history
  • Loading branch information
night-crawler committed Nov 6, 2023
1 parent d94efab commit 1448056
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 5 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Python package

on: [push]

jobs:
build:

runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
python-version: ["3.9", "3.10", "3.11", "pypy3.9", "pypy3.10"]

steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.10'
- name: Install dependencies
run: python -m pip install --upgrade pip setuptools wheel maturin pytest
- name: Build wheels
run: python -m maturin build --release --out dist --find-interpreter
- name: Test
run: |
python -m pip install dist/*.whl
python -m pytest
21 changes: 17 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# jsonpath-rust-bindings

![PyPI - Downloads](https://img.shields.io/pypi/dm/jsonpath-rust-bindings)
![GitHub Workflow Status (with event)](https://img.shields.io/github/actions/workflow/status/night-crawler/jsonpath-rust-bindings/CI.yml)
![GitHub Workflow Status (with event)](https://img.shields.io/github/actions/workflow/status/night-crawler/jsonpath-rust-bindings/test.yml?label=tests)

Python bindings for [jsonpath-rust](https://github.com/besok/jsonpath-rust).

## Installation
Expand Down Expand Up @@ -72,16 +76,25 @@ f = Finder(sample)
for query in queries:
print(query, f.find(query), '\n')

# You will see a bunch of found items like
# $..book[?(@.author ~= '.*Rees')].price [JsonPathResult(data=8.95, path=Some("$.['store'].['book'][0].['price']"), is_new_value=False)]

```

`JsonPathResult` has the following attributes:

- data: the found value
- path: the path to the found value
- is_new_value: whether the value is a new value or a copy of the original value

`JsonPathResult` can't be constructed from Python; it is only returned by `Finder.find()`.

## Caveats

THe current implementation cloning the original PyObject when initializing the `Finder` instance.
THe current implementation cloning the original PyObject when initializing the `Finder` instance.
It has yet another consequence demonstrated in the following example:

```python
Python 3.11.5 (main, Aug 30 2023, 19:09:52) [GCC 13.2.1 20230730] on linux
Type "help", "copyright", "credits" or "license" for more information.
```python-repl
>>> original_object_i_want_to_mutate = {'a': {'b': 'sample b'}}
>>> from jsonpath_rust_bindings import Finder
>>> f = Finder(original_object_i_want_to_mutate)
Expand Down
21 changes: 21 additions & 0 deletions jsonpath_rust_bindings.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from typing import List, Dict, Optional


class JsonPathResult:
@property
def data(self) -> Optional[Dict]: ...

@property
def path(self) -> Optional[str]: ...

@property
def is_new_value(self) -> bool: ...


class Finder:
def __init__(
self,
obj: List | Dict
) -> None: ...

def find(self, query: str) -> List[JsonPathResult]: ...
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl JsonPathResult {
"JsonPathResult(data={}, path={:?}, is_new_value={})",
data_repr,
slf.path,
slf.is_new_value
if slf.is_new_value { "True" } else { "False"}
))
}
}
Expand Down
46 changes: 46 additions & 0 deletions tests/test_bindings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from jsonpath_rust_bindings import Finder


import pytest

@pytest.fixture
def sample_data() -> dict:
return {
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95,
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99,
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99,
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99,
},
],
"bicycle": {"color": "red", "price": 19.95},
},
"expensive": 10,
}


def test_sanity(sample_data):
finder = Finder(sample_data)
finder.find('$..book[?(@.price<=$.expensive)]')

0 comments on commit 1448056

Please sign in to comment.