From e03fa9c195cc9796b115bb8fd4d23a803b54cf16 Mon Sep 17 00:00:00 2001 From: Jason Lai Date: Sat, 13 Oct 2012 13:54:54 +0800 Subject: [PATCH] fix the bugs about gid field of record and pass the unittest. --- pypw/handler.py | 26 ++++++++++++++++++-------- pypw/handler_test.py | 35 +++++++++++++++++++++++++++-------- 2 files changed, 45 insertions(+), 16 deletions(-) diff --git a/pypw/handler.py b/pypw/handler.py index e6bb09b..ad1094e 100644 --- a/pypw/handler.py +++ b/pypw/handler.py @@ -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 @@ -134,16 +130,28 @@ 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, @@ -151,6 +159,8 @@ def _compose_record(self, group, item, value, note=None): 'created': created, 'updated': created, } + self.data['currentID'] += 1 + self.data['records'].append(record) return record @classmethod diff --git a/pypw/handler_test.py b/pypw/handler_test.py index 27e9c2b..3ed1eb5 100644 --- a/pypw/handler_test.py +++ b/pypw/handler_test.py @@ -2,6 +2,7 @@ import unittest import tempfile +import math from handler import AESHandler @@ -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')