td-client 0.8.62 → 0.8.63

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,165 @@
1
+ require 'spec_helper'
2
+ require 'td/client/spec_resources'
3
+
4
+ describe 'Table API' do
5
+ include_context 'spec symbols'
6
+ include_context 'common helper'
7
+
8
+ let :api do
9
+ API.new(nil)
10
+ end
11
+
12
+ let :client do
13
+ Client.new(apikey)
14
+ end
15
+
16
+ describe "'create_log_table' API" do
17
+ it 'should return 404 error if the database does not exist' do
18
+ err_msg = "Create log table failed: Couldn't find UserDatabase with name = #{db_name}"
19
+ stub_api_request(:post, "/v3/table/create/#{e db_name}/#{e(table_name)}/log")
20
+ .to_return(:status => 404, :body => {'message' => err_msg}.to_json)
21
+
22
+ expect {
23
+ api.create_log_table(db_name, table_name)
24
+ }.to raise_error(TreasureData::NotFoundError, /#{err_msg}/)
25
+ end
26
+
27
+ it 'should create a new table if the database exists' do
28
+ stub_api_request(:post, "/v3/table/create/#{e db_name}/#{e(table_name)}/log")
29
+ .to_return(:body => {'database' => db_name, 'table' => table_name, 'type' => 'log'}.to_json)
30
+ api.create_log_table(db_name, table_name).should be_true
31
+ end
32
+
33
+ it 'should return 400 error with invalid name' do
34
+ invalid_name = 'a'
35
+ err_msg = "Name must be 3 to 256 characters, got #{invalid_name.length} characters. name = '#{invalid_name}'"
36
+ stub_api_request(:post, "/v3/table/create/#{e db_name}/#{e invalid_name}/log")
37
+ .to_return(:status => 400, :body => {'message' => err_msg}.to_json)
38
+
39
+ expect {
40
+ api.create_log_table(db_name, invalid_name)
41
+ }.to raise_error(TreasureData::APIError, /#{err_msg}/)
42
+ end
43
+
44
+ it 'should return 409 error with duplicated name' do
45
+ err_msg = "Table #{table_name} already exists"
46
+ stub_api_request(:post, "/v3/table/create/#{e db_name}/#{e table_name}/log")
47
+ .to_return(:status => 409, :body => {'message' => err_msg}.to_json)
48
+
49
+ expect {
50
+ api.create_log_table(db_name, table_name)
51
+ }.to raise_error(TreasureData::AlreadyExistsError, /#{err_msg}/)
52
+ end
53
+ end
54
+
55
+ describe "'list_tables' API" do
56
+ it 'should list the tables in a Hash whose values include type, count, created_at, updated_at, schema, ...' do
57
+ tables = [
58
+ ["table_1", "item", "[[\"time\",\"long\"],[\"value\",\"string\"]]", 111, "2013-01-21 01:51:41 UTC", "2014-01-21 01:51:41 UTC"],
59
+ ["table_2", "log", "[[\"time\",\"long\"],[\"value\",\"long\"]]", 222, "2013-02-22 02:52:42 UTC", "2014-02-22 02:52:42 UTC"],
60
+ ["table_3", "item", "[[\"time\",\"long\"],[\"value\",\"string\"]]", 333, "2013-03-23 03:53:43 UTC", "2014-03-23 03:53:43 UTC"],
61
+ ["table_4", "log", "[[\"time\",\"long\"],[\"value\",\"long\"]]", 444, "2013-04-24 04:54:44 UTC", "2014-04-24 04:54:44 UTC"]
62
+ ]
63
+ stub_api_request(:get, "/v3/table/list/#{e db_name}")
64
+ .to_return(:body => {'tables' => [
65
+ {'name' => tables[0][0], 'type' => tables[0][1], 'schema' => tables[0][2], 'count' => tables[0][3], 'created_at' => tables[0][4], 'updated_at' => tables[0][5]},
66
+ {'name' => tables[1][0], 'type' => tables[1][1], 'schema' => tables[1][2], 'count' => tables[1][3], 'created_at' => tables[1][4], 'updated_at' => tables[1][5]},
67
+ {'name' => tables[2][0], 'type' => tables[2][1], 'schema' => tables[2][2], 'count' => tables[2][3], 'created_at' => tables[2][4], 'updated_at' => tables[2][5]},
68
+ {'name' => tables[3][0], 'type' => tables[3][1], 'schema' => tables[3][2], 'count' => tables[3][3], 'created_at' => tables[3][4], 'updated_at' => tables[3][5]}
69
+ ]}.to_json)
70
+
71
+ table_list = api.list_tables(db_name)
72
+ tables.each {|table|
73
+ expect(table_list[table[0]][0]).to eq(table[1].to_sym)
74
+ expect(table_list[table[0]][1]).to eq(JSON.parse(table[2]))
75
+ expect(table_list[table[0]][2]).to eq(table[3])
76
+ expect(table_list[table[0]][3]).to eq(table[4])
77
+ expect(table_list[table[0]][4]).to eq(table[5])
78
+ }
79
+ end
80
+ end
81
+
82
+ describe "'tables' Client API" do
83
+ it 'should return an array of Table objects' do
84
+ tables = [
85
+ ["table_1", "item", "[[\"time\",\"long\"],[\"value\",\"string\"]]", 111, "2013-01-21 01:51:41 UTC", "2014-01-21 01:51:41 UTC"],
86
+ ["table_2", "log", "[[\"time\",\"long\"],[\"value\",\"long\"]]", 222, "2013-02-22 02:52:42 UTC", "2014-02-22 02:52:42 UTC"],
87
+ ["table_3", "item", "[[\"time\",\"long\"],[\"value\",\"string\"]]", 333, "2013-03-23 03:53:43 UTC", "2014-03-23 03:53:43 UTC"],
88
+ ["table_4", "log", "[[\"time\",\"long\"],[\"value\",\"long\"]]", 444, "2013-04-24 04:54:44 UTC", "2014-04-24 04:54:44 UTC"]
89
+ ]
90
+ stub_api_request(:get, "/v3/table/list/#{e db_name}")
91
+ .to_return(:body => {'tables' => [
92
+ {'name' => tables[0][0], 'type' => tables[0][1], 'schema' => tables[0][2], 'count' => tables[0][3], 'created_at' => tables[0][4], 'updated_at' => tables[0][5]},
93
+ {'name' => tables[1][0], 'type' => tables[1][1], 'schema' => tables[1][2], 'count' => tables[1][3], 'created_at' => tables[1][4], 'updated_at' => tables[1][5]},
94
+ {'name' => tables[2][0], 'type' => tables[2][1], 'schema' => tables[2][2], 'count' => tables[2][3], 'created_at' => tables[2][4], 'updated_at' => tables[2][5]},
95
+ {'name' => tables[3][0], 'type' => tables[3][1], 'schema' => tables[3][2], 'count' => tables[3][3], 'created_at' => tables[3][4], 'updated_at' => tables[3][5]}
96
+ ]}.to_json)
97
+
98
+ table_list = client.tables(db_name)
99
+
100
+ db_count = 0
101
+ tables.each {|table|
102
+ db_count += table[3]
103
+ }
104
+
105
+ # REST API call to fetch the database permission
106
+ stub_api_request(:get, "/v3/database/list")
107
+ .to_return(:body => {'databases' => [
108
+ {'name' => db_name, 'count' => db_count, 'created_at' => tables[0][4], 'updated_at' => tables[0][5], 'permission' => 'full_access'}
109
+ ]}.to_json)
110
+
111
+ tables.length.times {|i|
112
+ expect(table_list[i].db_name).to eq(db_name)
113
+ expect(table_list[i].name).to eq(tables[i][0])
114
+ expect(table_list[i].type).to eq(tables[i][1].to_sym)
115
+ expect(table_list[i].schema.to_json).to eq(eval(tables[i][2]).to_json)
116
+ expect(table_list[i].count).to eq(tables[i][3])
117
+ expect(table_list[i].created_at).to eq(Time.parse(tables[i][4]))
118
+ expect(table_list[i].updated_at).to eq(Time.parse(tables[i][5]))
119
+
120
+ # REST API call to fetch the database permission
121
+ stub_api_request(:get, "/v3/database/list")
122
+ .to_return(:body => {'databases' => [
123
+ {'name' => db_name, 'count' => db_count, 'created_at' => tables[0][4], 'updated_at' => tables[0][5], 'permission' => 'full_access'}
124
+ ]}.to_json)
125
+ expect(table_list[i].permission).to eq(:full_access)
126
+
127
+ # set up a trap to check this call never happens
128
+ # - if it did, the next assertion on the count would fail
129
+ stub_api_request(:get, "/v3/database/list")
130
+ .to_return(:body => {'databases' => [
131
+ {'name' => db_name, 'count' => db_count + 100, 'created_at' => tables[0][4], 'updated_at' => tables[0][5], 'permission' => 'full_access'}
132
+ ]}.to_json)
133
+ expect(table_list[i].database.count).to eq(db_count)
134
+ }
135
+ end
136
+ end
137
+
138
+ describe "'table' Client API" do
139
+ it 'should return the Table object corresponding to the name' do
140
+ tables = [
141
+ ["table_1", "item", "[[\"time\",\"long\"],[\"value\",\"string\"]]", 111, "2013-01-21 01:51:41 UTC", "2014-01-21 01:51:41 UTC"],
142
+ ["table_2", "log", "[[\"time\",\"long\"],[\"value\",\"long\"]]", 222, "2013-02-22 02:52:42 UTC", "2014-02-22 02:52:42 UTC"],
143
+ ["table_3", "item", "[[\"time\",\"long\"],[\"value\",\"string\"]]", 333, "2013-03-23 03:53:43 UTC", "2014-03-23 03:53:43 UTC"],
144
+ ["table_4", "log", "[[\"time\",\"long\"],[\"value\",\"long\"]]", 444, "2013-04-24 04:54:44 UTC", "2014-04-24 04:54:44 UTC"]
145
+ ]
146
+ stub_api_request(:get, "/v3/table/list/#{e db_name}")
147
+ .to_return(:body => {'tables' => [
148
+ {'name' => tables[0][0], 'type' => tables[0][1], 'schema' => tables[0][2], 'count' => tables[0][3], 'created_at' => tables[0][4], 'updated_at' => tables[0][5]},
149
+ {'name' => tables[1][0], 'type' => tables[1][1], 'schema' => tables[1][2], 'count' => tables[1][3], 'created_at' => tables[1][4], 'updated_at' => tables[1][5]},
150
+ {'name' => tables[2][0], 'type' => tables[2][1], 'schema' => tables[2][2], 'count' => tables[2][3], 'created_at' => tables[2][4], 'updated_at' => tables[2][5]},
151
+ {'name' => tables[3][0], 'type' => tables[3][1], 'schema' => tables[3][2], 'count' => tables[3][3], 'created_at' => tables[3][4], 'updated_at' => tables[3][5]}
152
+ ]}.to_json)
153
+
154
+ i = 1
155
+ table = client.table(db_name, tables[i][0])
156
+
157
+ expect(table.name).to eq(tables[i][0])
158
+ expect(table.type).to eq(tables[i][1].to_sym)
159
+ expect(table.schema.to_json).to eq(eval(tables[i][2]).to_json)
160
+ expect(table.count).to eq(tables[i][3])
161
+ expect(table.created_at).to eq(Time.parse(tables[i][4]))
162
+ expect(table.updated_at).to eq(Time.parse(tables[i][5]))
163
+ end
164
+ end
165
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: td-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.62
4
+ version: 0.8.63
5
5
  platform: ruby
6
6
  authors:
7
7
  - Treasure Data, Inc.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-19 00:00:00.000000000 Z
11
+ date: 2014-08-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: msgpack
@@ -151,7 +151,6 @@ files:
151
151
  - lib/td/client/compat_gzip_reader.rb
152
152
  - lib/td/client/model.rb
153
153
  - lib/td/client/version.rb
154
- - spec/api_spec.rb
155
154
  - spec/spec_helper.rb
156
155
  - spec/td/client/api_spec.rb
157
156
  - spec/td/client/bulk_import_spec.rb
@@ -162,6 +161,7 @@ files:
162
161
  - spec/td/client/result_api_spec.rb
163
162
  - spec/td/client/sched_api_spec.rb
164
163
  - spec/td/client/spec_resources.rb
164
+ - spec/td/client/table_api_spec.rb
165
165
  homepage: http://treasuredata.com/
166
166
  licenses: []
167
167
  metadata: {}
@@ -186,7 +186,6 @@ signing_key:
186
186
  specification_version: 4
187
187
  summary: Treasure Data API library for Ruby
188
188
  test_files:
189
- - spec/api_spec.rb
190
189
  - spec/td/client/api_spec.rb
191
190
  - spec/td/client/bulk_import_spec.rb
192
191
  - spec/td/client/db_api_spec.rb
@@ -195,3 +194,4 @@ test_files:
195
194
  - spec/td/client/partial_delete_api_spec.rb
196
195
  - spec/td/client/result_api_spec.rb
197
196
  - spec/td/client/sched_api_spec.rb
197
+ - spec/td/client/table_api_spec.rb
@@ -1,72 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe API do
4
- VALID_NAMES = [
5
- 'abc',
6
- 'abc_cd',
7
- '_abc_cd',
8
- '_abc_',
9
- 'ab0_',
10
- 'ab0',
11
- ]
12
-
13
- INVALID_NAMES = {
14
- 'a' => 'a__',
15
- 'a' * 256 => 'a' * 253 + '__',
16
- 'abcD' => 'abcd',
17
- 'a-b*' => 'a_b_',
18
- }
19
-
20
- it 'normalize_database_name should return normalized data' do
21
- INVALID_NAMES.each_pair {|ng,ok|
22
- API.normalize_database_name(ng).should == ok
23
- }
24
- lambda {
25
- API.normalize_database_name('')
26
- }.should raise_error(RuntimeError)
27
- end
28
-
29
- it 'normalize_table_name should return normalized data' do
30
- INVALID_NAMES.each_pair {|ng,ok|
31
- API.normalize_table_name(ng).should == ok
32
- }
33
- lambda {
34
- API.normalize_table_name('')
35
- }.should raise_error(RuntimeError)
36
- end
37
-
38
- it 'validate_database_name should raise errors' do
39
- INVALID_NAMES.each_pair {|ng,ok|
40
- lambda {
41
- API.validate_database_name(ng)
42
- }.should raise_error(ParameterValidationError)
43
- }
44
- lambda {
45
- API.validate_database_name('')
46
- }.should raise_error(ParameterValidationError)
47
- end
48
-
49
- it 'validate_table_name should raise errors' do
50
- INVALID_NAMES.each_pair {|ng,ok|
51
- lambda {
52
- API.validate_table_name(ng)
53
- }.should raise_error(ParameterValidationError)
54
- }
55
- lambda {
56
- API.validate_table_name('')
57
- }.should raise_error(ParameterValidationError)
58
- end
59
-
60
- it 'normalize_database_name should return valid data' do
61
- VALID_NAMES.each {|ok|
62
- API.normalize_database_name(ok).should == ok
63
- }
64
- end
65
-
66
- it 'validate_database_name should return valid data' do
67
- VALID_NAMES.each {|ok|
68
- API.validate_database_name(ok)
69
- }
70
- end
71
- end
72
-