Skip to content

Commit 4cb559f

Browse files
committed
Add category query
1 parent 5d0e9e3 commit 4cb559f

6 files changed

Lines changed: 135 additions & 0 deletions

File tree

opus/api.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from .mults import Mults
1414
from .range import Range
1515
from .fields import Fields, Field
16+
from .categories import Categories, Category
1617

1718
API_URL = 'https://tools.pds-rings.seti.org/opus/api'
1819

@@ -129,3 +130,11 @@ def field(self, field):
129130
def fields(self):
130131
'''Get list of all fields'''
131132
return Fields(self.load('fields'))
133+
134+
def category(self, name='obs_general'):
135+
'''Get all fields in a category'''
136+
return Category(self.load('category/'+name))
137+
138+
def categories(self):
139+
'''List category names'''
140+
return Categories(self.load('categories'))

opus/categories.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# -*- coding: utf-8 -*-
2+
3+
class Category(object):
4+
def __init__(self, json):
5+
self.name = json['table_name']
6+
self.label = json['label']
7+
8+
def __repr__(self):
9+
return '`{}` -> {}'.format(self.name, self.label)
10+
11+
12+
class Categories(object):
13+
def __init__(self, json):
14+
self._json = json
15+
self._data = {}
16+
for category in json:
17+
self._data[category['table_name']] = Category(category)
18+
19+
def __repr__(self):
20+
return 'OPUS API list of all categories ({}):\n'.format(len(self)) + \
21+
'\n'.join(
22+
' - {} ({})'.format(value.label, key) for key, value in self.items()
23+
)
24+
25+
def __len__(self):
26+
return len(self._json)
27+
28+
def __getitem__(self, attr):
29+
return self._data[attr.lower()]
30+
31+
def __iter__(self):
32+
return iter(self._data)
33+
34+
def keys(self):
35+
return self._data.keys()
36+
37+
def items(self):
38+
return self._data.items()
39+
40+
def values(self):
41+
return self._data.values()
42+

tests/api/categories.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[{"table_name": "obs_general", "label": "General Constraints"}, {"table_name": "obs_ring_geometry", "label": "Ring Geometry Constraints"}, {"table_name": "obs_wavelength", "label": "Wavelength Constraints"}]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"table_name": "obs_general", "label": "General Constraints"}

tests/test_api.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,3 +306,35 @@ def test_api_fields(api):
306306
assert responses.calls[0].response.text == fields
307307

308308
assert len(resp) == 3971
309+
310+
311+
@responses.activate
312+
def test_api_category(api):
313+
category = open('tests/api/category/obs_general.json', 'r').read()
314+
responses.add(responses.GET,
315+
'http://localhost/category/obs_general.json',
316+
body=category)
317+
318+
resp = api.category('obs_general')
319+
320+
assert len(responses.calls) == 1
321+
assert responses.calls[0].request.url == 'http://localhost/category/obs_general.json'
322+
assert responses.calls[0].response.text == category
323+
324+
assert resp.name == 'obs_general'
325+
326+
327+
@responses.activate
328+
def test_api_categories(api):
329+
categories = open('tests/api/categories.json', 'r').read()
330+
responses.add(responses.GET,
331+
'http://localhost/categories.json',
332+
body=categories)
333+
334+
resp = api.categories()
335+
336+
assert len(responses.calls) == 1
337+
assert responses.calls[0].request.url == 'http://localhost/categories.json'
338+
assert responses.calls[0].response.text == categories
339+
340+
assert len(resp) == 3

tests/test_categories.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# -*- coding: utf-8 -*-
2+
import json as JSON
3+
import pytest
4+
import six
5+
6+
from opus.categories import *
7+
8+
@pytest.fixture
9+
def category():
10+
json = JSON.loads(open('tests/api/category/obs_general.json', 'r').read())
11+
return Category(json)
12+
13+
@pytest.fixture
14+
def categories():
15+
json = JSON.loads(open('tests/api/categories.json', 'r').read())
16+
return Categories(json)
17+
18+
def test_category_repr(category):
19+
assert repr(category) == '`obs_general` -> General Constraints'
20+
21+
22+
def test_categories_repr(categories):
23+
if six.PY3:
24+
assert repr(categories) == \
25+
'OPUS API list of all categories (3):\n' + \
26+
' - General Constraints (obs_general)\n' + \
27+
' - Ring Geometry Constraints (obs_ring_geometry)\n' + \
28+
' - Wavelength Constraints (obs_wavelength)'
29+
else:
30+
r = repr(categories)
31+
assert 'OPUS API list of all categories (3):' in r
32+
assert 'obs_general' in r
33+
assert 'obs_ring_geometry' in r
34+
assert 'obs_wavelength' in r
35+
36+
def test_categories_iter(categories):
37+
assert 'obs_general' in categories
38+
39+
for key, value in categories.items():
40+
assert categories[key] == value
41+
break
42+
43+
def test_categories_keys_values(categories):
44+
keys = categories.keys()
45+
values = categories.values()
46+
assert len(keys) == 3
47+
assert len(values) == 3
48+
assert 'obs_general' in keys
49+
50+

0 commit comments

Comments
 (0)