test_factory.py 3.6 KB

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