From 92822c2fab72cf007727b1988d759d3002f0d974 Mon Sep 17 00:00:00 2001 From: pylover Date: Wed, 14 Aug 2024 02:07:42 +0330 Subject: [PATCH] Fix: raise ValueError when manipulating an empty form --- bddrest/authoring/given.py | 3 +++ tests/test_form_manipulation.py | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/bddrest/authoring/given.py b/bddrest/authoring/given.py index aaae714..27d9c78 100644 --- a/bddrest/authoring/given.py +++ b/bddrest/authoring/given.py @@ -46,6 +46,9 @@ def when(self, *args, record=True, **kwargs): # Checking for dictionary manipulators if any for k, v in kwargs.items(): if isinstance(v, Manipulator): + basevalue = getattr(self.base_call, k) + if basevalue is None: + raise ValueError(f'{k} argument is not given yet') clone = getattr(self.base_call, k).copy() v.apply(clone) kwargs[k] = clone diff --git a/tests/test_form_manipulation.py b/tests/test_form_manipulation.py index 0d7230a..87f911a 100644 --- a/tests/test_form_manipulation.py +++ b/tests/test_form_manipulation.py @@ -44,6 +44,16 @@ def test_append_form_field(): email='user@example.com' ) + call = dict( + title='test add form field', + url='/apiv1/devices/name: SM-12345678/id: 1', + verb='POST', + ) + with Given(wsgi_application, **call): + with pytest.raises(ValueError): + when('Appending a field to an empty form', + form=Append('email', 'foo@bar.baz')) + def test_remove_from_fields(): call = dict( @@ -63,6 +73,10 @@ def test_remove_from_fields(): when('Removing fields', form=Remove('email', 'phone')) assert response.json == dict(activationCode='746727') + with pytest.raises(ValueError): + when('Removing a field from an empty form', + json=Remove('email', 'phone')) + with pytest.raises(ValueError): Remove('a').apply(['b', 'c'])