Permalink
Browse files

Merge pull request #510 from tpodowd/fix_latest_key

Added missing (boolean) is_latest attribute to Key object.
  • Loading branch information...
2 parents d33fc85 + cab1346 commit fd334d9f37d62f1d29c6116674e1308c11e4e25e @garnaat garnaat committed Jan 25, 2012
Showing with 55 additions and 0 deletions.
  1. +6 −0 boto/s3/key.py
  2. +49 −0 tests/s3/test_versioning.py
View
@@ -54,6 +54,7 @@ def __init__(self, bucket=None, name=None):
self.content_encoding = None
self.filename = None
self.etag = None
+ self.is_latest = False
self.last_modified = None
self.owner = None
self.storage_class = 'STANDARD'
@@ -358,6 +359,11 @@ def endElement(self, name, value, connection):
self.name = value
elif name == 'ETag':
self.etag = value
+ elif name == 'IsLatest':
+ if value == 'true':
+ self.is_latest = True
+ else:
+ self.is_latest = False
elif name == 'LastModified':
self.last_modified = value
elif name == 'Size':
@@ -143,3 +143,52 @@ def test_1_versions(self):
# now delete bucket
c.delete_bucket(bucket)
print '--- tests completed ---'
+
+ def test_latest_version(self):
+ c = S3Connection()
+ bucket_name = 'version-%d' % int(time.time())
+ bucket = c.create_bucket(bucket_name)
+
+ bucket.configure_versioning(versioning=True)
+
+ # add v1 of an object
+ key_name = "key"
+ kv1 = bucket.new_key(key_name)
+ kv1.set_contents_from_string("v1")
+
+ # read list which should contain latest v1
+ listed_kv1 = iter(bucket.get_all_versions()).next()
+ self.assertEqual(listed_kv1.name, key_name)
+ self.assertEqual(listed_kv1.version_id, kv1.version_id)
+ self.assertEqual(listed_kv1.is_latest, True)
+
+ # add v2 of the object
+ kv2 = bucket.new_key(key_name)
+ kv2.set_contents_from_string("v2")
+
+ # read 2 versions, confirm v2 is latest
+ i = iter(bucket.get_all_versions())
+ listed_kv2 = i.next()
+ listed_kv1 = i.next()
+ self.assertEqual(listed_kv2.version_id, kv2.version_id)
+ self.assertEqual(listed_kv1.version_id, kv1.version_id)
+ self.assertEqual(listed_kv2.is_latest, True)
+ self.assertEqual(listed_kv1.is_latest, False)
+
+ # delete key, which creates a delete marker as latest
+ bucket.delete_key(key_name)
+ i = iter(bucket.get_all_versions())
+ listed_kv3 = i.next()
+ listed_kv2 = i.next()
+ listed_kv1 = i.next()
+ self.assertNotEqual(listed_kv3.version_id, None)
+ self.assertEqual(listed_kv2.version_id, kv2.version_id)
+ self.assertEqual(listed_kv1.version_id, kv1.version_id)
+ self.assertEqual(listed_kv3.is_latest, True)
+ self.assertEqual(listed_kv2.is_latest, False)
+ self.assertEqual(listed_kv1.is_latest, False)
+
+ # cleanup
+ for k in bucket.list_versions():
+ bucket.delete_key(k.name, version_id=k.version_id)
+ c.delete_bucket(bucket)

0 comments on commit fd334d9

Please sign in to comment.