From 39e2020ade12aa3e248dcda907ea8a15a5e7d004 Mon Sep 17 00:00:00 2001 From: Rolf Campbell Date: Sun, 24 Mar 2019 09:57:53 -0400 Subject: [PATCH 1/9] Add a blocking commit after each modification if autocommit is enabled. --- sqlitedict.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/sqlitedict.py b/sqlitedict.py index 7b60235..fcd351a 100755 --- a/sqlitedict.py +++ b/sqlitedict.py @@ -251,6 +251,8 @@ def __setitem__(self, key, value): ADD_ITEM = 'REPLACE INTO "%s" (key, value) VALUES (?,?)' % self.tablename self.conn.execute(ADD_ITEM, (key, self.encode(value))) + if self.autocommit: + self.commit() def __delitem__(self, key): if self.flag == 'r': @@ -260,6 +262,8 @@ def __delitem__(self, key): raise KeyError(key) DEL_ITEM = 'DELETE FROM "%s" WHERE key = ?' % self.tablename self.conn.execute(DEL_ITEM, (key,)) + if self.autocommit: + self.commit() def update(self, items=(), **kwds): if self.flag == 'r': @@ -275,6 +279,8 @@ def update(self, items=(), **kwds): self.conn.executemany(UPDATE_ITEMS, items) if kwds: self.update(kwds) + if self.autocommit: + self.commit() def __iter__(self): return self.iterkeys() From 76d79a9ffcc7ff099b18f9af496b57638a1df53e Mon Sep 17 00:00:00 2001 From: Rolf Campbell Date: Sun, 11 Aug 2019 10:13:23 -0400 Subject: [PATCH 2/9] Add test for autocommit near end of program. --- tests/autocommit.py | 6 ++++++ tests/autocommit2.py | 6 ++++++ tests/sqlitedict.py | 1 + tests/test_autocommit.py | 6 ++++++ 4 files changed, 19 insertions(+) create mode 100644 tests/autocommit.py create mode 100644 tests/autocommit2.py create mode 120000 tests/sqlitedict.py create mode 100644 tests/test_autocommit.py diff --git a/tests/autocommit.py b/tests/autocommit.py new file mode 100644 index 0000000..346eda3 --- /dev/null +++ b/tests/autocommit.py @@ -0,0 +1,6 @@ +import sqlitedict + +d = sqlitedict.SqliteDict(f'tests/db/autocommit.sqlite', autocommit=True ) + +for i in range(1000): + d[i] = i diff --git a/tests/autocommit2.py b/tests/autocommit2.py new file mode 100644 index 0000000..e6015f1 --- /dev/null +++ b/tests/autocommit2.py @@ -0,0 +1,6 @@ +import sqlitedict + +d = sqlitedict.SqliteDict('tests/db/autocommit.sqlite', autocommit=True ) + +for i in range(1000): + assert(d[i] == i) diff --git a/tests/sqlitedict.py b/tests/sqlitedict.py new file mode 120000 index 0000000..4650443 --- /dev/null +++ b/tests/sqlitedict.py @@ -0,0 +1 @@ +../sqlitedict.py \ No newline at end of file diff --git a/tests/test_autocommit.py b/tests/test_autocommit.py new file mode 100644 index 0000000..a766520 --- /dev/null +++ b/tests/test_autocommit.py @@ -0,0 +1,6 @@ +import sys, os + +def test(): + "Verify autocommit just before program exits." + assert(0 == os.system('%s tests/autocommit.py' % sys.executable)) + assert(0 == os.system('%s tests/autocommit2.py' % sys.executable)) From 70dd9eac268402890dd6e63ff65c441e94cd913b Mon Sep 17 00:00:00 2001 From: Rolf Campbell Date: Wed, 14 Aug 2019 19:35:36 -0400 Subject: [PATCH 3/9] Remove useless f-string to make it work before python 3.6. --- tests/autocommit.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/autocommit.py b/tests/autocommit.py index 346eda3..9bf0e26 100644 --- a/tests/autocommit.py +++ b/tests/autocommit.py @@ -1,6 +1,6 @@ import sqlitedict -d = sqlitedict.SqliteDict(f'tests/db/autocommit.sqlite', autocommit=True ) +d = sqlitedict.SqliteDict('tests/db/autocommit.sqlite', autocommit=True) for i in range(1000): d[i] = i From ac94b36fedbd7996402422079f7a646029d5e6b3 Mon Sep 17 00:00:00 2001 From: endlisnis Date: Sun, 29 Dec 2019 09:35:52 -0500 Subject: [PATCH 4/9] Remove parentheses from assert Co-Authored-By: Michael Penkov --- tests/autocommit2.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/autocommit2.py b/tests/autocommit2.py index e6015f1..52db210 100644 --- a/tests/autocommit2.py +++ b/tests/autocommit2.py @@ -3,4 +3,4 @@ d = sqlitedict.SqliteDict('tests/db/autocommit.sqlite', autocommit=True ) for i in range(1000): - assert(d[i] == i) + assert d[i] == i From d345a670f599d87fcd3aee862cd514c572753a02 Mon Sep 17 00:00:00 2001 From: Rolf Campbell Date: Sun, 29 Dec 2019 11:25:52 -0500 Subject: [PATCH 5/9] Avoid adding a symlink (use PYTHONPATH instead). Inline the autocommit2.py file (reduce file count). --- tests/autocommit2.py | 6 ------ tests/sqlitedict.py | 1 - tests/test_autocommit.py | 10 ++++++++-- 3 files changed, 8 insertions(+), 9 deletions(-) delete mode 100644 tests/autocommit2.py delete mode 120000 tests/sqlitedict.py diff --git a/tests/autocommit2.py b/tests/autocommit2.py deleted file mode 100644 index 52db210..0000000 --- a/tests/autocommit2.py +++ /dev/null @@ -1,6 +0,0 @@ -import sqlitedict - -d = sqlitedict.SqliteDict('tests/db/autocommit.sqlite', autocommit=True ) - -for i in range(1000): - assert d[i] == i diff --git a/tests/sqlitedict.py b/tests/sqlitedict.py deleted file mode 120000 index 4650443..0000000 --- a/tests/sqlitedict.py +++ /dev/null @@ -1 +0,0 @@ -../sqlitedict.py \ No newline at end of file diff --git a/tests/test_autocommit.py b/tests/test_autocommit.py index a766520..ac17607 100644 --- a/tests/test_autocommit.py +++ b/tests/test_autocommit.py @@ -2,5 +2,11 @@ def test(): "Verify autocommit just before program exits." - assert(0 == os.system('%s tests/autocommit.py' % sys.executable)) - assert(0 == os.system('%s tests/autocommit2.py' % sys.executable)) + assert(0 == os.system('PYTHONPATH=. %s tests/autocommit.py' % sys.executable)) + # The above script relies on the autocommit feature working correctly. + # Now, let's check if it actually worked. + import sqlitedict + d = sqlitedict.SqliteDict('tests/db/autocommit.sqlite') + expected = {str(i): i for i in range(1000)} + actual = {key: value for (key, value) in d.items()} + assert expected == actual, [expected, actual] From 07b18d7e076c7a683ca57cd601cd9660610464c1 Mon Sep 17 00:00:00 2001 From: Rolf Campbell Date: Sun, 29 Dec 2019 11:32:36 -0500 Subject: [PATCH 6/9] Avoid newer dictionary construction style to make it work in Python 2.6. --- tests/test_autocommit.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/test_autocommit.py b/tests/test_autocommit.py index ac17607..e666fe5 100644 --- a/tests/test_autocommit.py +++ b/tests/test_autocommit.py @@ -7,6 +7,5 @@ def test(): # Now, let's check if it actually worked. import sqlitedict d = sqlitedict.SqliteDict('tests/db/autocommit.sqlite') - expected = {str(i): i for i in range(1000)} - actual = {key: value for (key, value) in d.items()} - assert expected == actual, [expected, actual] + for i in range(1000): + assert d[i] == i, [d[i], i] From 3fce2afe3c21cf95fe320d9a4296de3190d1b608 Mon Sep 17 00:00:00 2001 From: endlisnis Date: Mon, 30 Dec 2019 16:59:31 -0500 Subject: [PATCH 7/9] Remove parentheses from assert Co-Authored-By: Michael Penkov --- tests/test_autocommit.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_autocommit.py b/tests/test_autocommit.py index e666fe5..fc21d59 100644 --- a/tests/test_autocommit.py +++ b/tests/test_autocommit.py @@ -2,7 +2,7 @@ def test(): "Verify autocommit just before program exits." - assert(0 == os.system('PYTHONPATH=. %s tests/autocommit.py' % sys.executable)) + assert os.system('PYTHONPATH=. %s tests/autocommit.py' % sys.executable) == 0 # The above script relies on the autocommit feature working correctly. # Now, let's check if it actually worked. import sqlitedict From f9a9f2ada738e62e0f80d00dd734aa04b53ee1ff Mon Sep 17 00:00:00 2001 From: endlisnis Date: Mon, 30 Dec 2019 17:00:53 -0500 Subject: [PATCH 8/9] Provide better assert message Co-Authored-By: Michael Penkov --- tests/test_autocommit.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_autocommit.py b/tests/test_autocommit.py index fc21d59..52fd428 100644 --- a/tests/test_autocommit.py +++ b/tests/test_autocommit.py @@ -8,4 +8,4 @@ def test(): import sqlitedict d = sqlitedict.SqliteDict('tests/db/autocommit.sqlite') for i in range(1000): - assert d[i] == i, [d[i], i] + assert d[i] == i, "actual: %d expected: %d" % (d[i], i) From 39e2aded760389ea14bd18be88f7b13ef2ab08ad Mon Sep 17 00:00:00 2001 From: Rolf Campbell Date: Mon, 30 Dec 2019 17:02:40 -0500 Subject: [PATCH 9/9] Move import to module level. Use %s instead of %d in assert string. --- tests/test_autocommit.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_autocommit.py b/tests/test_autocommit.py index 52fd428..65c37c9 100644 --- a/tests/test_autocommit.py +++ b/tests/test_autocommit.py @@ -1,11 +1,11 @@ import sys, os +import sqlitedict def test(): "Verify autocommit just before program exits." assert os.system('PYTHONPATH=. %s tests/autocommit.py' % sys.executable) == 0 # The above script relies on the autocommit feature working correctly. # Now, let's check if it actually worked. - import sqlitedict d = sqlitedict.SqliteDict('tests/db/autocommit.sqlite') for i in range(1000): - assert d[i] == i, "actual: %d expected: %d" % (d[i], i) + assert d[i] == i, "actual: %s expected: %s" % (d[i], i)