Permalink
Browse files

Better implementation for test_resource ids

  • Loading branch information...
1 parent 7979e88 commit 5c3695fac3b90f4aba88fcc53825f187b0d8eacf @sebaacuna sebaacuna committed Mar 9, 2012
Showing with 20 additions and 1 deletion.
  1. +20 −1 tastytools/test/resources.py
@@ -170,12 +170,29 @@ def save_test_obj(self, model):
raise ConnectionDoesNotExist("Tried: %s" % ', '.join(databases))
+ def get_model_cache(self):
+ if not hasattr(self.resource, "_models"):
+ self.resource._models = {}
+ return self.resource._models
+
+ def get_cached_model(self, id):
+ return self.get_model_cache().get(id, None)
+
+ def set_cached_model(self, id, model):
+ if id is not None:
+ self.get_model_cache()[id] = model
+
def create_test_model(self, data=False, force=False, id=None, *args,
**kwargs):
'''Creates a test model (or object asociated with the resource and
returns it
'''
+ cached_model = self.get_cached_model(id)
+ if cached_model is not None:
+ cached_model.save()
+ return cached_model
+
force = force or {}
data = data or self.sample_data(related=Related.Model, force=force,
@@ -247,6 +264,7 @@ def create_test_model(self, data=False, force=False, id=None, *args,
getattr(model, m2m_field).add(value)
data.set_related(model)
+ self.set_cached_model(id, model)
return model
#@property
@@ -255,7 +273,8 @@ def sample_data(self, related=Related.Model, force=False, id=None):
interacting with the resource
'''
- data = TestData(self.api, force, related, id=id)
+
+ data = TestData(self.api, force, related)
return self.get_data(data)
def get_data(self, data):

0 comments on commit 5c3695f

Please sign in to comment.