invest/tests/test_spec_utils.py

259 lines
9.0 KiB
Python

import unittest
from natcap.invest import spec_utils
from natcap.invest.unit_registry import u
from osgeo import gdal
gdal.UseExceptions()
class TestSpecUtils(unittest.TestCase):
def test_number_spec(self):
spec = {
"name": "Bar",
"about": "Description",
"type": "number",
"units": u.meter**3/u.month,
"expression": "value >= 0"
}
out = spec_utils.describe_arg_from_spec(spec['name'], spec)
expected_rst = ([
'**Bar** (`number <input_types.html#number>`__, '
'units: **m³/month**, *required*): Description'])
self.assertEqual(repr(out), repr(expected_rst))
def test_ratio_spec(self):
spec = {
"name": "Bar",
"about": "Description",
"type": "ratio"
}
out = spec_utils.describe_arg_from_spec(spec['name'], spec)
expected_rst = (['**Bar** (`ratio <input_types.html#ratio>`__, '
'*required*): Description'])
self.assertEqual(repr(out), repr(expected_rst))
def test_percent_spec(self):
spec = {
"name": "Bar",
"about": "Description",
"type": "percent",
"required": False
}
out = spec_utils.describe_arg_from_spec(spec['name'], spec)
expected_rst = (['**Bar** (`percent <input_types.html#percent>`__, '
'*optional*): Description'])
self.assertEqual(repr(out), repr(expected_rst))
def test_code_spec(self):
spec = {
"name": "Bar",
"about": "Description",
"type": "integer",
"required": True
}
out = spec_utils.describe_arg_from_spec(spec['name'], spec)
expected_rst = (['**Bar** (`integer <input_types.html#integer>`__, '
'*required*): Description'])
self.assertEqual(repr(out), repr(expected_rst))
def test_boolean_spec(self):
spec = {
"name": "Bar",
"about": "Description",
"type": "boolean"
}
out = spec_utils.describe_arg_from_spec(spec['name'], spec)
expected_rst = (['**Bar** (`true/false <input_types.html#truefalse>'
'`__): Description'])
self.assertEqual(repr(out), repr(expected_rst))
def test_freestyle_string_spec(self):
spec = {
"name": "Bar",
"about": "Description",
"type": "freestyle_string"
}
out = spec_utils.describe_arg_from_spec(spec['name'], spec)
expected_rst = (['**Bar** (`text <input_types.html#text>`__, '
'*required*): Description'])
self.assertEqual(repr(out), repr(expected_rst))
def test_option_string_spec_dictionary(self):
spec = {
"name": "Bar",
"about": "Description",
"type": "option_string",
"options": {
"option_a": {
"display_name": "A"
},
"Option_b": {
"description": "do something"
},
"option_c": {
"display_name": "c",
"description": "do something else"
}
}
}
# expect that option case is ignored
# otherwise, c would sort before A
out = spec_utils.describe_arg_from_spec(spec['name'], spec)
expected_rst = ([
'**Bar** (`option <input_types.html#option>`__, *required*): Description',
'\tOptions:',
'\t- A',
'\t- c: do something else',
'\t- Option_b: do something'
])
self.assertEqual(repr(out), repr(expected_rst))
def test_option_string_spec_list(self):
spec = {
"name": "Bar",
"about": "Description",
"type": "option_string",
"options": ["option_a", "Option_b"]
}
out = spec_utils.describe_arg_from_spec(spec['name'], spec)
expected_rst = ([
'**Bar** (`option <input_types.html#option>`__, *required*): Description',
'\tOptions: option_a, Option_b'
])
self.assertEqual(repr(out), repr(expected_rst))
def test_raster_spec(self):
spec = {
"type": "raster",
"bands": {1: {"type": "integer"}},
"about": "Description",
"name": "Bar"
}
out = spec_utils.describe_arg_from_spec(spec['name'], spec)
expected_rst = ([
'**Bar** (`raster <input_types.html#raster>`__, *required*): Description'
])
self.assertEqual(repr(out), repr(expected_rst))
spec = {
"type": "raster",
"bands": {1: {
"type": "number",
"units": u.millimeter/u.year
}},
"about": "Description",
"name": "Bar"
}
out = spec_utils.describe_arg_from_spec(spec['name'], spec)
expected_rst = ([
'**Bar** (`raster <input_types.html#raster>`__, units: **mm/year**, *required*): Description'
])
self.assertEqual(repr(out), repr(expected_rst))
def test_vector_spec(self):
spec = {
"type": "vector",
"fields": {},
"geometries": {"LINESTRING"},
"about": "Description",
"name": "Bar"
}
out = spec_utils.describe_arg_from_spec(spec['name'], spec)
expected_rst = ([
'**Bar** (`vector <input_types.html#vector>`__, linestring, *required*): Description'
])
self.assertEqual(repr(out), repr(expected_rst))
spec = {
"type": "vector",
"fields": {
"id": {
"type": "integer",
"about": "Unique identifier for each feature"
},
"precipitation": {
"type": "number",
"units": u.millimeter/u.year,
"about": "Average annual precipitation over the area"
}
},
"geometries": {"POLYGON", "MULTIPOLYGON"},
"about": "Description",
"name": "Bar"
}
out = spec_utils.describe_arg_from_spec(spec['name'], spec)
expected_rst = ([
'**Bar** (`vector <input_types.html#vector>`__, polygon/multipolygon, *required*): Description',
])
self.assertEqual(repr(out), repr(expected_rst))
def test_csv_spec(self):
spec = {
"type": "csv",
"about": "Description.",
"name": "Bar"
}
out = spec_utils.describe_arg_from_spec(spec['name'], spec)
expected_rst = ([
'**Bar** (`CSV <input_types.html#csv>`__, *required*): Description. '
'Please see the sample data table for details on the format.'
])
self.assertEqual(repr(out), repr(expected_rst))
# Test every type that can be nested in a CSV column:
# number, ratio, percent, code,
spec = {
"type": "csv",
"about": "Description",
"name": "Bar",
"columns": {
"b": {"type": "ratio", "about": "description"}
}
}
out = spec_utils.describe_arg_from_spec(spec['name'], spec)
expected_rst = ([
'**Bar** (`CSV <input_types.html#csv>`__, *required*): Description'
])
self.assertEqual(repr(out), repr(expected_rst))
def test_directory_spec(self):
self.maxDiff = None
spec = {
"type": "directory",
"about": "Description",
"name": "Bar",
"contents": {}
}
out = spec_utils.describe_arg_from_spec(spec['name'], spec)
expected_rst = ([
'**Bar** (`directory <input_types.html#directory>`__, *required*): Description'
])
self.assertEqual(repr(out), repr(expected_rst))
def test_multi_type_spec(self):
spec = {
"type": {"raster", "vector"},
"about": "Description",
"name": "Bar",
"bands": {1: {"type": "integer"}},
"geometries": {"POLYGON"},
"fields": {}
}
out = spec_utils.describe_arg_from_spec(spec['name'], spec)
expected_rst = ([
'**Bar** (`raster <input_types.html#raster>`__ or `vector <input_types.html#vector>`__, *required*): Description'
])
self.assertEqual(repr(out), repr(expected_rst))
def test_real_model_spec(self):
from natcap.invest import carbon
out = spec_utils.describe_arg_from_name(
'natcap.invest.carbon', 'carbon_pools_path', 'columns', 'lucode')
expected_rst = (
'.. _carbon-pools-path-columns-lucode:\n\n' +
'**lucode** (`integer <input_types.html#integer>`__, *required*): ' +
carbon.MODEL_SPEC['args']['carbon_pools_path']['columns']['lucode']['about']
)
self.assertEqual(repr(out), repr(expected_rst))