obvious 0.1.0 → 0.2.0
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.
- checksums.yaml +4 -4
- data/.github/workflows/obvious.yml +1 -1
- data/CHANGELOG.md +19 -0
- data/Gemfile.lock +1 -16
- data/README.md +4 -35
- data/Rakefile +8 -0
- data/lib/obvious/contract.rb +30 -31
- data/lib/obvious/obj.rb +12 -14
- data/lib/obvious/version.rb +1 -1
- data/lib/obvious.rb +0 -1
- data/obvious.gemspec +4 -6
- data/test/contract_test.rb +62 -0
- data/test/entity_test.rb +76 -0
- data/test/obj_test.rb +65 -0
- metadata +16 -42
- data/bin/obvious +0 -7
- data/lib/generators/application_generator.rb +0 -199
- data/lib/generators/descriptor.rb +0 -131
- data/lib/generators/helpers/application.rb +0 -47
- data/lib/obvious/files/Rakefile +0 -4
- data/lib/obvious/files/external/fs_plug.rb +0 -99
- data/lib/obvious/files/external/mongo_plug.rb +0 -62
- data/lib/obvious/files/external/mysql_plug.rb +0 -38
- data/lib/obvious/files/external/s3_plug.rb +0 -94
- data/spec/.spec_helper.rb.swp +0 -0
- data/spec/contract_spec.rb +0 -63
- data/spec/entity_spec.rb +0 -75
- data/spec/generators/descriptor_spec.rb +0 -34
- data/spec/obj_spec.rb +0 -64
- data/spec/spec_helper.rb +0 -3
data/spec/contract_spec.rb
DELETED
@@ -1,63 +0,0 @@
|
|
1
|
-
require_relative 'spec_helper'
|
2
|
-
require_relative '../lib/obvious/contract'
|
3
|
-
|
4
|
-
|
5
|
-
describe Hash do
|
6
|
-
describe '#has_shape?' do
|
7
|
-
it 'should return true for a valid shape' do
|
8
|
-
expect({ id: 1 }.has_shape?(id: Integer)).to be true
|
9
|
-
end
|
10
|
-
|
11
|
-
it 'should return false for an invalid shape' do
|
12
|
-
expect({ id: 1 }.has_shape?(id: String)).to be false
|
13
|
-
end
|
14
|
-
|
15
|
-
it 'should retrn the invalid field if return_field flag is set' do
|
16
|
-
expect({ id: 1 }.has_shape?({id: String}, true)).to eq [false, :id]
|
17
|
-
end
|
18
|
-
|
19
|
-
it 'should allow for nil values to be returned' do
|
20
|
-
expect({ id: nil }.has_shape?({id: String})).to be true
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
class TestContract < Obvious::Contract
|
26
|
-
contract_for :test, {
|
27
|
-
input: { id: Integer },
|
28
|
-
output: { id: Integer, value: String }
|
29
|
-
}
|
30
|
-
|
31
|
-
def test input
|
32
|
-
{ id: 1, value: 'this is a test' }
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
describe Obvious::Contract do
|
37
|
-
|
38
|
-
describe "#call_method" do
|
39
|
-
it 'should return the correct output for valid input and output shapes' do
|
40
|
-
tc = TestContract.new
|
41
|
-
result = tc.test id: 1
|
42
|
-
expect(result).to eq id: 1, value: 'this is a test'
|
43
|
-
end
|
44
|
-
|
45
|
-
it 'should raise a contract input error with bad input' do
|
46
|
-
tc = TestContract.new
|
47
|
-
expect { tc.test Hash.new }.to raise_error ContractInputError
|
48
|
-
end
|
49
|
-
|
50
|
-
it 'should raise a DataNotFound error if {} is returned' do
|
51
|
-
tc = TestContract.new
|
52
|
-
allow(tc).to receive(:test_alias).and_return({})
|
53
|
-
expect { tc.test id: 1 }.to raise_error DataNotFoundError
|
54
|
-
end
|
55
|
-
|
56
|
-
it 'should raise a contract output error if nil is returned' do
|
57
|
-
tc = TestContract.new
|
58
|
-
allow(tc).to receive(:test_alias).and_return(nil)
|
59
|
-
expect { tc.test id: 1 }.to raise_error ContractOutputError
|
60
|
-
end
|
61
|
-
|
62
|
-
end
|
63
|
-
end
|
data/spec/entity_spec.rb
DELETED
@@ -1,75 +0,0 @@
|
|
1
|
-
require_relative '../lib/obvious/entity'
|
2
|
-
|
3
|
-
class Thing < Obvious::Entity
|
4
|
-
value :id, Integer
|
5
|
-
value :name, String
|
6
|
-
end
|
7
|
-
|
8
|
-
class Thing2 < Obvious::Entity
|
9
|
-
value :foo , String
|
10
|
-
|
11
|
-
validation :something, Proc.new {
|
12
|
-
if foo != "hello world"
|
13
|
-
msg = "Validation Error: Invalid value for foo, should be 'hello world'"
|
14
|
-
raise Obvious::ValidationError.new msg
|
15
|
-
end
|
16
|
-
}
|
17
|
-
|
18
|
-
def modify_foo
|
19
|
-
@values[:foo] = 100
|
20
|
-
end
|
21
|
-
|
22
|
-
end
|
23
|
-
|
24
|
-
class Thing3 < Obvious::Entity
|
25
|
-
value :foo , String
|
26
|
-
|
27
|
-
validation :something, Proc.new {
|
28
|
-
@values[:foo] = 12
|
29
|
-
}
|
30
|
-
|
31
|
-
end
|
32
|
-
|
33
|
-
# To test the entity, we are going to use classes that inherit from it instead
|
34
|
-
# of poking at it directly. In this case, I think that makes the most sense.
|
35
|
-
describe Thing do
|
36
|
-
it 'should create a valid object with valid input' do
|
37
|
-
input = { name: 'Thing', id: 1 }
|
38
|
-
t = Thing.new input
|
39
|
-
expect(t.name).to eq 'Thing'
|
40
|
-
expect(t.id).to eq 1
|
41
|
-
end
|
42
|
-
|
43
|
-
it 'should raise an error with invalid input types' do
|
44
|
-
input = { name: nil, id: nil }
|
45
|
-
expect { Thing.new input }.to raise_error Obvious::TypeError
|
46
|
-
end
|
47
|
-
|
48
|
-
it 'should raise an error with extra fields in input' do
|
49
|
-
input = { name: 'Thing', id: 1, extra: 'should explode' }
|
50
|
-
expect { Thing.new input }.to raise_error Obvious::ShapeError
|
51
|
-
end
|
52
|
-
|
53
|
-
it 'should raise an error when a method tries to modify a value' do
|
54
|
-
t = Thing2.new foo: 'hello world'
|
55
|
-
expect { t.modify_foo }.to raise_error RuntimeError
|
56
|
-
end
|
57
|
-
|
58
|
-
describe '#to_hash' do
|
59
|
-
it 'should return a hash representation of the object' do
|
60
|
-
input = { name: 'Thing', id: 1 }
|
61
|
-
t = Thing.new input
|
62
|
-
expect(t.to_hash).to eq input
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
describe 'validation' do
|
67
|
-
it 'should raise a validation error on a failed validation' do
|
68
|
-
expect { Thing2.new foo: 'not valid I promise!' }.to raise_error Obvious::ValidationError
|
69
|
-
end
|
70
|
-
|
71
|
-
it 'should raise an error when trying to modify an object in a validation' do
|
72
|
-
expect { Thing3.new foo: 'hello world' }.to raise_error RuntimeError
|
73
|
-
end
|
74
|
-
end
|
75
|
-
end
|
@@ -1,34 +0,0 @@
|
|
1
|
-
require_relative '../../lib/generators/descriptor'
|
2
|
-
|
3
|
-
require File.expand_path('spec/spec_helper')
|
4
|
-
|
5
|
-
module Obvious
|
6
|
-
module Generators
|
7
|
-
describe Descriptor do
|
8
|
-
subject {Descriptor.new(yaml_file)}
|
9
|
-
|
10
|
-
describe "#to_file" do
|
11
|
-
|
12
|
-
context "when the descriptor is empty" do
|
13
|
-
let( :yaml_file ) { {} }
|
14
|
-
|
15
|
-
it "should raise a meaningful error" do
|
16
|
-
expect {subject.to_file}.to raise_error(InvalidDescriptorError)
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
["Action", "Code", "Description"].each do |section|
|
21
|
-
context "when the '#{section}' section is omitted" do
|
22
|
-
let( :yaml_file ) {
|
23
|
-
{"Action" => "Jackson", "Description" => "This is something"}.delete(section)
|
24
|
-
}
|
25
|
-
|
26
|
-
it "should raise a meaningful error" do
|
27
|
-
expect {subject.to_file}.to raise_error(InvalidDescriptorError)
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
data/spec/obj_spec.rb
DELETED
@@ -1,64 +0,0 @@
|
|
1
|
-
require_relative '../lib/obvious/obj'
|
2
|
-
|
3
|
-
class TestObj
|
4
|
-
include Obvious::Obj
|
5
|
-
|
6
|
-
def initialize
|
7
|
-
@local = 'set!'
|
8
|
-
end
|
9
|
-
|
10
|
-
define :defined_method, with_foo: [:foo, String], also_bar: [:bar, Integer] do |input|
|
11
|
-
input
|
12
|
-
end
|
13
|
-
|
14
|
-
define :defined_local do |input|
|
15
|
-
@local
|
16
|
-
end
|
17
|
-
|
18
|
-
end
|
19
|
-
|
20
|
-
describe Obvious::Obj do
|
21
|
-
|
22
|
-
before do
|
23
|
-
@test = TestObj.new
|
24
|
-
end
|
25
|
-
|
26
|
-
describe 'self.define' do
|
27
|
-
it 'should do the right thing with correct input' do
|
28
|
-
result = @test.defined_method with_foo: 'hello', also_bar: 12
|
29
|
-
expect(result).to eq foo: 'hello', bar: 12
|
30
|
-
end
|
31
|
-
|
32
|
-
it 'should have access to instance variables' do
|
33
|
-
result = @test.defined_local
|
34
|
-
expect(result).to eq 'set!'
|
35
|
-
end
|
36
|
-
|
37
|
-
it 'should raise an error for missing parameters' do
|
38
|
-
expect { @test.defined_method with_foo: 'hello' }.to raise_error { |error|
|
39
|
-
expect(error).to be_a ArgumentError
|
40
|
-
expect(error.message).to eq 'missing input field also_bar'
|
41
|
-
}
|
42
|
-
end
|
43
|
-
|
44
|
-
it 'should raise an error for extra parameters' do
|
45
|
-
expect { @test.defined_method with_foo: 'hello', also_bar: 12, and_extra: 'this is extra!' }.to raise_error { |error|
|
46
|
-
expect(error).to be_a ArgumentError
|
47
|
-
expect(error.message).to eq 'invalid input field and_extra'
|
48
|
-
}
|
49
|
-
end
|
50
|
-
|
51
|
-
it 'should raise an error for invalid types' do
|
52
|
-
expect { @test.defined_method with_foo: 1, also_bar: 12 }.to raise_error { |error|
|
53
|
-
expect(error).to be_a ArgumentError
|
54
|
-
expect(error.message).to eq 'invalid type for with_foo expected String'
|
55
|
-
}
|
56
|
-
|
57
|
-
expect {@test.defined_method with_foo: 'hello', also_bar: nil }.to raise_error { |error|
|
58
|
-
expect(error).to be_a ArgumentError
|
59
|
-
expect(error.message).to eq 'invalid type for also_bar expected Integer'
|
60
|
-
}
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
end
|
data/spec/spec_helper.rb
DELETED