Skip to content

Commit 3d87805

Browse files
committed
support for REST response, resultDataContents
1 parent 837b649 commit 3d87805

7 files changed

+50
-9
lines changed

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ Single statement
5353
# => [%{x: 0}, %{x: 1}, %{x: 2}]
5454
```
5555

56+
REST response
57+
58+
```elixir
59+
{:ok, response} = commit(session, statement: "CREATE (n {name: 'andreas'}) RETURN n", resultDataContents: [ "REST" ])
60+
# => [%{n: %{"all_relationships" => "http://localhost:7474/db/data/node/886/relationships/all",
61+
# "all_typed_relationships" => "http://localhost:7474/db/data/node/886/relationships/all/{-list|&|types}",
62+
# ...
63+
```
64+
5665
Multiple statements
5766

5867
```

lib/neoxir.ex

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ defmodule Neoxir do
2929

3030
def commit(session, statements) when is_list statements do
3131
response = Neoxir.TxEndPoint.commit(session, statements)
32+
3233
case Neoxir.CypherResponse.to_rows(response) do
3334
{:ok, rows} -> {:ok, List.first(rows)}
3435
{:error, location, reasons} -> {:error, location, List.first(reasons)}

lib/neoxir/cypher_response.ex

+4-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ defmodule Neoxir.CypherResponse do
2525
end
2626

2727
defp rows(data) do
28-
Enum.map(data, fn %{"row" => row} -> row end)
28+
Enum.map(data, fn
29+
%{"row" => row} -> row
30+
%{"rest" => rest} -> rest
31+
end)
2932
end
3033

3134
end

lib/neoxir/tx_end_point.ex

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ defmodule Neoxir.TxEndPoint do
2929
# You begin a new transaction by posting zero or more Cypher statements to the transaction endpoint.
3030
# The server will respond with the result of your statements, as well as the location of your open transaction.
3131
def begin_tx(%Neoxir.Session{tx_end_point_url: tx_end_point_url}, statements \\ []) do
32-
{:ok, payload} = JSX.encode [statements: statements]
33-
{:ok, response} = HTTPoison.post(tx_end_point_url, payload, @headers)
34-
{:ok, body } = JSX.decode response.body
32+
payload = JSX.encode! statements: statements
33+
response = HTTPoison.post!(tx_end_point_url, payload, @headers)
34+
body = JSX.decode! response.body
3535

3636
%Neoxir.Transaction{commit_url: body["commit"], errors: body["errors"], expires: body["transaction"]["expires"]}
3737
end

test/cypher_response_test.exs

+9
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ defmodule NeoxirCypherResponseTest do
3333
assert rows == [[%{x1: 253}], [%{x2: 254}]]
3434
end
3535

36+
test "with rest response" do
37+
body = "{\"results\":[{\"columns\":[\"n\"],\"data\":[{\"rest\":[{\"outgoing_relationships\":\"http://localhost:7474/db/data/node/849/relationships/out\",\"labels\":\"http://localhost:7474/db/data/node/849/labels\",\"traverse\":\"http://localhost:7474/db/data/node/849/traverse/{returnType}\",\"all_typed_relationships\":\"http://localhost:7474/db/data/node/849/relationships/all/{-list|&|types}\",\"self\":\"http://localhost:7474/db/data/node/849\",\"property\":\"http://localhost:7474/db/data/node/849/properties/{key}\",\"outgoing_typed_relationships\":\"http://localhost:7474/db/data/node/849/relationships/out/{-list|&|types}\",\"properties\":\"http://localhost:7474/db/data/node/849/properties\",\"incoming_relationships\":\"http://localhost:7474/db/data/node/849/relationships/in\",\"create_relationship\":\"http://localhost:7474/db/data/node/849/relationships\",\"paged_traverse\":\"http://localhost:7474/db/data/node/849/paged/traverse/{returnType}{?pageSize,leaseTime}\",\"all_relationships\":\"http://localhost:7474/db/data/node/849/relationships/all\",\"incoming_typed_relationships\":\"http://localhost:7474/db/data/node/849/relationships/in/{-list|&|types}\",\"metadata\":{\"id\":849,\"labels\":[]},\"data\":{}}]}]}],\"errors\":[]}"
38+
response = %Neoxir.CypherResponse{body: body}
39+
{:ok, [rows|_]} = Neoxir.CypherResponse.to_rows(response)
40+
assert length(rows) == 1
41+
assert rows |> List.first |> Dict.keys == [:n]
42+
assert rows |> List.first |> Dict.get(:n) |> Dict.get("data") == %{}
43+
end
44+
3645
test "complex results" do
3746
body = "{\"results\":[{\"columns\":[\"name\",\"related_type\",\"related_to\"],\"data\":[{\"row\":[\"Tom Hanks\",\"ACTED_IN\",{\"roles\":[\"Zachry\",\"Dr. Henry Goose\",\"Isaac Sachs\",\"Dermot Hoggins\"]}]},{\"row\":[\"Hugo Weaving\",\"ACTED_IN\",{\"roles\":[\"Bill Smoke\",\"Haskell Moore\",\"Tadeusz Kesselring\",\"Nurse Noakes\",\"Boardman Mephi\",\"Old Georgie\"]}]},{\"row\":[\"Halle Berry\",\"ACTED_IN\",{\"roles\":[\"Luisa Rey\",\"Jocasta Ayrs\",\"Ovid\",\"Meronym\"]}]},{\"row\":[\"Jim Broadbent\",\"ACTED_IN\",{\"roles\":[\"Vyvyan Ayrs\",\"Captain Molyneux\",\"Timothy Cavendish\"]}]},{\"row\":[\"Tom Tykwer\",\"DIRECTED\",{}]},{\"row\":[\"Andy Wachowski\",\"DIRECTED\",{}]},{\"row\":[\"Lana Wachowski\",\"DIRECTED\",{}]},{\"row\":[\"Stefan Arndt\",\"PRODUCED\",{}]},{\"row\":[\"David Mitchell\",\"WROTE\",{}]},{\"row\":[\"Jessica Thompson\",\"REVIEWED\",{\"summary\":\"An amazing journey\",\"rating\":95}]}]}],\"errors\":[]}"
3847
response = %Neoxir.CypherResponse{body: body}

test/neoxir_test.exs

+10
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ defmodule NeoxirTest do
3535
assert is_number(first_row[:x])
3636
end
3737

38+
test "commit: with REST response", %{session: session} do
39+
{:ok, rows} = commit(session, statement: "CREATE (n {name: 'andreas'}) RETURN n", resultDataContents: [ "REST" ])
40+
assert length(rows) == 1
41+
assert rows |> List.first |> Dict.keys == [:n]
42+
assert rows |> List.first |> Dict.get(:n) |> Dict.get("data") == %{"name" => "andreas"}
43+
end
44+
45+
46+
3847
test "commit: many valid statements", %{session: session} do
3948
statements = [
4049
[statement: "CREATE (n) RETURN ID(n) as x1"],
@@ -81,6 +90,7 @@ defmodule NeoxirTest do
8190
assert is_number(first_row[:x])
8291
end
8392

93+
8494
test "commit!: invalid statement", %{session: session} do
8595
assert_raise Neoxir.CypherResponseError, ~r/Invalid input/, fn ->
8696
commit!(session, statement: "CCCCREATE (n) RETURN ID(n) as x")

test/tx_end_point_test.exs

+14-5
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,35 @@ defmodule NeoxirTxEndPointTest do
1111
end
1212

1313

14-
test "Neoxir.TxEndPoint.commit: many valid cypher queries", %{session: session} do
14+
test "many valid cypher queries", %{session: session} do
1515
statements = [
1616
[statement: "CREATE (n) RETURN ID(n) as x1"],
1717
[statement: "CREATE (n) RETURN ID(n) as x2"]
1818
]
1919

20-
response = Neoxir.TxEndPoint.commit(session, statements) # , resultDataContents: [ "REST" ]
20+
response = Neoxir.TxEndPoint.commit(session, statements)
2121
assert response.status_code == 200
2222
assert String.valid?(response.body)
2323
end
2424

2525

26-
test "Neoxir.TxEndPoint.commit: a valid cypher query", %{session: session} do
26+
test "with rest response", %{session: session} do
27+
statement = "CREATE (TheMatrix:Movie {title:'The Matrix', released:1999, tagline:'Welcome to the Real World'}) return TheMatrix"
28+
response = Neoxir.TxEndPoint.commit(session, statement: statement, resultDataContents: [ "REST" ])
29+
assert response.status_code == 200
30+
assert String.valid?(response.body)
31+
assert Regex.match?(~r/metadata/, response.body)
32+
end
33+
34+
35+
test "a valid cypher query", %{session: session} do
2736
response = Neoxir.TxEndPoint.commit(session, statement: "CREATE (n) RETURN ID(n)")
2837
assert response.status_code == 200
2938
assert String.valid?(response.body)
3039
end
3140

3241

33-
test "Neoxir.TxEndPoint.commit and to_rows", %{session: session} do
42+
test "commit and to_rows", %{session: session} do
3443
response = Neoxir.TxEndPoint.commit(session, statement: "CREATE (n) RETURN ID(n) as x")
3544
{:ok, rows} = Neoxir.CypherResponse.to_rows(response)
3645

@@ -44,7 +53,7 @@ defmodule NeoxirTxEndPointTest do
4453
end
4554

4655

47-
test "commit an invalid cypher query", %{session: session} do
56+
test "an invalid cypher query", %{session: session} do
4857
response = Neoxir.TxEndPoint.commit(session, statement: "CREATEA (n) RETURN ID(n)")
4958
assert response.status_code == 200
5059
assert String.valid?(response.body)

0 commit comments

Comments
 (0)