test_prodcom_bulk_sold.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/sold_production/DS-056120_006-2016.nt.gz",
  24. "outputs/PRD_2017_20200617_185035.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), # 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.SoldProduction,
  52. object_code=code,
  53. )
  54. assert_no_measurement(result)
  55. @pytest.mark.parametrize("code,metric", [
  56. ("10841270", QUANTITYKIND.Volume),
  57. ("20132455", QUANTITYKIND.Mass),
  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.SoldProduction,
  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/sold_production/DS-056120_006-2017.nt.gz",
  74. "outputs/PRD_2017_20200617_185035.nt.gz"]
  75. @pytest.mark.parametrize("code,metric,expected_value", [
  76. ("10721253", QUANTITYKIND.Mass, 244871494),
  77. ("16212400", QUANTITYKIND.Volume, 0),
  78. ("23111290", QUANTITYKIND.Area, 28514781),
  79. ("25711430", QUANTITYKIND.AmountOfSubstance, 329685),
  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.SoldProduction,
  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/sold_production/DS-056120_006-2018.nt.gz",
  95. "outputs/PRD_2017_20200617_185035.nt.gz"]
  96. @pytest.mark.parametrize("code,metric,expected_value", [
  97. ("10131120", QUANTITYKIND.Mass, 104282828),
  98. ("11052000", QUANTITYKIND.Mass, 737526732),
  99. ("13921530", QUANTITYKIND.Area, 1197738),
  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.SoldProduction,
  108. object_code=code
  109. )
  110. assert_exact_results(result, expected_value)