From 782522944476b2ce2c8bb7ee9b744d4990cb4816 Mon Sep 17 00:00:00 2001 From: navjot Date: Tue, 5 Jan 2021 16:55:02 +0530 Subject: [PATCH] updated addressbook table in class_sqlThread module --- src/class_sqlThread.py | 26 +++++++++++++++++++++++--- src/helper_addressbook.py | 6 ++---- src/tests/core.py | 19 ++++++++++++++----- 3 files changed, 39 insertions(+), 12 deletions(-) diff --git a/src/class_sqlThread.py b/src/class_sqlThread.py index 563dc472..77318473 100644 --- a/src/class_sqlThread.py +++ b/src/class_sqlThread.py @@ -47,7 +47,7 @@ class sqlThread(threading.Thread): self.cur.execute( '''CREATE TABLE subscriptions (label text, address text, enabled bool)''') self.cur.execute( - '''CREATE TABLE addressbook (label text, address text)''') + '''CREATE TABLE addressbook (label text, address text, UNIQUE(address) ON CONFLICT REPLACE)''') self.cur.execute( '''CREATE TABLE blacklist (label text, address text, enabled bool)''') self.cur.execute( @@ -63,7 +63,7 @@ class sqlThread(threading.Thread): '''('Bitmessage new releases/announcements','BM-GtovgYdgs7qXPkoYaRgrLFuFKz1SFpsw',1)''') self.cur.execute( '''CREATE TABLE settings (key blob, value blob, UNIQUE(key) ON CONFLICT REPLACE)''') - self.cur.execute('''INSERT INTO settings VALUES('version','10')''') + self.cur.execute('''INSERT INTO settings VALUES('version','11')''') self.cur.execute('''INSERT INTO settings VALUES('lastvacuumtime',?)''', ( int(time.time()),)) self.cur.execute( @@ -388,7 +388,27 @@ class sqlThread(threading.Thread): logger.debug( 'In messages.dat database, done adding address field to the pubkeys table' ' and removing the hash field.') - self.cur.execute('''update settings set value=10 WHERE key='version';''') + item = '''update settings set value=? WHERE key='version';''' + parameters = (10,) + self.cur.execute(item, parameters) + + # Update the address colunm to unique in addressbook table + item = '''SELECT value FROM settings WHERE key='version';''' + parameters = '' + self.cur.execute(item, parameters) + currentVersion = int(self.cur.fetchall()[0][0]) + if currentVersion == 10: + logger.debug( + 'In messages.dat database, updating address column to UNIQUE' + ' in the addressbook table.') + self.cur.execute( + '''ALTER TABLE addressbook RENAME TO old_addressbook''') + self.cur.execute( + '''CREATE TABLE addressbook''' + ''' (label text, address text, UNIQUE(address) ON CONFLICT REPLACE)''') + self.cur.execute( + '''INSERT INTO addressbook SELECT label, address FROM old_addressbook;''') + self.cur.execute('''update settings set value=11 WHERE key='version';''') # Are you hoping to add a new option to the keys.dat file of existing # Bitmessage users or modify the SQLite database? Add it right diff --git a/src/helper_addressbook.py b/src/helper_addressbook.py index a55f479a..fb572150 100644 --- a/src/helper_addressbook.py +++ b/src/helper_addressbook.py @@ -3,14 +3,12 @@ Insert value into addressbook """ from bmconfigparser import BMConfigParser -from helper_sql import sqlExecute, sqlQuery +from helper_sql import sqlExecute def insert(address, label): """perform insert into addressbook""" - queryreturn = sqlQuery( - '''SELECT count(*) FROM addressbook WHERE address=?''', address) - if address not in BMConfigParser().addresses() and not queryreturn[0][0]: + if address not in BMConfigParser().addresses(): return sqlExecute('''INSERT INTO addressbook VALUES (?,?)''', label, address) == 1 return False diff --git a/src/tests/core.py b/src/tests/core.py index 90b342a2..d3b4da6c 100644 --- a/src/tests/core.py +++ b/src/tests/core.py @@ -285,22 +285,22 @@ class TestCore(unittest.TestCase): 'createRandomAddress', 4, streamNumberForAddress, "test1", 1, "", False)) - def delete_address_from_database(self): - """Deleting random address""" - sqlQuery('''delete from addressbook where address=?''', self.addr) + def delete_address_from_database(self, address): + """Clean up addressbook""" + sqlQuery('''delete from addressbook where address=?''', address) def test_add_same_address_twice_in_addressbook(self): """checking same address is added twice in addressbook""" self.assertTrue(helper_addressbook.insert(label='test1', address=self.addr)) self.assertFalse(helper_addressbook.insert(label='test1', address=self.addr)) - self.delete_address_from_database() + self.delete_address_from_database(self.addr) def test_is_address_present_in_addressbook(self): """checking is address added in addressbook or not""" helper_addressbook.insert(label='test1', address=self.addr) queryreturn = sqlQuery('''select count(*) from addressbook where address=?''', self.addr) self.assertTrue(bool(queryreturn[0][0])) - self.delete_address_from_database() + self.delete_address_from_database(self.addr) def test_is_own_address_add_to_addressbook(self): """Checking own address adding in addressbook""" @@ -312,6 +312,15 @@ class TestCore(unittest.TestCase): except IndexError: self.fail("Can't generate addresses") + def test_adding_two_same_case_sensitive_addresses(self): + """Testing same case sensitive address store in addressbook""" + address1 = 'BM-2cVWtdUzPwF7UNGDrZftWuHWiJ6xxBpiSP' + address2 = 'BM-2CvwTDuZpWf7ungdRzFTwUhwIj6XXbPIsp' + self.assertTrue(helper_addressbook.insert(label='test1', address=address1)) + self.assertTrue(helper_addressbook.insert(label='test2', address=address2)) + self.delete_address_from_database(address1) + self.delete_address_from_database(address2) + def run(): """Starts all tests defined in this module"""