From cc4ed2bef985b3914ca45529af0e622fa8296287 Mon Sep 17 00:00:00 2001 From: Mike Shoup Date: Thu, 27 Jun 2019 16:24:10 -0600 Subject: [PATCH] Add ability to put a document from a file --- src/humulus/couch.py | 11 +++++++++++ tests/assets/test_doc.json | 4 ++++ tests/test_couch.py | 11 ++++++++++- 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 tests/assets/test_doc.json diff --git a/src/humulus/couch.py b/src/humulus/couch.py index 1018f7f..6a8ece4 100644 --- a/src/humulus/couch.py +++ b/src/humulus/couch.py @@ -15,6 +15,7 @@ # limitations under the License. from datetime import datetime +import json import uuid import click @@ -118,3 +119,13 @@ def get_doc_or_404(id): except KeyError: abort(404) return doc + + +def put_doc_from_file(filename): + """Loads filename and stores it in couch. + + The file must be valid JSON. + """ + with open(filename, 'r') as doc: + response = put_doc(json.load(doc)) + return response diff --git a/tests/assets/test_doc.json b/tests/assets/test_doc.json new file mode 100644 index 0000000..01c89e3 --- /dev/null +++ b/tests/assets/test_doc.json @@ -0,0 +1,4 @@ +{ + "_id": "testfile", + "foo": "bar" +} diff --git a/tests/test_couch.py b/tests/test_couch.py index 2a1ff55..b248b4f 100644 --- a/tests/test_couch.py +++ b/tests/test_couch.py @@ -12,9 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. +import pathlib import uuid -from humulus.couch import get_doc, put_doc, update_doc +from humulus.couch import get_doc, put_doc, update_doc, put_doc_from_file def test_put_doc(app): @@ -64,3 +65,11 @@ def test_build_couch_command(runner, monkeypatch): def test_get_doc(app): with app.app_context(): assert get_doc('foobar')['data'] == 'test' + + +def test_put_doc_from_file(app): + here = pathlib.Path(__file__).parent + with app.app_context(): + response = put_doc_from_file(here/'assets'/'test_doc.json') + assert response['_id'] == 'testfile' + assert response['foo'] == 'bar'