graph_test.go 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package gold
  2. import (
  3. "strings"
  4. "testing"
  5. jsonld "github.com/linkeddata/gojsonld"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func TestJSONTerm2Term(t *testing.T) {
  9. term := jsonld.NewResource("http://test.org/")
  10. res1 := jterm2term(term)
  11. res2 := NewResource("http://test.org/")
  12. assert.True(t, res2.Equal(res1))
  13. term = jsonld.NewLiteralWithDatatype("text", jsonld.NewResource("http://www.w3.org/2001/XMLSchema#hexBinary"))
  14. res1 = jterm2term(term)
  15. res2 = NewLiteralWithDatatype("text", NewResource("http://www.w3.org/2001/XMLSchema#hexBinary"))
  16. assert.True(t, res2.Equal(res1))
  17. }
  18. func TestParseJSONLD(t *testing.T) {
  19. r := strings.NewReader(`{ "@id": "http://greggkellogg.net/foaf#me", "http://xmlns.com/foaf/0.1/name": "Gregg Kellogg" }`)
  20. g := NewGraph("https://test.org/")
  21. g.Parse(r, "application/ld+json")
  22. assert.Equal(t, 1, g.Len())
  23. }
  24. func TestSerializeJSONLD(t *testing.T) {
  25. g := NewGraph("https://test.org/")
  26. g.AddTriple(NewResource("a"), NewResource("b"), NewResource("c"))
  27. assert.Equal(t, 1, g.Len())
  28. toJSON, _ := g.Serialize("application/ld+json")
  29. assert.Equal(t, `[{"@id":"a","b":[{"@id":"c"}]}]`, toJSON)
  30. }
  31. func TestGraphPatch(t *testing.T) {
  32. var (
  33. buf string
  34. err error
  35. graph = NewGraph("https://test/")
  36. )
  37. graph.JSONPatch(strings.NewReader(`{"a":{"b":[{"type":"uri","value":"c"}]}}`))
  38. buf, err = graph.Serialize("text/turtle")
  39. assert.Nil(t, err)
  40. assert.Equal(t, buf, "@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .\n\n<a>\n <b> <c> .\n\n")
  41. graph.JSONPatch(strings.NewReader(`{"a":{"b":[{"type":"uri","value":"c2"}]}}`))
  42. buf, err = graph.Serialize("text/turtle")
  43. assert.Nil(t, err)
  44. assert.Equal(t, buf, "@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .\n\n<a>\n <b> <c2> .\n\n")
  45. graph.JSONPatch(strings.NewReader(`{"a":{"b2":[{"type":"uri","value":"c2"}]}}`))
  46. buf, err = graph.Serialize("text/turtle")
  47. assert.Nil(t, err)
  48. assert.Equal(t, buf, "@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .\n\n<a>\n <b> <c2> ;\n <b2> <c2> .\n\n")
  49. }
  50. func TestGraphOne(t *testing.T) {
  51. g := NewGraph("http://test/")
  52. g.AddTriple(NewResource("a"), NewResource("b"), NewResource("c"))
  53. assert.Equal(t, g.One(NewResource("a"), nil, nil).String(), "<a> <b> <c> .")
  54. assert.Equal(t, g.One(NewResource("a"), NewResource("b"), nil).String(), "<a> <b> <c> .")
  55. g.AddTriple(NewResource("a"), NewResource("b"), NewResource("d"))
  56. assert.Equal(t, g.One(NewResource("a"), NewResource("b"), NewResource("d")).String(), "<a> <b> <d> .")
  57. assert.Equal(t, g.One(nil, NewResource("b"), NewResource("d")).String(), "<a> <b> <d> .")
  58. g.AddTriple(NewResource("g"), NewResource("b2"), NewLiteral("e"))
  59. assert.Equal(t, g.One(nil, NewResource("b2"), nil).String(), "<g> <b2> \"e\" .")
  60. assert.Equal(t, g.One(nil, nil, NewLiteral("e")).String(), "<g> <b2> \"e\" .")
  61. assert.Nil(t, g.One(NewResource("x"), nil, nil))
  62. assert.Nil(t, g.One(nil, NewResource("x"), nil))
  63. assert.Nil(t, g.One(nil, nil, NewResource("x")))
  64. }
  65. func TestGraphAll(t *testing.T) {
  66. g := NewGraph("http://test/")
  67. g.AddTriple(NewResource("a"), NewResource("b"), NewResource("c"))
  68. g.AddTriple(NewResource("a"), NewResource("b"), NewResource("d"))
  69. g.AddTriple(NewResource("a"), NewResource("f"), NewLiteral("h"))
  70. g.AddTriple(NewResource("g"), NewResource("b2"), NewResource("e"))
  71. g.AddTriple(NewResource("g"), NewResource("b2"), NewResource("c"))
  72. assert.Equal(t, 0, len(g.All(nil, nil, nil)))
  73. assert.Equal(t, 3, len(g.All(NewResource("a"), nil, nil)))
  74. assert.Equal(t, 2, len(g.All(nil, NewResource("b"), nil)))
  75. assert.Equal(t, 1, len(g.All(nil, nil, NewResource("d"))))
  76. assert.Equal(t, 2, len(g.All(nil, nil, NewResource("c"))))
  77. }