test_factory.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. #from pwman.data.nodes import NewNode
  20. #from pwman.data.tags import TagNew
  21. from pwman.data import factory
  22. from pwman.data.drivers.sqlite import DatabaseException, SQLite
  23. from pwman.ui import get_ui_platform
  24. from pwman.data.database import __DB_FORMAT__
  25. import sys
  26. import unittest
  27. import os
  28. import os.path
  29. dummyfile = """
  30. [Encryption]
  31. [Readline]
  32. [Global]
  33. xsel = /usr/bin/xsel
  34. colors = yes
  35. umask = 0100
  36. cls_timeout = 5
  37. [Database]
  38. """
  39. #def node_factory(username, password, url, notes, tags=None):
  40. # node = NewNode()
  41. # node.username = username
  42. # node.password = password
  43. # node.url = url
  44. # node.notes = notes
  45. # tags = [TagNew(tn) for tn in tags]
  46. # node.tags = tags
  47. # return node
  48. _saveconfig = False
  49. PwmanCliNew, OSX = get_ui_platform(sys.platform)
  50. from .test_tools import (SetupTester)
  51. testdb = os.path.join(os.path.dirname(__file__), "test.pwman.db")
  52. class TestFactory(unittest.TestCase):
  53. def setUp(self):
  54. "test that the right db instance was created"
  55. self.dbtype = 'SQLite'
  56. self.db = factory.create(self.dbtype, __DB_FORMAT__, testdb)
  57. self.tester = SetupTester(__DB_FORMAT__, testdb)
  58. self.tester.create()
  59. def test_factory_check_db_ver(self):
  60. self.assertEqual(factory.check_db_version('SQLite', testdb), 0.6)
  61. def test_factory_check_db_file(self):
  62. factory.create('SQLite', version='0.3', filename='baz.db')
  63. self.assertEqual(factory.check_db_version('SQLite', 'baz.db'), 0.3)
  64. os.unlink('baz.db')
  65. def test_factory_create(self):
  66. db = factory.create('SQLite', filename='foo.db')
  67. db._open()
  68. self.assertTrue(os.path.exists('foo.db'))
  69. db.close()
  70. os.unlink('foo.db')
  71. self.assertIsInstance(db, SQLite)
  72. self.assertRaises(DatabaseException, factory.create, 'UNKNOWN')
  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)