From 062b75476dd05c67cafa647d4b4a4456440c4604 Mon Sep 17 00:00:00 2001 From: elf Pavlik Date: Mon, 9 Dec 2013 00:32:32 +0100 Subject: [PATCH] added new search example based on passing test --- README.md | 74 +++++++++++++++++++++++++++++------------- test/fixture/manu.json | 7 ++-- test/search_spec.js | 71 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 125 insertions(+), 27 deletions(-) create mode 100644 test/search_spec.js diff --git a/README.md b/README.md index c579ed8..4a972c3 100644 --- a/README.md +++ b/README.md @@ -159,35 +159,65 @@ db.jsonld.del(manu["@id"], function(err) { ### Searching with LevelGraph __LevelGraph-JSONLD__ does not support searching for objects, because -that problem is already solved by __LevelGraph__ itself, like these: +that problem is already solved by __LevelGraph__ itself. This example +search finds friends living near Paris: ```javascript -var nested = { +var manu = { "@context": { - "name": "http://xmlns.com/foaf/0.1/name" - , "knows": "http://xmlns.com/foaf/0.1/knows" + "@vocab": "http://xmlns.com/foaf/0.1/" + , "homepage": { "@type": "@id" } + , "knows": { "@type": "@id" } + , "based_near": { "@type": "@id" } } - , "@id": "http://matteocollina.com" - , "name": "matteo" - , "knows": [{ - "name": "daniele" + , "@id": "http://manu.sporny.org#person" + , "name": "Manu Sporny" + , "homepage": "http://manu.sporny.org/" + , "knows": [ + { + "@id": "https://my-profile.eu/people/deiu/card#me", + "name": "Andrei Vlad Sambra", + "based_near": "http://dbpedia.org/resource/Paris" }, { - "name": "lucio" - }] -}; + "@id": "http://melvincarvalho.com/#me", + "name": "Melvin Carvalho", + "based_near": "http://dbpedia.org/resource/Honolulu" + }, { + "@id": "http://bblfish.net/people/henry/card#me", + "name": "Henry Story", + "based_near": "http://dbpedia.org/resource/Paris" + }, { + "@id": "http://presbrey.mit.edu/foaf#presbrey", + "name": "Joe Presbrey", + "based_near": "http://dbpedia.org/resource/Cambridge" + } + ] +} -db.jsonld.put(nested, function(err) { +var paris = "http://dbpedia.org/resource/Paris"; + +db.jsonld.put(manu, function(){ db.join([{ - subject: db.v("person") - , predicate: "http://xmlns.com/foaf/0.1/knows" - , object: db.v("friend") + subject: manu["@id"], + predicate: "http://xmlns.com/foaf/0.1/knows", + object: db.v("webid") + }, { + subject: db.v("webid"), + predicate: "http://xmlns.com/foaf/0.1/based_near", + object: paris }, { - subject: db.v("friend") - , predicate: "http://xmlns.com/foaf/0.1/knows" - , object: "daniele" - }], function(err, solutions) { - // The solutions will be: - // 1. { person: "http://matteocollina.com", friend: "_:abcde" } - // 1. { person: "http://matteocollina.com", friend: "_:efghi" } + subject: db.v("webid"), + predicate: "http://xmlns.com/foaf/0.1/name", + object: db.v("name") + } + ], function(err, solution) { + // solution contains + // [{ + // webid: "http://bblfish.net/people/henry/card#me", + // name: "Henry Story" + // }, { + // webid: "https://my-profile.eu/people/deiu/card#me", + // name: "Andrei Vlad Sambra" + // }] }); }); ``` diff --git a/test/fixture/manu.json b/test/fixture/manu.json index c4a8b67..fee6465 100644 --- a/test/fixture/manu.json +++ b/test/fixture/manu.json @@ -1,10 +1,7 @@ { "@context": { - "name": "http://xmlns.com/foaf/0.1/name" - , "homepage": { - "@id": "http://xmlns.com/foaf/0.1/homepage" - , "@type": "@id" - } + "@vocab": "http://xmlns.com/foaf/0.1/" + , "homepage": { "@type": "@id" } } , "@id": "http://manu.sporny.org#person" , "name": "Manu Sporny" diff --git a/test/search_spec.js b/test/search_spec.js new file mode 100644 index 0000000..a1ea422 --- /dev/null +++ b/test/search_spec.js @@ -0,0 +1,71 @@ + +var level = require("level-test")() + , graph = require("levelgraph") + , jsonld = require("../"); + +describe("jsonld.put", function() { + + var db, gang, manu; + + beforeEach(function() { + db = jsonld(graph(level())); + manu = fixture("manu.json"); + manu["@context"]["knows"] = { "@type": "@id" }; + manu["@context"]["based_near"] = { "@type": "@id" }; + manu["knows"] = [ + { + "@id": "https://my-profile.eu/people/deiu/card#me", + "name": "Andrei Vlad Sambra", + "based_near": "http://dbpedia.org/resource/Paris" + }, { + "@id": "http://melvincarvalho.com/#me", + "name": "Melvin Carvalho", + "based_near": "http://dbpedia.org/resource/Honolulu" + }, { + "@id": "http://bblfish.net/people/henry/card#me", + "name": "Henry Story", + "based_near": "http://dbpedia.org/resource/Paris" + }, { + "@id": "http://presbrey.mit.edu/foaf#presbrey", + "name": "Joe Presbrey", + "based_near": "http://dbpedia.org/resource/Cambridge" + } + ]; + }); + + afterEach(function(done) { + db.close(done); + }); + + it("should find homies in Paris", function(done) { + var paris = "http://dbpedia.org/resource/Paris"; + var parisians = [{ + webid: "http://bblfish.net/people/henry/card#me", + name: "Henry Story" + }, { + webid: "https://my-profile.eu/people/deiu/card#me", + name: "Andrei Vlad Sambra" + }]; + + db.jsonld.put(manu, function(){ + db.join([{ + subject: manu["@id"], + predicate: "http://xmlns.com/foaf/0.1/knows", + object: db.v("webid") + }, { + subject: db.v("webid"), + predicate: "http://xmlns.com/foaf/0.1/based_near", + object: paris + }, { + subject: db.v("webid"), + predicate: "http://xmlns.com/foaf/0.1/name", + object: db.v("name") + } + ], function(err, solution) { + expect(solution).to.eql(parisians); + done(); + }); + }); + }); + +});