From 9fe79398eb47ffb1a5a51483642943233cb3915d Mon Sep 17 00:00:00 2001 From: Yaniv Aknin Date: Sun, 12 May 2024 09:47:23 +0100 Subject: [PATCH 1/2] ci: Test the petl executable - adds a simple test invoking the petl executable - installs the package in CI so the executable is available. --- .github/workflows/test-changes.yml | 1 + petl/test/test_executable.py | 10 ++++++++++ 2 files changed, 11 insertions(+) create mode 100644 petl/test/test_executable.py diff --git a/.github/workflows/test-changes.yml b/.github/workflows/test-changes.yml index 2a3bfa82..0e90831a 100644 --- a/.github/workflows/test-changes.yml +++ b/.github/workflows/test-changes.yml @@ -91,6 +91,7 @@ jobs: run: | python -m pip install --upgrade pip python -m pip install --prefer-binary -r requirements-tests.txt + python -m pip install -e . - name: Setup environment variables for remote filesystem testing if: matrix.os == 'ubuntu-latest' && matrix.python == '3.8' diff --git a/petl/test/test_executable.py b/petl/test/test_executable.py new file mode 100644 index 00000000..1b8645f4 --- /dev/null +++ b/petl/test/test_executable.py @@ -0,0 +1,10 @@ +from __future__ import print_function, division, absolute_import + +import subprocess + +def test_executable(): + result = subprocess.run(""" + (echo foo,bar ; echo a,b; echo c,d) | + petl 'fromcsv().cut("foo").head(1).tocsv()' + """, shell=True, check=True, capture_output=True) + assert result.stdout == b'foo\r\na\r\n' From 60818d522e252ce35644d1222077f24b36eef494 Mon Sep 17 00:00:00 2001 From: Yaniv Aknin Date: Thu, 25 Apr 2024 06:24:37 +0100 Subject: [PATCH 2/2] Fix fromjson() to support reading from stdin --- petl/io/json.py | 2 +- petl/test/test_executable.py | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/petl/io/json.py b/petl/io/json.py index 75600e6c..f7b8c39c 100644 --- a/petl/io/json.py +++ b/petl/io/json.py @@ -16,7 +16,7 @@ from petl.util.base import data, Table, dicts as _dicts, iterpeek -def fromjson(source, *args, **kwargs): +def fromjson(source=None, *args, **kwargs): """ Extract data from a JSON file. The file must contain a JSON array as the top level object, and each member of the array will be treated as a diff --git a/petl/test/test_executable.py b/petl/test/test_executable.py index 1b8645f4..795be4fd 100644 --- a/petl/test/test_executable.py +++ b/petl/test/test_executable.py @@ -3,8 +3,20 @@ import subprocess def test_executable(): - result = subprocess.run(""" + result = subprocess.check_output(""" (echo foo,bar ; echo a,b; echo c,d) | petl 'fromcsv().cut("foo").head(1).tocsv()' - """, shell=True, check=True, capture_output=True) - assert result.stdout == b'foo\r\na\r\n' + """, shell=True) + assert result == b'foo\r\na\r\n' + +def test_json_stdin(): + result = subprocess.check_output(""" + echo '[{"foo": "a", "bar": "b"}]' | + petl 'fromjson().tocsv()' + """, shell=True) + assert result == b'foo,bar\r\na,b\r\n' + result = subprocess.check_output(""" + ( echo '{"foo": "a", "bar": "b"}' ; echo '{"foo": "c", "bar": "d"}' ) | + petl 'fromjson(lines=True).tocsv()' + """, shell=True) + assert result == b'foo,bar\r\na,b\r\nc,d\r\n'