test_prodcom.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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
  7. from numpy.testing import assert_allclose
  8. PRODCOM2016 = Namespace("http://w3id.org/probs-lab/data/prodcom/2016/")
  9. PRODCOM2017 = Namespace("http://w3id.org/probs-lab/data/prodcom/2017/")
  10. GEONAMES = Namespace("https://sws.geonames.org/")
  11. def assert_exact_results(result, expected_value):
  12. assert len(result) == 1
  13. assert result[0].bound == PROBS.ExactBound
  14. assert_allclose(result[0].measurement, expected_value, rtol=1e-3)
  15. def assert_no_measurement(result):
  16. assert len(result) == 1
  17. assert isnan(result[0].measurement)
  18. def assert_no_result(result):
  19. assert len(result) == 0
  20. # Do most tests for 2016; then spot check a few values for the other years
  21. class TestProdcom2016Data:
  22. """Test PRODCOM2016 DATA conversion"""
  23. endpoint_data = ["outputs/PRODCOM2016DATA.nt.gz",
  24. "outputs/PRD_2016_20200617_185122.nt.gz"]
  25. @pytest.mark.parametrize("code,metric,expected_value", [
  26. ("10113250", QUANTITYKIND.Mass, 2717950),
  27. ("10841270", QUANTITYKIND.Mass, 622435736),
  28. ("15115100", QUANTITYKIND.Mass, 0),
  29. ("17121439", QUANTITYKIND.Mass, 43196000),
  30. ("20132455", QUANTITYKIND.MassAmountOfSubstance, 1396308),
  31. ])
  32. def test_expected_measurements(self, rdfox, code, metric, expected_value):
  33. result = rdfox.get_observations(
  34. PROBS.TimePeriod_YearOf2016,
  35. GEONAMES["2635167"],
  36. metric,
  37. PROBS.SoldProduction,
  38. object_code=code
  39. )
  40. assert_exact_results(result, expected_value)
  41. @pytest.mark.parametrize("code,metric", [
  42. ("14391090", QUANTITYKIND.AmountOfSubstance),
  43. ("20141325", QUANTITYKIND.Mass),
  44. # This one has no measurement or data at all, so is loaded with metric "Unknown"
  45. ("20421975", QUANTITYKIND.Unknown),
  46. ])
  47. def test_missing_measurements(self, rdfox, code, metric):
  48. # these codes have no data -- expect an observation with no measurement
  49. result = rdfox.get_observations(
  50. PROBS.TimePeriod_YearOf2016,
  51. GEONAMES["2635167"] ,
  52. metric,
  53. PROBS.SoldProduction,
  54. object_code=code
  55. )
  56. assert_no_measurement(result)
  57. @pytest.mark.parametrize("code,metric", [
  58. ("14391090", QUANTITYKIND.Mass),
  59. ("20141325", QUANTITYKIND.AmountOfSubstance),
  60. ])
  61. def test_no_result_with_wrong_metric(self, rdfox, code, metric):
  62. # same as above, but with the wrong metric
  63. result = rdfox.get_observations(
  64. PROBS.TimePeriod_YearOf2016,
  65. GEONAMES["2635167"] ,
  66. metric,
  67. PROBS.SoldProduction,
  68. object_code=code
  69. )
  70. assert_no_result(result)
  71. class TestProdcom2017Data:
  72. """Test PRODCOM2017 DATA conversion.
  73. Just include a few expected values, since other behaviour is tested for the 2016 data.
  74. """
  75. endpoint_data = ["outputs/PRODCOM2017DATA.nt.gz",
  76. "outputs/PRD_2017_20200617_185035.nt.gz"]
  77. @pytest.mark.parametrize("code,metric,expected_value", [
  78. ("10721253", QUANTITYKIND.Mass, 244871494),
  79. ("16212400", QUANTITYKIND.Volume, 0),
  80. ("23111290", QUANTITYKIND.Area, 28514781),
  81. ("25711430", QUANTITYKIND.AmountOfSubstance, 329685),
  82. ])
  83. def test_expected_measurements(self, rdfox, code, metric, expected_value):
  84. result = rdfox.get_observations(
  85. PROBS.TimePeriod_YearOf2017,
  86. GEONAMES["2635167"],
  87. metric,
  88. PROBS.SoldProduction,
  89. object_code=code
  90. )
  91. assert_exact_results(result, expected_value)
  92. class TestProdcom2018Data:
  93. """Test PRODCOM2018 DATA conversion.
  94. Just include a few expected values, since other behaviour is tested for the 2016 data.
  95. """
  96. endpoint_data = ["outputs/PRODCOM2018DATA.nt.gz",
  97. "outputs/PRD_2017_20200617_185035.nt.gz"]
  98. @pytest.mark.parametrize("code,metric,expected_value", [
  99. ("10131120", QUANTITYKIND.Mass, 104282828),
  100. ("11052000", QUANTITYKIND.Mass, 737526732),
  101. ("13921530", QUANTITYKIND.Area, 1197738),
  102. ("15112200", QUANTITYKIND.Area, 0),
  103. ])
  104. def test_expected_measurements(self, rdfox, code, metric, expected_value):
  105. result = rdfox.get_observations(
  106. PROBS.TimePeriod_YearOf2018,
  107. GEONAMES["2635167"],
  108. metric,
  109. PROBS.SoldProduction,
  110. object_code=code
  111. )
  112. assert_exact_results(result, expected_value)