Beispiel #1
0
def test_pull_all(with_test_config):
    doc = docman.Document.load({})
    mock = RequestsMock()
    requests.get = mock.get
    urllib.request.urlretrieve = mock.urlretrieve
    response = dict(
        some_id=dict(_id="some_id", pdf="some_pdf", scans=["one", "two"]))
    mock.set_get_response(json.dumps(response), 200)
    assert _run(doc, args(id="some_id", all=True,
                          output="some/output")) == (None, 0)
    assert mock.last_get_rq == ("http://localhost:8123/query",
                                dict(id="some_id"), None)
    assert len(mock.urlretrieve_requests) == 3
    assert mock.urlretrieve_requests[0] == (
        "http://localhost:8123/scan/one",
        "some/output/one",
    )
    assert mock.urlretrieve_requests[1] == (
        "http://localhost:8123/scan/two",
        "some/output/two",
    )
    assert mock.urlretrieve_requests[2] == (
        "http://localhost:8123/pdf/some_pdf",
        "some/output/some_pdf",
    )
Beispiel #2
0
def test_query_noarg(capfd, with_test_config):
    doc = docman.Document.load({})
    mock = RequestsMock()
    requests.get = mock.get
    mock.set_get_response("{}", 200)
    assert _run(doc, args()) == (None, 0)
    assert mock.last_get_rq == ("http://localhost:8123/query", {}, None)
    out, err = capfd.readouterr()
    assert err == ""
    assert out == "No results\n"
Beispiel #3
0
def test_query_raw(capfd, with_test_config):
    doc = docman.Document.load({})
    mock = RequestsMock()
    requests.get = mock.get
    result = dict(one=dict(), two=dict())
    mock.set_get_response(json.dumps(result), 200)
    assert _run(doc, args(raw=True)) == (None, 0)
    out, err = capfd.readouterr()
    assert err == ""
    assert out == json.dumps(result) + "\n"
Beispiel #4
0
def test_rm_nothing(capfd, with_test_config):
    doc = docman.Document.load({})
    mock = RequestsMock()
    requests.get = mock.get
    mock.set_get_response("{}", 404)
    assert _run(doc, args(ids=["zero"], noconfirm=True)) == (None, 0)
    out, err = capfd.readouterr()
    assert err == ""
    out = out.split("\n")
    assert out[0] == "Warning: No document found for id zero"
    assert out[1] == "Nothing to delete."
Beispiel #5
0
def test_pull_exception(with_test_config):
    doc = docman.Document.load({})
    mock = RequestsMock()
    requests.get = mock.get
    urllib.request.urlretrieve = mock.urlretrieve
    mock.set_get_response("{}", requests.exceptions.ConnectionError)
    assert _run(doc, args(id="some_id", all=True,
                          output="some/output")) == (None, 1)
    assert mock.last_get_rq == ("http://localhost:8123/query",
                                dict(id="some_id"), None)
    assert len(mock.urlretrieve_requests) == 0
Beispiel #6
0
def test_push_retval(with_test_config):
    doc = docman.Document.load({})
    doc.mode = "update"
    doc._id = "some_id"
    doc.tags = ["alpha", "beta"]
    doc.title = "title"
    doc.date = "date"
    doc.ocr = "ocr"
    mock = RequestsMock()
    requests.post = mock.post
    mock.set_post_response("OK", 200)
    assert _run(doc, None) == (None, 1)
Beispiel #7
0
def test_rm_multiple(capfd, with_test_config):
    doc = docman.Document.load({})
    mock = RequestsMock()
    requests.get = mock.get
    requests.post = mock.post
    # response for id lookup
    mock.set_get_response(json.dumps(dict(one=dict(scans=[1, 2, 3]))), 200)
    mock.set_get_response(json.dumps(dict(two=dict(scans=[1, 2]))), 200)
    # response for remove call
    mock.set_post_response("OK", 201)

    assert _run(doc, args(ids=["one", "one", "two"],
                          noconfirm=True)) == (None, 0)
    assert mock.last_get_rq == (
        "http://localhost:8123/query",
        {
            "id": "two"
        },
        None,
    )
    assert mock.last_post_rq == (
        "http://localhost:8123/remove",
        {
            "ids": ["one", "two"]
        },
        None,
    )
    out, err = capfd.readouterr()
    assert err == ""
    out = out.split("\n")
    assert out[0] == "You are about to delete 2 documents (5 scans)."
    assert out[1] == "Successfully removed 2 documents"
Beispiel #8
0
def test_rm_notyes(capfd, with_test_config):
    doc = docman.Document.load({})
    mock = RequestsMock()
    requests.get = mock.get
    # response for id lookup
    mock.set_get_response(json.dumps(dict(some_id=dict(scans=[1, 2, 3]))), 200)

    with replace_stdin("ye"):
        assert _run(doc, args(ids=["some_id"], noconfirm=False)) == (None, 1)
    out, err = capfd.readouterr()
    assert err == ""
    out = out.split("\n")
    assert out[0] == "You are about to delete 1 document (3 scans)."
    assert out[1] == "If you wish to continue, please type yes: Aborting..."
Beispiel #9
0
def test_query_table(capfd, with_test_config):
    doc = docman.Document.load({})
    mock = RequestsMock()
    requests.get = mock.get
    result = dict(
        one=dict(tags=["a", "b"], title="one", _id="idone", date="dateone"),
        two=dict(tags=["c", "d"], title="two", _id="idtwo", date="datetwo"),
    )
    mock.set_get_response(json.dumps(result), 200)
    assert _run(doc, args()) == (None, 0)
    out, err = capfd.readouterr()
    assert err == ""
    out = out.split("\n")
    assert out[0] == "     _id     date title tags"
    assert out[1] == "0  idone  dateone   One  a,b"
    assert out[2] == "1  idtwo  datetwo   Two  c,d"
Beispiel #10
0
def test_push_update(with_test_config):
    for exception in [True, False]:
        doc = docman.Document.load({})
        doc.mode = "update"
        doc._id = "some_id"
        doc.tags = ["alpha", "beta"]
        doc.title = "title"
        doc.date = "date"
        doc.ocr = "ocr"
        mock = RequestsMock()
        requests.post = mock.post
        if exception:
            mock.set_post_response("{}", requests.exceptions.ConnectionError)
        else:
            mock.set_post_response("OK", 201)
        doc, retval = _run(doc, None)
        assert mock.last_post_rq != (None, None, None)
        assert mock.last_post_rq[0] == "http://localhost:8123/update"
        assert mock.last_post_rq[2] is None
        post = mock.last_post_rq[1]
        assert isinstance(post, dict)
        assert "_id" in post
        if exception:
            assert retval == 1
            assert doc is None
        else:
            assert retval == 0
            assert doc.is_wip() == False
Beispiel #11
0
def test_rm_single(capfd, with_test_config):
    doc = docman.Document.load({})
    mock = RequestsMock()
    requests.get = mock.get
    requests.post = mock.post
    # response for id lookup
    mock.set_get_response(json.dumps(dict(some_id=dict(scans=[1, 2, 3]))), 200)
    # response for remove call
    mock.set_post_response("OK", 201)

    with replace_stdin("yes"):
        assert _run(doc, args(ids=["some_id"], noconfirm=False)) == (None, 0)
    assert mock.last_get_rq == (
        "http://localhost:8123/query",
        {
            "id": "some_id"
        },
        None,
    )
    assert mock.last_post_rq == (
        "http://localhost:8123/remove",
        {
            "ids": ["some_id"]
        },
        None,
    )
    out, err = capfd.readouterr()
    assert err == ""
    out = out.split("\n")
    assert out[0] == "You are about to delete 1 document (3 scans)."
    assert out[1] == (
        "If you wish to continue, please type yes: Successfully removed 1 document"
    )
Beispiel #12
0
def test_query_args(with_test_config):
    doc = docman.Document.load({})
    mock = RequestsMock()
    requests.get = mock.get
    mock.set_get_response("{}", 200)
    args_d = dict(
        id="id",
        text="text",
        tags="some,tags",
        date_from="2020-01-01",
        date_until="2023-01-01",
    )
    assert (
        _run(
            doc,
            args(**args_d),
        )
        == (None, 0)
    )
    # request data is same as arguments except tags are split
    req_d = args_d
    req_d["tags"] = ["some", "tags"]
    assert mock.last_get_rq == ("http://localhost:8123/query", req_d, None)
Beispiel #13
0
def test_query_wrong_date(capfd, with_test_config):
    doc = docman.Document.load({})
    mock = RequestsMock()
    requests.get = mock.get
    mock.set_get_response("{}", 200)
    assert _run(doc, args(date_from="20201201")) == (None, 1)
    out, err = capfd.readouterr()
    assert err == ""
    assert out == "Date must be in YYYY-MM-DD format.\n"

    doc = docman.Document.load({})
    mock = RequestsMock()
    requests.get = mock.get
    mock.set_get_response("{}", 200)
    assert _run(doc, args(date_until="2020-13-01")) == (None, 1)
    out, err = capfd.readouterr()
    assert err == ""
    assert out == "Date must be in YYYY-MM-DD format.\n"
Beispiel #14
0
def test_query_failed(capfd, with_test_config):
    # Retval
    doc = docman.Document.load({})
    mock = RequestsMock()
    requests.get = mock.get
    mock.set_get_response("{}", 201)
    assert _run(doc, args()) == (None, 1)
    out, err = capfd.readouterr()
    assert err == ""
    assert out == "Failed to connect to backend! (code 201)\n"

    # exception
    doc = docman.Document.load({})
    mock = RequestsMock()
    requests.get = mock.get
    mock.set_get_response("{}", requests.exceptions.ConnectionError)
    assert _run(doc, args()) == (None, 1)
    out, err = capfd.readouterr()
    assert err == ""
    assert out == "Failed to connect to http://localhost:8123/query\n"
Beispiel #15
0
def test_rm_fail(capfd, with_test_config):
    doc = docman.Document.load({})
    mock = RequestsMock()
    requests.get = mock.get
    requests.post = mock.post
    mock.set_get_response(json.dumps(dict(one=dict(scans=[1, 2, 3]))), 200)
    mock.set_post_response("Not OK", 400)
    assert _run(doc, args(ids=["one"], noconfirm=True)) == (None, 1)
    assert mock.last_post_rq == ("http://localhost:8123/remove", {
        "ids": ["one"]
    }, None)
    out, err = capfd.readouterr()
    assert err == ""
    out = out.split("\n")
    assert out[0] == "You are about to delete 1 document (3 scans)."
    assert out[1] == "Remove failed with code 400: Not OK"
Beispiel #16
0
def test_push_add_replace(with_test_config):
    with open("/tmp/test_docman.pdf", "w") as fp:
        fp.write("testpdf")
    with open("/tmp/test_scan_one.jpg", "w") as fp:
        fp.write("testscanone")
    with open("/tmp/test_scan_two.jpg", "w") as fp:
        fp.write("testscantwo")

    for exception in [True, False]:
        for mode in ["add", "replace"]:
            doc = docman.Document.load({})
            doc.mode = mode
            doc._id = "some_id"
            doc.pdf = "/tmp/test_docman.pdf"
            doc.scans = ["/tmp/test_scan_one.jpg", "/tmp/test_scan_two.jpg"]
            doc.tags = ["alpha", "beta"]
            doc.title = "title"
            doc.date = "date"
            doc.ocr = "ocr"
            mock = RequestsMock()
            requests.post = mock.post
            if exception:
                mock.set_post_response("{}",
                                       requests.exceptions.ConnectionError)
            else:
                mock.set_post_response("OK", 201)
            doc, retval = _run(doc, None)
            assert mock.last_post_rq != (None, None, None)
            assert mock.last_post_rq[0] == f"http://localhost:8123/{mode}"
            assert mock.last_post_rq[1] is None
            assert len(mock.last_post_rq[2]) == 4
            assert mock.last_post_rq[2][0][0] == "post"
            assert mock.last_post_rq[2][1][0] == "pdf"
            assert mock.last_post_rq[2][2][0] == "scan"
            assert mock.last_post_rq[2][3][0] == "scan"
            # in replace mode, _id is sent in post
            assert ("_id" in mock.last_post_rq[2][0][1]) == (mode == "replace")
            if exception:
                assert retval == 1
                assert doc is None
            else:
                assert retval == 0
                assert not doc.is_wip()