Permalink
Browse files

Added the beginnings of the counter class.

  • Loading branch information...
1 parent 19f0b6c commit d90e82a8c07631386d35fa7309c0d816eb5a109c @SeanOC SeanOC committed Feb 20, 2012
Showing with 75 additions and 2 deletions.
  1. +29 −2 albertson/base.py
  2. +46 −0 tests/base.py
View
@@ -3,6 +3,8 @@
import boto
from boto.dynamodb.exceptions import DynamoDBKeyNotFoundError
+ISO_FORMAT = '%Y-%m-%dT%H:%M:%S'
+
class CounterPool(object):
'''
@@ -165,15 +167,40 @@ def get_item(self, hash_key, start=0):
return item
-
- def get_counter(self, name):
+ def get_counter(self, name, start=0):
'''
Gets the DynamoDB item behind a counter and ties it to a Counter
instace.
'''
+ item = self.get_item(hash_key=name, start=start)
+ counter = Counter(dynamo_item=item, pool=self)
+
+ return counter
class Counter(object):
'''
Interface to individual counters.
'''
+
+ def __init__(self, dynamo_item, pool):
+ self.dynamo_item = dynamo_item
+ self.pool = pool
+
+ @property
+ def name(self):
+ return self.dynamo_item['counter_name']
+
+ @property
+ def count(self):
+ return self.dynamo_item['count']
+
+ @property
+ def created_on(self):
+ return datetime.strptime(self.dynamo_item['created_on'], ISO_FORMAT)
+
+ @property
+ def modified_on(self):
+ return datetime.strptime(self.dynamo_item['modified_on'], ISO_FORMAT)
+
+
View
@@ -253,3 +253,49 @@ def test_table_caching(self):
result = pool.get_table()
self.assertEqual(expected, result)
+
+ def test_get_counter(self):
+ pool = self.get_pool()
+ name = 'test'
+
+ result = pool.get_counter(name)
+
+ self.assertEqual(pool, result.pool)
+
+ def test_counter_name(self):
+
+ pool = self.get_pool()
+ expected = 'test'
+
+ result = pool.get_counter(expected).name
+
+ self.assertEquals(expected, result)
+
+ def test_counter_count(self):
+ pool = self.get_pool()
+ name = 'test'
+
+ expected = 0
+ result = pool.get_counter(name).count
+
+ self.assertEquals(expected, result)
+
+ def test_counter_created_on(self):
+ pool = self.get_pool()
+ name = 'test'
+ counter = pool.get_counter(name)
+
+ expected = counter.dynamo_item['created_on']
+ result = counter.created_on.isoformat()
+
+ self.assertEquals(expected, result)
+
+ def test_counter_modified_on(self):
+ pool = self.get_pool()
+ name = 'test'
+ counter = pool.get_counter(name)
+
+ expected = counter.dynamo_item['modified_on']
+ result = counter.modified_on.isoformat()
+
+ self.assertEquals(expected, result)

0 comments on commit d90e82a

Please sign in to comment.