1616
1717
1818class TestClient (unittest2 .TestCase ):
19+ PROJECT = 'PROJECT'
1920
2021 def _getTargetClass (self ):
2122 from gcloud .search .client import Client
@@ -26,14 +27,111 @@ def _makeOne(self, *args, **kw):
2627
2728 def test_ctor (self ):
2829 from gcloud .search .connection import Connection
29- PROJECT = 'PROJECT'
3030 creds = _Credentials ()
3131 http = object ()
32- client = self ._makeOne (project = PROJECT , credentials = creds , http = http )
32+ client = self ._makeOne (
33+ project = self .PROJECT , credentials = creds , http = http )
3334 self .assertTrue (isinstance (client .connection , Connection ))
3435 self .assertTrue (client .connection .credentials is creds )
3536 self .assertTrue (client .connection .http is http )
3637
38+ def test_list_indexes_defaults (self ):
39+ from gcloud .search .index import Index
40+ INDEX_1 = 'index-one'
41+ INDEX_2 = 'index-two'
42+ PATH = 'projects/%s/indexes' % self .PROJECT
43+ TOKEN = 'TOKEN'
44+ DATA = {
45+ 'nextPageToken' : TOKEN ,
46+ 'indexes' : [
47+ {'project' : self .PROJECT ,
48+ 'indexId' : INDEX_1 },
49+ {'project' : self .PROJECT ,
50+ 'indexId' : INDEX_2 },
51+ ]
52+ }
53+ creds = _Credentials ()
54+ client = self ._makeOne (self .PROJECT , creds )
55+ conn = client .connection = _Connection (DATA )
56+
57+ zones , token = client .list_indexes ()
58+
59+ self .assertEqual (len (zones ), len (DATA ['indexes' ]))
60+ for found , expected in zip (zones , DATA ['indexes' ]):
61+ self .assertTrue (isinstance (found , Index ))
62+ self .assertEqual (found .name , expected ['indexId' ])
63+ self .assertEqual (found .text_fields , None )
64+ self .assertEqual (found .atom_fields , None )
65+ self .assertEqual (found .html_fields , None )
66+ self .assertEqual (found .date_fields , None )
67+ self .assertEqual (found .number_fields , None )
68+ self .assertEqual (found .geo_fields , None )
69+ self .assertEqual (token , TOKEN )
70+
71+ self .assertEqual (len (conn ._requested ), 1 )
72+ req = conn ._requested [0 ]
73+ self .assertEqual (req ['method' ], 'GET' )
74+ self .assertEqual (req ['path' ], '/%s' % PATH )
75+
76+ def test_list_indexes_explicit (self ):
77+ from gcloud .search .index import Index
78+ INDEX_1 = 'index-one'
79+ INDEX_2 = 'index-two'
80+ PATH = 'projects/%s/indexes' % self .PROJECT
81+ TOKEN = 'TOKEN'
82+ DATA = {
83+ 'indexes' : [
84+ {'project' : self .PROJECT ,
85+ 'indexId' : INDEX_1 ,
86+ 'indexedField' : {'textFields' : ['text-1' ]}},
87+ {'project' : self .PROJECT ,
88+ 'indexId' : INDEX_2 ,
89+ 'indexedField' : {'htmlFields' : ['html-1' ]}},
90+ ]
91+ }
92+ creds = _Credentials ()
93+ client = self ._makeOne (self .PROJECT , creds )
94+ conn = client .connection = _Connection (DATA )
95+
96+ zones , token = client .list_indexes (
97+ max_results = 3 , page_token = TOKEN , prefix = 'index' , view = 'FULL' )
98+
99+ self .assertEqual (len (zones ), len (DATA ['indexes' ]))
100+ for found , expected in zip (zones , DATA ['indexes' ]):
101+ self .assertTrue (isinstance (found , Index ))
102+ self .assertEqual (found .name , expected ['indexId' ])
103+ field_info = expected ['indexedField' ]
104+ self .assertEqual (found .text_fields , field_info .get ('textFields' ))
105+ self .assertEqual (found .atom_fields , field_info .get ('atomFields' ))
106+ self .assertEqual (found .html_fields , field_info .get ('htmlFields' ))
107+ self .assertEqual (found .date_fields , field_info .get ('dateFields' ))
108+ self .assertEqual (found .number_fields ,
109+ field_info .get ('numberFields' ))
110+ self .assertEqual (found .geo_fields , field_info .get ('geoFields' ))
111+ self .assertEqual (token , None )
112+
113+ self .assertEqual (len (conn ._requested ), 1 )
114+ req = conn ._requested [0 ]
115+ self .assertEqual (req ['method' ], 'GET' )
116+ self .assertEqual (req ['path' ], '/%s' % PATH )
117+ self .assertEqual (req ['query_params' ],
118+ {'indexNamePrefix' : 'index' ,
119+ 'pageSize' : 3 ,
120+ 'pageToken' : TOKEN ,
121+ 'view' : 'FULL' })
122+
123+ def test_index (self ):
124+ from gcloud .search .index import Index
125+ INDEX_ID = 'index-id'
126+ creds = _Credentials ()
127+ http = object ()
128+ client = self ._makeOne (
129+ project = self .PROJECT , credentials = creds , http = http )
130+ index = client .index (INDEX_ID )
131+ self .assertTrue (isinstance (index , Index ))
132+ self .assertEqual (index .name , INDEX_ID )
133+ self .assertTrue (index ._client is client )
134+
37135
38136class _Credentials (object ):
39137
@@ -46,3 +144,15 @@ def create_scoped_required():
46144 def create_scoped (self , scope ):
47145 self ._scopes = scope
48146 return self
147+
148+
149+ class _Connection (object ):
150+
151+ def __init__ (self , * responses ):
152+ self ._responses = responses
153+ self ._requested = []
154+
155+ def api_request (self , ** kw ):
156+ self ._requested .append (kw )
157+ response , self ._responses = self ._responses [0 ], self ._responses [1 :]
158+ return response
0 commit comments