Commit 15acf547 authored by abuddenberg's avatar abuddenberg
Browse files

Gave chapter and figure number handling another try; wrote unit test to keep me honest

parent 2b780122
......@@ -79,7 +79,7 @@ class Figure(Gcisbase):
@property
def figure_num(self):
if self.chapter and self.chapter.number and self.ordinal:
if isinstance(self.chapter, Chapter) and self.chapter.number and self.ordinal:
return '{0}.{1}'.format(self.chapter.number, self.ordinal)
else:
return self.ordinal
......@@ -91,21 +91,20 @@ class Figure(Gcisbase):
chp, fig = value.split('.')
chp = int(chp)
fig = int(fig)
except ValueError:
print 'Invalid chapter/figure numbers: ' + value
chp = None
fig = None
self.ordinal = fig
if self.chapter:
#If we have an actual Chapter instance, populate it
if isinstance(self.chapter, Chapter):
self.chapter.number = chp
self.ordinal = fig
else:
self.ordinal = value
self.chapter = chp
def as_json(self, indent=0):
return super(Figure, self).as_json(omit_fields=['images', 'chapter'])
#Exclude a couple of fields
# out_fields = set(self.gcis_fields) - (self.omit_fields | set(['images', 'chapter']))
# return json.dumps({f: self.__dict__[f] for f in out_fields}, indent=indent)
return super(Figure, self).as_json(omit_fields=['images', 'chapter', 'kindred_figures', 'keywords'])
def __str__(self):
string = '{f_id}: Figure {f_num}: {f_name}\n\tImages: {imgs}'.format(
......
......@@ -284,4 +284,41 @@ test_image_json = """
"lat_min": "40.00",
"description": null
}
"""
webform_json_temp = """
{
"what_type_of_source_provided_this_figure": "person",
"when_was_this_figure_created": "2013-08-03 10:04:00",
"what_type_of_figure_is_this": "map",
"what_is_the_file_name_extension_of_the_figure": "CS_Net Change in Ann Temp_12910_v8.png",
"what_is_the_figure_id": "observed-us-temperature-change",
"what_is_the_name_of_the_figure_as_listed_in_the_report": "Observed U.S. Temperature Change",
"what_is_the_creators_phone_number": "(828) 257-3137",
"nid": "3175",
"how_many_images_are_included_within_the_figure": "11",
"what_is_the_creators_email_address": "ken.kunkel@noaa.gov",
"list_the_creator_of_the_figure": "Kenneth Kunkel, NCDC/CICS-NC",
"path": "/system/files/cs_net_change_in_ann_temp_12910_v10.png"
}
"""
webform_json_precip = """
{
"nid": "2506",
"what_type_of_source_provided_this_figure": "person",
"when_was_this_figure_created": "2013-07-02 13:07:00",
"what_type_of_figure_is_this": "map",
"what_is_the_file_name_extension_of_the_figure": "CS_Extreme Heavy precipitation_v7.png",
"what_is_the_creators_phone_number": "(828) 257-3137",
"what_is_the_figure_id": "observed-change-in-very-heavy-precipitation",
"what_is_the_name_of_the_figure_as_listed_in_the_report": "Observed Changes in Very Heavy Precipitation",
"what_is_the_figure_orientation": "original_for_the_report",
"ready_for_publication": "yes",
"what_is_the_chapter_and_figure_number": "2.17",
"how_many_images_are_included_within_the_figure": "10",
"what_is_the_creators_email_address": "ken.kunkel@noaa.gov",
"list_the_creator_of_the_figure": "Kenneth Kunkel, NOAA NCDC/CICS-NC",
"path": "/system/files/cs_extreme_heavy_precipitation_v7.png"
}
"""
\ No newline at end of file
......@@ -4,7 +4,7 @@ __author__ = 'abuddenberg'
# py.test -v test_suite.py
from test_data import test_figure_json, test_image_json
from test_data import test_figure_json, test_image_json, webform_json_temp, webform_json_precip
import json
from domain import Gcisbase, Figure, Image, Dataset, Chapter
......@@ -49,5 +49,23 @@ def test_domain_as_json():
assert all([omitted_key not in img_json_out for omitted_key in ['uri', 'href']])
def test_chapter_parsing():
webform_fig = Figure(json.loads(webform_json_precip))
gcis_fig = Figure(json.loads(test_figure_json))
assert isinstance(webform_fig.ordinal, int)
assert webform_fig.figure_num == 17
assert webform_fig.ordinal == 17
assert webform_fig.chapter is None
merged_figure = webform_fig.merge(gcis_fig)
#FYI, these are identical; I just wanted the variable name to reflect the merge
assert id(merged_figure) == id(webform_fig)
assert merged_figure.figure_num == '2.17'
assert merged_figure.ordinal == 17
assert isinstance(merged_figure.chapter, Chapter)
assert merged_figure.chapter.number == 2
if __name__ == '__main__':
test_domain_as_json()
\ No newline at end of file
test_chapter_parsing()
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment