|
1 | 1 | from collections import OrderedDict |
2 | 2 | import json |
| 3 | +import numpy as np |
3 | 4 | import os |
4 | 5 | import unittest |
5 | 6 |
|
@@ -46,19 +47,57 @@ def test_init_from_partial_template(self): |
46 | 47 | self.assertEqual(tdict['variables'], template.variables) |
47 | 48 | self.assertEqual(tdict['global_attributes'], template.global_attributes) |
48 | 49 |
|
| 50 | + # TODO: def test_json_validation(self): |
| 51 | + |
49 | 52 | # TODO: create template from other formats (later...) |
50 | 53 |
|
51 | | -# TODO: add global attributes |
52 | | -# e.g. template.title = 'Test dataset' |
| 54 | + def test_add_global_attributes(self): |
| 55 | + template = DatasetTemplate() |
| 56 | + template.global_attributes.update(self.global_attributes) |
| 57 | + self.assertEqual(self.global_attributes, template.global_attributes) |
| 58 | + |
| 59 | + def test_add_dimensions(self): |
| 60 | + template = DatasetTemplate.from_json(TEMPLATE_PARTIAL_JSON) |
| 61 | + template.dimensions['TIME'] = 100 |
| 62 | + template.dimensions['DEPTH'] = 10 |
| 63 | + self.assertEqual(OrderedDict([('TIME', 100), ('DEPTH', 10)]), template.dimensions) |
| 64 | + |
| 65 | + def test_update_dimensions(self): |
| 66 | + template = DatasetTemplate.from_json(TEMPLATE_JSON) |
| 67 | + template.dimensions['TIME'] = 100 |
| 68 | + template.dimensions['DEPTH'] = 10 |
| 69 | + self.assertDictContainsSubset(OrderedDict([('TIME', 100), ('DEPTH', 10)]), template.dimensions) |
| 70 | + |
| 71 | + def test_add_variables(self): |
| 72 | + template = DatasetTemplate.from_json(TEMPLATE_PARTIAL_JSON) |
| 73 | + template.variables['TIME'] = self.variables['TIME'] |
| 74 | + self.assertEqual(['TEMP', 'TIME'], template.variables.keys()) |
| 75 | + self.assertEqual(self.variables['TIME'], template.variables['TIME']) |
53 | 76 |
|
54 | | -# TODO: add dimensions |
55 | | -# e.g. template.dimensions['TIME'] = 100 |
| 77 | + def test_add_variable_dimensions(self): |
| 78 | + template = DatasetTemplate.from_json(TEMPLATE_PARTIAL_JSON) |
| 79 | + template.variables['TEMP']['dims'] = ['TIME', 'DEPTH'] |
| 80 | + self.assertEqual(['TIME', 'DEPTH'], template.variables['TEMP']['dims']) |
56 | 81 |
|
57 | | -# TODO: add variables |
58 | | -# TODO: add variable attributes |
59 | | -# e.g. template.variables['PRES']['units'] = 'dbar' |
| 82 | + def test_add_variable_attributes(self): |
| 83 | + template = DatasetTemplate.from_json(TEMPLATE_PARTIAL_JSON) |
| 84 | + template.variables['TEMP']['attr'].update([('units', 'Kelvin'), |
| 85 | + ('comment', 'ok') |
| 86 | + ]) |
| 87 | + self.assertEqual(OrderedDict([('standard_name', 'sea_water_temperature'), |
| 88 | + ('units', 'Kelvin'), |
| 89 | + ('comment', 'ok') |
| 90 | + ]), |
| 91 | + template.variables['TEMP']['attr'] |
| 92 | + ) |
| 93 | + |
| 94 | + def test_set_variable_values(self): |
| 95 | + template = DatasetTemplate.from_json(TEMPLATE_JSON) |
| 96 | + temp_val = np.arange(10, dtype=np.float32) |
| 97 | + template.variables['TEMP']['values'] = temp_val |
| 98 | + self.assertTrue(all(template.variables['TEMP']['values'] == temp_val)) |
60 | 99 |
|
61 | | -# TODO: add data from numpy arrays |
| 100 | +# TODO: add data from multiple numpy arrays |
62 | 101 | # e.g. template.add_data(TIME=time_values, TEMP=temp_values, PRES=pres_values) |
63 | 102 | # TODO: add data from Pandas dataframe (later...) |
64 | 103 | # e.g. template.add_data(dataframe) |
|
0 commit comments