forked from p15670423/monkey
Added documentation
This commit is contained in:
parent
bf3b2df253
commit
29b7bb1adf
|
@ -2,6 +2,15 @@ from mongoengine import Document, DateTimeField
|
||||||
|
|
||||||
|
|
||||||
class MonkeyTtl(Document):
|
class MonkeyTtl(Document):
|
||||||
|
"""
|
||||||
|
This model represents the monkey's TTL, and is referenced by the main Monkey document.
|
||||||
|
See https://docs.mongodb.com/manual/tutorial/expire-data/ and
|
||||||
|
https://stackoverflow.com/questions/55994379/mongodb-ttl-index-doesnt-delete-expired-documents/56021663#56021663
|
||||||
|
for more information about how TTL indexing works.
|
||||||
|
|
||||||
|
When initializing this object, do it like so:
|
||||||
|
t = MonkeyTtl(expire_at=datetime.utcnow() + timedelta(seconds=XXX))
|
||||||
|
"""
|
||||||
meta = {
|
meta = {
|
||||||
'indexes': [
|
'indexes': [
|
||||||
{
|
{
|
||||||
|
|
|
@ -13,6 +13,7 @@ from monkey_ttl import MonkeyTtl
|
||||||
|
|
||||||
class TestMonkey(TestCase):
|
class TestMonkey(TestCase):
|
||||||
def test_is_dead(self):
|
def test_is_dead(self):
|
||||||
|
# Arrange
|
||||||
alive_monkey_ttl = MonkeyTtl(expire_at=datetime.now() + timedelta(seconds=30))
|
alive_monkey_ttl = MonkeyTtl(expire_at=datetime.now() + timedelta(seconds=30))
|
||||||
alive_monkey_ttl.save()
|
alive_monkey_ttl.save()
|
||||||
alive_monkey = Monkey(
|
alive_monkey = Monkey(
|
||||||
|
@ -21,24 +22,30 @@ class TestMonkey(TestCase):
|
||||||
ttl_ref=alive_monkey_ttl.id)
|
ttl_ref=alive_monkey_ttl.id)
|
||||||
alive_monkey.save()
|
alive_monkey.save()
|
||||||
|
|
||||||
|
# MIA stands for Missing In Action
|
||||||
mia_monkey_ttl = MonkeyTtl(expire_at=datetime.now() + timedelta(seconds=30))
|
mia_monkey_ttl = MonkeyTtl(expire_at=datetime.now() + timedelta(seconds=30))
|
||||||
mia_monkey_ttl.save()
|
mia_monkey_ttl.save()
|
||||||
mia_monkey = Monkey(guid=str(uuid.uuid4()), dead=False, ttl_ref=mia_monkey_ttl)
|
mia_monkey = Monkey(guid=str(uuid.uuid4()), dead=False, ttl_ref=mia_monkey_ttl)
|
||||||
mia_monkey.save()
|
mia_monkey.save()
|
||||||
# Emulate timeout
|
# Emulate timeout - ttl is manually deleted here, since we're using mongomock and not a real mongo instance.
|
||||||
sleep(1)
|
sleep(1)
|
||||||
mia_monkey_ttl.delete()
|
mia_monkey_ttl.delete()
|
||||||
|
|
||||||
dead_monkey = Monkey(guid=str(uuid.uuid4()), dead=True)
|
dead_monkey = Monkey(guid=str(uuid.uuid4()), dead=True)
|
||||||
dead_monkey.save()
|
dead_monkey.save()
|
||||||
|
|
||||||
|
# act + assert
|
||||||
self.assertTrue(dead_monkey.is_dead())
|
self.assertTrue(dead_monkey.is_dead())
|
||||||
self.assertTrue(mia_monkey.is_dead())
|
self.assertTrue(mia_monkey.is_dead())
|
||||||
self.assertFalse(alive_monkey.is_dead())
|
self.assertFalse(alive_monkey.is_dead())
|
||||||
|
|
||||||
def test_get_single_monkey_by_id(self):
|
def test_get_single_monkey_by_id(self):
|
||||||
|
# Arrange
|
||||||
a_monkey = Monkey(guid=str(uuid.uuid4()))
|
a_monkey = Monkey(guid=str(uuid.uuid4()))
|
||||||
a_monkey.save()
|
a_monkey.save()
|
||||||
|
|
||||||
|
# Act + assert
|
||||||
|
# Find the existing one
|
||||||
self.assertIsNotNone(Monkey.get_single_monkey_by_id(a_monkey.id))
|
self.assertIsNotNone(Monkey.get_single_monkey_by_id(a_monkey.id))
|
||||||
|
# Raise on non-existent monkey
|
||||||
self.assertRaises(MonkeyNotFoundError, Monkey.get_single_monkey_by_id, "abcdefabcdefabcdefabcdef")
|
self.assertRaises(MonkeyNotFoundError, Monkey.get_single_monkey_by_id, "abcdefabcdefabcdefabcdef")
|
||||||
|
|
Loading…
Reference in New Issue