test_prodcom_list.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #!/usr/bin/env python3
  2. import pytest
  3. from numpy import isnan
  4. from hashlib import sha256
  5. from rdflib import Namespace
  6. from probs_runner import PROBS, QUANTITYKIND, answer_queries
  7. from numpy.testing import assert_allclose
  8. from decimal import Decimal
  9. #from uuid import uuid4
  10. PRODCOM2016 = Namespace("http://w3id.org/probs-lab/data/prodcom/2016/")
  11. PRODCOM2017 = Namespace("http://w3id.org/probs-lab/data/prodcom/2017/")
  12. GEONAMES = Namespace("https://sws.geonames.org/")
  13. def assert_exact_results(result, expected_value):
  14. assert len(result) == 1
  15. assert result[0].bound == PROBS.ExactBound
  16. assert_allclose(result[0].measurement, expected_value, rtol=1e-3)
  17. def assert_no_measurement(result):
  18. assert len(result) == 1
  19. assert isnan(result[0].measurement)
  20. class TestProdcomList2016:
  21. """Test PRODCOM List 2016 file conversion"""
  22. endpoint_data = [ "outputs/PRD_2016_20200617_185122.nt.gz" ]
  23. @pytest.mark.parametrize("code,parent,description", [
  24. ("20136700", "201367", "Roasted iron pyrites"),
  25. ("23121150", "231211", "Optical glass of HS 7003, 7004 or 7005, bent, edge-worked, engraved, etc."),
  26. ("271232", "2712", \
  27. "Boards and other bases, equipped with electrical switching or protecting apparatus, for a voltage > 1 000 V")
  28. ])
  29. def test_object_attributes(self, rdfox, code, parent, description):
  30. # test object with parent from PRODCOM 2016 list has correct attributes
  31. obj_name = "\"PRODCOM Object from Code " + code + "\""
  32. code_name = "\"" + code + "\""
  33. code_description = "\"" + description + "\""
  34. parent_name = "\"PRODCOM Object from Code " + parent + "\""
  35. query = r""" PREFIX ufpc: <http://w3id.org/probs-lab/data/prodcom/>
  36. SELECT ?a WHERE {
  37. ?a rdfs:label %s ;
  38. :hasClassificationCode ?b .
  39. ?b rdfs:label %s ;
  40. :codeDescription %s ;
  41. :partOfList ufpc:Prodcom2016List .
  42. ?c rdfs:label %s ;
  43. :objectComposedOf ?a .
  44. } """ % (obj_name, code_name, code_description, parent_name)
  45. result = rdfox.query_records(query)
  46. assert len(result) == 1
  47. @pytest.mark.parametrize("code,description", [
  48. ("0710", "Mining of iron ores"),
  49. ("1020", "Processing and preserving of fish, crustaceans and molluscs"),
  50. ("3230", "Manufacture of sports goods")
  51. ])
  52. def test_object_no_parent(self, rdfox, code, description):
  53. # test object not having a parent from PRODCOM 2016 list has correct attributes
  54. obj_name = "\"PRODCOM Object from Code " + code + "\""
  55. code_name = "\"" + code + "\""
  56. code_description = "\"" + description + "\""
  57. query = r""" PREFIX ufpc: <http://w3id.org/probs-lab/data/prodcom/>
  58. SELECT ?a WHERE {
  59. ?a rdfs:label %s ;
  60. :hasClassificationCode ?b .
  61. ?b rdfs:label %s ;
  62. :codeDescription %s ;
  63. :partOfList ufpc:Prodcom2016List .
  64. FILTER NOT EXISTS {
  65. ?c :objectComposedOf ?a .
  66. }
  67. } """ % (obj_name, code_name, code_description)
  68. result = rdfox.query_records(query)
  69. assert len(result) == 1
  70. class TestProdcomList2017:
  71. """Test PRODCOM List 2017 file conversion"""
  72. endpoint_data = [ "outputs/PRD_2017_20200617_185035.nt.gz" ]
  73. @pytest.mark.parametrize("code,parent,description", [
  74. ("104125", "1041", "Palm oil, crude"),
  75. ("20121973", "201219", "Molybdenum oxides and hydroxides"),
  76. ("26601300", "266013", "Ultraviolet or infrared apparatus used in medical, surgical, dental or veterinary sciences")
  77. ])
  78. def test_object_attributes(self, rdfox, code, parent, description):
  79. # test object with parent from PRODCOM 2017 list has correct attributes
  80. obj_name = "\"PRODCOM Object from Code " + code + "\""
  81. code_name = "\"" + code + "\""
  82. code_description = "\"" + description + "\""
  83. parent_name = "\"PRODCOM Object from Code " + parent + "\""
  84. query = r""" PREFIX ufpc: <http://w3id.org/probs-lab/data/prodcom/>
  85. SELECT ?a WHERE {
  86. ?a rdfs:label %s ;
  87. :hasClassificationCode ?b .
  88. ?b rdfs:label %s ;
  89. :codeDescription %s ;
  90. :partOfList ufpc:Prodcom2017List .
  91. ?c rdfs:label %s ;
  92. :objectComposedOf ?a .
  93. } """ % (obj_name, code_name, code_description, parent_name)
  94. result = rdfox.query_records(query)
  95. assert len(result) == 1
  96. @pytest.mark.parametrize("code,description", [
  97. ("1083", "Processing of tea and coffee"),
  98. ("1723", "Manufacture of paper stationery"),
  99. ("0099t", "T-Codes")
  100. ])
  101. def test_object_no_parent(self, rdfox, code, description):
  102. # test object not having a parent from PRODCOM 2017 list has correct attributes
  103. obj_name = "\"PRODCOM Object from Code " + code + "\""
  104. code_name = "\"" + code + "\""
  105. code_description = "\"" + description + "\""
  106. query = r""" PREFIX ufpc: <http://w3id.org/probs-lab/data/prodcom/>
  107. SELECT ?a WHERE {
  108. ?a rdfs:label %s ;
  109. :hasClassificationCode ?b .
  110. ?b rdfs:label %s ;
  111. :codeDescription %s ;
  112. :partOfList ufpc:Prodcom2017List .
  113. FILTER NOT EXISTS {
  114. ?c :objectComposedOf ?a .
  115. }
  116. } """ % (obj_name, code_name, code_description)
  117. result = rdfox.query_records(query)
  118. assert len(result) == 1