2016-04-28 15 views
1

次のモデルは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) 

答えて

0

clean_dob()は、インスタンスのメンバ関数であり、第一引数としてselfを取得する必要があり、私のユニットテストを実行しようとすると、私は次のエラーを取得します。

同様:

class MyModel(ndb.Model): 
    def clean_dob(self, value): 
    pass 

それとも、静的メソッドとして定義することができます

class MyModel(ndb.Model): 
    @staticmethod 
    def clean_dob(value): 
    pass 

またはクラスの外に移動します。

def clean_dob(value): 
    pass 

class MyModel(ndb.Model): 
    # ... 
0

App Engineのdocsはと言いますバリデータ関数は、2つの引数:propとで呼び出されます。、あなたがあなたの関数を変更する必要があるので:それは、スタンドアロン関数としてクラス本体の外側に配置されなければならないので、

def clean_dob(prop, value): 
    if value.year < 1900: 
     raise Exception("Year cannot be less than 1900") 

小道具引数がないモデルクラス、検証されるプロパティのインスタンスです。それをクラスの中に残すことは、それをモデルのインスタンスメソッドと見なす可能性があるコードの読者には混乱させます。

関連する問題