test_init.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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.abspath(os.path.join(os.path.dirname(__file__),
  38. "test.pwman.db"))
  39. class TestFactory(unittest.TestCase):
  40. @classmethod
  41. def tearDownClass(cls):
  42. SetupTester().clean()
  43. class TestInit(unittest.TestCase):
  44. @classmethod
  45. def setUpClass(cls):
  46. with open('dummy.cfg', 'w') as d:
  47. d.write(dummyfile)
  48. @classmethod
  49. def tearDownClass(cls):
  50. for item in ('dummy.cfg', testdb):
  51. try:
  52. os.unlink(item)
  53. except OSError:
  54. continue
  55. def setUp(self):
  56. "test that the right db instance was created"
  57. self.dbtype = 'sqlite'
  58. self.db = factory.createdb('sqlite://' + os.path.abspath(testdb),
  59. __DB_FORMAT__)
  60. self.tester = SetupTester(__DB_FORMAT__, dburi=testdb)
  61. self.tester.create()
  62. def test_get_db_version(self):
  63. v = get_db_version(self.tester.configp, 'sqlite')
  64. self.assertEqual(v, u"'0.6'")
  65. v = get_db_version(self.tester.configp, 'sqlite')
  66. self.assertEqual(v, u"'0.6'")
  67. os.unlink(testdb)
  68. def test_set_xsel(self):
  69. Args = namedtuple('args', 'cfile, dbase, algo')
  70. args = Args(cfile='dummy.cfg', dbase='dummy.db', algo='AES')
  71. xsel, dbtype, configp = get_conf_options(args, 'True')
  72. set_xsel(configp, False)
  73. set_xsel(configp, True)
  74. def test_get_conf_file(self):
  75. Args = namedtuple('args', 'cfile')
  76. args = Args(cfile='dummy.cfg')
  77. get_conf(args)
  78. def test_get_conf_options(self):
  79. Args = namedtuple('args', 'cfile, dbase, algo')
  80. args = Args(cfile='dummy.cfg', dbase='dummy.db', algo='AES')
  81. xsel, dburi, configp = get_conf_options(args, 'True')
  82. self.assertEqual(dburi, 'dummy.db')
  83. if __name__ == '__main__':
  84. try:
  85. unittest.main(verbosity=2, failfast=True)
  86. except SystemExit:
  87. if os.path.exists(testdb):
  88. os.remove(testdb)