tests/lib/test_my_db.py
author fabien@tzone.org
Wed, 31 Dec 2008 01:00:21 -0500
changeset 70 1798859f7f6c
parent 66 a0aa025ee6a2
permissions -rw-r--r--
Add my_db.MyDBConnexion.map_keywords
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
66
a0aa025ee6a2 New version with tester.
fabien@tzone.org
parents: 65
diff changeset
     1
import testconfig
65
b975a3d7606a Partial submission, to include tests.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
     2
import unittest
b975a3d7606a Partial submission, to include tests.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
     3
import my_db
b975a3d7606a Partial submission, to include tests.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
     4
import pgdb
b975a3d7606a Partial submission, to include tests.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
     5
b975a3d7606a Partial submission, to include tests.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
     6
class TestMyDbConnexion(unittest.TestCase):
70
1798859f7f6c Add my_db.MyDBConnexion.map_keywords
fabien@tzone.org
parents: 66
diff changeset
     7
	def setUp(self):
1798859f7f6c Add my_db.MyDBConnexion.map_keywords
fabien@tzone.org
parents: 66
diff changeset
     8
		" Setup a test-db environment."
1798859f7f6c Add my_db.MyDBConnexion.map_keywords
fabien@tzone.org
parents: 66
diff changeset
     9
		self.db = my_db.connect(testconfig.CONFIG, "test")
1798859f7f6c Add my_db.MyDBConnexion.map_keywords
fabien@tzone.org
parents: 66
diff changeset
    10
		self.prefs = { 'keywords_box' : 10,
1798859f7f6c Add my_db.MyDBConnexion.map_keywords
fabien@tzone.org
parents: 66
diff changeset
    11
			       'keywords_sort' : 'count',
1798859f7f6c Add my_db.MyDBConnexion.map_keywords
fabien@tzone.org
parents: 66
diff changeset
    12
			       'keywords_reverse': True,
1798859f7f6c Add my_db.MyDBConnexion.map_keywords
fabien@tzone.org
parents: 66
diff changeset
    13
			       'fullname': 'Test User',
1798859f7f6c Add my_db.MyDBConnexion.map_keywords
fabien@tzone.org
parents: 66
diff changeset
    14
			       'default_view': 1 }
1798859f7f6c Add my_db.MyDBConnexion.map_keywords
fabien@tzone.org
parents: 66
diff changeset
    15
	    
1798859f7f6c Add my_db.MyDBConnexion.map_keywords
fabien@tzone.org
parents: 66
diff changeset
    16
	def tearDown(self):
1798859f7f6c Add my_db.MyDBConnexion.map_keywords
fabien@tzone.org
parents: 66
diff changeset
    17
		" Tear down the test-db environment"
1798859f7f6c Add my_db.MyDBConnexion.map_keywords
fabien@tzone.org
parents: 66
diff changeset
    18
		self.db.delete_user(self.db.userid)
1798859f7f6c Add my_db.MyDBConnexion.map_keywords
fabien@tzone.org
parents: 66
diff changeset
    19
1798859f7f6c Add my_db.MyDBConnexion.map_keywords
fabien@tzone.org
parents: 66
diff changeset
    20
	def testConnect(self):
1798859f7f6c Add my_db.MyDBConnexion.map_keywords
fabien@tzone.org
parents: 66
diff changeset
    21
		self.assertNotEqual(self.db, None)
1798859f7f6c Add my_db.MyDBConnexion.map_keywords
fabien@tzone.org
parents: 66
diff changeset
    22
		self.assertNotEqual(self.db.userid, -1)
1798859f7f6c Add my_db.MyDBConnexion.map_keywords
fabien@tzone.org
parents: 66
diff changeset
    23
1798859f7f6c Add my_db.MyDBConnexion.map_keywords
fabien@tzone.org
parents: 66
diff changeset
    24
	def testGetPreferences(self):
1798859f7f6c Add my_db.MyDBConnexion.map_keywords
fabien@tzone.org
parents: 66
diff changeset
    25
		prefs = self.db.get_preferences()
1798859f7f6c Add my_db.MyDBConnexion.map_keywords
fabien@tzone.org
parents: 66
diff changeset
    26
		keys = prefs.keys()
1798859f7f6c Add my_db.MyDBConnexion.map_keywords
fabien@tzone.org
parents: 66
diff changeset
    27
		keys.sort()
1798859f7f6c Add my_db.MyDBConnexion.map_keywords
fabien@tzone.org
parents: 66
diff changeset
    28
		expKeys = self.prefs.keys()
1798859f7f6c Add my_db.MyDBConnexion.map_keywords
fabien@tzone.org
parents: 66
diff changeset
    29
		expKeys.sort()
1798859f7f6c Add my_db.MyDBConnexion.map_keywords
fabien@tzone.org
parents: 66
diff changeset
    30
		self.assertEqual(expKeys, keys)
65
b975a3d7606a Partial submission, to include tests.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    31
70
1798859f7f6c Add my_db.MyDBConnexion.map_keywords
fabien@tzone.org
parents: 66
diff changeset
    32
	def testSetPreferences(self):
1798859f7f6c Add my_db.MyDBConnexion.map_keywords
fabien@tzone.org
parents: 66
diff changeset
    33
		self.db.set_preferences(self.prefs)
1798859f7f6c Add my_db.MyDBConnexion.map_keywords
fabien@tzone.org
parents: 66
diff changeset
    34
		prefs = self.db.get_preferences()
1798859f7f6c Add my_db.MyDBConnexion.map_keywords
fabien@tzone.org
parents: 66
diff changeset
    35
		self.assertEqual(prefs, self.prefs)
1798859f7f6c Add my_db.MyDBConnexion.map_keywords
fabien@tzone.org
parents: 66
diff changeset
    36
1798859f7f6c Add my_db.MyDBConnexion.map_keywords
fabien@tzone.org
parents: 66
diff changeset
    37
	def testAddKeyword(self):
1798859f7f6c Add my_db.MyDBConnexion.map_keywords
fabien@tzone.org
parents: 66
diff changeset
    38
		kw = self.db.add_keyword('keyword')
1798859f7f6c Add my_db.MyDBConnexion.map_keywords
fabien@tzone.org
parents: 66
diff changeset
    39
		self.assertEqual(self.db.get_keyword(kw), 'keyword')
65
b975a3d7606a Partial submission, to include tests.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    40
70
1798859f7f6c Add my_db.MyDBConnexion.map_keywords
fabien@tzone.org
parents: 66
diff changeset
    41
	def testRemoveKeyword(self):
1798859f7f6c Add my_db.MyDBConnexion.map_keywords
fabien@tzone.org
parents: 66
diff changeset
    42
		kw = self.db.add_keyword('keyword')
1798859f7f6c Add my_db.MyDBConnexion.map_keywords
fabien@tzone.org
parents: 66
diff changeset
    43
		self.db.remove_keyword(kw)
1798859f7f6c Add my_db.MyDBConnexion.map_keywords
fabien@tzone.org
parents: 66
diff changeset
    44
		kws = self.db.get_all_keywords()
1798859f7f6c Add my_db.MyDBConnexion.map_keywords
fabien@tzone.org
parents: 66
diff changeset
    45
		# There is always '--' by default
1798859f7f6c Add my_db.MyDBConnexion.map_keywords
fabien@tzone.org
parents: 66
diff changeset
    46
		self.assertEqual(len(kws), 1)
65
b975a3d7606a Partial submission, to include tests.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    47
70
1798859f7f6c Add my_db.MyDBConnexion.map_keywords
fabien@tzone.org
parents: 66
diff changeset
    48
	def testMapKeywords(self):
1798859f7f6c Add my_db.MyDBConnexion.map_keywords
fabien@tzone.org
parents: 66
diff changeset
    49
		kws = ('kw1', 'kw2', 'kw3', 'kw4')
1798859f7f6c Add my_db.MyDBConnexion.map_keywords
fabien@tzone.org
parents: 66
diff changeset
    50
		kwids = map(self.db.add_keyword, kws[:-1])
1798859f7f6c Add my_db.MyDBConnexion.map_keywords
fabien@tzone.org
parents: 66
diff changeset
    51
		expected_map = { kws[-1]: -1 }
1798859f7f6c Add my_db.MyDBConnexion.map_keywords
fabien@tzone.org
parents: 66
diff changeset
    52
		for kw, id in zip(kws[:-1], kwids):
1798859f7f6c Add my_db.MyDBConnexion.map_keywords
fabien@tzone.org
parents: 66
diff changeset
    53
			expected_map[kw] = id
1798859f7f6c Add my_db.MyDBConnexion.map_keywords
fabien@tzone.org
parents: 66
diff changeset
    54
		kw_map = self.db.map_keywords(kws)
1798859f7f6c Add my_db.MyDBConnexion.map_keywords
fabien@tzone.org
parents: 66
diff changeset
    55
		self.assertEqual(expected_map, kw_map)
65
b975a3d7606a Partial submission, to include tests.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    56
b975a3d7606a Partial submission, to include tests.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    57
if __name__ == '__main__':
b975a3d7606a Partial submission, to include tests.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    58
    unittest.main()