[repository,tests] use the new connection api (part 1/2)
`add_info` should not play with the cnxset nor the mode.
Static analysis showed that:
* add_info is an internal api called with a non-none cnx.cnxset and
also in write mode
* only the altered test violated this assumption, which is now
somewhat documented
--- a/server/sources/native.py Mon Jun 02 11:47:30 2014 +0200
+++ b/server/sources/native.py Mon Jun 02 12:28:50 2014 +0200
@@ -888,32 +888,32 @@
def add_info(self, cnx, entity, source, extid):
"""add type and source info for an eid into the system table"""
- with cnx.ensure_cnx_set:
- # begin by inserting eid/type/source/extid into the entities table
- if extid is not None:
- assert isinstance(extid, str)
- extid = b64encode(extid)
- attrs = {'type': entity.cw_etype, 'eid': entity.eid, 'extid': extid,
- 'asource': source.uri}
- self._handle_insert_entity_sql(cnx, self.sqlgen.insert('entities', attrs), attrs)
- # insert core relations: is, is_instance_of and cw_source
- try:
- self._handle_is_relation_sql(cnx, 'INSERT INTO is_relation(eid_from,eid_to) VALUES (%s,%s)',
- (entity.eid, eschema_eid(cnx, entity.e_schema)))
- except IndexError:
- # during schema serialization, skip
- pass
- else:
- for eschema in entity.e_schema.ancestors() + [entity.e_schema]:
- self._handle_is_relation_sql(cnx,
- 'INSERT INTO is_instance_of_relation(eid_from,eid_to) VALUES (%s,%s)',
- (entity.eid, eschema_eid(cnx, eschema)))
- if 'CWSource' in self.schema and source.eid is not None: # else, cw < 3.10
- self._handle_is_relation_sql(cnx, 'INSERT INTO cw_source_relation(eid_from,eid_to) VALUES (%s,%s)',
- (entity.eid, source.eid))
- # now we can update the full text index
- if self.do_fti and self.need_fti_indexation(entity.cw_etype):
- self.index_entity(cnx, entity=entity)
+ assert cnx.cnxset is not None
+ # begin by inserting eid/type/source/extid into the entities table
+ if extid is not None:
+ assert isinstance(extid, str)
+ extid = b64encode(extid)
+ attrs = {'type': entity.cw_etype, 'eid': entity.eid, 'extid': extid,
+ 'asource': source.uri}
+ self._handle_insert_entity_sql(cnx, self.sqlgen.insert('entities', attrs), attrs)
+ # insert core relations: is, is_instance_of and cw_source
+ try:
+ self._handle_is_relation_sql(cnx, 'INSERT INTO is_relation(eid_from,eid_to) VALUES (%s,%s)',
+ (entity.eid, eschema_eid(cnx, entity.e_schema)))
+ except IndexError:
+ # during schema serialization, skip
+ pass
+ else:
+ for eschema in entity.e_schema.ancestors() + [entity.e_schema]:
+ self._handle_is_relation_sql(cnx,
+ 'INSERT INTO is_instance_of_relation(eid_from,eid_to) VALUES (%s,%s)',
+ (entity.eid, eschema_eid(cnx, eschema)))
+ if 'CWSource' in self.schema and source.eid is not None: # else, cw < 3.10
+ self._handle_is_relation_sql(cnx, 'INSERT INTO cw_source_relation(eid_from,eid_to) VALUES (%s,%s)',
+ (entity.eid, source.eid))
+ # now we can update the full text index
+ if self.do_fti and self.need_fti_indexation(entity.cw_etype):
+ self.index_entity(cnx, entity=entity)
def update_info(self, cnx, entity, need_fti_update):
"""mark entity as being modified, fulltext reindex if needed"""
--- a/server/test/unittest_repository.py Mon Jun 02 11:47:30 2014 +0200
+++ b/server/test/unittest_repository.py Mon Jun 02 12:28:50 2014 +0200
@@ -677,19 +677,21 @@
self.assertRaises(UnknownEid, self.repo.type_from_eid, -2, self.session)
def test_add_delete_info(self):
- entity = self.repo.vreg['etypes'].etype_class('Personne')(self.session)
- entity.eid = -1
- entity.complete = lambda x: None
- self.session.set_cnxset()
- self.repo.add_info(self.session, entity, self.repo.system_source)
- cu = self.session.system_sql('SELECT * FROM entities WHERE eid = -1')
- data = cu.fetchall()
- self.assertEqual(tuplify(data), [(-1, 'Personne', 'system', None)])
- self.repo.delete_info(self.session, entity, 'system')
- #self.repo.commit()
- cu = self.session.system_sql('SELECT * FROM entities WHERE eid = -1')
- data = cu.fetchall()
- self.assertEqual(data, [])
+ with self.admin_access.repo_cnx() as cnx:
+ with cnx.ensure_cnx_set:
+ cnx.mode = 'write'
+ entity = self.repo.vreg['etypes'].etype_class('Personne')(cnx)
+ entity.eid = -1
+ entity.complete = lambda x: None
+ self.repo.add_info(cnx, entity, self.repo.system_source)
+ cu = cnx.system_sql('SELECT * FROM entities WHERE eid = -1')
+ data = cu.fetchall()
+ self.assertEqual(tuplify(data), [(-1, 'Personne', 'system', None)])
+ self.repo.delete_info(cnx, entity, 'system')
+ #self.repo.commit()
+ cu = cnx.system_sql('SELECT * FROM entities WHERE eid = -1')
+ data = cu.fetchall()
+ self.assertEqual(data, [])
class FTITC(CubicWebTC):