test_init.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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) 2014 Oz Nahum Tiram <nahumoz@gmail.com>
  18. # ============================================================================
  19. import unittest
  20. from collections import namedtuple
  21. import os
  22. import os.path
  23. from pwman import set_xsel
  24. from pwman.data import factory
  25. from pwman.data.database import __DB_FORMAT__
  26. from pwman import (get_conf, get_conf_options, get_db_version)
  27. from .test_tools import SetupTester
  28. dummyfile = """
  29. [Encryption]
  30. [Readline]
  31. [Global]
  32. xsel = /usr/bin/xsel
  33. colors = yes
  34. cls_timeout = 5
  35. [Database]
  36. """
  37. testdb = os.path.join(os.path.dirname(__file__), "test.pwman.db")
  38. class TestFactory(unittest.TestCase):
  39. @classmethod
  40. def tearDownClass(cls):
  41. SetupTester().clean()
  42. class TestInit(unittest.TestCase):
  43. @classmethod
  44. def setUpClass(cls):
  45. with open('dummy.cfg', 'w') as d:
  46. d.write(dummyfile)
  47. @classmethod
  48. def tearDownClass(cls):
  49. for item in ('dummy.cfg', testdb):
  50. try:
  51. os.unlink(item)
  52. except OSError:
  53. continue
  54. def setUp(self):
  55. "test that the right db instance was created"
  56. self.dbtype = 'SQLite'
  57. self.db = factory.create(self.dbtype, __DB_FORMAT__, testdb)
  58. self.tester = SetupTester(__DB_FORMAT__, testdb)
  59. self.tester.create()
  60. def test_get_db_version(self):
  61. v = get_db_version(self.tester.configp, 'SQLite', None)
  62. self.assertEqual(v, __DB_FORMAT__)
  63. os.unlink(testdb)
  64. v = get_db_version(self.tester.configp, 'SQLite', None)
  65. self.assertEqual(v, 0.6)
  66. def test_set_xsel(self):
  67. Args = namedtuple('args', 'cfile, dbase, algo')
  68. args = Args(cfile='dummy.cfg', dbase='dummy.db', algo='AES')
  69. xsel, dbtype, configp = get_conf_options(args, 'True')
  70. set_xsel(configp, False)
  71. set_xsel(configp, True)
  72. def test_get_conf_file(self):
  73. Args = namedtuple('args', 'cfile')
  74. args = Args(cfile='dummy.cfg')
  75. get_conf(args)
  76. def test_get_conf_options(self):
  77. Args = namedtuple('args', 'cfile, dbase, algo')
  78. args = Args(cfile='dummy.cfg', dbase='dummy.db', algo='AES')
  79. xsel, dbtype, configp = get_conf_options(args, 'True')
  80. self.assertEqual(dbtype, 'SQLite')
  81. if __name__ == '__main__':
  82. try:
  83. unittest.main(verbosity=2, failfast=True)
  84. except SystemExit:
  85. if os.path.exists(testdb):
  86. os.remove(testdb)