def test_mixed_session_inheritance(self): """test_mixed_session_inheritance Verify that an explicit session passed to a middle level in a tripple nested session_scope is independent from the outer and inner levels. """ id1, id2, id3 = str(uuid.uuid4()), str(uuid.uuid4()), str(uuid.uuid4()) external = g._new_session() with g.session_scope() as outer: g.node_insert(PsqlNode(id1, 'foo')) with g.session_scope(external) as inner: self.assertEqual(inner, external) self.assertNotEqual(inner, outer) g.node_insert(PsqlNode(id2, 'test')) with g.session_scope(outer) as third: self.assertEqual(third, outer) g.node_insert(PsqlNode(id3, 'foo')) third.rollback() with g.session_scope(): self.assertEqual(g.node_lookup(id2).count(), 0) external.commit() with g.session_scope(): self.assertEqual(g.node_lookup(id1).count(), 0) self.assertEqual(g.node_lookup(id2).count(), 1) self.assertEqual(g.node_lookup(id3).count(), 0) self.assertFalse(g.has_session())
def test_explicit_to_inherit_nested_session(self): """test_explicit_to_inherit_nested_session Verify that the children of an explicitly passed session scope inherit the explicit session and commit all updates. """ id1, id2, id3 = str(uuid.uuid4()), str(uuid.uuid4()), str(uuid.uuid4()) outer = g._new_session() # don't do this with g.session_scope(outer): g.node_insert(PsqlNode(id1, 'test')) with g.session_scope() as inner: self.assertEqual(inner, outer) g.node_insert(PsqlNode(id2, 'foo')) with g.session_scope() as third: self.assertEqual(third, outer) g.node_insert(PsqlNode(id3, 'foo')) with g.session_scope(): self.assertEqual(g.node_lookup(id2).count(), 0) outer.commit() with g.session_scope(): self.assertEqual(g.node_lookup(id1).count(), 1) self.assertEqual(g.node_lookup(id2).count(), 1) self.assertEqual(g.node_lookup(id3).count(), 1) self.assertFalse(g.has_session())
def test_commit_automatic_session(self): """test_commit_automatic_session Make sure that when not wrapped in a session scope the successful commit of a conflicting node does not rollback previously committed nodes. (i.e. the statements don't inherit the same session) """ nid = str(uuid.uuid4()) g.node_insert(PsqlNode(nid, 'test')) self.assertRaises(IntegrityError, g.node_insert, PsqlNode(nid, 'test')) with g.session_scope(): self.assertEqual(g.node_lookup(nid).one().label, 'test') self.assertFalse(g.has_session())
def test_rollback_automatic_session(self): """test_rollback_automatic_session Make sure that within a session scope, an error causes the entire scope to be rolled back even without an explicit session being passed """ nid = str(uuid.uuid4()) with self.assertRaises(IntegrityError): with g.session_scope(): g.node_insert(PsqlNode(nid, 'test')) g.node_insert(PsqlNode(nid, 'test')) with g.session_scope(): self.assertEqual(len(list(g.node_lookup(nid).all())), 0)
def test_explicit_session(self): """test_explicit_session Verify that passing a session explicitly functions as expected """ id1, id2 = str(uuid.uuid4()), str(uuid.uuid4()) with g.session_scope() as session: g.node_insert(PsqlNode(id1, 'foo')) g.node_insert(PsqlNode(id2, 'foo'), session) session.rollback() with g.session_scope(): self.assertEqual(g.node_lookup(id2).count(), 0) self.assertEqual(g.node_lookup(id1).count(), 0) self.assertFalse(g.has_session())
def test_automatic_nested_session_inherit_valid(self): """test_automatic_nested_session_inherit_valid Verify that implicitly nested session scopes correctly inherit the parent session for valid node insertion """ id1, id2 = str(uuid.uuid4()), str(uuid.uuid4()) with g.session_scope(): g.node_insert(PsqlNode(id1, 'test')) with g.session_scope(): g.node_insert(PsqlNode(id2, 'foo')) self.assertEqual(g.node_lookup(id1).one().label, 'test') self.assertEqual(g.node_lookup(id2).one().label, 'foo') self.assertFalse(g.has_session())
def test_automatic_nested_session(self): """test_automatic_nested_session Make sure that given a call to explicitly nest session scopes, the nested session commits first """ nid = str(uuid.uuid4()) with self.assertRaises(IntegrityError): with g.session_scope(): g.node_insert(PsqlNode(nid, 'test')) with g.session_scope(can_inherit=False): g.node_insert(PsqlNode(nid, 'test')) with g.session_scope(): self.assertEqual(g.node_lookup(nid).one().label, 'test') self.assertFalse(g.has_session())
def test_library_functions_use_session_implicitly(self): """Test that library functions use the session they're scoped in """ id1 = str(uuid.uuid4()) with g.session_scope(): g.node_insert(PsqlNode(id1, 'test')) g.node_lookup(node_id=id1).one()
def test_automatic_nested_session_inherit_invalid(self): """test_automatic_nested_session_inherit_invalid Verify that implicitly nested session scopes correctly inherit the parent session. """ id1, id2 = str(uuid.uuid4()), str(uuid.uuid4()) with g.session_scope() as outer: g.node_insert(PsqlNode(id1, 'test')) with g.session_scope() as inner: self.assertEqual(inner, outer) g.node_insert(PsqlNode(id2, 'foo')) inner.rollback() with g.session_scope(): self.assertEqual(g.node_lookup(id1).count(), 0) self.assertEqual(g.node_lookup(id2).count(), 0) self.assertFalse(g.has_session())
def test_automatic_nested_session2(self): """test_automatic_nested_session2 Make sure that given a call to explicitly nest session scopes, failure of the nested session scope does not affect the parent scope. """ id1 = str(uuid.uuid4()) id2 = str(uuid.uuid4()) g.node_insert(PsqlNode(id1, 'foo')) with g.session_scope(): g.node_insert(PsqlNode(id2, 'test')) with self.assertRaises(IntegrityError): with g.session_scope(can_inherit=False): g.node_insert(PsqlNode(id1, 'foo')) with g.session_scope(): self.assertEqual(g.node_lookup(id2).one().label, 'test') self.assertFalse(g.has_session())
def test_automatic_nested_session3(self): """test_automatic_nested_session3 Also, verify that two statements in a nested session_scope inherit the same session (i.e. the session stack is working properly). """ id1, id2, id3 = str(uuid.uuid4()), str(uuid.uuid4()), str(uuid.uuid4()) g.node_insert(PsqlNode(id1, 'foo')) with g.session_scope(): g.node_insert(PsqlNode(id2, 'test')) with self.assertRaises(IntegrityError): with g.session_scope(can_inherit=False): g.node_insert(PsqlNode(id1, 'foo')) g.node_insert(PsqlNode(id3, 'foo')) with g.session_scope(): self.assertEqual(g.node_lookup(id2).one().label, 'test') self.assertEqual(g.node_lookup(id3).count(), 0) self.assertFalse(g.has_session())
def test_simple_automatic_session(self): idA = str(uuid.uuid4()) with g.session_scope(): g.node_insert(PsqlNode(idA, 'test')) with g.session_scope(): g.node_lookup(idA).one()