From 160bf7f2615b26547055ec23befb0c1ed68b6f69 Mon Sep 17 00:00:00 2001 From: Cedric Abunar Date: Thu, 14 Mar 2013 08:08:35 -0400 Subject: [PATCH] new unittest for rollback --- .../xivo_dao/helpers/tests/test_session.py | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 xivo-dao/xivo_dao/helpers/tests/test_session.py diff --git a/xivo-dao/xivo_dao/helpers/tests/test_session.py b/xivo-dao/xivo_dao/helpers/tests/test_session.py new file mode 100644 index 00000000..6b3c16fa --- /dev/null +++ b/xivo-dao/xivo_dao/helpers/tests/test_session.py @@ -0,0 +1,73 @@ +# -*- coding: UTF-8 -*- + +# Copyright (C) 2013 Avencall +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see + + +from xivo_dao.alchemy.agentfeatures import AgentFeatures +from xivo_dao.tests.test_dao import DAOTestCase +from sqlalchemy.exc import IntegrityError + + +class TestSession(DAOTestCase): + + tables = [AgentFeatures] + + def setUp(self): + self.empty_tables() + + def _insert_valid_agent(self): + agent = AgentFeatures() + agent.numgroup = 6 + agent.number = '15' + agent.passwd = '' + agent.context = 'default' + agent.language = '' + + self.session.add(agent) + + def _insert_inalid_agent(self): + agent = AgentFeatures() + agent.numgroup = 6 + agent.number = '15' + agent.passwd = '' + agent.context = 'default' + + self.session.add(agent) + + def test_commit(self): + self.session.begin() + try: + self._insert_valid_agent() + self.session.commit() + except IntegrityError: + self.session.rollback() + + res = self.session.query(AgentFeatures).all() + + self.assertEqual(len(res), 1) + + def test_rollback(self): + self.session.begin() + try: + self._insert_valid_agent() + self._insert_inalid_agent() + self.session.commit() + except IntegrityError: + self.session.rollback() + + res = self.session.query(AgentFeatures).all() + + self.assertEqual(len(res), 0) -- 2.22.0