fauna 2.3.0 → 2.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d8996ca66ac2a5d3c0eb4d33251e3148e4a1e4de
4
- data.tar.gz: 4b29fdaad2dc9b4a6f704bd5e6df7111abf25b0a
3
+ metadata.gz: aeba4b71fb3aa58412e5f78514faa422f3d71eab
4
+ data.tar.gz: 0a53bd599a46461131c1c552b7ef6e1893833cd0
5
5
  SHA512:
6
- metadata.gz: fa414baf0fff3c8a3e07c14fb7ec3ce000bbb4d306be7b1286be7c9495281584398d34b71505a4bc6d58ad34ebf8620f521bdc415c7822992be35f9018304045
7
- data.tar.gz: e40f66fc8c23f1fb10d5e918e4788142503a017e76c74c444768c7b0efc7a2d3b6b76f729b3583d5f740c4531f782f890b21243f7402f8e58c4bfda9ae151be9
6
+ metadata.gz: 5f4b77c6451c409b30135a918ddacd7bc6751658d9ddb4c38733b8503032be6c148a0cf9e78b105259e444150b68a0a0054b909fac48ce977c2386136afaf9d4
7
+ data.tar.gz: 524c60bcada18bc69e7411cac1fbc17afc58624aaa448763bf38f467b43edd4063f5a5d0eed6b0c9ba834fb4f5bb3d452725dbd4e83c300489cf49ad45182317
data/CHANGELOG CHANGED
@@ -1,3 +1,9 @@
1
+ 2.4.0
2
+ * Handle HTTP errors 502 and 504 as `Fauna::UnavailableError`.
3
+ * Added support for user-defined functions.
4
+ * Added support for the `@query` type (via `Fauna::QueryV`).
5
+ * Added `create_function`, `function`, and `call` query functions.
6
+
1
7
  2.3.0
2
8
  * Change default domain to `db.fauna.com`.
3
9
  * Added `key_from_secret` and `at` query functions.
@@ -215,9 +215,7 @@ module Fauna
215
215
  end_time = Time.now
216
216
 
217
217
  message = e.class.name
218
- unless e.message.nil?
219
- message += ": #{e.message}"
220
- end
218
+ message += ": #{e.message}" unless e.message.nil?
221
219
 
222
220
  request_result = RequestResult.new(self,
223
221
  action, path, query, data,
@@ -238,10 +236,6 @@ module Fauna
238
236
 
239
237
  @observer.call(request_result) unless @observer.nil?
240
238
 
241
- if response_json.nil? && response.status != 503
242
- fail UnexpectedError.new('Invalid JSON.', request_result)
243
- end
244
-
245
239
  FaunaError.raise_for_status_code(request_result)
246
240
  UnexpectedError.get_or_raise request_result, response_content, :resource
247
241
  end
@@ -48,8 +48,12 @@ module Fauna
48
48
  fail MethodNotAllowed.new(request_result)
49
49
  when 500
50
50
  fail InternalError.new(request_result)
51
+ when 502
52
+ fail UnavailableError.new(request_result)
51
53
  when 503
52
54
  fail UnavailableError.new(request_result)
55
+ when 504
56
+ fail UnavailableError.new(request_result)
53
57
  else
54
58
  fail UnexpectedError.new('Unexpected status code.', request_result)
55
59
  end
@@ -63,6 +67,10 @@ module Fauna
63
67
  @request_result = request_result
64
68
 
65
69
  begin
70
+ if request_result.response_content.nil?
71
+ fail UnexpectedError.new('Invalid JSON.', request_result)
72
+ end
73
+
66
74
  errors_raw = UnexpectedError.get_or_raise request_result, request_result.response_content, :errors
67
75
  @errors = catch :invalid_response do
68
76
  throw :invalid_response unless errors_raw.is_a? Array
@@ -89,7 +97,7 @@ module Fauna
89
97
  msg
90
98
  end.join(' ')
91
99
  rescue UnexpectedError => e
92
- if request_result.status_code != 503
100
+ unless self.is_a?(UnavailableError) && [502, 503, 504].include?(request_result.status_code)
93
101
  raise e
94
102
  end
95
103
 
@@ -126,8 +134,7 @@ module Fauna
126
134
  # failure within the database.
127
135
  class InternalError < FaunaError; end
128
136
 
129
- ##
130
- # An exception thrown if FaunaDB responds with an HTTP 503.
137
+ # An exception thrown if FaunaDB responds with an HTTP 502, 503, or 504.
131
138
  class UnavailableError < FaunaError; end
132
139
 
133
140
  # Data for one error returned by the server.
@@ -22,6 +22,8 @@ module Fauna
22
22
  Date.iso8601 obj[:@date]
23
23
  elsif obj.key? :@bytes
24
24
  Bytes.from_base64 obj[:@bytes]
25
+ elsif obj.key? :@query
26
+ QueryV.new deserialize(obj[:@query])
25
27
  else
26
28
  Hash[obj.collect { |k, v| [k, deserialize(v)] }]
27
29
  end
@@ -128,4 +128,36 @@ module Fauna
128
128
  new(Base64.urlsafe_decode64(enc))
129
129
  end
130
130
  end
131
+
132
+ ##
133
+ # A QueryV.
134
+ #
135
+ # Reference: {FaunaDB Special Types}[https://fauna.com/documentation/queries-values-special_types]
136
+ class QueryV
137
+ # The raw query hash.
138
+ attr_accessor :value
139
+
140
+ ##
141
+ # Creates a new QueryV with the given parameters.
142
+ #
143
+ # +params+:: Hash of parameters to build the QueryV with.
144
+ #
145
+ # Reference: {FaunaDB Special Types}[https://fauna.com/documentation/queries-values-special_types]
146
+ def initialize(params = {})
147
+ self.value = params
148
+ end
149
+
150
+ # Converts the QueryV to Hash form.
151
+ def to_hash
152
+ { :@query => value }
153
+ end
154
+
155
+ # Returns +true+ if +other+ is a QueryV and contains the same value.
156
+ def ==(other)
157
+ return false unless other.is_a? QueryV
158
+ value == other.value
159
+ end
160
+
161
+ alias_method :eql?, :==
162
+ end
131
163
  end
@@ -64,6 +64,14 @@ module Fauna
64
64
  Expr.new object: Expr.wrap_values(fields)
65
65
  end
66
66
 
67
+ ##
68
+ # A query expression
69
+ #
70
+ # Reference: {FaunaDB Basic Forms}[https://fauna.com/documentation/queries#basic_forms]
71
+ def query(expr)
72
+ Expr.new query: Expr.wrap(expr)
73
+ end
74
+
67
75
  # :section: Basic forms
68
76
 
69
77
  ##
@@ -174,6 +182,14 @@ module Fauna
174
182
  Expr.new lambda: Expr.wrap(var), expr: Expr.wrap(expr)
175
183
  end
176
184
 
185
+ ##
186
+ # A call expression
187
+ #
188
+ # Reference: {FaunaDB Basic Forms}[https://fauna.com/documentation/queries#basic_forms]
189
+ def call(name, *args)
190
+ Expr.new call: Expr.wrap(name), arguments: Expr.wrap_varargs(args)
191
+ end
192
+
177
193
  # :section: Collection Functions
178
194
 
179
195
  ##
@@ -357,6 +373,14 @@ module Fauna
357
373
  Expr.new create_key: Expr.wrap(params)
358
374
  end
359
375
 
376
+ ##
377
+ # A create function expression
378
+ #
379
+ # Reference: {FaunaDB Write functions}[https://fauna.com/documentation/queries#write_functions]
380
+ def create_function(params)
381
+ Expr.new create_function: Expr.wrap(params)
382
+ end
383
+
360
384
  # :section: Set Functions
361
385
 
362
386
  ##
@@ -518,6 +542,14 @@ module Fauna
518
542
  Expr.new index: Expr.wrap(name)
519
543
  end
520
544
 
545
+ ##
546
+ # A function function
547
+ #
548
+ # Reference: {FaunaDB Miscellaneous Functions}[https://fauna.com/documentation#queries-misc_functions]
549
+ def function(name)
550
+ Expr.new function: Expr.wrap(name)
551
+ end
552
+
521
553
  ##
522
554
  # An equals function
523
555
  #
@@ -1,4 +1,4 @@
1
1
  module Fauna
2
2
  # The version of the Fauna gem
3
- VERSION = '2.3.0'.freeze
3
+ VERSION = '2.4.0'.freeze
4
4
  end
@@ -128,11 +128,26 @@ RSpec.describe 'Fauna Errors' do
128
128
  expect { stub_client.get '' }.to raise_fauna_error(Fauna::UnavailableError, 'unavailable')
129
129
  end
130
130
 
131
+ it 'handles upstream 502' do
132
+ stub_client = stub_get 502, 'Bad gateway'
133
+ expect { stub_client.get '' }.to raise_error(Fauna::UnavailableError, 'Bad gateway')
134
+ end
135
+
131
136
  it 'handles upstream 503' do
132
137
  stub_client = stub_get 503, 'Unable to reach server'
133
138
  expect { stub_client.get '' }.to raise_error(Fauna::UnavailableError, 'Unable to reach server')
134
139
  end
135
140
 
141
+ it 'handles upstream 504' do
142
+ stub_client = stub_get 504, 'Server timeout'
143
+ expect { stub_client.get '' }.to raise_error(Fauna::UnavailableError, 'Server timeout')
144
+ end
145
+
146
+ it 'handles upstream json 503' do
147
+ stub_client = stub_get 503, '{ "error" : "service unavailable" }'
148
+ expect { stub_client.get '' }.to raise_error(Fauna::UnavailableError, '{ "error" : "service unavailable" }')
149
+ end
150
+
136
151
  it 'handles timeout error' do
137
152
  stub_client = stub_error Faraday::TimeoutError.new('timeout')
138
153
  expect { stub_client.get '' }.to raise_error(Fauna::UnavailableError, 'Faraday::TimeoutError: timeout')
@@ -19,6 +19,13 @@ RSpec.describe Fauna::FaunaJson do
19
19
  expect(Fauna::FaunaJson.deserialize(data)).to eq(obj)
20
20
  end
21
21
 
22
+ it 'deserializes query' do
23
+ query = { lambda: 'a', expr: { add: [{ var: 'a' }, 1] } }
24
+ obj = Fauna::QueryV.new(query)
25
+
26
+ expect(Fauna::FaunaJson.deserialize(:@query => query)).to eq(obj)
27
+ end
28
+
22
29
  it 'deserializes obj' do
23
30
  obj = { a: random_string, b: random_string }
24
31
  data = { :@obj => obj }
@@ -88,6 +95,13 @@ RSpec.describe Fauna::FaunaJson do
88
95
  expect(Fauna::FaunaJson.serialize(obj)).to eq(data)
89
96
  end
90
97
 
98
+ it 'serializes query' do
99
+ query = { lambda: 'a', expr: { add: [{ var: 'a' }, 1] } }
100
+ obj = Fauna::QueryV.new(query)
101
+
102
+ expect(Fauna::FaunaJson.serialize(obj)).to eq(:@query => query)
103
+ end
104
+
91
105
  it 'serializes expr' do
92
106
  data = { a: random_string, b: random_number }
93
107
  obj = Fauna::Query::Expr.new(data)
@@ -123,6 +123,13 @@ RSpec.describe Fauna::Query do
123
123
  end
124
124
  end
125
125
 
126
+ describe '#query' do
127
+ it 'wraps fields in query' do
128
+ data = Fauna::Query::Expr.new(lambda: random_string, expr: Fauna::Query::Expr.new(add: Fauna::Query::Expr.wrap([1, 1])))
129
+ expect(Fauna::Query.query(data).raw).to eq(query: data)
130
+ end
131
+ end
132
+
126
133
  describe '#at' do
127
134
  it 'performs at for given expression' do
128
135
  instance = create_instance
@@ -215,6 +222,38 @@ RSpec.describe Fauna::Query do
215
222
  end
216
223
  end
217
224
 
225
+ describe '#call' do
226
+ it 'performs called function' do
227
+ test_func = client.query do
228
+ func_body = lambda do |x|
229
+ [add(x, 1), add(x, 2), add(x, 3)]
230
+ end
231
+
232
+ create ref('functions'), name: 'call_func_test', body: query(func_body)
233
+ end
234
+
235
+ x = random_number
236
+
237
+ expect(client.query { call(test_func[:ref], x) }).to eq([x + 1, x + 2, x + 3])
238
+ end
239
+
240
+ it 'handles multiple arguments' do
241
+ test_func = client.query do
242
+ func_body = lambda do |x, y, z|
243
+ [multiply(x, 2), multiply(y, 3), multiply(z, 4)]
244
+ end
245
+
246
+ create ref('functions'), name: 'call_multiarg_test', body: query(func_body)
247
+ end
248
+
249
+ x = random_number
250
+ y = random_number
251
+ z = random_number
252
+
253
+ expect(client.query { call(test_func[:ref], x, y, z) }).to eq([x * 2, y * 3, z * 4])
254
+ end
255
+ end
256
+
218
257
  describe '#map' do
219
258
  it 'performs map from expression' do
220
259
  input = (1..3).collect { random_number }
@@ -458,6 +497,16 @@ RSpec.describe Fauna::Query do
458
497
  end
459
498
  end
460
499
 
500
+ describe '#create_function' do
501
+ it 'creates a function' do
502
+ # Create a function
503
+ ref = client.query { create_function(name: random_string, body: query(lambda { |a| add(a, 1) })) }[:ref]
504
+
505
+ # Assert it was created
506
+ expect(client.query { exists(ref) }).to be(true)
507
+ end
508
+ end
509
+
461
510
  describe 'sets' do
462
511
  before do
463
512
  @x_value = random_number
@@ -656,7 +705,7 @@ RSpec.describe Fauna::Query do
656
705
  end
657
706
  end
658
707
 
659
- describe '#class' do
708
+ describe '#class_' do
660
709
  it 'gets an existing class' do
661
710
  # Create a class
662
711
  name = random_string
@@ -679,6 +728,17 @@ RSpec.describe Fauna::Query do
679
728
  end
680
729
  end
681
730
 
731
+ describe '#function' do
732
+ it 'gets an existing function' do
733
+ # Create a function
734
+ name = random_string
735
+ ref = client.query { create_function(name: name, body: query(lambda { |a| add(a, 1) })) }[:ref]
736
+
737
+ # Get the function ref
738
+ expect(client.query { function(name) }).to eq(ref)
739
+ end
740
+ end
741
+
682
742
  describe '#equals' do
683
743
  it 'performs equals' do
684
744
  expect(client.query { equals(1, 1, 1) }).to be(true)
@@ -0,0 +1,25 @@
1
+ RSpec.describe Fauna::QueryV do
2
+ describe '#==' do
3
+ it 'equals same query' do
4
+ lambda = Fauna::Query.expr { lambda_expr([:a], add(var(:a), var(:a))) }
5
+ query = Fauna::QueryV.new(lambda)
6
+
7
+ expect(query).to eq(Fauna::QueryV.new(lambda))
8
+ end
9
+
10
+ it 'does not equal different query' do
11
+ lambda = Fauna::Query.expr { lambda_expr([:a], add(var(:a), var(:a))) }
12
+ query = Fauna::QueryV.new(lambda)
13
+
14
+ other_lambda = Fauna::Query.expr { lambda_expr([:b], multiply(var(:b), var(:b))) }
15
+ expect(query).not_to eq(Fauna::QueryV.new(other_lambda))
16
+ end
17
+
18
+ it 'does not equal other type' do
19
+ lambda = Fauna::Query.expr { lambda_expr([:a], add(var(:a), var(:a))) }
20
+ query = Fauna::QueryV.new(lambda)
21
+
22
+ expect(query).not_to eq(lambda)
23
+ end
24
+ end
25
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fauna
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.3.0
4
+ version: 2.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fauna, Inc.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-03-08 00:00:00.000000000 Z
11
+ date: 2017-09-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -130,6 +130,7 @@ files:
130
130
  - spec/json_spec.rb
131
131
  - spec/page_spec.rb
132
132
  - spec/query_spec.rb
133
+ - spec/queryv_spec.rb
133
134
  - spec/ref_spec.rb
134
135
  - spec/setref_spec.rb
135
136
  - spec/spec_helper.rb
@@ -159,21 +160,22 @@ required_rubygems_version: !ruby/object:Gem::Requirement
159
160
  version: '0'
160
161
  requirements: []
161
162
  rubyforge_project:
162
- rubygems_version: 2.6.10
163
+ rubygems_version: 2.4.5
163
164
  signing_key:
164
165
  specification_version: 4
165
166
  summary: FaunaDB Ruby driver
166
167
  test_files:
167
- - spec/util_spec.rb
168
+ - spec/queryv_spec.rb
169
+ - spec/client_spec.rb
170
+ - spec/setref_spec.rb
171
+ - spec/json_spec.rb
168
172
  - spec/context_spec.rb
169
173
  - spec/client_logger_spec.rb
170
174
  - spec/page_spec.rb
171
- - spec/query_spec.rb
172
- - spec/fauna_helper.rb
173
- - spec/client_spec.rb
174
- - spec/errors_spec.rb
175
- - spec/setref_spec.rb
175
+ - spec/spec_helper.rb
176
+ - spec/util_spec.rb
176
177
  - spec/ref_spec.rb
177
178
  - spec/bytes_spec.rb
178
- - spec/spec_helper.rb
179
- - spec/json_spec.rb
179
+ - spec/errors_spec.rb
180
+ - spec/query_spec.rb
181
+ - spec/fauna_helper.rb