|
| 1 | +import decimal |
| 2 | + |
| 3 | +from marklogic.documents import Document |
1 | 4 | from requests_toolbelt.multipart.decoder import MultipartDecoder |
| 5 | +from pytest import raises |
2 | 6 |
|
3 | 7 |
|
4 | | -def test_eval(client): |
5 | | - """ |
6 | | - This shows how a user would do an eval today. It's a good example of how a multipart/mixed |
7 | | - response is a little annoying to deal with, as it requires using the requests_toolbelt |
8 | | - library and a class called MultipartDecoder. |
| 8 | +def test_xquery_common_primitives(client): |
| 9 | + parts = client.eval.xquery( |
| 10 | + """( |
| 11 | + 'A', 1, 1.1, fn:false(), fn:doc('/musicians/logo.png')) |
| 12 | + """ |
| 13 | + ) |
| 14 | + __verify_common_primitives(parts) |
9 | 15 |
|
10 | | - Client support for this might look like this: |
11 | | - response = client.eval.xquery("<hello>world</hello>") |
12 | 16 |
|
13 | | - And then it's debatable whether we want to do anything beyond what MultipartDecoder |
14 | | - is doing for handling the response. |
15 | | - """ |
16 | | - response = client.post( |
17 | | - "v1/eval", |
18 | | - headers={"Content-type": "application/x-www-form-urlencoded"}, |
19 | | - data={"xquery": "<hello>world</hello>"}, |
| 17 | +def test_javascript_common_primitives(client): |
| 18 | + parts = client.eval.javascript( |
| 19 | + """xdmp.arrayValues([ |
| 20 | + 'A', 1, 1.1, false, fn.doc('/musicians/logo.png') |
| 21 | + ])""" |
| 22 | + ) |
| 23 | + __verify_common_primitives(parts) |
| 24 | + |
| 25 | + |
| 26 | +def test_xquery_specific_primitives(client): |
| 27 | + parts = client.eval.xquery( |
| 28 | + """( |
| 29 | + <hello>world</hello>, |
| 30 | + object-node {'A': 'a'}, |
| 31 | + fn:doc('/doc2.xml'), |
| 32 | + document {<test/>}, |
| 33 | + array-node {1, "23", 4} |
| 34 | + )""" |
20 | 35 | ) |
| 36 | + assert type(parts[0]) is str |
| 37 | + assert "<hello>world</hello>" == parts[0] |
| 38 | + assert type(parts[1]) is dict |
| 39 | + assert {"A": "a"} == parts[1] |
| 40 | + assert type(parts[2]) is Document |
| 41 | + assert "/doc2.xml" == parts[2].uri |
| 42 | + assert "<hello>world</hello>" in parts[2].content |
| 43 | + assert type(parts[3]) is str |
| 44 | + assert '<?xml version="1.0" encoding="UTF-8"?>\n<test/>' == parts[3] |
| 45 | + assert type(parts[4]) is list |
| 46 | + assert "23" == parts[4][1] |
| 47 | + assert 3 == len(parts[4]) |
| 48 | + |
| 49 | + |
| 50 | +def test_javascript_specific_primitives(client): |
| 51 | + parts = client.eval.javascript( |
| 52 | + """xdmp.arrayValues([ |
| 53 | + {'A': 'a'}, |
| 54 | + ['Z', 'Y', 1], |
| 55 | + fn.head(cts.search('Armstrong')) |
| 56 | + ])""" |
| 57 | + ) |
| 58 | + assert type(parts[0]) is dict |
| 59 | + assert {"A": "a"} == parts[0] |
| 60 | + assert type(parts[1]) is list |
| 61 | + assert "Z" == parts[1][0] |
| 62 | + assert 3 == len(parts[1]) |
| 63 | + assert type(parts[2]) is Document |
| 64 | + assert "/musicians/musician1.json" == parts[2].uri |
| 65 | + assert { |
| 66 | + "musician": { |
| 67 | + "lastName": "Armstrong", |
| 68 | + "firstName": "Louis", |
| 69 | + "dob": "1901-08-04", |
| 70 | + "instrument": ["trumpet", "vocal"], |
| 71 | + } |
| 72 | + } == parts[2].content |
| 73 | + |
| 74 | + |
| 75 | +def test_javascript_noquery(client): |
| 76 | + with raises(ValueError, match="No script found; must specify a javascript"): |
| 77 | + client.eval.javascript(None) |
| 78 | + |
| 79 | + |
| 80 | +def test_xquery_noquery(client): |
| 81 | + with raises(ValueError, match="No script found; must specify a xquery"): |
| 82 | + client.eval.xquery(None) |
| 83 | + |
| 84 | + |
| 85 | +def test_xquery_with_return_response(client): |
| 86 | + response = client.eval.xquery("('A', 1, 1.1, fn:false())", return_response=True) |
| 87 | + assert 200 == response.status_code |
| 88 | + parts = MultipartDecoder.from_response(response).parts |
| 89 | + assert 4 == len(parts) |
| 90 | + |
| 91 | + |
| 92 | +def test_xquery_vars(client): |
| 93 | + vars = {"word1": "hello", "word2": "world"} |
| 94 | + script = """ |
| 95 | + xquery version "1.0-ml"; |
| 96 | + declare variable $word1 as xs:string external; |
| 97 | + declare variable $word2 as xs:string external; |
| 98 | + fn:concat($word1, " ", $word2) |
| 99 | + """ |
| 100 | + parts = client.eval.xquery(script, vars) |
| 101 | + assert type(parts[0]) is str |
| 102 | + assert "hello world" == parts[0] |
| 103 | + |
| 104 | + |
| 105 | +def test_javascript_vars(client): |
| 106 | + vars = {"word1": "hello", "word2": "world"} |
| 107 | + parts = client.eval.javascript("xdmp.arrayValues([word1, word2])", vars) |
| 108 | + assert type(parts[0]) is str |
| 109 | + assert "hello" == parts[0] |
| 110 | + |
| 111 | + |
| 112 | +def test_xquery_empty_sequence(client): |
| 113 | + parts = client.eval.xquery("()") |
| 114 | + assert parts is None |
| 115 | + |
| 116 | + |
| 117 | +def test_javascript_script(client): |
| 118 | + parts = client.eval.javascript("[]") |
| 119 | + assert [[]] == parts |
| 120 | + |
21 | 121 |
|
22 | | - decoder = MultipartDecoder.from_response(response) |
23 | | - content = decoder.parts[0].text |
24 | | - assert "<hello>world</hello>" == content |
| 122 | +def __verify_common_primitives(parts): |
| 123 | + assert type(parts[0]) is str |
| 124 | + assert "A" == parts[0] |
| 125 | + assert type(parts[1]) is int |
| 126 | + assert 1 == parts[1] |
| 127 | + assert type(parts[2]) is decimal.Decimal |
| 128 | + assert decimal.Decimal("1.1") == parts[2] |
| 129 | + assert type(parts[3]) is bool |
| 130 | + assert parts[3] is False |
| 131 | + assert type(parts[4]) is Document |
| 132 | + assert "/musicians/logo.png" == parts[4].uri |
| 133 | + assert b"PNG" in parts[4].content |
0 commit comments