Skip to content

Commit

Permalink
Merge pull request #26 from Miserlou/develop
Browse files Browse the repository at this point in the history
fixed cached .delete() bug, testing now with 2.7, 3.5, 3.6, 3.7
  • Loading branch information
bendog authored Nov 18, 2019
2 parents 0a4954a + 03e5f95 commit fd65244
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 17 deletions.
6 changes: 4 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
language: python
python:
- "2.7"
# - "3.5"
- "3.5"
- "3.6"
- "3.7"
# command to install dependencies
cache:
- pip
Expand All @@ -11,7 +13,7 @@ install:
# command to run tests
env:
- AWS_ACCESS_KEY_ID=foo AWS_SECRET_ACCESS_KEY=bar TESTCASE=tests/tests.py
script:
script:
- nosetests $TESTCASE --with-coverage --cover-package=nodb --with-timer
# - coverage combine --append
after_success:
Expand Down
18 changes: 16 additions & 2 deletions nodb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ def load(self, index, metainfo=False, default=None):

# If cache enabled, check local filestore for bytes
cache_hit = False
cache_path = None
if self.cache:
base_cache_path = self._get_base_cache_path()
cache_path = os.path.join(base_cache_path, real_index)
Expand All @@ -137,13 +138,16 @@ def load(self, index, metainfo=False, default=None):
return default

# Store the cache result
if self.cache:
if self.cache and cache_path:

if not os.path.exists(cache_path):
open(cache_path, 'w+').close()

with open(cache_path, "wb") as in_file:
in_file.write(serialized.encode(self.encoding))
if type(serialized) is bytes:
in_file.write(serialized)
else:
in_file.write(serialized.encode(self.encoding))

logging.debug("Wrote to cache file: " + cache_path)

Expand All @@ -167,6 +171,14 @@ def delete(self, index):
# First, calculate the real index
real_index = self._format_index_value(index)

# handle delete from cache
if self.cache:
base_cache_path = self._get_base_cache_path()
cache_path = os.path.join(base_cache_path, real_index)
# Cache hit!
if os.path.isfile(cache_path):
os.remove(cache_path)

# Next, get the bytes (if any)
serialized_s3 = self.s3.Object(self.bucket, self.prefix + real_index)
result = serialized_s3.delete()
Expand Down Expand Up @@ -235,6 +247,8 @@ def _deserialize(self, serialized):
"""

obj = None
if type(serialized) == bytes:
serialized = serialized.decode()
deserialized = json.loads(serialized)
return_me = {}

Expand Down
11 changes: 2 additions & 9 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
appdirs>=1.4.3
boto3>=1.4.4
botocore>=1.5.38
docutils>=0.13.1
# @bendog not sure why the following are needed
appdirs>=1.4.3
funcsigs>=1.0.2
futures>=3.0.5
jmespath>=0.9.2
packaging>=16.8
pbr>=2.0.0
pyparsing>=2.2.0
python-dateutil==2.6.0
s3transfer>=0.1.10
six>=1.10.0
6 changes: 3 additions & 3 deletions test_requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
coverage==4.3.4
coverage>=4.3.4
coveralls>=1.1
moto==1.3.7
nose==1.3.7
moto>=1.3.7
nose>=1.3.7
nose-timer>=0.6.0
14 changes: 13 additions & 1 deletion tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ def test_nodb_aws_profile_name(self):

@moto.mock_s3
def test_nodb_cache(self):
nodb = NoDB('dummy')
bucket_name = 'dummy'
nodb = NoDB(bucket_name)
self._create_mock_bucket(bucket_name)
nodb.index = "Name"
nodb.cache = True

Expand All @@ -128,6 +130,16 @@ def test_nodb_cache(self):
self.assertEqual(loaded, jeff)
loaded = nodb.load("Jeff", default="Booty")
self.assertEqual(loaded, jeff)
# test the cached item is deleted
nodb.delete('Jeff')
loaded = nodb.load("Jeff")
self.assertIsNone(loaded)
# test read from bucket when cache enabled
# remove cached file
nodb.save(jeff)
if os.path.isfile(cache_path):
os.remove(cache_path)
nodb.load('Jeff')

bcp = nodb._get_base_cache_path()

Expand Down

0 comments on commit fd65244

Please sign in to comment.