acfs 0.50.0 → 1.0.0.dev.1.b297

Sign up to get free protection for your applications and to get access to all the features.
Files changed (90) hide show
  1. checksums.yaml +13 -5
  2. data/CHANGELOG.md +0 -80
  3. data/README.md +9 -8
  4. data/acfs.gemspec +7 -7
  5. data/lib/acfs.rb +5 -7
  6. data/lib/acfs/adapter/base.rb +2 -0
  7. data/lib/acfs/adapter/typhoeus.rb +13 -17
  8. data/lib/acfs/collections/paginatable.rb +10 -10
  9. data/lib/acfs/configuration.rb +5 -4
  10. data/lib/acfs/errors.rb +9 -10
  11. data/lib/acfs/global.rb +7 -16
  12. data/lib/acfs/location.rb +11 -11
  13. data/lib/acfs/middleware/base.rb +2 -1
  14. data/lib/acfs/middleware/json_decoder.rb +16 -0
  15. data/lib/acfs/middleware/json_encoder.rb +20 -0
  16. data/lib/acfs/middleware/logger.rb +2 -0
  17. data/lib/acfs/middleware/msgpack_decoder.rb +26 -0
  18. data/lib/acfs/middleware/msgpack_encoder.rb +19 -0
  19. data/lib/acfs/middleware/print.rb +2 -0
  20. data/lib/acfs/operation.rb +5 -5
  21. data/lib/acfs/request.rb +4 -4
  22. data/lib/acfs/request/callbacks.rb +3 -2
  23. data/lib/acfs/resource.rb +2 -2
  24. data/lib/acfs/resource/attributes.rb +38 -11
  25. data/lib/acfs/resource/attributes/base.rb +19 -10
  26. data/lib/acfs/resource/attributes/boolean.rb +8 -10
  27. data/lib/acfs/resource/attributes/date_time.rb +9 -6
  28. data/lib/acfs/resource/attributes/float.rb +5 -11
  29. data/lib/acfs/resource/attributes/integer.rb +5 -7
  30. data/lib/acfs/resource/attributes/list.rb +6 -13
  31. data/lib/acfs/resource/attributes/string.rb +5 -3
  32. data/lib/acfs/resource/attributes/uuid.rb +17 -8
  33. data/lib/acfs/resource/dirty.rb +15 -3
  34. data/lib/acfs/resource/loadable.rb +1 -0
  35. data/lib/acfs/resource/locatable.rb +4 -0
  36. data/lib/acfs/resource/operational.rb +2 -0
  37. data/lib/acfs/resource/persistence.rb +17 -17
  38. data/lib/acfs/resource/query_methods.rb +7 -8
  39. data/lib/acfs/resource/service.rb +2 -0
  40. data/lib/acfs/resource/validation.rb +4 -4
  41. data/lib/acfs/response.rb +2 -1
  42. data/lib/acfs/response/formats.rb +3 -2
  43. data/lib/acfs/response/status.rb +5 -3
  44. data/lib/acfs/runner.rb +11 -21
  45. data/lib/acfs/service.rb +6 -4
  46. data/lib/acfs/service/middleware.rb +30 -20
  47. data/lib/acfs/singleton_resource.rb +2 -0
  48. data/lib/acfs/stub.rb +21 -30
  49. data/lib/acfs/util.rb +1 -1
  50. data/lib/acfs/version.rb +4 -6
  51. data/spec/acfs/adapter/typhoeus_spec.rb +4 -12
  52. data/spec/acfs/collection_spec.rb +33 -45
  53. data/spec/acfs/configuration_spec.rb +1 -9
  54. data/spec/acfs/global_spec.rb +3 -21
  55. data/spec/acfs/middleware/json_decoder_spec.rb +45 -0
  56. data/spec/acfs/middleware/msgpack_decoder_spec.rb +36 -0
  57. data/spec/acfs/request/callbacks_spec.rb +8 -8
  58. data/spec/acfs/request_spec.rb +5 -5
  59. data/spec/acfs/resource/attributes/boolean_spec.rb +9 -40
  60. data/spec/acfs/resource/attributes/date_time_spec.rb +35 -29
  61. data/spec/acfs/resource/attributes/float_spec.rb +9 -48
  62. data/spec/acfs/resource/attributes/list_spec.rb +19 -43
  63. data/spec/acfs/resource/attributes/uuid_spec.rb +54 -31
  64. data/spec/acfs/resource/attributes_spec.rb +31 -17
  65. data/spec/acfs/resource/locatable_spec.rb +2 -2
  66. data/spec/acfs/resource/persistance_spec.rb +34 -65
  67. data/spec/acfs/resource/query_methods_spec.rb +90 -97
  68. data/spec/acfs/resource/validation_spec.rb +5 -4
  69. data/spec/acfs/response/formats_spec.rb +4 -4
  70. data/spec/acfs/response/status_spec.rb +1 -1
  71. data/spec/acfs/runner_spec.rb +3 -28
  72. data/spec/acfs/service/middleware_spec.rb +20 -4
  73. data/spec/acfs/service_spec.rb +5 -3
  74. data/spec/acfs/singleton_resource_spec.rb +2 -1
  75. data/spec/acfs/stub_spec.rb +22 -137
  76. data/spec/acfs_spec.rb +19 -22
  77. data/spec/spec_helper.rb +2 -3
  78. data/spec/support/service.rb +6 -10
  79. data/spec/support/shared/find_callbacks.rb +7 -7
  80. metadata +36 -43
  81. data/lib/acfs/middleware/json.rb +0 -27
  82. data/lib/acfs/middleware/msgpack.rb +0 -30
  83. data/lib/acfs/middleware/serializer.rb +0 -39
  84. data/lib/acfs/resource/attributes/dict.rb +0 -37
  85. data/lib/acfs/service/middleware/stack.rb +0 -63
  86. data/spec/acfs/middleware/json_spec.rb +0 -63
  87. data/spec/acfs/middleware/msgpack_spec.rb +0 -60
  88. data/spec/acfs/operation_spec.rb +0 -10
  89. data/spec/acfs/resource/attributes/dict_spec.rb +0 -75
  90. data/spec/acfs/resource/attributes/integer_spec.rb +0 -34
@@ -11,59 +11,47 @@ describe Acfs::Collection do
11
11
 
12
12
  context 'without explicit page parameter' do
13
13
  before do
14
- stub_request(:get, 'http://users.example.org/users')
15
- .to_return response([{id: 1, name: 'Anon', age: 12, born_at: 'Berlin'}],
16
- headers: {
17
- 'X-Total-Pages' => '2',
18
- 'X-Total-Count' => '10'
19
- })
14
+ stub_request(:get, 'http://users.example.org/users').
15
+ to_return response([{id: 1, name: 'Anon', age: 12, born_at: 'Berlin'}],
16
+ headers: {'X-Total-Pages' => '2'})
20
17
  end
21
18
 
22
19
  its(:total_pages) { should eq 2 }
23
20
  its(:current_page) { should eq 1 }
24
- its(:total_count) { should eq 10 }
25
21
  end
26
22
 
27
23
  context 'with page parameter' do
28
24
  let(:params) { {page: 2} }
29
25
  before do
30
- stub_request(:get, 'http://users.example.org/users?page=2')
31
- .to_return response([{id: 1, name: 'Anon', age: 12, born_at: 'Berlin'}],
32
- headers: {
33
- 'X-Total-Pages' => '2',
34
- 'X-Total-Count' => '10'
35
- })
26
+ stub_request(:get, 'http://users.example.org/users?page=2').
27
+ to_return response([{id: 1, name: 'Anon', age: 12, born_at: 'Berlin'}],
28
+ headers: {'X-Total-Pages' => '2'})
36
29
  end
37
30
 
38
31
  its(:total_pages) { should eq 2 }
39
32
  its(:current_page) { should eq 2 }
40
- its(:total_count) { should eq 10 }
41
33
  end
42
34
 
43
35
  context 'with non-numerical page parameter' do
44
36
  let(:params) { {page: 'e546f5'} }
45
37
  before do
46
- stub_request(:get, 'http://users.example.org/users?page=e546f5')
47
- .to_return response([{id: 1, name: 'Anon', age: 12, born_at: 'Berlin'}],
48
- headers: {
49
- 'X-Total-Pages' => '2',
50
- 'X-Total-Count' => '10'
51
- })
38
+ stub_request(:get, 'http://users.example.org/users?page=e546f5').
39
+ to_return response([{id: 1, name: 'Anon', age: 12, born_at: 'Berlin'}],
40
+ headers: {'X-Total-Pages' => '2'})
52
41
  end
53
42
 
54
43
  its(:total_pages) { should eq 2 }
55
44
  its(:current_page) { should eq 'e546f5' }
56
- its(:total_count) { should eq 10 }
57
45
  end
58
46
 
59
47
  describe '#next_page' do
60
48
  before do
61
- stub_request(:get, 'http://users.example.org/users')
62
- .to_return response([{id: 1, name: 'Anon', age: 12, born_at: 'Berlin'}],
63
- headers: {
64
- 'X-Total-Pages' => '2',
65
- 'Link' => '<http://users.example.org/users?page=2>; rel="next"'
66
- })
49
+ stub_request(:get, 'http://users.example.org/users').
50
+ to_return response([{id: 1, name: 'Anon', age: 12, born_at: 'Berlin'}],
51
+ headers: {
52
+ 'X-Total-Pages' => '2',
53
+ 'Link' => '<http://users.example.org/users?page=2>; rel="next"'
54
+ })
67
55
  end
68
56
  let!(:req) do
69
57
  stub_request(:get, 'http://users.example.org/users?page=2').to_return response([])
@@ -82,12 +70,12 @@ describe Acfs::Collection do
82
70
 
83
71
  describe '#prev_page' do
84
72
  before do
85
- stub_request(:get, 'http://users.example.org/users?page=2')
86
- .to_return response([{id: 2, name: 'Anno', age: 1604, born_at: 'Santa Maria'}],
87
- headers: {
88
- 'X-Total-Pages' => '2',
89
- 'Link' => '<http://users.example.org/users>; rel="prev"'
90
- })
73
+ stub_request(:get, 'http://users.example.org/users?page=2').
74
+ to_return response([{id: 2, name: 'Anno', age: 1604, born_at: 'Santa Maria'}],
75
+ headers: {
76
+ 'X-Total-Pages' => '2',
77
+ 'Link' => '<http://users.example.org/users>; rel="prev"'
78
+ })
91
79
  end
92
80
  let!(:req) do
93
81
  stub_request(:get, 'http://users.example.org/users').to_return response([])
@@ -106,12 +94,12 @@ describe Acfs::Collection do
106
94
 
107
95
  describe '#first_page' do
108
96
  before do
109
- stub_request(:get, 'http://users.example.org/users?page=2')
110
- .to_return response([{id: 2, name: 'Anno', age: 1604, born_at: 'Santa Maria'}],
111
- headers: {
112
- 'X-Total-Pages' => '2',
113
- 'Link' => '<http://users.example.org/users>; rel="first"'
114
- })
97
+ stub_request(:get, 'http://users.example.org/users?page=2').
98
+ to_return response([{id: 2, name: 'Anno', age: 1604, born_at: 'Santa Maria'}],
99
+ headers: {
100
+ 'X-Total-Pages' => '2',
101
+ 'Link' => '<http://users.example.org/users>; rel="first"'
102
+ })
115
103
  end
116
104
  let!(:req) do
117
105
  stub_request(:get, 'http://users.example.org/users').to_return response([])
@@ -130,12 +118,12 @@ describe Acfs::Collection do
130
118
 
131
119
  describe '#last_page' do
132
120
  before do
133
- stub_request(:get, 'http://users.example.org/users?page=2')
134
- .to_return response([{id: 2, name: 'Anno', age: 1604, born_at: 'Santa Maria'}],
135
- headers: {
136
- 'X-Total-Pages' => '2',
137
- 'Link' => '<http://users.example.org/users?page=12>; rel="last"'
138
- })
121
+ stub_request(:get, 'http://users.example.org/users?page=2').
122
+ to_return response([{id: 2, name: 'Anno', age: 1604, born_at: 'Santa Maria'}],
123
+ headers: {
124
+ 'X-Total-Pages' => '2',
125
+ 'Link' => '<http://users.example.org/users?page=12>; rel="last"'
126
+ })
139
127
  end
140
128
  let!(:req) do
141
129
  stub_request(:get, 'http://users.example.org/users?page=12').to_return response([])
@@ -1,6 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Acfs::Configuration do
4
+
4
5
  let(:cfg) { Acfs::Configuration.new }
5
6
  before { @configuration = Acfs::Configuration.current.dup }
6
7
  after { Acfs::Configuration.set @configuration }
@@ -39,13 +40,4 @@ describe Acfs::Configuration do
39
40
  end
40
41
  end
41
42
  end
42
-
43
- describe '#adapter' do
44
- let(:object) { Object.new }
45
-
46
- it 'should be a accessor' do
47
- cfg.adapter = object
48
- expect(cfg.adapter).to eq object
49
- end
50
- end
51
43
  end
@@ -14,11 +14,11 @@ describe ::Acfs::Global do
14
14
  let(:adapter) { ::NullAdapter.new }
15
15
  let(:runner) { double 'runner' }
16
16
  let(:collector) { NotificationCollector.new }
17
- let(:acfs) { Object.new.tap {|o| o.extend ::Acfs::Global } }
17
+ let(:acfs) { Object.new.tap { |o| o.extend ::Acfs::Global } }
18
18
 
19
19
  describe 'instrumentation' do
20
20
  before do
21
- # allow(runner).to receive(:start)
21
+ #allow(runner).to receive(:start)
22
22
  allow(acfs).to receive(:runner).and_return runner
23
23
  end
24
24
 
@@ -48,7 +48,7 @@ describe ::Acfs::Global do
48
48
  stub_request(:get, %r{http://users.example.org/users/\d+}).to_return(
49
49
  status: 200,
50
50
  body: '{}',
51
- headers: {'Content-Type' => 'application/json'})
51
+ headers: { 'Content-Type' => 'application/json' })
52
52
  end
53
53
 
54
54
  it 'should invoke when both resources' do
@@ -72,22 +72,4 @@ describe ::Acfs::Global do
72
72
  Acfs.run
73
73
  end
74
74
  end
75
-
76
- describe '#runner' do
77
- it 'returns per-thread runner' do
78
- runner1 = Thread.new { acfs.runner } .value
79
- runner2 = Thread.new { acfs.runner } .value
80
-
81
- expect(runner1).to_not equal runner2
82
- end
83
-
84
- it 'uses configurated adapter' do
85
- adapter = double :adapter
86
- expect(Acfs::Configuration.current).to receive(:adapter).and_return(-> { adapter })
87
-
88
- runner = Thread.new { acfs.runner }.value
89
-
90
- expect(runner.adapter).to equal adapter
91
- end
92
- end
93
75
  end
@@ -0,0 +1,45 @@
1
+ require 'spec_helper'
2
+
3
+ describe Acfs::Middleware::JsonDecoder do
4
+ let(:data) { [{id: 1, name: "Anon"},{id: 2, name:"John", friends: [ 1 ]}] }
5
+ let(:body) { data.to_param }
6
+ let(:headers) { {} }
7
+ let(:request) { Acfs::Request.new "fubar" }
8
+ let(:response) { Acfs::Response.new request, status: 200, headers: headers, body: body }
9
+ let(:decoder) { Acfs::Middleware::JsonDecoder.new lambda { |req| req } }
10
+
11
+ before do
12
+ decoder.call request
13
+ end
14
+
15
+ context 'with JSON response' do
16
+ let(:headers) { { 'Content-Type' => 'application/json; charset=utf-8' } }
17
+ let(:body) { data.to_json }
18
+
19
+ it 'should decode body data' do
20
+ request.complete! response
21
+
22
+ expect(response.data).to be == data.map(&:stringify_keys)
23
+ end
24
+ end
25
+
26
+ context 'with invalid JSON response' do
27
+ let(:headers) { { 'Content-Type' => 'application/json' } }
28
+ let(:body) { data.to_json[4..-4] }
29
+
30
+ it 'should raise an error' do
31
+ expect { request.complete! response }.to raise_error(MultiJson::LoadError)
32
+ end
33
+ end
34
+
35
+ context 'without JSON response' do
36
+ let(:headers) { { 'Content-Type' => 'application/text' } }
37
+ let(:body) { data.to_json }
38
+
39
+ it 'should not decode non-JSON encoded responses' do
40
+ request.complete! response
41
+
42
+ expect(response.data).to be_nil
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ describe Acfs::Middleware::MessagePackDecoder do
4
+ let(:data) { [{id: 1, name: "Anon"},{id: 2, name:"John", friends: [ 1 ]}] }
5
+ let(:body) { data.to_param }
6
+ let(:headers) { {} }
7
+ let(:request) { Acfs::Request.new "fubar" }
8
+ let(:response) { Acfs::Response.new request, status: 200, headers: headers, body: body }
9
+ let(:decoder) { Acfs::Middleware::MessagePackDecoder.new lambda { |req| req } }
10
+
11
+ before do
12
+ decoder.call request
13
+ end
14
+
15
+ context 'with Message Pack response' do
16
+ let(:headers) { { 'Content-Type' => 'application/x-msgpack' } }
17
+ let(:body) { MessagePack.pack data }
18
+
19
+ it 'should decode body data' do
20
+ request.complete! response
21
+
22
+ expect(response.data).to be == data.map(&:stringify_keys)
23
+ end
24
+ end
25
+
26
+ context 'without Message Pack response' do
27
+ let(:headers) { { 'Content-Type' => 'application/text' } }
28
+ let(:body) { data.to_json }
29
+
30
+ it 'should not decode non-MessagePack encoded responses' do
31
+ request.complete! response
32
+
33
+ expect(response.data).to be_nil
34
+ end
35
+ end
36
+ end
@@ -1,20 +1,20 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Acfs::Request::Callbacks do
4
- let(:callback) { ->(_res) {} }
4
+ let(:callback) { lambda { |res| } }
5
5
  let(:request) { Acfs::Request.new('fubar') }
6
6
 
7
7
  describe '#on_complete' do
8
8
  it 'should store a given callback' do
9
- request.on_complete(&callback)
9
+ request.on_complete &callback
10
10
 
11
11
  expect(request.callbacks).to have(1).item
12
12
  expect(request.callbacks[0]).to be == callback
13
13
  end
14
14
 
15
15
  it 'should store multiple callback' do
16
- request.on_complete {|_res| 'abc' }
17
- request.on_complete(&callback)
16
+ request.on_complete { |res| "abc" }
17
+ request.on_complete &callback
18
18
 
19
19
  expect(request.callbacks).to have(2).item
20
20
  expect(request.callbacks[0]).to be == callback
@@ -27,16 +27,16 @@ describe Acfs::Request::Callbacks do
27
27
  it 'should trigger registered callbacks with given response' do
28
28
  expect(callback).to receive(:call).with(response, kind_of(Proc))
29
29
 
30
- request.on_complete(&callback)
30
+ request.on_complete &callback
31
31
  request.complete! response
32
32
  end
33
33
 
34
34
  it 'should trigger multiple callback in reverted insertion order' do
35
35
  check = []
36
36
 
37
- request.on_complete {|res, nxt| check << 1; nxt.call res }
38
- request.on_complete {|res, nxt| check << 2; nxt.call res }
39
- request.on_complete {|res, nxt| check << 3; nxt.call res }
37
+ request.on_complete { |res, nxt| check << 1; nxt.call res }
38
+ request.on_complete { |res, nxt| check << 2; nxt.call res }
39
+ request.on_complete { |res, nxt| check << 3; nxt.call res }
40
40
 
41
41
  request.complete! response
42
42
 
@@ -15,7 +15,7 @@ describe Acfs::Request do
15
15
  end
16
16
 
17
17
  context 'with parameters' do
18
- let(:params) { {id: 10} }
18
+ let(:params) { { id: 10 }}
19
19
 
20
20
  it 'should return URL without query' do
21
21
  expect(request.url).to be == "#{url}"
@@ -24,7 +24,7 @@ describe Acfs::Request do
24
24
  end
25
25
 
26
26
  describe '#headers' do
27
- let(:headers) { {'Accept' => 'application/json'} }
27
+ let(:headers) { { 'Accept' => 'application/json' } }
28
28
 
29
29
  it 'should return request headers' do
30
30
  expect(request.headers).to be == headers
@@ -46,7 +46,7 @@ describe Acfs::Request do
46
46
  end
47
47
 
48
48
  describe '#params' do
49
- let(:params) { {id: 10} }
49
+ let(:params) { { id: 10 }}
50
50
 
51
51
  it 'should return request headers' do
52
52
  expect(request.params).to be == params
@@ -54,7 +54,7 @@ describe Acfs::Request do
54
54
  end
55
55
 
56
56
  describe '#data' do
57
- let(:data) { {id: 10, name: 'Anon'} }
57
+ let(:data) { { id: 10, name: 'Anon' } }
58
58
 
59
59
  it 'should return request data' do
60
60
  expect(request.data).to be == data
@@ -63,7 +63,7 @@ describe Acfs::Request do
63
63
 
64
64
  describe '#data' do
65
65
  context 'with data' do
66
- let(:data) { {id: 10, name: 'Anon'} }
66
+ let(:data) { { id: 10, name: 'Anon' } }
67
67
 
68
68
  it { expect(request).to be_data }
69
69
  end
@@ -3,54 +3,23 @@ require 'spec_helper'
3
3
  describe Acfs::Resource::Attributes::Boolean do
4
4
  subject { Acfs::Resource::Attributes::Boolean.new }
5
5
 
6
- describe '#cast' do
7
- it 'casts nil' do
8
- expect(subject.cast(nil)).to eq nil
9
- end
10
-
11
- it 'casts empty string to false' do
12
- expect(subject.cast('')).to eq nil
13
- end
14
-
15
- it 'casts blank string to false' do
16
- expect(subject.cast(" \t")).to eq nil
17
- end
18
-
19
- it 'preserves boolean values' do
6
+ describe 'cast' do
7
+ it 'should preserve boolean values' do
20
8
  expect(subject.cast(false)).to eq false
21
9
  expect(subject.cast(true)).to eq true
22
10
  end
23
11
 
24
- it 'casts falsy values to false' do
25
- expect(subject.cast(false)).to eq false
26
- expect(subject.cast(0)).to eq false
27
- expect(subject.cast('0')).to eq false
28
- expect(subject.cast('no')).to eq false
29
- expect(subject.cast('NO')).to eq false
30
- expect(subject.cast('off')).to eq false
31
- expect(subject.cast('OFF')).to eq false
32
- expect(subject.cast('false')).to eq false
33
- expect(subject.cast('FALSE')).to eq false
34
- expect(subject.cast('f')).to eq false
35
- expect(subject.cast('F')).to eq false
36
- end
37
-
38
- it 'casts any other value to true' do
39
- expect(subject.cast(true)).to eq true
40
- expect(subject.cast(1)).to eq true
41
- expect(subject.cast('1')).to eq true
12
+ it 'should cast TRUE_VALUES to true' do
42
13
  expect(subject.cast('yes')).to eq true
43
- expect(subject.cast('YES')).to eq true
44
14
  expect(subject.cast('on')).to eq true
45
- expect(subject.cast('ON')).to eq true
46
15
  expect(subject.cast('true')).to eq true
47
- expect(subject.cast('TRUE')).to eq true
48
- expect(subject.cast('t')).to eq true
49
- expect(subject.cast('T')).to eq true
16
+ expect(subject.cast('1')).to eq true
17
+ end
50
18
 
51
- expect(subject.cast(2)).to eq true
52
- expect(subject.cast('wrong')).to eq true
53
- expect(subject.cast('random')).to eq true
19
+ it 'should cast any other value to false' do
20
+ expect(subject.cast('')).to eq false
21
+ expect(subject.cast('wrong')).to eq false
22
+ expect(subject.cast('random')).to eq false
54
23
  end
55
24
  end
56
25
  end
@@ -1,49 +1,55 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Acfs::Resource::Attributes::DateTime do
4
- let(:type) { Acfs::Resource::Attributes::DateTime.new }
4
+ let(:model) { Class.new Acfs::Resource }
5
+ let(:params) { {} }
6
+ subject { Acfs::Resource::Attributes::DateTime.new params }
5
7
 
6
- describe '#cast' do
7
- subject { -> { type.cast value } }
8
-
9
- context 'with nil' do
10
- let(:value) { nil }
11
- it { expect(subject.call).to eq nil }
8
+ describe 'cast' do
9
+ it 'should return same object, if obj is already of DateTime class' do
10
+ date_time = DateTime.now
11
+ retval = subject.cast(date_time)
12
+ expect(retval).to be == date_time
12
13
  end
13
14
 
14
- context 'with empty string' do
15
- let(:value) { '' }
16
- it { expect(subject.call).to eq nil }
15
+ it 'should return parsed object, if obj is of Time class' do
16
+ time = Time.now
17
+ retval = subject.cast(time)
18
+ expect(retval).to be == DateTime.iso8601(time.iso8601)
17
19
  end
18
20
 
19
- context 'with blank string' do
20
- let(:value) { " \t" }
21
- it { expect(subject.call).to eq nil }
21
+ it 'should return parsed object, if obj is of Date class' do
22
+ date = Date.today
23
+ retval = subject.cast(date)
24
+ expect(retval).to be == DateTime.iso8601(date.iso8601)
22
25
  end
23
26
 
24
- context 'with DateTime' do
25
- let(:value) { DateTime.now }
26
- it { expect(subject.call).to eq value }
27
+ it 'should return parsed object, if obj is of String class in ISO-8601 format' do
28
+ date_time_string = DateTime.now.iso8601
29
+ retval = subject.cast(date_time_string)
30
+ expect(retval).to be == DateTime.iso8601(date_time_string)
27
31
  end
28
32
 
29
- context 'with Time' do
30
- let(:value) { Time.now }
31
- it { expect(subject.call).to eq value.to_datetime }
33
+ it 'should raise an error if obj is of String class not in valid ISO-8601 format' do
34
+ malformed_string = 'qwe123'
35
+ expect {
36
+ subject.cast(malformed_string)
37
+ }.to raise_error ArgumentError
32
38
  end
33
39
 
34
- context 'with Date' do
35
- let(:value) { Date.today }
36
- it { expect(subject.call).to eq value.to_datetime }
40
+ it 'should raise an error if obj is of wrong class (Fixnum)' do
41
+ fixnum = 12
42
+ expect {
43
+ subject.cast(fixnum)
44
+ }.to raise_error TypeError
37
45
  end
38
46
 
39
- context 'with ISO8601' do
40
- let(:value) { DateTime.now.iso8601 }
41
- it { expect(subject.call.iso8601).to eq value }
42
- end
47
+ context 'with allow_nil option' do
48
+ let(:params) { {allow_nil: true} }
43
49
 
44
- context 'with invalid string' do
45
- let(:value) { 'qwe123' }
46
- it { is_expected.to raise_error ArgumentError }
50
+ it 'should accept empty string as nil' do
51
+ expect(subject.cast('')).to eq nil
52
+ end
47
53
  end
48
54
  end
49
55
  end