Skip to content

Commit

Permalink
Merge eb11f1f into e41bd91
Browse files Browse the repository at this point in the history
  • Loading branch information
izapolsk committed Mar 6, 2019
2 parents e41bd91 + eb11f1f commit a1ed9bf
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 29 deletions.
26 changes: 19 additions & 7 deletions src/widgetastic/widget/table.py
Expand Up @@ -26,9 +26,10 @@

class TableColumn(Widget, ClickableMixin):
"""Represents a cell in the row."""
def __init__(self, parent, position, logger=None):
def __init__(self, parent, position, absolute_position=None, logger=None):
Widget.__init__(self, parent, logger=logger)
self.position = position
self.position = position # relative position
self.absolute_position = absolute_position # absolute position according to row/colspan

def __locator__(self):
return self.browser.element('./td[{}]'.format(self.position + 1), parent=self.parent)
Expand All @@ -40,19 +41,29 @@ def __repr__(self):
def column_name(self):
"""If there is a name associated with this column, return it. Otherwise returns None."""
try:
return self.row.position_to_column_name(self.position)
if self.absolute_position and self.absolute_position != self.position:
position = self.absolute_position
else:
position = self.position
return self.row.position_to_column_name(position)
except KeyError:
return None

@cached_property
@property
def widget(self):
"""Returns the associated widget if defined. If there is none defined, returns None."""
args = ()
kwargs = {}

if self.absolute_position and self.absolute_position != self.position:
position = self.absolute_position
else:
position = self.position

if self.column_name is None:
if self.position not in self.table.column_widgets:
if position not in self.table.column_widgets:
return None
wcls = self.table.column_widgets[self.position]
wcls = self.table.column_widgets[position]
else:
if self.column_name not in self.table.column_widgets:
return None
Expand Down Expand Up @@ -903,7 +914,8 @@ def _process_table(self):
queue.append(cur_node)
elif cur_tag == 'td':
cur_position = self._get_position_respecting_spans(node)
cur_obj = TableColumn(parent=node.obj, position=cur_position)
cur_obj = TableColumn(parent=node.obj, position=cur_position,
absolute_position=cur_position)
Node(name=cur_tag, parent=node, obj=cur_obj, position=cur_position)

rowsteps = range(1, int(child.get_attribute('rowspan') or 0))
Expand Down
87 changes: 66 additions & 21 deletions testing/test_basic_widgets.py
Expand Up @@ -134,11 +134,14 @@ def test_table(browser):
class TestForm(View):
table = Table('#with-thead')
table1 = Table('#rowcolspan_table',
column_widgets={'Last Name': TextInput(locator='./input')})
column_widgets={'First Name': TextInput(locator='./input'),
'Last Name': TextInput(locator='./input'),
'Widget': TextInput(locator='./input'),
})

view = TestForm(browser)
assert view.table.headers == (None, 'Column 1', 'Column 2', 'Column 3', 'Column 4')
assert view.table1.headers == ('#', 'First Name', 'Last Name', 'Username')
assert view.table1.headers == ('#', 'First Name', 'Last Name', 'Username', 'Widget')
dir(view.table[0])
assert len(list(view.table.rows())) == 3
assert len(list(view.table1.rows())) == 8
Expand Down Expand Up @@ -265,13 +268,15 @@ class TestForm(View):
assert row.read() == {u'#': u'3',
u'First Name': u'Larry the Bird',
u'Last Name': u'Larry the Bird',
u'Username': u'@slacker'}
u'Username': u'@slacker',
u'Widget': u'widget3'}

unpacking_fake_read = [(header, column.text) for header, column in row]
assert unpacking_fake_read == [(u'#', u'3'),
(u'First Name', u'Larry the Bird'),
(u'Last Name', u'Larry the Bird'),
(u'Username', u'@slacker')]
(u'Username', u'@slacker'),
(u'Widget', u'')]

assert view.table1[1].last_name.text == 'Thornton'

Expand All @@ -290,22 +295,46 @@ class TestForm(View):
row = next(view.table1.rows())
assert row.first_name.text == 'Mark'

assert view.table1.read() == [{u'#': u'1', u'First Name': u'Mark', u'Last Name': u'Otto',
u'Username': u'@mdo'},
{u'#': u'2', u'First Name': u'Jacob', u'Last Name': u'Thornton',
u'Username': u'@fat'},
{u'#': u'3', u'First Name': u'Larry the Bird',
u'Last Name': u'Larry the Bird', u'Username': u'@slacker'},
{u'#': u'4', u'First Name': u'Pete Savage', u'Last Name': u'',
u'Username': u'@psav'},
{u'#': u'4', u'First Name': u'Pete Savage', u'Last Name': u'',
u'Username': u'@psav1'},
{u'#': u'5', u'First Name': u'Mike Shriver',
u'Last Name': u'Mike Shriver', u'Username': u'@mshriver'},
{u'#': u'5', u'First Name': u'Mike Shriver',
u'Last Name': u'Mike Shriver', u'Username': u'@iamhero'},
{u'#': u'6', u'First Name': u'', u'Last Name': u'',
u'Username': u'@blabla'}]
assert view.table1.read() == [{u'#': u'1',
u'First Name': u'Mark',
u'Last Name': u'Otto',
u'Username': u'@mdo',
u'Widget': u'widget1'},
{u'#': u'2',
u'First Name': u'Jacob',
u'Last Name': u'Thornton',
u'Username': u'@fat',
u'Widget': u'widget2'},
{u'#': u'3',
u'First Name': u'Larry the Bird',
u'Last Name': u'Larry the Bird',
u'Username': u'@slacker',
u'Widget': u'widget3'},
{u'#': u'4',
u'First Name': u'Pete Savage',
u'Last Name': u'',
u'Username': u'@psav',
u'Widget': u'widget41'},
{u'#': u'4',
u'First Name': u'Pete Savage',
u'Last Name': u'',
u'Username': u'@psav1',
u'Widget': u'widget42'},
{u'#': u'5',
u'First Name': u'Mike Shriver',
u'Last Name': u'Mike Shriver',
u'Username': u'@mshriver',
u'Widget': u'widget51'},
{u'#': u'5',
u'First Name': u'Mike Shriver',
u'Last Name': u'Mike Shriver',
u'Username': u'@iamhero',
u'Widget': u'widget52'},
{u'#': u'6',
u'First Name': u'',
u'Last Name': u'',
u'Username': u'@blabla',
u'Widget': u'widget6'}]


def test_table_no_header(browser):
Expand Down Expand Up @@ -371,7 +400,9 @@ class TestForm(View):
class TestForm1(View):
table1 = Table('#rowcolspan_table',
column_widgets={'First Name': TextInput(locator='./input'),
'Last Name': TextInput(locator='./input')})
'Last Name': TextInput(locator='./input'),
'Widget': TextInput(locator='./input'),
})

view1 = TestForm1(browser)

Expand Down Expand Up @@ -401,6 +432,20 @@ class TestForm1(View):
assert view1.table1[7]['Last Name'].fill('new value')
assert view1.table1[7]['Last Name'].read() == 'new value'

old_state = view1.table1.read()
ending = ' updated'
for row in view1.table1.rows():
cell = row['Widget']
value = cell.read()
row['Widget'].fill(str(value) + ending)
assert old_state != view1.table1.read()

for row in view1.table1.rows():
cell = row['Widget']
value = cell.read()
row['Widget'].fill(str(value)[:-len(ending)])
assert old_state == view1.table1.read()

with pytest.raises(TypeError):
# There is nothing to be filled
view.fill({'table': [{0: 'explode'}]})
Expand Down
11 changes: 10 additions & 1 deletion testing/testing_page.html
Expand Up @@ -172,13 +172,14 @@ <h3 id="switchabletesting-2">bartest</h3>
});
</script>

<table class="table table-striped table-bordered" id="rowcolspan_table">
<table class="table table-striped table-bordered" border="1" id="rowcolspan_table">
<thead>
<tr>
<th>#</th>
<th>First Name</th>
<th>Last Name</th>
<th>Username</th>
<th>Widget</th>
</tr>
</thead>
<tbody>
Expand All @@ -187,43 +188,51 @@ <h3 id="switchabletesting-2">bartest</h3>
<td>Mark</td>
<td>Otto</td>
<td>@mdo</td>
<td><input name="widget1" type="text" value="widget1"></td>
</tr>
<tr data-test="abc-345">
<td>2</td>
<td>Jacob</td>
<td>Thornton</td>
<td>@fat</td>
<td><input name="widget2" type="text" value="widget2"></td>
</tr>
<tr data-test="def-345">
<td>3</td>
<td colspan="2">Larry the Bird</td>
<td>@slacker</td>
<td><input name="widget3" type="text" value="widget3"></td>
</tr>
<tr>
<td rowspan="2">4</td>
<td rowspan="2">Pete Savage</td>
<td></td>
<td>@psav</td>
<td><input name="widget41" type="text" value="widget41"></td>
</tr>
<tr>
<td></td>
<td>@psav1</td>
<td><input name="widget42" type="text" value="widget42"></td>
</tr>

<tr>
<td rowspan="2">5</td>
<td rowspan="2" colspan="2">Mike Shriver</td>
<td>@mshriver</td>
<td><input name="widget51" type="text" value="widget51"></td>
</tr>
<tr>
<td>@iamhero</td>
<td><input name="widget52" type="text" value="widget52"></td>
</tr>
<tr>
<td>6</td>
<td colspan="2">
<input name="some_input_obj" type="text">
</td>
<td>@blabla</td>
<td><input name="widget6" type="text" value="widget6"></td>
</tr>
</tbody>
</table>
Expand Down

0 comments on commit a1ed9bf

Please sign in to comment.