Last active
August 29, 2015 14:16
-
-
Save hello-josh/4a6884f8884e5c8704f3 to your computer and use it in GitHub Desktop.
GAE Datastore Fixtures idea
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
from nosegae.fixture import NdbFixture | |
import models | |
class MyGreetings(NdbFixture): | |
"""Holds all data fixtures to be managed by the library | |
Each inner class's attributes are used as `kwargs` | |
in `model`'s constructor like similar to | |
>>> for k, attrs in MyGreetings.inner_classes_as_dict() | |
... entity = MyGreetings.model(**attrs) | |
... MyGreetings.entities[k] = entity | |
MyGreetings.entities is now a dict similar to: | |
{'GreetingOne': Greeting(content="FIRST!", date=datetime.datetime(2015, 1, 1, 10, 30, 00)), | |
'GreetingTwo': ...} | |
""" | |
model = models.Greeting # Model name for all data objects | |
class GreetingOne(object): | |
content = "FIRST!" | |
date = datetime.datetime(2015, 1, 1, 10, 30, 00) | |
class GreetingTwo(object): | |
content = "I don't know how I ever lived without you!" | |
date = datetime.datetime(2015, 1, 1, 16, 45, 00) | |
class GreetingThree(object): | |
content = "Hi guys, thanks for the site!" | |
date = datetime.datetime(2015, 1, 2, 22, 00, 00) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import models | |
def greetings_since_date(start_date): | |
"""Gets a list of greetings since `start_date` | |
Args | |
:param datetime.datetime start_date: Filter for the oldest Greeting date | |
Returns | |
:return: A list of Greetings since the datetime given | |
:rtype: list of Greeting | |
""" | |
return list(models.Greeting.query(models.Greeting.date >= start_date)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from google.appengine.ext import ndb | |
class Greeting(ndb.Model): | |
"""Models an individual Guestbook entry with content and date.""" | |
content = ndb.StringProperty() | |
date = ndb.DateTimeProperty() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
import greetinglib | |
import fixtures | |
from nosegae.fixture import data | |
# data() creates the entities and passes them as `kwargs` into | |
# the wrapped function and then cleans them up after execution. | |
# similar to: | |
# >>> def data(func): | |
# ... def wrapper(fixture): | |
# ... fixture_instance = fixture() | |
# ... wrapped(**fixture_instance.get_entities()) | |
# ... fixture_instance.cleanup() | |
# ... return wrapper | |
@data(fixtures.MyGreetings) | |
def test_greetings_since_date(**entities): | |
# entities is fixtures.MyGreetings.entities | |
start_date = datetime.datetime(2015, 1, 1, 11, 00, 00) | |
greetings = greetinglib.greetings_since_date(start_date) | |
# assert the list of Greetings is the same length as all data fixtures that have | |
# a date >= start_date | |
assert len(greetings) == len([v for v in entities.values() if v.date >= start_date]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment