Skip to content

Commit eec20ef

Browse files
committed
Validate against invalid fields when initializing the dataset
1 parent b9352c1 commit eec20ef

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

ravenpackapi/models/dataset.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def __init__(self, api=None, name=None,
3838
if 'id' in kwargs:
3939
if uuid:
4040
raise Exception("Please provide or the id or the uuid, not both: they are aliases")
41-
uuid = kwargs['id']
41+
uuid = kwargs.pop('id')
4242

4343
self.api = api
4444
self.product_version = product_version
@@ -58,8 +58,11 @@ def __init__(self, api=None, name=None,
5858

5959
self.tags = tags
6060

61-
self.creation_time = kwargs.get('creation_time')
62-
self.last_modified = kwargs.get('last_modified')
61+
self.creation_time = kwargs.pop('creation_time', None)
62+
self.last_modified = kwargs.pop('last_modified', None)
63+
64+
if kwargs:
65+
raise ValueError("Invalid fields for the dataset: %s" % ", ".join(kwargs.keys()))
6366

6467
# the dataset can be initialized with just the uuid and the name
6568
# in that case we'll ask the server for the missing info

ravenpackapi/tests/test_dataset_crud.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,21 @@ def test_create_and_delete(self):
4747

4848
owned_dataset = self.api.list_datasets()
4949
assert new_dataset.id not in owned_dataset, "The new dataset should be deleted"
50+
51+
52+
class TestDatasetCreation(object):
53+
54+
def test_valid_additional_fields(self):
55+
dt = '2020-01-01'
56+
d = Dataset(id='us30', creation_time=dt, last_modified=dt)
57+
assert d.id == 'us30'
58+
assert d.creation_time == d.last_modified == dt
59+
60+
def test_valid_uuid(self):
61+
d = Dataset(uuid='us30')
62+
assert d.id == 'us30'
63+
64+
def test_invalid_additional_fields(self):
65+
dt = '2020-01-01'
66+
with pytest.raises(ValueError):
67+
Dataset(id='us30', creation_time=dt, last_modified=dt, invalid_field=1)

0 commit comments

Comments
 (0)