Skip to content

Commit

Permalink
fix the bugs about gid field of record and pass the unittest.
Browse files Browse the repository at this point in the history
  • Loading branch information
liwei-lai committed Oct 13, 2012
1 parent 281f723 commit e03fa9c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 16 deletions.
26 changes: 18 additions & 8 deletions pypw/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,6 @@ def write(self):
def add_record(self, group, item, value, note=None):
try:
record = self._compose_record(group, item, value, note)
self.data['records'].append(record)
self.data['currentID'] += 1
if not self._records['gid'].has_key(record['gid']):
self.data['currentGID'] += 1
self.write()
self._adjust_structure(record)
return True
Expand Down Expand Up @@ -134,23 +130,37 @@ def _adjust_structure(self, record):
rid, gid = record['id'], record['gid']
group = record['group']
item = record['itemname']
self._records['rid'][rid] = record
self._records['gid'].setdefault(gid, []).append(record)
self._records['_rid'][rid] = record
self._records['_gid'].setdefault(gid, []).append(record)
self._records[group][item] = record

# groupmap is a helper subdict contain (group, gid) pairs
if not self._records['_gidmap'].has_key(group):
self._records['_gidmap'][group] = record['gid']

def _compose_record(self, group, item, value, note=None):
created = datetime.today().isoformat('_')
# TODO: should judge the correct gid
if group in ('_rid', '_gid', '_gidmap'):
group = 'Invalid Group Name'
gid = float('nan') # Not a number
elif self._records.has_key(group):
gid = self._records['_gidmap'][group]
else:
gid = self.data['currentGID']
self.data['currentGID'] += 1

record = {
'id': self.data['currentID'],
'gid': self.data['currentGID'],
'gid': gid,
'group': group,
'itemname': item,
'value': value,
'note': note,
'created': created,
'updated': created,
}
self.data['currentID'] += 1
self.data['records'].append(record)
return record

@classmethod
Expand Down
35 changes: 27 additions & 8 deletions pypw/handler_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import unittest
import tempfile
import math

from handler import AESHandler

Expand All @@ -19,19 +20,37 @@ def tearDown(self):
def test_data_persistance(self):
self.assertTrue(self.handler.add_record(u'web', u'facebook', u'lol2012'))
self.assertTrue(self.handler.add_record(u'web', u'google', u'answer42'))
self.assertTrue(self.handler.add_record(u'bank', u'BOA', u'money888'))
self.assertTrue(self.handler.add_record(u'银行', u'招商银行', u'money888'))
# there is a invalid group name, so that the gid would be NaN
self.assertTrue(self.handler.add_record(u'_rid', u'testgroup', u'test123'))

print self.handler.data
self.assertEqual(len(self.handler.data['records']), 3)
self.assertEqual(self.handler.data['currentID'], 3)
self.assertEqual(len(self.handler.data['records']), 4)
self.assertEqual(self.handler.data['currentID'], 4)
self.assertEqual(self.handler.data['currentGID'], 2)
self.assertEqual(len(self.handler._records['_rid']), 4)
self.assertEqual(len(self.handler._records['_gid']), 3)
self.assertEqual(len(self.handler._records['_gidmap']), 3)
self.assertTrue(
self.handler._records['_gidmap'].has_key('Invalid Group Name')
)

handler2 = AESHandler()
handler2.initialize('provide a key', self.tmpfile.name)

self.assertDictEqual(self.handler.data, handler2.data)
self.assertListEqual(self.handler.data['records'],
handler2.data['records'])

self.assertEqual(self.handler.data['digest'], handler2.data['digest'])
self.assertEqual(self.handler.data['currentID'], handler2.data['currentID'])
self.assertEqual(self.handler.data['currentGID'], handler2.data['currentGID'])
self.assertEqual(len(self.handler.data['records']),
len(handler2.data['records']))
# compare the `handler.data['records']` between two handlers
for i in range(len(handler2.data['records'])):
for key in handler2.data['records'][i]:
# should be ignore the "not a number" case
if isinstance(handler2.data['records'][i][key], float) and \
math.isnan(handler2.data['records'][i][key]):
continue
self.assertEqual(self.handler.data['records'][i][key],
handler2.data['records'][i][key])

def test_32byte_key_generate(self):
key1 = AESHandler.figure_32Byte_key('not enough 32 bytes')
Expand Down

0 comments on commit e03fa9c

Please sign in to comment.