Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add __repr__ method to Field classes #47

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions robobrowser/forms/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,14 @@ def __init__(self, parsed):
super(Input, self).__init__(parsed)
self.value = self._parsed.get('value')

def __repr__(self):
return '<Input name={0}, value={1}>'.format(self.name, self.value)


class Submit(Input):
pass

def __repr__(self):
return '<Submit name={0}, value={1}>'.format(self.name, self.value)


class FileInput(BaseField):
Expand All @@ -72,6 +77,9 @@ def value(self, value):
# in requests.
payload_key = 'files'

def __repr__(self):
return '<FileInput name={0}, value={1}>'.format(self.name, self.value)


class MultiOptionField(BaseField):

Expand Down Expand Up @@ -195,13 +203,21 @@ def __init__(self, parsed):
super(Textarea, self).__init__(parsed)
self.value = self._parsed.text.rstrip('\r').rstrip('\n')

def __repr__(self):
return '<Textarea name={0}, value={1}>'.format(self.name, self.value)


class Checkbox(FlatOptionField, MultiValueField):
pass

def __repr__(self):
values = ', '.join(value for value in self.value)
return '<Checkbox name={0}, value=[{1}]>'.format(self.name, values)


class Radio(FlatOptionField, MultiOptionField):
pass

def __repr__(self):
return '<Radio name={0}, value={1}>'.format(self.name, self.value)


class Select(NestedOptionField, MultiOptionField):
Expand All @@ -213,6 +229,12 @@ def _set_initial(self, initial):
if not self._value and self.options:
self.value = self.options[0]

def __repr__(self):
return '<Select name={0}, value={1}>'.format(self.name, self.value)


class MultiSelect(NestedOptionField, MultiValueField):
pass

def __repr__(self):
values = ', '.join(value for value in self.value)
return '<MultiSelect name={0}, value=[{1}]>'.format(self.name, values)
145 changes: 145 additions & 0 deletions tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,19 @@ def test_serialize_multi(self):
assert_false('submit2' in serialized.data)


class TestSubmit(unittest.TestCase):

def setUp(self):
self.submit = fields.Submit(
'<input type="submit" name="submit1" value="value1" />')

def test_repr(self):
assert_equal(
repr(self.submit),
'<Submit name=submit1, value=value1>'
)


class TestParser(unittest.TestCase):

def setUp(self):
Expand Down Expand Up @@ -282,6 +295,12 @@ def test_invalid_name(self):
html = '<input />'
assert_raises(exceptions.InvalidNameError, lambda: fields.Input(html))

def test_repr(self):
assert_equal(
repr(self.input),
'<Input name=brian, value=may>'
)


class TestInputBlank(unittest.TestCase):

Expand All @@ -299,6 +318,12 @@ def test_serialize(self):
{'blank': ''}
)

def test_repr(self):
assert_equal(
repr(self.input),
'<Input name=blank, value=>'
)


class TestTextarea(unittest.TestCase):

Expand All @@ -324,6 +349,12 @@ def test_serialize(self):
{'roger': 'taylor'}
)

def test_repr(self):
assert_equal(
repr(self.input),
'<Textarea name=roger, value=taylor>'
)


class TestTextareaBlank(unittest.TestCase):

Expand All @@ -341,6 +372,12 @@ def test_serialize(self):
{'blank': ''}
)

def test_repr(self):
assert_equal(
repr(self.input),
'<Textarea name=blank, value=>'
)


class TestSelect(unittest.TestCase):

Expand Down Expand Up @@ -383,6 +420,20 @@ def test_serialize(self):
{'john': "you're"}
)

def test_repr(self):
# Test default for given html.
assert_equal(
repr(self.input),
"<Select name=john, value=you're>"
)

# Test a different value.
self.input.value = 'tie'
assert_equal(
repr(self.input),
'<Select name=john, value=tie>'
)


class TestSelectBlank(unittest.TestCase):

Expand All @@ -409,6 +460,12 @@ def test_serialize(self):
{'john': 'tie'}
)

def test_repr(self):
assert_equal(
repr(self.input),
'<Select name=john, value=tie>'
)


class TestMultiSelect(unittest.TestCase):

Expand All @@ -422,6 +479,39 @@ def setUp(self):
'''
self.input = fields.MultiSelect(BeautifulSoup(self.html).find('select'))

def test_repr(self):
# Test default for given html.
assert_equal(
repr(self.input),
"<MultiSelect name=john, value=[you're]>"
)

# Test after selecting different options.
self.input.value = ['tie', "you're", 'the']
assert_equal(
repr(self.input),
"<MultiSelect name=john, value=[tie, you're, the]>"
)


class TestMultiSelectBlank(unittest.TestCase):

def setUp(self):
self.html = '''
<select name="john" multiple>
<option value="tie">your mother down</option>
<option value="you're">my best friend</option>
<option value="the">millionaire waltz</option>
</select>
'''
self.input = fields.MultiSelect(BeautifulSoup(self.html).find('select'))

def test_repr(self):
assert_equal(
repr(self.input),
"<MultiSelect name=john, value=[]>"
)


class TestMixedCase(unittest.TestCase):

Expand Down Expand Up @@ -491,6 +581,20 @@ def test_serialize(self):
{'members': 'mercury'}
)

def test_repr(self):
# Test default for given html.
assert_equal(
repr(self.input),
'<Radio name=members, value=mercury>'
)

# Test after selecting a different option.
self.input.value = 'may'
assert_equal(
repr(self.input),
'<Radio name=members, value=may>'
)


class TestRadioBlank(unittest.TestCase):

Expand All @@ -512,6 +616,12 @@ def test_serialize(self):
{'member': ''}
)

def test_repr(self):
assert_equal(
repr(self.input),
'<Radio name=member, value=>'
)


class TestCheckbox(unittest.TestCase):

Expand Down Expand Up @@ -560,6 +670,20 @@ def test_serialize(self):
{'member': ['mercury', 'deacon']}
)

def test_repr(self):
# Test default for given html.
assert_equal(
repr(self.input),
"<Checkbox name=member, value=[mercury, deacon]>"
)

# Test after selecting different inputs.
self.input.value = ['mercury']
assert_equal(
repr(self.input),
"<Checkbox name=member, value=[mercury]>"
)


class TestCheckboxBlank(unittest.TestCase):

Expand All @@ -583,6 +707,12 @@ def test_serialize(self):
{'member': []}
)

def test_repr(self):
assert_equal(
repr(self.input),
'<Checkbox name=member, value=[]>'
)


class TestFileInput(unittest.TestCase):

Expand Down Expand Up @@ -615,6 +745,21 @@ def test_serialize(self):
{'song': file}
)

def test_repr(self):
# Test with no file.
assert_equal(
repr(self.input),
'<FileInput name=song, value=>'
)

# Test with given temporary file.
temp_file = tempfile.TemporaryFile('r')
self.input.value = temp_file
assert_equal(
repr(self.input),
"<FileInput name=song, value={0}>".format(temp_file)
)


class TestDisabledValues(unittest.TestCase):

Expand Down