test-issue17.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. //
  2. // test-issue17.c
  3. //
  4. // Copyright (c) 2015-2018, Marcus Rohrmoser mobile Software, http://mro.name/~me
  5. // All rights reserved.
  6. //
  7. // Redistribution and use in source and binary forms, with or without modification, are permitted
  8. // provided that the following conditions are met:
  9. //
  10. // 1. Redistributions of source code must retain the above copyright notice, this list of conditions
  11. // and the following disclaimer.
  12. //
  13. // 2. The software must not be used for military or intelligence or related purposes nor
  14. // anything that's in conflict with human rights as declared in http://www.un.org/en/documents/udhr/ .
  15. //
  16. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  17. // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  18. // FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  19. // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  22. // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  23. // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. //
  25. #define LIBRDF_STORAGE_SQLITE_MRO_CONVENIENCE 1
  26. #define VANILLA
  27. #ifdef VANILLA
  28. #include <librdf.h>
  29. #else
  30. #include "../rdf_storage_sqlite_mro.h"
  31. #endif
  32. #include "mtest.h"
  33. #include <unistd.h>
  34. #include <string.h>
  35. int tests_run = 0;
  36. static char *test_sparql();
  37. static char *all_tests()
  38. {
  39. MUTestRun(test_sparql);
  40. return NULL;
  41. }
  42. int main(int argc, char **argv)
  43. {
  44. char *result = all_tests();
  45. if( result != 0 ) {
  46. printf("%s\n", result);
  47. } else {
  48. printf(ANSI_COLOR_F_GREEN "✓" ANSI_COLOR_RESET " ALL TESTS PASSED\n");
  49. }
  50. printf("Tests run: %d\n", tests_run);
  51. return result != 0;
  52. }
  53. //
  54. // https://github.com/nlphacker/Audacity/blob/570c5651c5934469c18dad25db03f05076f91225/lib-src/redland/rasqal/src/rasqal_query_test.c
  55. /*
  56. * test-sparql1.c - run SPARQL query against a librdf.sqlite model
  57. *
  58. * based on https://gist.github.com/rtravis/cd32113c0fc543da120d
  59. *
  60. * compile:
  61. * gcc -I/usr/include/raptor2 -I/usr/include/rasqal -o "test-sparql1" "test-sparql1.c"
  62. */
  63. static char *test_sparql()
  64. {
  65. const unsigned char query_string[] = ""
  66. "SELECT ?author ?title\n"
  67. "WHERE {\n"
  68. " ?book a <http://dbpedia.org/ontology/Book> .\n"
  69. " ?book <http://xmlns.com/foaf/0.1/name> ?title .\n"
  70. " ?book <http://dbpedia.org/ontology/author> ?author.\n" "}\n";
  71. librdf_world *world = librdf_new_world();
  72. MUAssert(0 != sizeof(query_string), "uhu");
  73. #ifndef VANILLA
  74. librdf_init_storage_sqlite_mro(world);
  75. #endif
  76. {
  77. const char options[] = "new='yes',contexts='yes',synchronous='off'";
  78. #ifdef VANILLA
  79. librdf_storage *store = librdf_new_storage(world, "sqlite", "tmp/test-issue17.sqlite", options);
  80. #else
  81. librdf_storage *store = librdf_new_storage(world, LIBRDF_STORAGE_SQLITE_MRO, "tmp/test-issue17.sqlite", options);
  82. #endif
  83. MUAssert(store, "Failed to create store");
  84. {
  85. librdf_model *model = librdf_new_model(world, store, NULL);
  86. MUAssert(model, "Failed to create model");
  87. {
  88. librdf_uri *uri = NULL;
  89. {
  90. char src_uri[1024] = "file://";
  91. MUAssert(getcwd( src_uri + strlen(src_uri), sizeof(src_uri) - strlen(src_uri) ),
  92. "Failed to get current working dir.");
  93. strncat(src_uri, "/test-issue17.ttl", sizeof(src_uri) - strlen(src_uri) - 1);
  94. uri = librdf_new_uri(world, (const unsigned char *)src_uri);
  95. MUAssert(uri, "Failed to create URI");
  96. }
  97. {
  98. librdf_parser *parser = librdf_new_parser(world, "turtle", NULL, NULL);
  99. MUAssert(parser, "Failed to create parser");
  100. fprintf( stdout, "%s: Parsing URI %s\n", __FILE__, librdf_uri_as_string(uri) );
  101. if( librdf_parser_parse_into_model(parser, uri, uri, model) ) {
  102. // fprintf(stderr, "%s: Failed to parse RDF into model\n", program);
  103. MUAssert(0, "Failed to parse RDF");
  104. }
  105. librdf_free_parser(parser);
  106. }
  107. librdf_free_uri(uri);
  108. }
  109. {
  110. librdf_query *rdf_query =
  111. librdf_new_query(world, "sparql", NULL, query_string, NULL);
  112. MUAssert(rdf_query, "Failed to create query");
  113. {
  114. librdf_query_results *res = librdf_query_execute(rdf_query, model);
  115. MUAssert(0 == librdf_query_results_get_count(res), "expected 0 so far");
  116. { // 1st result row
  117. MUAssert(0 == librdf_query_results_next(res), "oha 0");
  118. MUAssert(1 == librdf_query_results_get_count(res), "expected 1 so far");
  119. MUAssert(2 == librdf_query_results_get_bindings_count(res), "why has this to be 2?");
  120. MUAssert(0 == strcmp("author", librdf_query_results_get_binding_name(res, 0)), "ouch");
  121. {
  122. librdf_node *nod = librdf_query_results_get_binding_value(res, 0);
  123. MUAssert(0 == strcmp("http://dbpedia.org/resource/Donald_Knuth", (char*)librdf_uri_as_string(librdf_node_get_uri(nod))), "uhu");
  124. librdf_free_node(nod);
  125. }
  126. MUAssert(0 == strcmp("title", librdf_query_results_get_binding_name(res, 1)), "ouch");
  127. {
  128. librdf_node *nod = librdf_query_results_get_binding_value(res, 1);
  129. MUAssert(0 == strcmp("The Art of Computer Programming", (char*)librdf_node_get_literal_value(nod)), "uhu");
  130. librdf_free_node(nod);
  131. }
  132. MUAssert(NULL == librdf_query_results_get_binding_name(res, 2), "ouch");
  133. // fprintf(stdout, "name: %s\n", librdf_query_results_get_binding_value(res, 2) );
  134. }
  135. { // 2nd result row
  136. MUAssert(0 == librdf_query_results_next(res), "oha 0");
  137. MUAssert(2 == librdf_query_results_get_count(res), "expected 2 so far");
  138. MUAssert(2 == librdf_query_results_get_bindings_count(res), "why has this to be 2?");
  139. MUAssert(0 == strcmp("author", librdf_query_results_get_binding_name(res, 0)), "ouch");
  140. {
  141. librdf_node *nod = librdf_query_results_get_binding_value(res, 0);
  142. MUAssert(0 == strcmp("http://dbpedia.org/resource/Plato", (char*)librdf_uri_as_string(librdf_node_get_uri(nod))), "uhu");
  143. librdf_free_node(nod);
  144. }
  145. MUAssert(0 == strcmp("title", librdf_query_results_get_binding_name(res, 1)), "ouch");
  146. {
  147. librdf_node *nod = librdf_query_results_get_binding_value(res, 1);
  148. MUAssert(0 == strcmp("Republic", (char*)librdf_node_get_literal_value(nod)), "uhu");
  149. librdf_free_node(nod);
  150. }
  151. MUAssert(NULL == librdf_query_results_get_binding_name(res, 2), "ouch");
  152. // fprintf(stdout, "name: %s\n", librdf_query_results_get_binding_value(res, 0) );
  153. }
  154. { // 3rd result row
  155. MUAssert(0 != librdf_query_results_next(res), "oha 2");
  156. }
  157. librdf_free_query_results(res);
  158. }
  159. librdf_free_query(rdf_query);
  160. }
  161. librdf_free_model(model);
  162. }
  163. librdf_free_storage(store);
  164. }
  165. librdf_free_world(world);
  166. return NULL;
  167. }