test_prodcom_bulk_total.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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/total_production/DS-056121_006-2016.nt.gz",
  24. "outputs/PRD_2017_20200617_185035.nt.gz"]
  25. @pytest.mark.parametrize("code,metric,expected_value", [
  26. ("13103100", QUANTITYKIND.Mass, 224299),
  27. ("13911910", QUANTITYKIND.Mass, 12566190),
  28. ("15115100", QUANTITYKIND.Mass, 0),
  29. ("20132455", QUANTITYKIND.MassAmountOfSubstance, 1369740),
  30. ("20141290", QUANTITYKIND.Mass, 208123),
  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.TotalProduction,
  38. object_code=code
  39. )
  40. assert_exact_results(result, expected_value)
  41. @pytest.mark.parametrize("code,metric", [
  42. ("13202042", QUANTITYKIND.Area), # No PRDQNT, :C for PQNTFLAG
  43. ("20141325", QUANTITYKIND.Mass), # No PRDQNT, :C for PQNTFLAG
  44. ])
  45. def test_missing_measurements(self, rdfox, code, metric):
  46. # these codes have confidential PRDQNT (observation created with no measurement)
  47. result = rdfox.get_observations(
  48. PROBS.TimePeriod_YearOf2016,
  49. GEONAMES["2635167"] ,
  50. metric,
  51. PROBS.TotalProduction,
  52. object_code=code,
  53. )
  54. assert_no_measurement(result)
  55. @pytest.mark.parametrize("code,metric", [
  56. ("20132455", QUANTITYKIND.Mass),
  57. ("20141325", QUANTITYKIND.AmountOfSubstance),
  58. ])
  59. def test_no_result_with_wrong_metric(self, rdfox, code, metric):
  60. # query using wrong metric
  61. result = rdfox.get_observations(
  62. PROBS.TimePeriod_YearOf2016,
  63. GEONAMES["2635167"] ,
  64. metric,
  65. PROBS.TotalProduction,
  66. object_code=code,
  67. )
  68. assert_no_result(result)
  69. class TestProdcom2017Data:
  70. """Test PRODCOM2017 DATA conversion.
  71. Just include a few expected values, since other behaviour is tested for the 2016 data.
  72. """
  73. endpoint_data = ["outputs/total_production/DS-056121_006-2017.nt.gz",
  74. "outputs/PRD_2017_20200617_185035.nt.gz"]
  75. @pytest.mark.parametrize("code,metric,expected_value", [
  76. ("08111250", QUANTITYKIND.Mass, 604132000),
  77. ("20111120", QUANTITYKIND.Volume, 387410000),
  78. ("13201230", QUANTITYKIND.Area, 8038713),
  79. ("20122415", QUANTITYKIND.MassAmountOfSubstance, 31291132),
  80. ])
  81. def test_expected_measurements(self, rdfox, code, metric, expected_value):
  82. result = rdfox.get_observations(
  83. PROBS.TimePeriod_YearOf2017,
  84. GEONAMES["2635167"],
  85. metric,
  86. PROBS.TotalProduction,
  87. object_code=code
  88. )
  89. assert_exact_results(result, expected_value)
  90. class TestProdcom2018Data:
  91. """Test PRODCOM2018 DATA conversion.
  92. Just include a few expected values, since other behaviour is tested for the 2016 data.
  93. """
  94. endpoint_data = ["outputs/total_production/DS-056121_006-2018.nt.gz",
  95. "outputs/PRD_2017_20200617_185035.nt.gz"]
  96. @pytest.mark.parametrize("code,metric,expected_value", [
  97. ("08114000", QUANTITYKIND.Mass, 41455000),
  98. ("13106135", QUANTITYKIND.Mass, 9193633),
  99. ("13203210", QUANTITYKIND.Area, 4300488),
  100. ("15112200", QUANTITYKIND.Area, 0),
  101. ])
  102. def test_expected_measurements(self, rdfox, code, metric, expected_value):
  103. result = rdfox.get_observations(
  104. PROBS.TimePeriod_YearOf2018,
  105. GEONAMES["2635167"],
  106. metric,
  107. PROBS.TotalProduction,
  108. object_code=code
  109. )
  110. assert_exact_results(result, expected_value)