test_4_0_toolbox.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import pytest
  2. from flask import g
  3. from rdflib.term import URIRef
  4. from lakesuperior.dictionaries.namespaces import ns_collection as nsc
  5. @pytest.fixture
  6. def app_ctx(client):
  7. '''
  8. Initialize the app context.
  9. '''
  10. return client.head('/ldp')
  11. @pytest.mark.usefixtures('app_ctx')
  12. class TestToolbox:
  13. '''
  14. Unit tests for toolbox methods.
  15. '''
  16. #def test_camelcase(self):
  17. # '''
  18. # Test conversion from underscore notation to camelcase.
  19. # '''
  20. # assert g.tbox.camelcase('test_input_string') == 'TestInputString'
  21. # assert g.tbox.camelcase('_test_input_string') == '_TestInputString'
  22. # assert g.tbox.camelcase('test__input__string') == 'Test_Input_String'
  23. def test_uid_to_uri(self):
  24. assert g.tbox.uid_to_uri('/1234') == URIRef(g.webroot + '/1234')
  25. assert g.tbox.uid_to_uri('/1/2/34') == URIRef(g.webroot + '/1/2/34')
  26. assert g.tbox.uid_to_uri('') == URIRef(g.webroot)
  27. def test_uri_to_uid(self):
  28. assert g.tbox.uri_to_uid(URIRef(g.webroot) + '/test01') == '/test01'
  29. assert g.tbox.uri_to_uid(URIRef(g.webroot) + '/test01/test02') == \
  30. '/test01/test02'
  31. assert g.tbox.uri_to_uid(URIRef(g.webroot)) == '/'
  32. assert g.tbox.uri_to_uid(nsc['fcres']['/']) == '/'
  33. assert g.tbox.uri_to_uid(nsc['fcres']['/1234']) == '/1234'
  34. assert g.tbox.uri_to_uid(nsc['fcres']['/1234/5678']) == '/1234/5678'
  35. def test_localize_uri_string(self):
  36. '''
  37. Test string localization.
  38. '''
  39. assert g.tbox.localize_uri_string(g.webroot + '/test/uid') == \
  40. g.tbox.localize_uri_string(g.webroot + '/test/uid/') == \
  41. str(nsc['fcres']['/test/uid'])
  42. assert g.tbox.localize_uri_string(g.webroot) == str(nsc['fcres']['/'])
  43. assert g.tbox.localize_uri_string('http://bogus.org/test/uid') == \
  44. 'http://bogus.org/test/uid'
  45. def test_localize_term(self):
  46. '''
  47. Test term localization.
  48. '''
  49. assert g.tbox.localize_term(g.webroot + '/test/uid') == \
  50. g.tbox.localize_term(g.webroot + '/test/uid/') == \
  51. nsc['fcres']['/test/uid']
  52. def test_localize_ext_str(self):
  53. '''
  54. Test extended string localization.
  55. '''
  56. input = '''
  57. @prefix bogus: <http://bogs.r.us#>
  58. @prefix ex: <{0}/ns#>
  59. FREE GRATUITOUS {{
  60. <#blah> a <{0}/type#A> .
  61. <> <info:ex/p> <xyz> .
  62. <#c#r#a#zy> ex:virtue <?blah#e> .
  63. <{0}> <{0}/go#lala>
  64. <{0}/heythere/gulp> .
  65. }} GARB AGE TOKENS {{
  66. <{0}/hey?there#hoho> <https://goglyeyes.com#cearch>
  67. <{0}#hehe> .
  68. <{0}?there#haha> <info:auth/blok> "Hi I'm a strong" .
  69. }}
  70. '''.format(g.webroot)
  71. exp_output = '''
  72. @prefix bogus: <http://bogs.r.us#>
  73. @prefix ex: <info:fcres/ns#>
  74. FREE GRATUITOUS {
  75. <info:fcres/123#blah> a <info:fcres/type#A> .
  76. <info:fcres/123> <info:ex/p> <xyz> .
  77. <info:fcres/123#c#r#a#zy> ex:virtue <info:fcres/123?blah#e> .
  78. <info:fcres/> <info:fcres/go#lala>
  79. <info:fcres/heythere/gulp> .
  80. } GARB AGE TOKENS {
  81. <info:fcres/hey?there#hoho> <https://goglyeyes.com#cearch>
  82. <info:fcres/#hehe> .
  83. <info:fcres/?there#haha> <info:auth/blok> "Hi I'm a strong" .
  84. }
  85. '''
  86. assert g.tbox.localize_ext_str(
  87. input, nsc['fcres']['/123']) == exp_output