diff --git a/.travis.yml b/.travis.yml index 6cd2081..de45385 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,9 @@ language: python python: - "2.7" -# - "3.5" + - "3.5" + - "3.6" + - "3.7" # command to install dependencies cache: - pip @@ -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: diff --git a/nodb/__init__.py b/nodb/__init__.py index 8380687..3735e5c 100644 --- a/nodb/__init__.py +++ b/nodb/__init__.py @@ -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) @@ -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) @@ -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() @@ -235,6 +247,8 @@ def _deserialize(self, serialized): """ obj = None + if type(serialized) == bytes: + serialized = serialized.decode() deserialized = json.loads(serialized) return_me = {} diff --git a/requirements.txt b/requirements.txt index ab1bb72..10b770f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 diff --git a/test_requirements.txt b/test_requirements.txt index 69740ab..4a94c17 100644 --- a/test_requirements.txt +++ b/test_requirements.txt @@ -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 diff --git a/tests/tests.py b/tests/tests.py index f3c5ea5..9905f87 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -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 @@ -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()