kintone-oauth-extension 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) hide show
  1. checksums.yaml +7 -0
  2. data/.github/workflows/gem-push.yml +37 -0
  3. data/.github/workflows/test.yml +21 -0
  4. data/.gitignore +21 -0
  5. data/.rspec +1 -0
  6. data/.rubocop.yml +47 -0
  7. data/CHANGELOG.md +13 -0
  8. data/CODE_OF_CONDUCT.md +74 -0
  9. data/Gemfile +4 -0
  10. data/LICENSE.txt +21 -0
  11. data/README.md +374 -0
  12. data/bin/console +4 -0
  13. data/bin/setup +6 -0
  14. data/kintone.gemspec +38 -0
  15. data/lib/kintone.rb +8 -0
  16. data/lib/kintone/api.rb +121 -0
  17. data/lib/kintone/api/guest.rb +44 -0
  18. data/lib/kintone/command.rb +12 -0
  19. data/lib/kintone/command/accessor.rb +109 -0
  20. data/lib/kintone/command/apis.rb +22 -0
  21. data/lib/kintone/command/app.rb +11 -0
  22. data/lib/kintone/command/app_acl.rb +11 -0
  23. data/lib/kintone/command/apps.rb +12 -0
  24. data/lib/kintone/command/bulk_request.rb +12 -0
  25. data/lib/kintone/command/field_acl.rb +11 -0
  26. data/lib/kintone/command/file.rb +15 -0
  27. data/lib/kintone/command/form.rb +11 -0
  28. data/lib/kintone/command/guests.rb +17 -0
  29. data/lib/kintone/command/preview_form.rb +11 -0
  30. data/lib/kintone/command/record.rb +23 -0
  31. data/lib/kintone/command/record_acl.rb +11 -0
  32. data/lib/kintone/command/records.rb +29 -0
  33. data/lib/kintone/command/space.rb +15 -0
  34. data/lib/kintone/command/space_body.rb +11 -0
  35. data/lib/kintone/command/space_guests.rb +11 -0
  36. data/lib/kintone/command/space_members.rb +16 -0
  37. data/lib/kintone/command/space_thread.rb +14 -0
  38. data/lib/kintone/command/template_space.rb +12 -0
  39. data/lib/kintone/kintone_error.rb +12 -0
  40. data/lib/kintone/oauth_api.rb +91 -0
  41. data/lib/kintone/query.rb +152 -0
  42. data/lib/kintone/query/extension.rb +23 -0
  43. data/lib/kintone/type.rb +6 -0
  44. data/lib/kintone/type/extension/enumerable.rb +5 -0
  45. data/lib/kintone/type/extension/hash.rb +5 -0
  46. data/lib/kintone/type/extension/object.rb +5 -0
  47. data/lib/kintone/type/record.rb +11 -0
  48. data/lib/kintone/version.rb +3 -0
  49. data/spec/kintone/api/guest_spec.rb +289 -0
  50. data/spec/kintone/api_spec.rb +566 -0
  51. data/spec/kintone/command/apis_spec.rb +179 -0
  52. data/spec/kintone/command/app_acl_spec.rb +43 -0
  53. data/spec/kintone/command/app_spec.rb +54 -0
  54. data/spec/kintone/command/apps_spec.rb +90 -0
  55. data/spec/kintone/command/bulk_request_spec.rb +92 -0
  56. data/spec/kintone/command/field_acl_spec.rb +47 -0
  57. data/spec/kintone/command/file_spec.rb +65 -0
  58. data/spec/kintone/command/form_spec.rb +47 -0
  59. data/spec/kintone/command/guests_spec.rb +107 -0
  60. data/spec/kintone/command/preview_form_spec.rb +30 -0
  61. data/spec/kintone/command/record_acl_spec.rb +48 -0
  62. data/spec/kintone/command/record_spec.rb +210 -0
  63. data/spec/kintone/command/records_spec.rb +463 -0
  64. data/spec/kintone/command/space_body_spec.rb +47 -0
  65. data/spec/kintone/command/space_guests_spec.rb +55 -0
  66. data/spec/kintone/command/space_members_spec.rb +117 -0
  67. data/spec/kintone/command/space_spec.rb +86 -0
  68. data/spec/kintone/command/space_thread_spec.rb +77 -0
  69. data/spec/kintone/command/template_space_spec.rb +59 -0
  70. data/spec/kintone/kintone_error_spec.rb +93 -0
  71. data/spec/kintone/oauth_api_spec.rb +164 -0
  72. data/spec/kintone/query_spec.rb +506 -0
  73. data/spec/kintone/type/record_spec.rb +38 -0
  74. data/spec/spec_helper.rb +10 -0
  75. metadata +302 -0
@@ -0,0 +1,164 @@
1
+ require 'spec_helper'
2
+
3
+ describe Kintone::OAuthApi do
4
+ let(:domain) { 'www.example.com' }
5
+ let(:path) { '/k/v1/path' }
6
+ let(:token) { 'access_token' }
7
+ let(:request_verb) { nil }
8
+ let(:default_request_headers) { { 'Authorization' => "Bearer #{token}" } }
9
+ let(:option_request_headers) { nil }
10
+ let(:params) { nil }
11
+ let(:body) { nil }
12
+ let(:response_body) { nil }
13
+ let(:response_status) { 200 }
14
+
15
+ let(:add_stub_request) do
16
+ url = "https://#{domain}#{path}"
17
+ url += "?#{URI.encode_www_form(params)}" if params && !params.empty?
18
+
19
+ stub_request(request_verb, url) \
20
+ .with do |req|
21
+ req.body = body.to_json if body
22
+ headers = default_request_headers
23
+ headers.merge! option_request_headers if option_request_headers
24
+ req.headers = headers
25
+ end
26
+ .to_return(
27
+ body: response_body.to_json,
28
+ status: response_status,
29
+ headers: { 'Content-type' => 'application/json' }
30
+ )
31
+ end
32
+
33
+ context 'specify token argument only' do
34
+ let(:target) { Kintone::OAuthApi.new(domain, token) }
35
+
36
+ describe '#get' do
37
+ before(:each) do
38
+ add_stub_request
39
+ end
40
+ let(:request_verb) { :get }
41
+ let(:response_body) { { abc: 'def' } }
42
+ let(:response_status) { 200 }
43
+ subject { target.get(path, params) }
44
+
45
+ context 'with some params' do
46
+ let(:params) { { 'p1' => 'abc', 'p2' => 'def' } }
47
+ it { is_expected.to eq 'abc' => 'def' }
48
+ end
49
+
50
+ context 'with empty params' do
51
+ let(:params) { {} }
52
+ it { is_expected.to eq 'abc' => 'def' }
53
+ end
54
+
55
+ context 'with nil params' do
56
+ let(:params) { nil }
57
+ it { is_expected.to eq 'abc' => 'def' }
58
+ end
59
+
60
+ context 'fail to request' do
61
+ let(:response_status) { 500 }
62
+ let(:response_body) { { message: '不正なJSON文字列です。', id: '1505999166-897850006', code: 'CB_IJ01' }.to_json }
63
+ before(:each) do
64
+ add_stub_request
65
+ end
66
+ it { expect { subject }.to raise_error Kintone::KintoneError }
67
+ end
68
+ end
69
+
70
+ describe '#post' do
71
+ before(:each) do
72
+ add_stub_request
73
+ end
74
+ let(:request_verb) { :post }
75
+ let(:response_status) { 200 }
76
+ let(:response_body) { { abc: 'def' } }
77
+ let(:params) { nil }
78
+ let(:body) { { p1: 'abc', p2: 'def' } }
79
+ let(:option_headers) { { 'Content-Type' => 'application/json' } }
80
+ subject { target.post(path, body) }
81
+
82
+ it { is_expected.to eq 'abc' => 'def' }
83
+ end
84
+
85
+ describe '#put' do
86
+ before(:each) do
87
+ add_stub_request
88
+ end
89
+ let(:request_verb) { :put }
90
+ let(:response_status) { 200 }
91
+ let(:response_body) { { abc: 'def' } }
92
+ let(:params) { nil }
93
+ let(:body) { { p1: 'abc', p2: 'def' } }
94
+ let(:option_headers) { { 'Content-Type' => 'application/json' } }
95
+ subject { target.put(path, body) }
96
+
97
+ it { is_expected.to eq 'abc' => 'def' }
98
+ end
99
+
100
+ describe '#delete' do
101
+ before(:each) do
102
+ add_stub_request
103
+ end
104
+ let(:request_verb) { :delete }
105
+ let(:response_status) { 200 }
106
+ let(:response_body) { { abc: 'def' } }
107
+ let(:params) { nil }
108
+ let(:body) { { p1: 'abc', p2: 'def' } }
109
+ let(:option_headers) { { 'Content-Type' => 'application/json' } }
110
+ subject { target.delete(path, body) }
111
+
112
+ it { is_expected.to eq 'abc' => 'def' }
113
+ end
114
+
115
+ describe '#refresh!' do
116
+ subject { target.refresh! }
117
+ it { expect { subject }.to raise_error RuntimeError }
118
+ end
119
+ end
120
+
121
+ context 'specify token and oauth_options arguments' do
122
+ let(:target) { Kintone::OAuthApi.new(domain, token, oauth_options) }
123
+ let(:oauth_options) do
124
+ {
125
+ client_id: 'client_id',
126
+ client_secret: 'client_secret',
127
+ refresh_token: 'refresh_token',
128
+ expires_at: 1_598_886_000
129
+ }
130
+ end
131
+ let(:access_token_response_body) do
132
+ {
133
+ access_token: 'new_access_token',
134
+ token_type: 'bearer',
135
+ expires_in: 3600,
136
+ scope: 'k:app_record:read k:app_record:write k:app_settings:read k:app_settings:write k:file:read k:file:write'
137
+ }
138
+ end
139
+ describe '#refresh!' do
140
+ before(:each) do
141
+ url = 'https://www.example.com/oauth2/token'
142
+ stub_request(:post, url)\
143
+ .with(
144
+ body: {
145
+ client_id: oauth_options[:client_id],
146
+ client_secret: oauth_options[:client_secret],
147
+ grant_type: 'refresh_token',
148
+ refresh_token: oauth_options[:refresh_token]
149
+ },
150
+ headers: {
151
+ 'Content-Type': 'application/x-www-form-urlencoded'
152
+ }
153
+ )
154
+ .to_return(
155
+ body: access_token_response_body.to_json,
156
+ status: 200,
157
+ headers: { 'Content-type' => 'application/json' }
158
+ )
159
+ end
160
+ subject { target.refresh!.token }
161
+ it { is_expected.to eq 'new_access_token' }
162
+ end
163
+ end
164
+ end
@@ -0,0 +1,506 @@
1
+ require 'spec_helper'
2
+ require 'kintone/query'
3
+
4
+ describe Kintone::Query do
5
+ describe '#to_s' do
6
+ subject { target.to_s }
7
+
8
+ context '==', 'with field' do
9
+ where(:target, :result) do
10
+ [
11
+ [Kintone::Query.new { field(:text) == '"Hello, world."' }, 'text = "Hello, world."'],
12
+ [Kintone::Query.new { field(:text) == 'Hello, world.' }, 'text = "Hello, world."'],
13
+ [Kintone::Query.new { field('作成日時') == now }, '作成日時 = NOW()'],
14
+ [Kintone::Query.new { field('作成日時') == today }, '作成日時 = TODAY()'],
15
+ [Kintone::Query.new { field('作成日時') == this_month }, '作成日時 = THIS_MONTH()'],
16
+ [Kintone::Query.new { field('作成日時') == last_month }, '作成日時 = LAST_MONTH()'],
17
+ [Kintone::Query.new { field('作成日時') == this_year }, '作成日時 = THIS_YEAR()'],
18
+ [Kintone::Query.new { field(:number) == 100 }, 'number = 100']
19
+ ]
20
+ end
21
+
22
+ with_them do
23
+ it { expect(subject).to eq result }
24
+ end
25
+ end
26
+
27
+ context '==', 'with f' do
28
+ where(:target, :result) do
29
+ [
30
+ [Kintone::Query.new { f(:text) == '"Hello, world."' }, 'text = "Hello, world."'],
31
+ [Kintone::Query.new { f(:text) == 'Hello, world.' }, 'text = "Hello, world."'],
32
+ [Kintone::Query.new { f('作成日時') == now }, '作成日時 = NOW()'],
33
+ [Kintone::Query.new { f('作成日時') == today }, '作成日時 = TODAY()'],
34
+ [Kintone::Query.new { f('作成日時') == this_month }, '作成日時 = THIS_MONTH()'],
35
+ [Kintone::Query.new { f('作成日時') == last_month }, '作成日時 = LAST_MONTH()'],
36
+ [Kintone::Query.new { f('作成日時') == this_year }, '作成日時 = THIS_YEAR()'],
37
+ [Kintone::Query.new { f(:number) == 100 }, 'number = 100']
38
+ ]
39
+ end
40
+
41
+ with_them do
42
+ it { expect(subject).to eq result }
43
+ end
44
+ end
45
+
46
+ context '!=', 'with field' do
47
+ where(:target, :result) do
48
+ [
49
+ [Kintone::Query.new { field(:text) != '"Hello, world."' }, 'text != "Hello, world."'],
50
+ [Kintone::Query.new { field(:text) != 'Hello, world.' }, 'text != "Hello, world."'],
51
+ [Kintone::Query.new { field('作成日時') != now }, '作成日時 != NOW()'],
52
+ [Kintone::Query.new { field('作成日時') != today }, '作成日時 != TODAY()'],
53
+ [Kintone::Query.new { field('作成日時') != this_month }, '作成日時 != THIS_MONTH()'],
54
+ [Kintone::Query.new { field('作成日時') != last_month }, '作成日時 != LAST_MONTH()'],
55
+ [Kintone::Query.new { field('作成日時') != this_year }, '作成日時 != THIS_YEAR()'],
56
+ [Kintone::Query.new { field(:number) != 100 }, 'number != 100']
57
+ ]
58
+ end
59
+
60
+ with_them do
61
+ it { expect(subject).to eq result }
62
+ end
63
+ end
64
+
65
+ context '!=', 'with f' do
66
+ where(:target, :result) do
67
+ [
68
+ [Kintone::Query.new { f(:text) != '"Hello, world."' }, 'text != "Hello, world."'],
69
+ [Kintone::Query.new { f(:text) != 'Hello, world.' }, 'text != "Hello, world."'],
70
+ [Kintone::Query.new { f('作成日時') != now }, '作成日時 != NOW()'],
71
+ [Kintone::Query.new { f('作成日時') != today }, '作成日時 != TODAY()'],
72
+ [Kintone::Query.new { f('作成日時') != this_month }, '作成日時 != THIS_MONTH()'],
73
+ [Kintone::Query.new { f('作成日時') != last_month }, '作成日時 != LAST_MONTH()'],
74
+ [Kintone::Query.new { f('作成日時') != this_year }, '作成日時 != THIS_YEAR()'],
75
+ [Kintone::Query.new { f(:number) != 100 }, 'number != 100']
76
+ ]
77
+ end
78
+
79
+ with_them do
80
+ it { expect(subject).to eq result }
81
+ end
82
+ end
83
+
84
+ context '>', 'with field' do
85
+ where(:target, :result) do
86
+ [
87
+ [Kintone::Query.new { field(:text) > '"Hello, world."' }, 'text > "Hello, world."'],
88
+ [Kintone::Query.new { field(:text) > 'Hello, world.' }, 'text > "Hello, world."'],
89
+ [Kintone::Query.new { field('作成日時') > now }, '作成日時 > NOW()'],
90
+ [Kintone::Query.new { field('作成日時') > today }, '作成日時 > TODAY()'],
91
+ [Kintone::Query.new { field('作成日時') > this_month }, '作成日時 > THIS_MONTH()'],
92
+ [Kintone::Query.new { field('作成日時') > last_month }, '作成日時 > LAST_MONTH()'],
93
+ [Kintone::Query.new { field('作成日時') > this_year }, '作成日時 > THIS_YEAR()'],
94
+ [Kintone::Query.new { field(:number) > 100 }, 'number > 100']
95
+ ]
96
+ end
97
+
98
+ with_them do
99
+ it { expect(subject).to eq result }
100
+ end
101
+ end
102
+
103
+ context '>', 'with f' do
104
+ where(:target, :result) do
105
+ [
106
+ [Kintone::Query.new { f(:text) > '"Hello, world."' }, 'text > "Hello, world."'],
107
+ [Kintone::Query.new { f(:text) > 'Hello, world.' }, 'text > "Hello, world."'],
108
+ [Kintone::Query.new { f('作成日時') > now }, '作成日時 > NOW()'],
109
+ [Kintone::Query.new { f('作成日時') > today }, '作成日時 > TODAY()'],
110
+ [Kintone::Query.new { f('作成日時') > this_month }, '作成日時 > THIS_MONTH()'],
111
+ [Kintone::Query.new { f('作成日時') > last_month }, '作成日時 > LAST_MONTH()'],
112
+ [Kintone::Query.new { f('作成日時') > this_year }, '作成日時 > THIS_YEAR()'],
113
+ [Kintone::Query.new { f(:number) > 100 }, 'number > 100']
114
+ ]
115
+ end
116
+
117
+ with_them do
118
+ it { expect(subject).to eq result }
119
+ end
120
+ end
121
+
122
+ context '<', 'with field' do
123
+ where(:target, :result) do
124
+ [
125
+ [Kintone::Query.new { field(:text) < '"Hello, world."' }, 'text < "Hello, world."'],
126
+ [Kintone::Query.new { field(:text) < 'Hello, world.' }, 'text < "Hello, world."'],
127
+ [Kintone::Query.new { field('作成日時') < now }, '作成日時 < NOW()'],
128
+ [Kintone::Query.new { field('作成日時') < today }, '作成日時 < TODAY()'],
129
+ [Kintone::Query.new { field('作成日時') < this_month }, '作成日時 < THIS_MONTH()'],
130
+ [Kintone::Query.new { field('作成日時') < last_month }, '作成日時 < LAST_MONTH()'],
131
+ [Kintone::Query.new { field('作成日時') < this_year }, '作成日時 < THIS_YEAR()'],
132
+ [Kintone::Query.new { field(:number) < 100 }, 'number < 100']
133
+ ]
134
+ end
135
+
136
+ with_them do
137
+ it { expect(subject).to eq result }
138
+ end
139
+ end
140
+
141
+ context '<', 'with f' do
142
+ where(:target, :result) do
143
+ [
144
+ [Kintone::Query.new { f(:text) < '"Hello, world."' }, 'text < "Hello, world."'],
145
+ [Kintone::Query.new { f(:text) < 'Hello, world.' }, 'text < "Hello, world."'],
146
+ [Kintone::Query.new { f('作成日時') < now }, '作成日時 < NOW()'],
147
+ [Kintone::Query.new { f('作成日時') < today }, '作成日時 < TODAY()'],
148
+ [Kintone::Query.new { f('作成日時') < this_month }, '作成日時 < THIS_MONTH()'],
149
+ [Kintone::Query.new { f('作成日時') < last_month }, '作成日時 < LAST_MONTH()'],
150
+ [Kintone::Query.new { f('作成日時') < this_year }, '作成日時 < THIS_YEAR()'],
151
+ [Kintone::Query.new { f(:number) < 100 }, 'number < 100']
152
+ ]
153
+ end
154
+
155
+ with_them do
156
+ it { expect(subject).to eq result }
157
+ end
158
+ end
159
+
160
+ context '>=', 'with field' do
161
+ where(:target, :result) do
162
+ [
163
+ [Kintone::Query.new { field(:text) >= '"Hello, world."' }, 'text >= "Hello, world."'],
164
+ [Kintone::Query.new { field(:text) >= 'Hello, world.' }, 'text >= "Hello, world."'],
165
+ [Kintone::Query.new { field('作成日時') >= now }, '作成日時 >= NOW()'],
166
+ [Kintone::Query.new { field('作成日時') >= today }, '作成日時 >= TODAY()'],
167
+ [Kintone::Query.new { field('作成日時') >= this_month }, '作成日時 >= THIS_MONTH()'],
168
+ [Kintone::Query.new { field('作成日時') >= last_month }, '作成日時 >= LAST_MONTH()'],
169
+ [Kintone::Query.new { field('作成日時') >= this_year }, '作成日時 >= THIS_YEAR()'],
170
+ [Kintone::Query.new { field(:number) >= 100 }, 'number >= 100']
171
+ ]
172
+ end
173
+
174
+ with_them do
175
+ it { expect(subject).to eq result }
176
+ end
177
+ end
178
+
179
+ context '>=', 'with f' do
180
+ where(:target, :result) do
181
+ [
182
+ [Kintone::Query.new { f(:text) >= '"Hello, world."' }, 'text >= "Hello, world."'],
183
+ [Kintone::Query.new { f(:text) >= 'Hello, world.' }, 'text >= "Hello, world."'],
184
+ [Kintone::Query.new { f('作成日時') >= now }, '作成日時 >= NOW()'],
185
+ [Kintone::Query.new { f('作成日時') >= today }, '作成日時 >= TODAY()'],
186
+ [Kintone::Query.new { f('作成日時') >= this_month }, '作成日時 >= THIS_MONTH()'],
187
+ [Kintone::Query.new { f('作成日時') >= last_month }, '作成日時 >= LAST_MONTH()'],
188
+ [Kintone::Query.new { f('作成日時') >= this_year }, '作成日時 >= THIS_YEAR()'],
189
+ [Kintone::Query.new { f(:number) >= 100 }, 'number >= 100']
190
+ ]
191
+ end
192
+
193
+ with_them do
194
+ it { expect(subject).to eq result }
195
+ end
196
+ end
197
+
198
+ context '<=', 'with field' do
199
+ where(:target, :result) do
200
+ [
201
+ [Kintone::Query.new { field(:text) <= '"Hello, world."' }, 'text <= "Hello, world."'],
202
+ [Kintone::Query.new { field(:text) <= 'Hello, world.' }, 'text <= "Hello, world."'],
203
+ [Kintone::Query.new { field('作成日時') <= now }, '作成日時 <= NOW()'],
204
+ [Kintone::Query.new { field('作成日時') <= today }, '作成日時 <= TODAY()'],
205
+ [Kintone::Query.new { field('作成日時') <= this_month }, '作成日時 <= THIS_MONTH()'],
206
+ [Kintone::Query.new { field('作成日時') <= last_month }, '作成日時 <= LAST_MONTH()'],
207
+ [Kintone::Query.new { field('作成日時') <= this_year }, '作成日時 <= THIS_YEAR()'],
208
+ [Kintone::Query.new { field(:number) <= 100 }, 'number <= 100']
209
+ ]
210
+ end
211
+
212
+ with_them do
213
+ it { expect(subject).to eq result }
214
+ end
215
+ end
216
+
217
+ context '<=', 'with f' do
218
+ where(:target, :result) do
219
+ [
220
+ [Kintone::Query.new { f(:text) <= '"Hello, world."' }, 'text <= "Hello, world."'],
221
+ [Kintone::Query.new { f(:text) <= 'Hello, world.' }, 'text <= "Hello, world."'],
222
+ [Kintone::Query.new { f('作成日時') <= now }, '作成日時 <= NOW()'],
223
+ [Kintone::Query.new { f('作成日時') <= today }, '作成日時 <= TODAY()'],
224
+ [Kintone::Query.new { f('作成日時') <= this_month }, '作成日時 <= THIS_MONTH()'],
225
+ [Kintone::Query.new { f('作成日時') <= last_month }, '作成日時 <= LAST_MONTH()'],
226
+ [Kintone::Query.new { f('作成日時') <= this_year }, '作成日時 <= THIS_YEAR()'],
227
+ [Kintone::Query.new { f(:number) <= 100 }, 'number <= 100']
228
+ ]
229
+ end
230
+
231
+ with_them do
232
+ it { expect(subject).to eq result }
233
+ end
234
+ end
235
+
236
+ context 'in', 'with field' do
237
+ where(:target, :result) do
238
+ [
239
+ [Kintone::Query.new { field(:dropdown).in(['"A"', '"B"']) }, 'dropdown in ("A", "B")'],
240
+ [Kintone::Query.new { field(:dropdown).in(%w(A B)) }, 'dropdown in ("A", "B")'],
241
+ [Kintone::Query.new { field(:dropdown).in([:A, :B]) }, 'dropdown in ("A", "B")'],
242
+ [Kintone::Query.new { field(:dropdown).in([100, 200]) }, 'dropdown in (100, 200)'],
243
+ [Kintone::Query.new { field('作成者').in([login_user]) }, '作成者 in (LOGINUSER())']
244
+ ]
245
+ end
246
+
247
+ with_them do
248
+ it { expect(subject).to eq result }
249
+ end
250
+ end
251
+
252
+ context 'in', 'with f' do
253
+ where(:target, :result) do
254
+ [
255
+ [
256
+ Kintone::Query.new { f(:dropdown).in(['"A"', '"B"']) },
257
+ 'dropdown in ("A", "B")'
258
+ ],
259
+ [
260
+ Kintone::Query.new { f(:dropdown).in(%w(A B)) },
261
+ 'dropdown in ("A", "B")'
262
+ ],
263
+ [
264
+ Kintone::Query.new { f(:dropdown).in([:A, :B]) },
265
+ 'dropdown in ("A", "B")'
266
+ ],
267
+ [
268
+ Kintone::Query.new { f(:dropdown).in([100, 200]) },
269
+ 'dropdown in (100, 200)'
270
+ ],
271
+ [
272
+ Kintone::Query.new { f('作成者').in([login_user]) },
273
+ '作成者 in (LOGINUSER())'
274
+ ],
275
+ [
276
+ Kintone::Query.new { field('組織').in([primary_organization]) },
277
+ '組織 in (PRIMARY_ORGANIZATION())'
278
+ ]
279
+ ]
280
+ end
281
+
282
+ with_them do
283
+ it { expect(subject).to eq result }
284
+ end
285
+ end
286
+
287
+ context 'not in', 'with field' do
288
+ where(:target, :result) do
289
+ [
290
+ [
291
+ Kintone::Query.new { field(:dropdown).not_in(['"A"', '"B"']) },
292
+ 'dropdown not in ("A", "B")'
293
+ ],
294
+ [
295
+ Kintone::Query.new { field(:dropdown).not_in(%w(A B)) },
296
+ 'dropdown not in ("A", "B")'
297
+ ],
298
+ [
299
+ Kintone::Query.new { field(:dropdown).not_in([:A, :B]) },
300
+ 'dropdown not in ("A", "B")'
301
+ ],
302
+ [
303
+ Kintone::Query.new { field(:dropdown).not_in([100, 200]) },
304
+ 'dropdown not in (100, 200)'
305
+ ],
306
+ [
307
+ Kintone::Query.new { field('作成者').not_in([login_user]) },
308
+ '作成者 not in (LOGINUSER())'
309
+ ],
310
+ [
311
+ Kintone::Query.new { field('組織').not_in([primary_organization]) },
312
+ '組織 not in (PRIMARY_ORGANIZATION())'
313
+ ]
314
+ ]
315
+ end
316
+
317
+ with_them do
318
+ it { expect(subject).to eq result }
319
+ end
320
+ end
321
+
322
+ context 'not in', 'with field' do
323
+ where(:target, :result) do
324
+ [
325
+ [
326
+ Kintone::Query.new { f(:dropdown).not_in(['"A"', '"B"']) },
327
+ 'dropdown not in ("A", "B")'
328
+ ],
329
+ [
330
+ Kintone::Query.new { f(:dropdown).not_in(%w(A B)) },
331
+ 'dropdown not in ("A", "B")'
332
+ ],
333
+ [
334
+ Kintone::Query.new { f(:dropdown).not_in([:A, :B]) },
335
+ 'dropdown not in ("A", "B")'
336
+ ],
337
+ [
338
+ Kintone::Query.new { f(:dropdown).not_in([100, 200]) },
339
+ 'dropdown not in (100, 200)'
340
+ ],
341
+ [
342
+ Kintone::Query.new { f('作成者').not_in([login_user]) },
343
+ '作成者 not in (LOGINUSER())'
344
+ ]
345
+ ]
346
+ end
347
+
348
+ with_them do
349
+ it { expect(subject).to eq result }
350
+ end
351
+ end
352
+
353
+ context 'like', 'with field' do
354
+ where(:target, :result) do
355
+ [
356
+ [Kintone::Query.new { field(:text).like('Hello') }, 'text like "Hello"'],
357
+ [Kintone::Query.new { field(:text).like('"Hello"') }, 'text like "Hello"'],
358
+ [Kintone::Query.new { field(:text).like(:Hello) }, 'text like "Hello"']
359
+ ]
360
+ end
361
+
362
+ with_them do
363
+ it { expect(subject).to eq result }
364
+ end
365
+ end
366
+
367
+ context 'like', 'with f' do
368
+ where(:target, :result) do
369
+ [
370
+ [Kintone::Query.new { f(:text).like('Hello') }, 'text like "Hello"'],
371
+ [Kintone::Query.new { f(:text).like('"Hello"') }, 'text like "Hello"'],
372
+ [Kintone::Query.new { f(:text).like(:Hello) }, 'text like "Hello"']
373
+ ]
374
+ end
375
+
376
+ with_them do
377
+ it { expect(subject).to eq result }
378
+ end
379
+ end
380
+
381
+ context 'not like', 'with field' do
382
+ where(:target, :result) do
383
+ [
384
+ [Kintone::Query.new { field(:text).not_like('Hello') }, 'text not like "Hello"'],
385
+ [Kintone::Query.new { field(:text).not_like('"Hello"') }, 'text not like "Hello"'],
386
+ [Kintone::Query.new { field(:text).not_like(:Hello) }, 'text not like "Hello"']
387
+ ]
388
+ end
389
+
390
+ with_them do
391
+ it { expect(subject).to eq result }
392
+ end
393
+ end
394
+
395
+ context 'not like', 'with f' do
396
+ where(:target, :result) do
397
+ [
398
+ [Kintone::Query.new { f(:text).not_like('Hello') }, 'text not like "Hello"'],
399
+ [Kintone::Query.new { f(:text).not_like('"Hello"') }, 'text not like "Hello"'],
400
+ [Kintone::Query.new { f(:text).not_like(:Hello) }, 'text not like "Hello"']
401
+ ]
402
+ end
403
+
404
+ with_them do
405
+ it { expect(subject).to eq result }
406
+ end
407
+ end
408
+
409
+ context 'and!' do
410
+ let(:target) do
411
+ Kintone::Query.new do
412
+ field(:text).like 'Hello'
413
+ and!
414
+ field(:number) > 100
415
+ end
416
+ end
417
+
418
+ def result
419
+ 'text like "Hello" and number > 100'
420
+ end
421
+
422
+ it { expect(subject).to eq result }
423
+ end
424
+
425
+ context 'or!' do
426
+ let(:target) do
427
+ Kintone::Query.new do
428
+ field(:text).like 'Hello'
429
+ or!
430
+ field(:number) > 100
431
+ end
432
+ end
433
+
434
+ def result
435
+ 'text like "Hello" or number > 100'
436
+ end
437
+
438
+ it { expect(subject).to eq result }
439
+ end
440
+
441
+ context 'precede' do
442
+ let(:target) do
443
+ Kintone::Query.new do
444
+ precede do
445
+ field(:text).like 'Hello'
446
+ or!
447
+ field(:number) > 100
448
+ end
449
+ and!
450
+ precede do
451
+ field(:text2).not_like 'Hello'
452
+ and!
453
+ field(:number2) <= 100
454
+ end
455
+ end
456
+ end
457
+
458
+ def result
459
+ '(text like "Hello" or number > 100) and (text2 not like "Hello" and number2 <= 100)'
460
+ end
461
+
462
+ it { expect(subject).to eq result }
463
+ end
464
+
465
+ context 'order by' do
466
+ where(:target, :result) do
467
+ [
468
+ [Kintone::Query.new { order_by(:record_id, :asc) }, 'order by record_id asc'],
469
+ [Kintone::Query.new { order_by(:record_id, :desc) }, 'order by record_id desc'],
470
+ [Kintone::Query.new { order_by(:record_id) }, 'order by record_id asc'],
471
+ [Kintone::Query.new { order_by('作成日時', :desc) }, 'order by 作成日時 desc']
472
+ ]
473
+ end
474
+
475
+ with_them do
476
+ it { expect(subject).to eq result }
477
+ end
478
+ end
479
+
480
+ context 'limit' do
481
+ where(:target, :result) do
482
+ [
483
+ [Kintone::Query.new { limit(20) }, 'limit 20'],
484
+ [Kintone::Query.new { limit('20') }, 'limit 20']
485
+ ]
486
+ end
487
+
488
+ with_them do
489
+ it { expect(subject).to eq result }
490
+ end
491
+ end
492
+
493
+ context 'offset' do
494
+ where(:target, :result) do
495
+ [
496
+ [Kintone::Query.new { offset(30) }, 'offset 30'],
497
+ [Kintone::Query.new { offset('30') }, 'offset 30']
498
+ ]
499
+ end
500
+
501
+ with_them do
502
+ it { expect(subject).to eq result }
503
+ end
504
+ end
505
+ end
506
+ end