<s> import json <EOL> import os <EOL> from google . appengine . ext import ndb <EOL> from google . appengine . ext import testbed <EOL> import mock <EOL> import unittest2 <EOL> from oauth2client . client import Credentials <EOL> from oauth2client . client import flow_from_clientsecrets <EOL> from oauth2client . contrib . appengine import CredentialsNDBProperty <EOL> from oauth2client . contrib . appengine import FlowNDBProperty <EOL> DATA_DIR = os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT:..>' , '<STR_LIT:data>' ) <EOL> def datafile ( filename ) : <EOL> return os . path . join ( DATA_DIR , filename ) <EOL> class TestNDBModel ( ndb . Model ) : <EOL> flow = FlowNDBProperty ( ) <EOL> creds = CredentialsNDBProperty ( ) <EOL> class TestFlowNDBProperty ( unittest2 . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . testbed = testbed . Testbed ( ) <EOL> self . testbed . activate ( ) <EOL> self . testbed . init_datastore_v3_stub ( ) <EOL> self . testbed . init_memcache_stub ( ) <EOL> def tearDown ( self ) : <EOL> self . testbed . deactivate ( ) <EOL> def test_flow_get_put ( self ) : <EOL> instance = TestNDBModel ( <EOL> flow = flow_from_clientsecrets ( datafile ( '<STR_LIT>' ) , <EOL> '<STR_LIT:foo>' , redirect_uri = '<STR_LIT>' ) , <EOL> id = '<STR_LIT:foo>' <EOL> ) <EOL> instance . put ( ) <EOL> retrieved = TestNDBModel . get_by_id ( '<STR_LIT:foo>' ) <EOL> self . assertEqual ( '<STR_LIT>' , retrieved . flow . client_id ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_validate_success ( self , mock_logger ) : <EOL> flow_prop = TestNDBModel . flow <EOL> flow_val = flow_from_clientsecrets ( datafile ( '<STR_LIT>' ) , <EOL> '<STR_LIT:foo>' , redirect_uri = '<STR_LIT>' ) <EOL> flow_prop . _validate ( flow_val ) <EOL> mock_logger . info . assert_called_once_with ( '<STR_LIT>' , <EOL> type ( flow_val ) ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_validate_none ( self , mock_logger ) : <EOL> flow_prop = TestNDBModel . flow <EOL> flow_val = None <EOL> flow_prop . _validate ( flow_val ) <EOL> mock_logger . info . assert_called_once_with ( '<STR_LIT>' , <EOL> type ( flow_val ) ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_validate_bad_type ( self , mock_logger ) : <EOL> flow_prop = TestNDBModel . flow <EOL> flow_val = object ( ) <EOL> with self . assertRaises ( TypeError ) : <EOL> flow_prop . _validate ( flow_val ) <EOL> mock_logger . info . assert_called_once_with ( '<STR_LIT>' , <EOL> type ( flow_val ) ) <EOL> class TestCredentialsNDBProperty ( unittest2 . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . testbed = testbed . Testbed ( ) <EOL> self . testbed . activate ( ) <EOL> self . testbed . init_datastore_v3_stub ( ) <EOL> self . testbed . init_memcache_stub ( ) <EOL> def tearDown ( self ) : <EOL> self . testbed . deactivate ( ) <EOL> def test_valid_creds_get_put ( self ) : <EOL> creds = Credentials ( ) <EOL> instance = TestNDBModel ( creds = creds , id = '<STR_LIT:bar>' ) <EOL> instance . put ( ) <EOL> retrieved = TestNDBModel . get_by_id ( '<STR_LIT:bar>' ) <EOL> self . assertIsInstance ( retrieved . creds , Credentials ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_validate_success ( self , mock_logger ) : <EOL> creds_prop = TestNDBModel . creds <EOL> creds_val = Credentials ( ) <EOL> creds_prop . _validate ( creds_val ) <EOL> mock_logger . info . assert_called_once_with ( '<STR_LIT>' , <EOL> type ( creds_val ) ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_validate_none ( self , mock_logger ) : <EOL> creds_prop = TestNDBModel . creds <EOL> creds_val = None <EOL> creds_prop . _validate ( creds_val ) <EOL> mock_logger . info . assert_called_once_with ( '<STR_LIT>' , <EOL> type ( creds_val ) ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_validate_bad_type ( self , mock_logger ) : <EOL> creds_prop = TestNDBModel . creds <EOL> creds_val = object ( ) <EOL> with self . assertRaises ( TypeError ) : <EOL> creds_prop . _validate ( creds_val ) <EOL> mock_logger . info . assert_called_once_with ( '<STR_LIT>' , <EOL> type ( creds_val ) ) <EOL> def test__to_base_type_valid_creds ( self ) : <EOL> creds_prop = TestNDBModel . creds <EOL> creds = Credentials ( ) <EOL> creds_json = json . loads ( creds_prop . _to_base_type ( creds ) ) <EOL> self . assertDictEqual ( creds_json , { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : None , <EOL> } ) <EOL> def test__to_base_type_null_creds ( self ) : <EOL> creds_prop = TestNDBModel . creds <EOL> self . assertEqual ( creds_prop . _to_base_type ( None ) , '<STR_LIT>' ) <EOL> def test__from_base_type_valid_creds ( self ) : <EOL> creds_prop = TestNDBModel . creds <EOL> creds_json = json . dumps ( { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : None , <EOL> } ) <EOL> creds = creds_prop . _from_base_type ( creds_json ) <EOL> self . assertIsInstance ( creds , Credentials ) <EOL> def test__from_base_type_false_value ( self ) : <EOL> creds_prop = TestNDBModel . creds <EOL> self . assertIsNone ( creds_prop . _from_base_type ( '<STR_LIT>' ) ) <EOL> self . assertIsNone ( creds_prop . _from_base_type ( False ) ) <EOL> self . assertIsNone ( creds_prop . _from_base_type ( None ) ) <EOL> self . assertIsNone ( creds_prop . _from_base_type ( [ ] ) ) <EOL> self . assertIsNone ( creds_prop . _from_base_type ( { } ) ) <EOL> def test__from_base_type_bad_json ( self ) : <EOL> creds_prop = TestNDBModel . creds <EOL> creds_json = '<STR_LIT>' <EOL> self . assertIsNone ( creds_prop . _from_base_type ( creds_json ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest2 . main ( ) </s>
