Skip to content

Instantly share code, notes, and snippets.

@divad12
Created March 5, 2014 04:24
Show Gist options
  • Save divad12/5fd40c6278548fd43ac5 to your computer and use it in GitHub Desktop.
Save divad12/5fd40c6278548fd43ac5 to your computer and use it in GitHub Desktop.
import rmc.models as m
import rmc.test.lib as testlib
def gen_professor(**kwargs):
attrs = dict({
'id': 'meredith_swift', # I'm a cat meow meow!!!!
'first_name': 'Meredith',
'last_name': 'Swift',
}, **kwargs)
return m.Professor(**attrs)
class ProfessorTest(testlib.ModelTestCase):
def test_save_ratings(self):
prof_meredith = gen_professor()
# Taylor approves of her new roommate, professor Meredith.
prof_meredith.passion.update_aggregate_after_replacement(None, 1)
prof_meredith.save()
# But Meredith exhibits bad posture, so Taylor retracts her old rating.
prof_meredith.passion.update_aggregate_after_replacement(1, None)
prof_meredith.save()
# Now we are internally in an inconsistent state, but the fault has not
# propagated to the user as an error yet.
# Meredith learns how to moves her ears on command and Taylor is happy.
prof_meredith.passion.update_aggregate_after_replacement(None, 1)
prof_meredith.save()
# But Ed is dejected after Meredith gives him a look of disapproval.
prof_meredith.passion.update_aggregate_after_replacement(None, 0)
prof_meredith.save()
# Meredith meowed on about how Taylor can't sing, which Taylor thought
# was mean, so she changed her old approval to a disapproval.
prof_meredith.passion.update_aggregate_after_replacement(1, 0)
# Now the fault gets manifested into an exception.
prof_meredith.save()
# At this point we get:
# ValidationError: ValidationError(rating.Float value is too small: ['passion'])
@divad12
Copy link
Author

divad12 commented Mar 5, 2014

Running this will produce the following output:

[ rmc ] (master) PYTHONPATH=.. nosetests models/professor_test.py
F
======================================================================
FAIL: test_save_ratings (rmc.models.professor_test.ProfessorTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/david/redccnt/code/rmc/models/professor_test.py", line 43, in test_save_ratings
    prof_meredith.save()
  File "/Users/david/redccnt/code/rmc/models/professor.py", line 73, in save
    super(Professor, self).save(*args, **kwargs)
  File "/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mongoengine/document.py", line 208, in save
    self.validate()
  File "/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mongoengine/base.py", line 1025, in validate
    raise ValidationError('ValidationError', errors=errors)
ValidationError: ValidationError(rating.Float value is too small: ['passion'])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment