Permalink
Browse files

Adding a default __init__ method to Models

  • Loading branch information...
stevearc committed Mar 1, 2014
1 parent 1d31797 commit 40068c27d49657d02d0de710ef5cad68fca6847c
Showing with 23 additions and 37 deletions.
  1. +10 −0 flywheel/models.py
  2. +2 −2 flywheel/tests.py
  3. +4 −6 tests/test_fields.py
  4. +4 −11 tests/test_models.py
  5. +1 −15 tests/test_queries.py
  6. +2 −3 tests/test_scan.py
@@ -122,6 +122,16 @@ class Model(object):
_persisted = False
_loading = False
def __init__(self, *args, **kwargs):
if len(args) > 2 or len(args) > 1 and self.meta_.range_key is None:
raise TypeError("Too many positional arguments!")
if len(args) > 0:
setattr(self, self.meta_.hash_key.name, args[0])
if len(args) > 1:
setattr(self, self.meta_.range_key.name, args[1])
for key, value in kwargs.iteritems():
setattr(self, key, value)
def refresh(self, consistent=False):
""" Overwrite model data with freshest from database """
if self.__engine__ is None:
@@ -19,7 +19,7 @@
import unittest
DYNAMO_LOCAL = 'https://s3-us-west-2.amazonaws.com/dynamodb-local/dynamodb_local_2013-12-12.tar.gz'
DYNAMO_LOCAL = 'https://s3-us-west-2.amazonaws.com/dynamodb-local/dynamodb_local_2014-01-08.tar.gz'
class DynamoLocalPlugin(nose.plugins.Plugin):
@@ -86,7 +86,7 @@ def dynamo(self):
lib_path = os.path.join(self.path, 'DynamoDBLocal_lib')
jar_path = os.path.join(self.path, 'DynamoDBLocal.jar')
cmd = [self.java, '-Djava.library.path=' + lib_path, '-jar',
jar_path, '--port', str(self.port)]
jar_path, '--port', str(self.port), '--inMemory']
self._dynamo_local = subprocess.Popen(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
@@ -60,9 +60,8 @@ class Widget(Model):
merge=lambda *a: '/'.join([x or '' for x in a]))
def __init__(self, **kwargs):
self.string = 'abc'
for key, val in kwargs.iteritems():
setattr(self, key, val)
kwargs.setdefault('string', 'abc')
super(Widget, self).__init__(**kwargs)
class TestCreateFields(unittest.TestCase):
@@ -559,9 +558,8 @@ class PrimitiveWidget(Model):
price = Field(data_type=Decimal)
def __init__(self, **kwargs):
self.string = 'abc'
for key, val in kwargs.iteritems():
setattr(self, key, val)
kwargs.setdefault('string', 'abc')
super(PrimitiveWidget, self).__init__(**kwargs)
class TestPrimitiveDataTypes(DynamoSystemTest):
@@ -32,9 +32,7 @@ class Widget(Model):
ts = Field(data_type=NUMBER)
def __init__(self, userid, id, ts):
self.userid = userid
self.id = id
self.ts = ts
super(Widget, self).__init__(userid, id=id, ts=ts)
class Post(Model):
@@ -62,11 +60,8 @@ class Post(Model):
merge=lambda t, a: t.split() + a.split(), coerce=True)
def __init__(self, userid, id, ts, text='foo', about='bar'):
self.userid = userid
self.id = id
self.ts = ts
self.text = text
self.about = about
super(Post, self).__init__(userid=userid, id=id, ts=ts, text=text,
about=about)
class TestComposite(DynamoSystemTest):
@@ -153,9 +148,7 @@ class Article(Model):
views = Field(data_type=int)
def __init__(self, title='Drugs win Drug War', **kwargs):
self.title = title
for key, val in kwargs.iteritems():
setattr(self, key, val)
super(Article, self).__init__(title, **kwargs)
class TestModelMutation(DynamoSystemTest):
@@ -17,10 +17,6 @@ class User(Model):
score = Field(data_type=NUMBER, index='score-index', default=0)
str_set = Field(data_type=STRING_SET)
def __init__(self, **kwargs):
for key, val in kwargs.iteritems():
setattr(self, key, val)
def score_merge(ts, upvotes):
""" Merge the ts and upvotes """
@@ -45,10 +41,6 @@ class Post(Model):
ts = Field(data_type=NUMBER, default=0)
upvotes = Field(data_type=NUMBER, default=0)
def __init__(self, **kwargs):
for key, val in kwargs.iteritems():
setattr(self, key, val)
class TestQueries(DynamoSystemTest):
@@ -397,12 +389,6 @@ class Widget(Model):
alpha = Field(data_type=int, index='alpha-index')
beta = Field(data_type=int, index='beta-index')
def __init__(self, id=None, name=None, **kwargs):
self.id = id
self.name = name
for key, val in kwargs.iteritems():
setattr(self, key, val)
class TestOrder(DynamoSystemTest):
@@ -446,7 +432,7 @@ class SingleKeyModel(Model):
id = Field(hash_key=True)
def __init__(self, id='a'):
self.id = id
super(SingleKeyModel, self).__init__(id)
class TestEngine(DynamoSystemTest):
@@ -301,9 +301,8 @@ class Widget(Model):
price = Field(data_type=Decimal)
def __init__(self, **kwargs):
self.id = 'abc'
for key, val in kwargs.iteritems():
setattr(self, key, val)
kwargs.setdefault('id', 'abc')
super(Widget, self).__init__(**kwargs)
class TestFilterFields(DynamoSystemTest):

0 comments on commit 40068c2

Please sign in to comment.