db_tests.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import os
  2. import os.path
  3. _saveconfig = True
  4. import sys
  5. if 'darwin' in sys.platform:
  6. from pwman.ui.mac import PwmanCliMac as PwmanCli
  7. from pwman.ui.mac import PwmanCliMacNew as PwmanCliNew
  8. OSX = True
  9. elif 'win' in sys.platform:
  10. from pwman.ui.cli import PwmanCli
  11. from pwman.ui.win import PwmanCliWinNew as PwmanCliNew
  12. OSX = False
  13. else:
  14. from pwman.ui.cli import PwmanCli
  15. from pwman.ui.cli import PwmanCliNew
  16. OSX = False
  17. import pwman.util.config as config
  18. import pwman.data.factory
  19. from pwman.data.nodes import NewNode
  20. from pwman.data.tags import Tag
  21. # set cls_timout to negative number (e.g. -1) to disable
  22. default_config = {'Global': {'umask': '0100', 'colors': 'yes',
  23. 'cls_timeout': '5'
  24. },
  25. 'Database': {'type': 'SQLite',
  26. 'filename': os.path.join("tests", "pwman.db")},
  27. 'Encryption': {'algorithm': 'AES'},
  28. 'Readline': {'history': os.path.join("tests",
  29. "history")}
  30. }
  31. config.set_defaults(default_config)
  32. import unittest
  33. class DBTests(unittest.TestCase):
  34. """test everything related to db"""
  35. def setUp(self):
  36. "test that the right db instance was created"
  37. dbver = 0.4
  38. self.dbtype = config.get_value("Database", "type")
  39. self.db = pwman.data.factory.create(self.dbtype, dbver)
  40. def test(self):
  41. self.assertTrue(True)
  42. def test_db_created(self):
  43. "test that the right db instance was created"
  44. # self.db = pwman.data.factory.create(dbtype, dbver)
  45. self.assertIn(self.dbtype, self.db.__class__.__name__)
  46. def test_db_opened(self):
  47. "db was successfuly opened"
  48. # it will have a file name associated
  49. self.assertTrue(hasattr(self.db, '_filename'))
  50. def test_create_node(self):
  51. "test that a node can be successfuly created"
  52. # this method does not test do_new
  53. # which is a UI method, rather we test
  54. # _db.addnodes
  55. username = 'tester'
  56. password = 'Password'
  57. url = 'example.org'
  58. notes = 'some notes'
  59. node = NewNode(username, password, url, notes)
  60. tags = [Tag(tn) for tn in ['testing1', 'testing2']]
  61. node.tags = tags
  62. self.db.open()
  63. self.db.addnodes([node])
  64. idx_created = node._id
  65. new_node = self.db.getnodes([idx_created])[0]
  66. for key, attr in {'password': password, 'username': username,
  67. 'url': url, 'notes': notes}.iteritems():
  68. self.assertEquals(attr, eval('new_node.'+key))
  69. self.db.close()
  70. class CLITests(unittest.TestCase):
  71. """test command line functionallity"""
  72. def test(self):
  73. self.assertTrue(True)