def test_get_rows_dict(self): """ get_rows_dict returns correct rows when no arguments are given. """ self.init_itertable( self.pagetable_mock, columnDesc=["c1", "c2"], page=1, pageSize=2, start=5, numRows=10, numPageRows=2) self.pagetable_mock.getRow(5).AndReturn( [CORBA.Any(CORBA.TC_string, 'test value 1.1'), CORBA.Any(CORBA.TC_string, 'test value 1.2')]) self.pagetable_mock.getRowId(5).AndReturn(5) self.pagetable_mock.getRow(6).AndReturn( [CORBA.Any(CORBA.TC_string, 'test value 2.1'), CORBA.Any(CORBA.TC_string, 'test value 2.2')]) self.pagetable_mock.getRowId(6).AndReturn(6) self.corba_mock.ReplayAll() table = IterTable( "test_req_object", test_corba_session_string, pagesize=2) rows = table.get_rows_dict() assert len(rows) == 2 assert len(rows[0]) == 3 assert rows[0].get(u'Id') == u'5' assert rows[0].get(u'c1') == u'test value 1.1' assert rows[0].get(u'c2') == u'test value 1.2' assert len(rows[1]) == 3 assert rows[1].get(u'Id') == u'6' assert rows[1].get(u'c1') == u'test value 2.1' assert rows[1].get(u'c2') == u'test value 2.2' self.corba_mock.VerifyAll()
def test_get_rows_dict(self): """ get_rows_dict returns correct rows when no arguments are given. """ self.init_itertable(columnDesc=["c1", "c2"], page=1, pageSize=2, start=5, numRows=10, numPageRows=2) self.pagetable_mock.getRow.side_effect = lambda row_num: \ {5: [CORBA.Any(CORBA.TC_string, 'test value 1.1'), CORBA.Any(CORBA.TC_string, 'test value 1.2')], 6: [CORBA.Any(CORBA.TC_string, 'test value 2.1'), CORBA.Any(CORBA.TC_string, 'test value 2.2')], }[row_num] self.pagetable_mock.getRowId.side_effect = lambda row_num: row_num table = IterTable('test_req_object', test_corba_session_string, pagesize=2) rows = table.get_rows_dict() assert_equal(len(rows), 2) assert_equal(len(rows[0]), 3) assert_equal(rows[0].get(u'Id'), u'5') assert_equal(rows[0].get(u'c1'), u'test value 1.1') assert_equal(rows[0].get(u'c2'), u'test value 1.2') assert_equal(len(rows[1]), 3) assert_equal(rows[1].get(u'Id'), u'6') assert_equal(rows[1].get(u'c1'), u'test value 2.1') assert_equal(rows[1].get(u'c2'), u'test value 2.2')
def test_get_rows_dict_multiple_rows(self): """ get_rows_dict returns multiple rows correctly. """ self.init_itertable(self.pagetable_mock, columnDesc=["c1", "c2"], start=0, numPageRows=1, numRows=20, pageSize=10) self.pagetable_mock._set_pageSize(11) self.itertable_update(self.pagetable_mock) for i in range(5, 16): self.pagetable_mock.getRow(i).AndReturn([ CORBA.Any(CORBA.TC_string, 'test value %i.1' % i), CORBA.Any(CORBA.TC_string, 'test value %i.2' % i) ]) self.pagetable_mock.getRowId(i).AndReturn(i) self.corba_mock.ReplayAll() table = IterTable("test_req_object", test_corba_session_string, pagesize=10) rows = table.get_rows_dict(start=5, limit=11) assert len(rows) == 11 assert len(rows[6]) == 3 assert rows[6].get(u'Id') == u'11' assert rows[6].get(u'c1') == u'test value 11.1' assert rows[6].get(u'c2') == u'test value 11.2'
def test_get_row_id_negative_index(self): """ get_row_id raises IndexError when index negative. """ self.init_itertable(self.pagetable_mock, columnDesc=["c1", "c2"], start=0, numPageRows=1, pageSize=50) self.pagetable_mock.getRowId(-1).AndRaise(Registry.Table.INVALID_ROW()) self.corba_mock.ReplayAll() table = IterTable("test_req_object", test_corba_session_string, pagesize=50) id = table.get_row_id(index=-1) # negative index
def test_get_row_id_index_out_of_bounds(self): """ get_row_id raises IndexError when index is too big. """ self.init_itertable() self.pagetable_mock.getRowId.side_effect = Registry.Table.INVALID_ROW table = IterTable('test_req_object', test_corba_session_string, pagesize=50) table.get_row_id(index=10000) # index out of bounds
def test_get_row_id_index_out_of_bounds(self): """ get_row_id raises IndexError when index is too big. """ self.init_itertable(self.pagetable_mock, columnDesc=["c1", "c2"], start=0, numPageRows=1, pageSize=50) self.pagetable_mock.getRowId(10000).AndRaise(Registry.Table.INVALID_ROW()) self.corba_mock.ReplayAll() table = IterTable("test_req_object", test_corba_session_string, pagesize=50) id = table.get_row_id(index=10000) # index out of bounds
def test__get_row_out_of_index(self): """ _get_row raises IndexError when index is out of range. """ self.init_itertable(numPageRows=1, pageSize=50) self.pagetable_mock.getRow.side_effect = Registry.Table.INVALID_ROW table = IterTable('test_req_object', test_corba_session_string, pagesize=50) table._get_row(2)
def test_get_rows_dict_multiple_rows(self): """ get_row_dict returns multiple rows correctly. """ self.init_itertable(self.pagetable_mock, columnDesc=["c1", "c2"], start=0, numPageRows=1, numRows=20, pageSize=10) self.pagetable_mock._set_pageSize(100) self.itertable_update(self.pagetable_mock) self.pagetable_mock.getRow(21).AndRaise(Registry.Table.INVALID_ROW) self.corba_mock.ReplayAll() table = IterTable( "test_req_object", test_corba_session_string, pagesize=10) rows = table.get_rows_dict(start=21, limit=100)
def test__get_row_invalid_argument(self): """ _get_row fails when index is not an integer. """ self.init_itertable(numPageRows=1, pageSize=50) # simulate that getRow was called with bad argument like string instead int: self.pagetable_mock.getRow.side_effect = \ CORBA.BAD_PARAM(omniORB.BAD_PARAM_WrongPythonType, CORBA.COMPLETED_NO) table = IterTable("test_req_object", test_corba_session_string, pagesize=50) table._get_row("intentional error - this should be an integer")
def test__get_row_out_of_index(self): """ _get_row raises IndexError when index is out of range. """ self.init_itertable(self.pagetable_mock, numPageRows=1, pageSize=50) self.pagetable_mock.getRow(-1).AndRaise( CORBA.BAD_PARAM(omniORB.BAD_PARAM_PythonValueOutOfRange, CORBA.COMPLETED_NO)) self.pagetable_mock.getRowId(-2).AndReturn(1) self.corba_mock.ReplayAll() table = IterTable("test_req_object", test_corba_session_string, pagesize=50) table._get_row(-1) self.corba_mock.VerifyAll()
def test_get_row_id(self): """ get_row_id returns correct id when index is OK. """ self.init_itertable(self.pagetable_mock, columnDesc=["c1", "c2"], start=0, numPageRows=1, numRows=20, pageSize=5) self.pagetable_mock.getRowId(1).AndReturn(1) self.corba_mock.ReplayAll() table = IterTable( "test_req_object", test_corba_session_string, pagesize=5) id = table.get_row_id(1) assert id == 1 self.corba_mock.VerifyAll()
def test_get_rows_dict_multiple_rows_index_error(self): """ get_row_dict raises IndexError when wrong index used. """ self.init_itertable(columnDesc=["c1", "c2"], start=0, numPageRows=1, numRows=20, pageSize=10) self.itertable_update() self.pagetable_mock.getRow.side_effect = Registry.Table.INVALID_ROW table = IterTable('test_req_object', test_corba_session_string, pagesize=10) table.get_rows_dict(start=21, limit=100)
def test_get_row_id(self): """ get_row_id returns correct id when index is OK. """ self.init_itertable(columnDesc=["c1", "c2"], start=0, numPageRows=1, numRows=20, pageSize=5) self.pagetable_mock.getRowId.return_value = 1 table = IterTable('test_req_object', test_corba_session_string, pagesize=5) row_id = table.get_row_id(1) assert_equal(row_id, 1)
def test__get_row_invalid_argument(self): """ _get_row raises ValueError when index is not an integer. """ self.init_itertable(self.pagetable_mock, numPageRows=1, pageSize=50) self.pagetable_mock.getRow( "intentional error - this should be an integer").AndRaise( CORBA.BAD_PARAM( omniORB.BAD_PARAM_WrongPythonType,CORBA.COMPLETED_NO)) self.pagetable_mock.getRowId(2).AndReturn(1) self.corba_mock.ReplayAll() table = IterTable( "test_req_object", test_corba_session_string, pagesize=50) table._get_row("intentional error - this should be an integer") self.corba_mock.VerifyAll()
def test_get_row_id_index_out_of_bounds(self): """ get_row_id raises IndexError when index is too big. """ self.init_itertable(self.pagetable_mock, columnDesc=["c1", "c2"], start=0, numPageRows=1, pageSize=50) self.pagetable_mock.getRowId(10000).AndRaise( Registry.Table.INVALID_ROW()) self.corba_mock.ReplayAll() table = IterTable("test_req_object", test_corba_session_string, pagesize=50) id = table.get_row_id(index=10000) # index out of bounds
def test_next(self): """ IterTable works as an iterator (`for row in itertable` expression works). """ self.init_itertable(self.pagetable_mock, columnDesc=["c1", "c2"], start=0, numPageRows=10, numRows=20, pageSize=5) for i in range(0, 10): self.pagetable_mock.getRow(i).AndReturn([ CORBA.Any(CORBA.TC_string, 'test value %i.1' % i), CORBA.Any(CORBA.TC_string, 'test value %i.2' % i) ]) self.pagetable_mock.getRowId(i).AndReturn(i) self.corba_mock.ReplayAll() table = IterTable("test_req_object", test_corba_session_string, pagesize=5) for i, row in enumerate(table): assert row is not None assert len(row) == 3 if i == 0: assert row[0].get(u'value') == "" else: assert row[0].get(u'value') == str(i) self.corba_mock.VerifyAll()
def test__get_row_invalid_argument(self): """ _get_row raises ValueError when index is not an integer. """ self.init_itertable(self.pagetable_mock, numPageRows=1, pageSize=50) self.pagetable_mock.getRow( "intentional error - this should be an integer").AndRaise( CORBA.BAD_PARAM(omniORB.BAD_PARAM_WrongPythonType, CORBA.COMPLETED_NO)) self.pagetable_mock.getRowId(2).AndReturn(1) self.corba_mock.ReplayAll() table = IterTable("test_req_object", test_corba_session_string, pagesize=50) table._get_row("intentional error - this should be an integer") self.corba_mock.VerifyAll()
def test_get_rows_dict_multiple_rows(self): """ get_row_dict returns multiple rows correctly. """ self.init_itertable(self.pagetable_mock, columnDesc=["c1", "c2"], start=0, numPageRows=1, numRows=20, pageSize=10) self.pagetable_mock._set_pageSize(100) self.itertable_update(self.pagetable_mock) self.pagetable_mock.getRow(21).AndRaise(Registry.Table.INVALID_ROW) self.corba_mock.ReplayAll() table = IterTable("test_req_object", test_corba_session_string, pagesize=10) rows = table.get_rows_dict(start=21, limit=100)
def test_init_unknown_request_object(self): """ IterTable throws KeyError when request_object is not known. """ self.init_itertable(self.pagetable_mock) self.corba_mock.ReplayAll() table = IterTable("test_invalid_req_object", test_corba_session_string, pagesize=50) self.corba_mock.VerifyAll()
def test_get_row_id(self): """ get_row_id returns correct id when index is OK. """ self.init_itertable(self.pagetable_mock, columnDesc=["c1", "c2"], start=0, numPageRows=1, numRows=20, pageSize=5) self.pagetable_mock.getRowId(1).AndReturn(1) self.corba_mock.ReplayAll() table = IterTable("test_req_object", test_corba_session_string, pagesize=5) id = table.get_row_id(1) assert id == 1 self.corba_mock.VerifyAll()
def test_init(self): """ IterTable initializes correctly. """ self.init_itertable(self.pagetable_mock, pageSize=50) self.corba_mock.ReplayAll() table = IterTable("test_req_object", test_corba_session_string, pagesize=50) assert table is not None self.corba_mock.VerifyAll()
def test__get_row(self): """_get_row returns row correctly. """ self.init_itertable(self.pagetable_mock, pageSize=50) self.pagetable_mock.getRow(2).AndReturn([CORBA.Any(CORBA.TC_string, 'test value')]) self.pagetable_mock.getRowId(2).AndReturn(12) self.corba_mock.ReplayAll() table = IterTable("test_req_object", test_corba_session_string, pagesize=50) row = table._get_row(2) assert len(row) == 2 assert row[0]['url'] == "www.test.foo/baz/detail/?id=12" assert row[0]['index'] == 0 assert row[0]['icon'] is not None assert row[1]['index'] == 1 assert row[1]['value'] == 'test value' self.corba_mock.VerifyAll()
def test_get_rows_dict_multiple_rows(self): """ get_rows_dict returns multiple rows correctly. """ self.init_itertable(self.pagetable_mock, columnDesc=["c1", "c2"], start=0, numPageRows=1, numRows=20, pageSize=10) self.pagetable_mock._set_pageSize(11) self.itertable_update(self.pagetable_mock) for i in range(5, 16): self.pagetable_mock.getRow(i).AndReturn( [CORBA.Any(CORBA.TC_string, 'test value %i.1' % i), CORBA.Any(CORBA.TC_string, 'test value %i.2' % i)]) self.pagetable_mock.getRowId(i).AndReturn(i) self.corba_mock.ReplayAll() table = IterTable( "test_req_object", test_corba_session_string, pagesize=10) rows = table.get_rows_dict(start=5, limit=11) assert len(rows) == 11 assert len(rows[6]) == 3 assert rows[6].get(u'Id') == u'11' assert rows[6].get(u'c1') == u'test value 11.1' assert rows[6].get(u'c2') == u'test value 11.2'
def test_get_rows_dict(self): """ get_rows_dict returns correct rows when no arguments are given. """ self.init_itertable(self.pagetable_mock, columnDesc=["c1", "c2"], page=1, pageSize=2, start=5, numRows=10, numPageRows=2) self.pagetable_mock.getRow(5).AndReturn([ CORBA.Any(CORBA.TC_string, 'test value 1.1'), CORBA.Any(CORBA.TC_string, 'test value 1.2') ]) self.pagetable_mock.getRowId(5).AndReturn(5) self.pagetable_mock.getRow(6).AndReturn([ CORBA.Any(CORBA.TC_string, 'test value 2.1'), CORBA.Any(CORBA.TC_string, 'test value 2.2') ]) self.pagetable_mock.getRowId(6).AndReturn(6) self.corba_mock.ReplayAll() table = IterTable("test_req_object", test_corba_session_string, pagesize=2) rows = table.get_rows_dict() assert len(rows) == 2 assert len(rows[0]) == 3 assert rows[0].get(u'Id') == u'5' assert rows[0].get(u'c1') == u'test value 1.1' assert rows[0].get(u'c2') == u'test value 1.2' assert len(rows[1]) == 3 assert rows[1].get(u'Id') == u'6' assert rows[1].get(u'c1') == u'test value 2.1' assert rows[1].get(u'c2') == u'test value 2.2' self.corba_mock.VerifyAll()
def test_get_rows_dict_multiple_rows(self): """ get_rows_dict returns multiple rows correctly. """ self.init_itertable(columnDesc=["c1", "c2"], start=0, numPageRows=1, numRows=20, pageSize=10) self.pagetable_mock.getRow.side_effect = lambda row_num: \ [CORBA.Any(CORBA.TC_string, 'test value %i.1' % row_num), CORBA.Any(CORBA.TC_string, 'test value %i.2' % row_num)] self.pagetable_mock.getRowId.side_effect = lambda row_num: row_num table = IterTable('test_req_object', test_corba_session_string, pagesize=10) rows = table.get_rows_dict(start=5, limit=11) assert_equal(len(rows), 11) assert_equal(len(rows[6]), 3) assert_equal(rows[6].get(u'Id'), u'11') assert_equal(rows[6].get(u'c1'), u'test value 11.1') assert_equal(rows[6].get(u'c2'), u'test value 11.2')
def _get_itertable(self, request_object = None): if not request_object: request_object = self.classname key = cherrypy.session.get('corbaSessionString', '') size = config.tablesize timeout = config.tabletimeout user = cherrypy.session.get('user') if user and user.table_page_size: size = cherrypy.session.get('user').table_page_size itertable = IterTable(request_object, key, size, timeout) return itertable
def test__get_row(self): """_get_row returns row correctly. """ self.init_itertable(self.pagetable_mock, pageSize=50) self.pagetable_mock.getRow(2).AndReturn( [CORBA.Any(CORBA.TC_string, 'test value')]) self.pagetable_mock.getRowId(2).AndReturn(12) self.corba_mock.ReplayAll() table = IterTable("test_req_object", test_corba_session_string, pagesize=50) row = table._get_row(2) assert len(row) == 2 assert row[0]['url'] == "www.test.foo/baz/detail/?id=12" assert row[0]['index'] == 0 assert row[0]['icon'] is not None assert row[1]['index'] == 1 assert row[1]['value'] == 'test value' self.corba_mock.VerifyAll()
def test__get_row(self): """_get_row returns row correctly. """ self.init_itertable(pageSize=50) self.pagetable_mock.getRow.side_effect = lambda row_num: \ [CORBA.Any(CORBA.TC_string, 'test value')] if row_num == 2 else None self.pagetable_mock.getRowId.side_effect = lambda row_num: \ 12 if row_num == 2 else None table = IterTable('test_req_object', test_corba_session_string, pagesize=50) row = table._get_row(2) assert_equal(len(row), 2) assert_equal(row[0]['url'], "www.test.foo/baz/detail/?id=12") assert_equal(row[0]['index'], 0) assert_is_not_none(row[0]['icon']) assert_equal(row[1]['index'], 1) assert_equal(row[1]['value'], 'test value') expected_calls = [call.getRow(2), call.getRowId(2)] self.pagetable_mock.assert_has_calls(expected_calls, any_order=True)
def test_filter_loader(self): 'Tests filter loader - create, save to severver and load back a filter' from fred_webadmin.itertable import IterTable, FilterLoader print 'printing' input_filter_data = [{u'object': [False, {u'handle': [False, u'test.cz']}]}, {u'registrar': [False, {u'handle': [True, u'REG-FRED_A']}]}] itable = IterTable('action', self.corbaSessionString) print "SET FILTERS:" FilterLoader.set_filter(itable, input_filter_data) print "GET FILTERS DATA:" output_filter_data = FilterLoader.get_filter_data(itable) # dom = self.admin.getDomainById(38442) assert input_filter_data == output_filter_data, 'This should be same:\n' + str(input_filter_data) + '\n' + str(output_filter_data)
def get_form(self, filter): key = cherrypy.session.get('corbaSessionString', '') itertable = IterTable(f_id_name[int(filter['Type'])], key) itertable.load_filter(int(filter['Id'])) if not itertable.all_fields_filled(): filter_data = itertable.get_filter_data() form_class = self._get_form_class(f_id_name[int(filter['Type'])]) form = UnionFilterForm(filter_data, data_cleaned=True, form_class=form_class) else: form = '' return form
def _get_itertable(self, request_object=None): if not request_object: request_object = self.classname key = cherrypy.session.get('corbaSessionString', '') page_size = config.table_page_size timeout = config.table_timeout user = cherrypy.session.get('user') if user and user.table_page_size: page_size = user.table_page_size max_row_limit = config.table_max_row_limit_per_obj.get( request_object, config.table_max_row_limit) itertable = IterTable(request_object, key, page_size, timeout, max_row_limit) return itertable
def test_next(self): """ IterTable works as an iterator (`for row in itertable` expression works). """ self.init_itertable(columnDesc=["c1", "c2"], start=0, numPageRows=10, numRows=20, pageSize=5) self.pagetable_mock.getRow.side_effect = lambda row_num: \ [CORBA.Any(CORBA.TC_string, 'test value %i.1' % row_num), CORBA.Any(CORBA.TC_string, 'test value %i.2' % row_num)] self.pagetable_mock.getRowId.side_effect = lambda row_num: row_num table = IterTable('test_req_object', test_corba_session_string, pagesize=5) for i, row in enumerate(table): assert_is_not_none(row) assert_equal(len(row), 3) if i == 0: assert_equal(row[0].get(u'value'), "") else: assert_equal(row[0].get(u'value'), str(i))
def test_init(self): """ IterTable initializes correctly. """ self.init_itertable(pageSize=50) table = IterTable("test_req_object", test_corba_session_string, pagesize=50) assert_is_not_none(table) expected_calls = [ call._set_pageSize(50), call.setTimeout(10000), call.setLimit(1000), call.getColumnHeaders(), call._get_page(), call._get_pageSize(), call._get_start(), call._get_numRows(), call.numRowsOverLimit(), call._get_numPages(), call._get_numPageRows() ] self.pagetable_mock.assert_has_calls(expected_calls, any_order=True)
def test_init_unknown_request_object(self): """ IterTable throws KeyError when request_object is not known. """ self.init_itertable() IterTable("test_invalid_req_object", test_corba_session_string, pagesize=50)