test_init.py 3.5 KB

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