sparqlupdate_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package gold
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. func TestSPARQLParseInsert(t *testing.T) {
  8. sparql := NewSPARQLUpdate("https://test/")
  9. sparql.Parse(strings.NewReader("INSERT { <a> <b> <c> . }"))
  10. assert.Equal(t, len(sparql.queries), 1)
  11. if len(sparql.queries) > 0 {
  12. assert.Equal(t, sparql.queries[0].verb, "INSERT")
  13. assert.Equal(t, sparql.queries[0].body, " <a> <b> <c> . ")
  14. }
  15. sparql = NewSPARQLUpdate("https://test/")
  16. sparql.Parse(strings.NewReader("INSERT { <a> <b> <data:image/jpeg;base64,/9j/4AAQSkZ=> . }"))
  17. assert.Equal(t, len(sparql.queries), 1)
  18. if len(sparql.queries) > 0 {
  19. assert.Equal(t, sparql.queries[0].verb, "INSERT")
  20. assert.Equal(t, sparql.queries[0].body, " <a> <b> <data:image/jpeg;base64,/9j/4AAQSkZ=> . ")
  21. }
  22. }
  23. func TestSPARQLParseInsertDeleteUri(t *testing.T) {
  24. sparql := NewSPARQLUpdate("https://test/")
  25. sparql.Parse(strings.NewReader("INSERT DATA { <a> <b> <c> . }; DELETE DATA { <a> <b> <c> . }"))
  26. assert.Equal(t, len(sparql.queries), 2)
  27. if len(sparql.queries) > 1 {
  28. assert.Equal(t, sparql.queries[0].verb, "INSERT DATA")
  29. assert.Equal(t, sparql.queries[0].body, " <a> <b> <c> . ")
  30. assert.Equal(t, sparql.queries[1].verb, "DELETE DATA")
  31. assert.Equal(t, sparql.queries[1].body, " <a> <b> <c> . ")
  32. }
  33. }
  34. func TestSPARQLParseInsertDeleteLiteral(t *testing.T) {
  35. sparql := NewSPARQLUpdate("https://test/")
  36. sparql.Parse(strings.NewReader("INSERT DATA { <a> <b> \"};{\" . }; DELETE DATA { <a> <b> \"};{\" . }"))
  37. assert.Equal(t, len(sparql.queries), 2)
  38. if len(sparql.queries) > 1 {
  39. assert.Equal(t, sparql.queries[0].verb, "INSERT DATA")
  40. assert.Equal(t, sparql.queries[0].body, " <a> <b> \"};{\" . ")
  41. assert.Equal(t, sparql.queries[1].verb, "DELETE DATA")
  42. assert.Equal(t, sparql.queries[1].body, " <a> <b> \"};{\" . ")
  43. }
  44. }
  45. func TestSPARQLInsertLiteralWithDataType(t *testing.T) {
  46. sparql := NewSPARQLUpdate("https://test/")
  47. err := sparql.Parse(strings.NewReader("INSERT DATA { <a> <b> \"123\"^^<http://www.w3.org/2001/XMLSchema#int> . }"))
  48. assert.NoError(t, err)
  49. assert.Equal(t, len(sparql.queries), 1)
  50. assert.Equal(t, "INSERT DATA", sparql.queries[0].verb)
  51. assert.Equal(t, " <a> <b> \"123\"^^<http://www.w3.org/2001/XMLSchema#int> . ", sparql.queries[0].body)
  52. graph := NewGraph("https://test/")
  53. code, err := graph.SPARQLUpdate(sparql)
  54. assert.Equal(t, 200, code)
  55. assert.NoError(t, err)
  56. assert.Equal(t, 1, graph.Len())
  57. }
  58. func TestSPARQLUpdateBnodePresent(t *testing.T) {
  59. graph := NewGraph("https://test/")
  60. sparql := NewSPARQLUpdate("https://test/")
  61. err := sparql.Parse(strings.NewReader("INSERT DATA { <a> <b> [ <c> <d> ] . }"))
  62. assert.NoError(t, err)
  63. code, err := graph.SPARQLUpdate(sparql)
  64. assert.Equal(t, 200, code)
  65. assert.NoError(t, err)
  66. assert.Equal(t, 2, graph.Len())
  67. err = sparql.Parse(strings.NewReader("DELETE DATA { <a> <b> [ <c> <d> ] . }"))
  68. assert.NoError(t, err)
  69. code, err = graph.SPARQLUpdate(sparql)
  70. assert.Equal(t, 500, code)
  71. assert.Error(t, err)
  72. }
  73. func TestSPARQLUpdateTripleNotPresent(t *testing.T) {
  74. graph := NewGraph("https://test/")
  75. sparql := NewSPARQLUpdate("https://test/")
  76. err := sparql.Parse(strings.NewReader("INSERT DATA { <a> <b> <c> . }"))
  77. assert.NoError(t, err)
  78. code, err := graph.SPARQLUpdate(sparql)
  79. assert.Equal(t, 200, code)
  80. assert.NoError(t, err)
  81. assert.Equal(t, 1, graph.Len())
  82. err = sparql.Parse(strings.NewReader("DELETE DATA { <a> <b> <d> . }"))
  83. assert.NoError(t, err)
  84. code, err = graph.SPARQLUpdate(sparql)
  85. assert.Equal(t, 409, code)
  86. assert.Error(t, err)
  87. }
  88. func TestSPARQLUpdateMultipleTriples(t *testing.T) {
  89. graph := NewGraph("https://test/")
  90. sparql := NewSPARQLUpdate("https://test/")
  91. err := sparql.Parse(strings.NewReader("INSERT DATA { <a> <b> <c> . }; INSERT DATA { <a> <b> <d> . }"))
  92. assert.NoError(t, err)
  93. code, err := graph.SPARQLUpdate(sparql)
  94. assert.Equal(t, 200, code)
  95. assert.NoError(t, err)
  96. assert.Equal(t, 2, graph.Len())
  97. sparql = NewSPARQLUpdate("https://test/")
  98. err = sparql.Parse(strings.NewReader("DELETE DATA { <a> <b> <c> . }; DELETE DATA { <a> <b> <d> . }; INSERT DATA { <a> <b> <f> . }"))
  99. assert.NoError(t, err)
  100. code, err = graph.SPARQLUpdate(sparql)
  101. assert.Equal(t, 200, code)
  102. assert.NoError(t, err)
  103. assert.Equal(t, 1, graph.Len())
  104. }