read_test.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. // Copyright 2015 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package bigquery
  15. import (
  16. "errors"
  17. "reflect"
  18. "testing"
  19. "golang.org/x/net/context"
  20. )
  21. type readTabledataArgs struct {
  22. conf *readTableConf
  23. tok string
  24. }
  25. type readQueryArgs struct {
  26. conf *readQueryConf
  27. tok string
  28. }
  29. // readServiceStub services read requests by returning data from an in-memory list of values.
  30. type readServiceStub struct {
  31. // values and pageTokens are used as sources of data to return in response to calls to readTabledata or readQuery.
  32. values [][][]Value // contains pages / rows / columns.
  33. pageTokens map[string]string // maps incoming page token to returned page token.
  34. // arguments are recorded for later inspection.
  35. readTabledataCalls []readTabledataArgs
  36. readQueryCalls []readQueryArgs
  37. service
  38. }
  39. func (s *readServiceStub) readValues(tok string) *readDataResult {
  40. result := &readDataResult{
  41. pageToken: s.pageTokens[tok],
  42. rows: s.values[0],
  43. }
  44. s.values = s.values[1:]
  45. return result
  46. }
  47. func (s *readServiceStub) readTabledata(ctx context.Context, conf *readTableConf, token string) (*readDataResult, error) {
  48. s.readTabledataCalls = append(s.readTabledataCalls, readTabledataArgs{conf, token})
  49. return s.readValues(token), nil
  50. }
  51. func (s *readServiceStub) readQuery(ctx context.Context, conf *readQueryConf, token string) (*readDataResult, error) {
  52. s.readQueryCalls = append(s.readQueryCalls, readQueryArgs{conf, token})
  53. return s.readValues(token), nil
  54. }
  55. func TestRead(t *testing.T) {
  56. // The data for the service stub to return is populated for each test case in the testCases for loop.
  57. service := &readServiceStub{}
  58. c := &Client{
  59. service: service,
  60. }
  61. queryJob := &Job{
  62. projectID: "project-id",
  63. jobID: "job-id",
  64. service: service,
  65. isQuery: true,
  66. }
  67. for _, src := range []ReadSource{defaultTable, queryJob} {
  68. testCases := []struct {
  69. data [][][]Value
  70. pageTokens map[string]string
  71. want []ValueList
  72. }{
  73. {
  74. data: [][][]Value{{{1, 2}, {11, 12}}, {{30, 40}, {31, 41}}},
  75. pageTokens: map[string]string{"": "a", "a": ""},
  76. want: []ValueList{{1, 2}, {11, 12}, {30, 40}, {31, 41}},
  77. },
  78. {
  79. data: [][][]Value{{{1, 2}, {11, 12}}, {{30, 40}, {31, 41}}},
  80. pageTokens: map[string]string{"": ""}, // no more pages after first one.
  81. want: []ValueList{{1, 2}, {11, 12}},
  82. },
  83. }
  84. for _, tc := range testCases {
  85. service.values = tc.data
  86. service.pageTokens = tc.pageTokens
  87. if got, ok := doRead(t, c, src); ok {
  88. if !reflect.DeepEqual(got, tc.want) {
  89. t.Errorf("reading: got:\n%v\nwant:\n%v", got, tc.want)
  90. }
  91. }
  92. }
  93. }
  94. }
  95. // doRead calls Read with a ReadSource. Get is repeatedly called on the Iterator returned by Read and the results are returned.
  96. func doRead(t *testing.T, c *Client, src ReadSource) ([]ValueList, bool) {
  97. it, err := c.Read(context.Background(), src)
  98. if err != nil {
  99. t.Errorf("err calling Read: %v", err)
  100. return nil, false
  101. }
  102. var got []ValueList
  103. for it.Next(context.Background()) {
  104. var vals ValueList
  105. if err := it.Get(&vals); err != nil {
  106. t.Errorf("err calling Get: %v", err)
  107. return nil, false
  108. } else {
  109. got = append(got, vals)
  110. }
  111. }
  112. return got, true
  113. }
  114. func TestNoMoreValues(t *testing.T) {
  115. c := &Client{
  116. service: &readServiceStub{
  117. values: [][][]Value{{{1, 2}, {11, 12}}},
  118. },
  119. }
  120. it, err := c.Read(context.Background(), defaultTable)
  121. if err != nil {
  122. t.Fatalf("err calling Read: %v", err)
  123. }
  124. var vals ValueList
  125. // We expect to retrieve two values and then fail on the next attempt.
  126. if !it.Next(context.Background()) {
  127. t.Fatalf("Next: got: false: want: true")
  128. }
  129. if !it.Next(context.Background()) {
  130. t.Fatalf("Next: got: false: want: true")
  131. }
  132. if err := it.Get(&vals); err != nil {
  133. t.Fatalf("Get: got: %v: want: nil", err)
  134. }
  135. if it.Next(context.Background()) {
  136. t.Fatalf("Next: got: true: want: false")
  137. }
  138. if err := it.Get(&vals); err == nil {
  139. t.Fatalf("Get: got: %v: want: non-nil", err)
  140. }
  141. }
  142. // delayedReadStub simulates reading results from a query that has not yet
  143. // completed. Its readQuery method initially reports that the query job is not
  144. // yet complete. Subsequently, it proxies the request through to another
  145. // service stub.
  146. type delayedReadStub struct {
  147. numDelays int
  148. readServiceStub
  149. }
  150. func (s *delayedReadStub) readQuery(ctx context.Context, conf *readQueryConf, token string) (*readDataResult, error) {
  151. if s.numDelays > 0 {
  152. s.numDelays--
  153. return nil, errIncompleteJob
  154. }
  155. return s.readServiceStub.readQuery(ctx, conf, token)
  156. }
  157. // TestIncompleteJob tests that an Iterator which reads from a query job will block until the job is complete.
  158. func TestIncompleteJob(t *testing.T) {
  159. service := &delayedReadStub{
  160. numDelays: 2,
  161. readServiceStub: readServiceStub{
  162. values: [][][]Value{{{1, 2}}},
  163. },
  164. }
  165. c := &Client{service: service}
  166. queryJob := &Job{
  167. projectID: "project-id",
  168. jobID: "job-id",
  169. service: service,
  170. isQuery: true,
  171. }
  172. it, err := c.Read(context.Background(), queryJob)
  173. if err != nil {
  174. t.Fatalf("err calling Read: %v", err)
  175. }
  176. var got ValueList
  177. want := ValueList{1, 2}
  178. if !it.Next(context.Background()) {
  179. t.Fatalf("Next: got: false: want: true")
  180. }
  181. if err := it.Get(&got); err != nil {
  182. t.Fatalf("Error calling Get: %v", err)
  183. }
  184. if service.numDelays != 0 {
  185. t.Errorf("remaining numDelays : got: %v want:0", service.numDelays)
  186. }
  187. if !reflect.DeepEqual(got, want) {
  188. t.Errorf("reading: got:\n%v\nwant:\n%v", got, want)
  189. }
  190. }
  191. type errorReadService struct {
  192. service
  193. }
  194. func (s *errorReadService) readTabledata(ctx context.Context, conf *readTableConf, token string) (*readDataResult, error) {
  195. return nil, errors.New("bang!")
  196. }
  197. func TestReadError(t *testing.T) {
  198. // test that service read errors are propagated back to the caller.
  199. c := &Client{service: &errorReadService{}}
  200. it, err := c.Read(context.Background(), defaultTable)
  201. if err != nil {
  202. // Read should not return an error; only Err should.
  203. t.Fatalf("err calling Read: %v", err)
  204. }
  205. if it.Next(context.Background()) {
  206. t.Fatalf("Next: got: true: want: false")
  207. }
  208. if err := it.Err(); err.Error() != "bang!" {
  209. t.Fatalf("Get: got: %v: want: bang!", err)
  210. }
  211. }
  212. func TestReadTabledataOptions(t *testing.T) {
  213. // test that read options are propagated.
  214. s := &readServiceStub{
  215. values: [][][]Value{{{1, 2}}},
  216. }
  217. c := &Client{service: s}
  218. it, err := c.Read(context.Background(), defaultTable, RecordsPerRequest(5))
  219. if err != nil {
  220. t.Fatalf("err calling Read: %v", err)
  221. }
  222. if !it.Next(context.Background()) {
  223. t.Fatalf("Next: got: false: want: true")
  224. }
  225. want := []readTabledataArgs{{
  226. conf: &readTableConf{
  227. projectID: "project-id",
  228. datasetID: "dataset-id",
  229. tableID: "table-id",
  230. paging: pagingConf{
  231. recordsPerRequest: 5,
  232. setRecordsPerRequest: true,
  233. },
  234. },
  235. tok: "",
  236. }}
  237. if !reflect.DeepEqual(s.readTabledataCalls, want) {
  238. t.Errorf("reading: got:\n%v\nwant:\n%v", s.readTabledataCalls, want)
  239. }
  240. }
  241. func TestReadQueryOptions(t *testing.T) {
  242. // test that read options are propagated.
  243. s := &readServiceStub{
  244. values: [][][]Value{{{1, 2}}},
  245. }
  246. c := &Client{service: s}
  247. queryJob := &Job{
  248. projectID: "project-id",
  249. jobID: "job-id",
  250. service: s,
  251. isQuery: true,
  252. }
  253. it, err := c.Read(context.Background(), queryJob, RecordsPerRequest(5))
  254. if err != nil {
  255. t.Fatalf("err calling Read: %v", err)
  256. }
  257. if !it.Next(context.Background()) {
  258. t.Fatalf("Next: got: false: want: true")
  259. }
  260. want := []readQueryArgs{{
  261. conf: &readQueryConf{
  262. projectID: "project-id",
  263. jobID: "job-id",
  264. paging: pagingConf{
  265. recordsPerRequest: 5,
  266. setRecordsPerRequest: true,
  267. },
  268. },
  269. tok: "",
  270. }}
  271. if !reflect.DeepEqual(s.readQueryCalls, want) {
  272. t.Errorf("reading: got:\n%v\nwant:\n%v", s.readQueryCalls, want)
  273. }
  274. }