test_factory.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # ============================================================================
  2. # This file is part of Pwman3.
  3. #
  4. # Pwman3 is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License, version 2
  6. # as published by the Free Software Foundation;
  7. #
  8. # Pwman3 is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with Pwman3; if not, write to the Free Software
  15. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. # ============================================================================
  17. # Copyright (C) 2013-2014 Oz Nahum Tiram <nahumoz@gmail.com>
  18. # ============================================================================
  19. import sys
  20. import unittest
  21. import os
  22. import os.path
  23. from pwman.data import factory
  24. from pwman.data.drivers.sqlite import DatabaseException, SQLite
  25. from pwman.ui import get_ui_platform
  26. from pwman.data.database import __DB_FORMAT__
  27. from .test_tools import (SetupTester)
  28. PwmanCliNew, OSX = get_ui_platform(sys.platform)
  29. testdb = os.path.join(os.path.dirname(__file__), "test.pwman.db")
  30. _saveconfig = False
  31. class TestFactory(unittest.TestCase):
  32. @classmethod
  33. def tearDownClass(cls):
  34. SetupTester().clean()
  35. def setUp(self):
  36. "test that the right db instance was created"
  37. self.dbtype = 'SQLite'
  38. self.db = factory.create(self.dbtype, __DB_FORMAT__, testdb)
  39. self.tester = SetupTester(__DB_FORMAT__, testdb)
  40. self.tester.create()
  41. def test_factory_check_db_ver(self):
  42. self.assertEqual(factory.check_db_version('SQLite', testdb), 0.6)
  43. def test_factory_check_db_file(self):
  44. factory.create('SQLite', version='0.3', filename='baz.db')
  45. self.assertEqual(factory.check_db_version('SQLite', 'baz.db'), 0.3)
  46. os.unlink('baz.db')
  47. def test_factory_create(self):
  48. db = factory.create('SQLite', filename='foo.db')
  49. db._open()
  50. self.assertTrue(os.path.exists('foo.db'))
  51. db.close()
  52. os.unlink('foo.db')
  53. self.assertIsInstance(db, SQLite)
  54. self.assertRaises(DatabaseException, factory.create, 'UNKNOWN')
  55. if __name__ == '__main__':
  56. # make sure we use local pwman
  57. sys.path.insert(0, os.getcwd())
  58. # check if old DB exists, if so remove it.
  59. # excuted only once when invoked upon import or
  60. # upon run
  61. SetupTester().clean()
  62. unittest.main(verbosity=1, failfast=True)