test_factory.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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.database import DatabaseException
  25. from pwman.data.drivers.sqlite import SQLite
  26. from pwman.data.drivers.postgresql import PostgresqlDatabase
  27. from pwman.data.database import __DB_FORMAT__
  28. from .test_tools import (SetupTester)
  29. testdb = os.path.abspath(os.path.join(os.path.dirname(__file__),
  30. "test.pwman.db"))
  31. _saveconfig = False
  32. class TestFactory(unittest.TestCase):
  33. @classmethod
  34. def tearDownClass(cls):
  35. SetupTester().clean()
  36. def setUp(self):
  37. "test that the right db instance was created"
  38. self.dbtype = 'sqlite'
  39. self.db = factory.createdb('sqlite:///'+testdb, __DB_FORMAT__)
  40. self.tester = SetupTester(__DB_FORMAT__, testdb)
  41. self.tester.create()
  42. def test_factory_check_db_ver(self):
  43. self.assertEqual(factory.check_db_version('sqlite://'+testdb), u"'0.6'")
  44. @unittest.skip("not supported at the moment")
  45. def test_factory_check_db_file(self):
  46. fn = os.path.join(os.path.dirname(__file__), 'baz.db')
  47. db = factory.createdb('sqlite:///'+os.path.abspath(fn), 0.3)
  48. db._open()
  49. self.assertEqual(factory.check_db_version('sqlite:///'+fn), 0.3)
  50. os.unlink(fn)
  51. def test_factory_create(self):
  52. fn = os.path.join(os.path.dirname(__file__), 'foo.db')
  53. db = factory.createdb('sqlite://'+os.path.abspath(fn), 0.6)
  54. db._open()
  55. self.assertTrue(os.path.exists(fn))
  56. db.close()
  57. os.unlink(fn)
  58. self.assertIsInstance(db, SQLite)
  59. self.assertRaises(DatabaseException, factory.createdb, *('UNKNOWN',
  60. __DB_FORMAT__))
  61. def test_factory_createdb(self):
  62. db = factory.createdb("sqlite:///test.db", 0.6)
  63. self.assertIsInstance(db, SQLite)
  64. del db
  65. db = factory.createdb("postgresql:///pwman", 0.6)
  66. self.assertIsInstance(db, PostgresqlDatabase)
  67. del db
  68. db = factory.createdb("sqlite:///test.db", 0.7)
  69. self.assertIsInstance(db, SQLite)
  70. del db
  71. db = factory.createdb("postgresql:///pwman", 0.7)
  72. self.assertIsInstance(db, PostgresqlDatabase)
  73. if __name__ == '__main__':
  74. # make sure we use local pwman
  75. sys.path.insert(0, os.getcwd())
  76. # check if old DB exists, if so remove it.
  77. # excuted only once when invoked upon import or
  78. # upon run
  79. SetupTester().clean()
  80. unittest.main(verbosity=1, failfast=True)