次のモデルはndb.Modelに基づいています。モデルdate_of_birthプロパティを検証しようとしています。プロパティの検証に失敗しました。Googleデータストア
from google.appengine.ext import ndb
class CFCSocialUser(ndb.Model):
def clean_dob(value):
if value.year < 1900:
raise Exception("Year cannot be less than 1900")
username = ndb.StringProperty()
userid = ndb.IntegerProperty()
email = ndb.StringProperty()
date_of_birth = ndb.DateProperty(validator=clean_dob)
@staticmethod
def create_new_user(name, email, date_of_birth):
app = CFCSocialUser(username=name,
email=email,
date_of_birth=date_of_birth)
app.put()
以下は、検証をテストするはずのユニットテストです。
from datetime import date
from unittest import TestCase
from google.appengine.ext import ndb
from google.appengine.ext import testbed
from mainsite.rainbow.models.CFCSocialUser import CFCSocialUser
def create_user_before_1900():
user = CFCSocialUser.create_new_user(name="Vinay Joseph",
email="[email protected]",
date_of_birth=date(1899, 3, 11))
class TestCFCSocialUser(TestCase):
def setUp(self):
self.testbed = testbed.Testbed()
self.testbed.activate()
self.testbed.init_datastore_v3_stub()
ndb.get_context().clear_cache()
def tearDown(self):
self.testbed.deactivate()
def test_validation(self):
self.assertRaises(Exception, create_user_before_1900())
私は
======================================================================
ERROR: test_validation (tests.test_CFCSocialUser.TestCFCSocialUser)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/vinay/App-Engine/Rainbow/cfc-social-media-website/tests/test_CFCSocialUser.py", line 26, in test_validation
self.assertRaises(Exception, create_user_before_1900())
File "/Users/vinay/App-Engine/Rainbow/cfc-social-media-website/tests/test_CFCSocialUser.py", line 12, in create_user_before_1900
date_of_birth=date(1899, 3, 11))
File "/Users/vinay/App-Engine/Rainbow/cfc-social-media-website/mainsite/rainbow/models/CFCSocialUser.py", line 19, in create_new_user
date_of_birth=date_of_birth)
File "/Users/vinay/google-cloud-sdk/platform/google_appengine/google/appengine/ext/ndb/model.py", line 2947, in __init__
self._set_attributes(kwds)
File "/Users/vinay/google-cloud-sdk/platform/google_appengine/google/appengine/ext/ndb/model.py", line 2993, in _set_attributes
prop._set_value(self, value)
File "/Users/vinay/google-cloud-sdk/platform/google_appengine/google/appengine/ext/ndb/model.py", line 1145, in _set_value
value = self._do_validate(value)
File "/Users/vinay/google-cloud-sdk/platform/google_appengine/google/appengine/ext/ndb/model.py", line 1094, in _do_validate
newvalue = self._validator(self, value)
TypeError: clean_dob() takes exactly 1 argument (2 given)