def test_sort(self): rs = ResultSet([[12000, 'adim'], [13000, 'syt'], [14000, 'nico']], 'Any U,L where U is CWUser, U login L', description=[['CWUser', 'String']] * 3) with self.admin_access.web_request() as req: rs.req = req rs.vreg = self.vreg rs2 = rs.sorted_rset(lambda e: e.cw_attr_cache['login']) self.assertEqual(len(rs2), 3) self.assertEqual([login for _, login in rs2], ['adim', 'nico', 'syt']) # make sure rs is unchanged self.assertEqual([login for _, login in rs], ['adim', 'syt', 'nico']) rs2 = rs.sorted_rset(lambda e: e.cw_attr_cache['login'], reverse=True) self.assertEqual(len(rs2), 3) self.assertEqual([login for _, login in rs2], ['syt', 'nico', 'adim']) # make sure rs is unchanged self.assertEqual([login for _, login in rs], ['adim', 'syt', 'nico']) rs3 = rs.sorted_rset(lambda row: row[1], col=-1) self.assertEqual(len(rs3), 3) self.assertEqual([login for _, login in rs3], ['adim', 'nico', 'syt']) # make sure rs is unchanged self.assertEqual([login for _, login in rs], ['adim', 'syt', 'nico'])
def test_limit(self): rs = ResultSet([[12000, 'adim'], [13000, 'syt'], [14000, 'nico']], 'Any U,L where U is CWUser, U login L', description=[['CWUser', 'String']] * 3) with self.admin_access.web_request() as req: rs.req = req rs.vreg = self.vreg self.assertEqual(rs.limit(2).rows, [[12000, 'adim'], [13000, 'syt']]) rs2 = rs.limit(2, offset=1) self.assertEqual(rs2.rows, [[13000, 'syt'], [14000, 'nico']]) self.assertEqual(rs2.get_entity(0, 0).cw_row, 0) self.assertEqual(rs.limit(2, offset=2).rows, [[14000, 'nico']]) self.assertEqual(rs.limit(2, offset=3).rows, [])
def test_filter(self): rs = ResultSet([[12000, 'adim'], [13000, 'syt'], [14000, 'nico']], 'Any U,L where U is CWUser, U login L', description=[['CWUser', 'String']] * 3) with self.admin_access.web_request() as req: rs.req = req rs.vreg = self.vreg def test_filter(entity): return entity.login != 'nico' rs2 = rs.filtered_rset(test_filter) self.assertEqual(len(rs2), 2) self.assertEqual([login for _, login in rs2], ['adim', 'syt']) self.assertEqual(rs2.description, rs.description[1:])
def test_split(self): rs = ResultSet([[12000, 'adim', u'Adim chez les pinguins'], [12000, 'adim', u'Jardiner facile'], [13000, 'syt', u'Le carrelage en 42 leçons'], [14000, 'nico', u'La tarte tatin en 15 minutes'], [14000, 'nico', u"L'épluchage du castor commun"]], ('Any U, L, T WHERE U is CWUser, U login L,' 'D created_by U, D title T'), description=[['CWUser', 'String', 'String']] * 5) with self.admin_access.web_request() as req: rs.req = req rs.vreg = self.vreg rsets = rs.split_rset(lambda e: e.cw_attr_cache['login']) self.assertEqual(len(rsets), 3) self.assertEqual([login for _, login, _ in rsets[0]], ['adim', 'adim']) self.assertEqual([login for _, login, _ in rsets[1]], ['syt']) self.assertEqual([login for _, login, _ in rsets[2]], ['nico', 'nico']) # make sure rs is unchanged self.assertEqual([login for _, login, _ in rs], ['adim', 'adim', 'syt', 'nico', 'nico']) rsets = rs.split_rset(lambda e: e.cw_attr_cache['login'], return_dict=True) self.assertEqual(len(rsets), 3) self.assertEqual([login for _, login, _ in rsets['nico']], ['nico', 'nico']) self.assertEqual([login for _, login, _ in rsets['adim']], ['adim', 'adim']) self.assertEqual([login for _, login, _ in rsets['syt']], ['syt']) # make sure rs is unchanged self.assertEqual([login for _, login, _ in rs], ['adim', 'adim', 'syt', 'nico', 'nico']) rsets = rs.split_rset(lambda s: s.count('d'), col=2) self.assertEqual(len(rsets), 2) self.assertEqual([title for _, _, title in rsets[0]], [u"Adim chez les pinguins", u"Jardiner facile", u"L'épluchage du castor commun"]) self.assertEqual([title for _, _, title in rsets[1]], [u"Le carrelage en 42 leçons", u"La tarte tatin en 15 minutes"]) # make sure rs is unchanged self.assertEqual([title for _, _, title in rs], [u'Adim chez les pinguins', u'Jardiner facile', u'Le carrelage en 42 leçons', u'La tarte tatin en 15 minutes', u"L'épluchage du castor commun"])