test_3_2_query.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import json
  2. import pdb
  3. import pytest
  4. from flask import g
  5. from lakesuperior.dictionaries.namespaces import ns_collection as nsc
  6. @pytest.mark.usefixtures('client_class')
  7. @pytest.mark.usefixtures('db')
  8. class TestTermSearch:
  9. """
  10. Test term search endpoint.
  11. """
  12. def test_query_all(self):
  13. """
  14. Query all LDP resources.
  15. """
  16. self.client.get('/ldp')
  17. rsp = self.client.post(
  18. '/query/term_search', data=json.dumps({
  19. 'logic': 'and',
  20. 'terms': [{
  21. 'pred': 'rdf:type',
  22. 'op': '_id',
  23. 'val': 'ldp:Resource',
  24. }],
  25. }), content_type='application/json')
  26. assert g.webroot + '/' in rsp.json
  27. def test_query_non_root(self):
  28. """
  29. Query non-root resources.
  30. """
  31. put_resp = self.client.put('/ldp/test_term_search',
  32. data=b'<> <http://purl.org/dc/terms/title> "Hello" .',
  33. content_type='text/turtle')
  34. assert put_resp.status_code == 201
  35. self.client.get('/ldp')
  36. rsp = self.client.post(
  37. '/query/term_search', data=json.dumps({
  38. 'logic': 'and',
  39. 'terms': [{
  40. 'pred': 'dcterms:title',
  41. 'op': '_id',
  42. 'val': '"Hello"',
  43. }],
  44. }), content_type='application/json')
  45. assert rsp.json == [g.webroot + '/test_term_search']
  46. def test_query_root(self):
  47. """
  48. Query root.
  49. """
  50. self.client.get('/ldp')
  51. rsp = self.client.post(
  52. '/query/term_search', data=json.dumps({
  53. 'logic': 'and',
  54. 'terms': [{
  55. 'pred': 'rdf:type',
  56. 'op': '_id',
  57. 'val': 'fcrepo:RepositoryRoot',
  58. }],
  59. }), content_type='application/json')
  60. assert rsp.json == [g.webroot + '/']
  61. def test_query_string_eq(self):
  62. """
  63. Query by string-wise equality.
  64. """
  65. self.client.get('/ldp')
  66. rsp = self.client.post(
  67. '/query/term_search', data=json.dumps({
  68. 'logic': 'and',
  69. 'terms': [{
  70. 'pred': 'dcterms:title',
  71. 'op': '=',
  72. 'val': 'Hello',
  73. }],
  74. }), content_type='application/json')
  75. assert rsp.json == [g.webroot + '/test_term_search']
  76. def test_query_string_neq(self):
  77. """
  78. Query by string-wise inequality.
  79. """
  80. self.client.get('/ldp')
  81. rsp = self.client.post(
  82. '/query/term_search', data=json.dumps({
  83. 'logic': 'and',
  84. 'terms': [{
  85. 'pred': 'dcterms:title',
  86. 'op': '!=',
  87. 'val': 'Repository Root',
  88. }],
  89. }), content_type='application/json')
  90. assert rsp.json == [g.webroot + '/test_term_search']
  91. def test_query_fquri(self):
  92. """
  93. Query using fully qualified URIs.
  94. """
  95. self.client.get('/ldp')
  96. rsp = self.client.post(
  97. '/query/term_search', data=json.dumps({
  98. 'logic': 'and',
  99. 'terms': [{
  100. 'pred': '<http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ',
  101. 'op': '_id',
  102. 'val': '<http://fedora.info/definitions/v4/repository#RepositoryRoot>',
  103. }],
  104. }), content_type='application/json')
  105. assert rsp.json == [g.webroot + '/']
  106. def test_query_multi_term_and(self):
  107. """
  108. Query using two terms and AND logic.
  109. """
  110. self.client.get('/ldp')
  111. rsp = self.client.post(
  112. '/query/term_search', data=json.dumps({
  113. 'logic': 'and',
  114. 'terms': [
  115. {
  116. 'pred': 'rdf:type',
  117. 'op': '_id',
  118. 'val': 'ldp:Container',
  119. },
  120. {
  121. 'pred': 'dcterms:title',
  122. 'op': '=',
  123. 'val': 'Hello',
  124. },
  125. ],
  126. }), content_type='application/json')
  127. assert rsp.json == [g.webroot + '/test_term_search']
  128. def test_query_multi_term_or(self):
  129. """
  130. Query using two terms and AND logic.
  131. """
  132. self.client.get('/ldp')
  133. rsp = self.client.post(
  134. '/query/term_search', data=json.dumps({
  135. 'logic': 'or',
  136. 'terms': [
  137. {
  138. 'pred': 'rdf:type',
  139. 'op': '_id',
  140. 'val': 'ldp:Container',
  141. },
  142. {
  143. 'pred': 'dcterms:title',
  144. 'op': '=',
  145. 'val': 'Hello',
  146. },
  147. ],
  148. }), content_type='application/json')
  149. assert g.webroot + '/' in rsp.json
  150. assert g.webroot + '/test_term_search' in rsp.json