sbf-dm-types 1.3.0.beta
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +45 -0
- data/.rspec +1 -0
- data/.rubocop.yml +468 -0
- data/.travis.yml +52 -0
- data/Gemfile +67 -0
- data/LICENSE +20 -0
- data/README.rdoc +3 -0
- data/Rakefile +4 -0
- data/dm-types.gemspec +26 -0
- data/lib/dm-types/api_key.rb +30 -0
- data/lib/dm-types/bcrypt_hash.rb +33 -0
- data/lib/dm-types/comma_separated_list.rb +28 -0
- data/lib/dm-types/csv.rb +34 -0
- data/lib/dm-types/enum.rb +55 -0
- data/lib/dm-types/epoch_time.rb +35 -0
- data/lib/dm-types/file_path.rb +24 -0
- data/lib/dm-types/flag.rb +64 -0
- data/lib/dm-types/ip_address.rb +34 -0
- data/lib/dm-types/json.rb +48 -0
- data/lib/dm-types/paranoid/base.rb +56 -0
- data/lib/dm-types/paranoid_boolean.rb +23 -0
- data/lib/dm-types/paranoid_datetime.rb +22 -0
- data/lib/dm-types/regexp.rb +21 -0
- data/lib/dm-types/slug.rb +28 -0
- data/lib/dm-types/support/dirty_minder.rb +168 -0
- data/lib/dm-types/support/flags.rb +41 -0
- data/lib/dm-types/uri.rb +27 -0
- data/lib/dm-types/uuid.rb +64 -0
- data/lib/dm-types/version.rb +5 -0
- data/lib/dm-types/yaml.rb +39 -0
- data/lib/dm-types.rb +23 -0
- data/spec/fixtures/api_user.rb +14 -0
- data/spec/fixtures/article.rb +35 -0
- data/spec/fixtures/bookmark.rb +23 -0
- data/spec/fixtures/invention.rb +7 -0
- data/spec/fixtures/network_node.rb +36 -0
- data/spec/fixtures/person.rb +25 -0
- data/spec/fixtures/software_package.rb +33 -0
- data/spec/fixtures/ticket.rb +21 -0
- data/spec/fixtures/tshirt.rb +24 -0
- data/spec/integration/api_key_spec.rb +27 -0
- data/spec/integration/bcrypt_hash_spec.rb +47 -0
- data/spec/integration/comma_separated_list_spec.rb +87 -0
- data/spec/integration/dirty_minder_spec.rb +197 -0
- data/spec/integration/enum_spec.rb +78 -0
- data/spec/integration/epoch_time_spec.rb +61 -0
- data/spec/integration/file_path_spec.rb +160 -0
- data/spec/integration/flag_spec.rb +72 -0
- data/spec/integration/ip_address_spec.rb +153 -0
- data/spec/integration/json_spec.rb +72 -0
- data/spec/integration/slug_spec.rb +67 -0
- data/spec/integration/uri_spec.rb +117 -0
- data/spec/integration/uuid_spec.rb +102 -0
- data/spec/integration/yaml_spec.rb +87 -0
- data/spec/shared/flags_shared_spec.rb +37 -0
- data/spec/shared/identity_function_group.rb +5 -0
- data/spec/spec_helper.rb +30 -0
- data/spec/unit/bcrypt_hash_spec.rb +155 -0
- data/spec/unit/csv_spec.rb +142 -0
- data/spec/unit/dirty_minder_spec.rb +65 -0
- data/spec/unit/enum_spec.rb +126 -0
- data/spec/unit/epoch_time_spec.rb +74 -0
- data/spec/unit/file_path_spec.rb +75 -0
- data/spec/unit/flag_spec.rb +114 -0
- data/spec/unit/ip_address_spec.rb +109 -0
- data/spec/unit/json_spec.rb +126 -0
- data/spec/unit/paranoid_boolean_spec.rb +149 -0
- data/spec/unit/paranoid_datetime_spec.rb +153 -0
- data/spec/unit/regexp_spec.rb +63 -0
- data/spec/unit/uri_spec.rb +83 -0
- data/spec/unit/yaml_spec.rb +111 -0
- data/tasks/spec.rake +21 -0
- data/tasks/yard.rake +9 -0
- data/tasks/yardstick.rake +19 -0
- metadata +229 -0
@@ -0,0 +1,114 @@
|
|
1
|
+
require_relative '../spec_helper'
|
2
|
+
|
3
|
+
require_relative '../fixtures/tshirt'
|
4
|
+
|
5
|
+
try_spec do
|
6
|
+
describe DataMapper::Property::Flag do
|
7
|
+
describe '.dump' do
|
8
|
+
before :all do
|
9
|
+
@flag = DataMapper::TypesFixtures::TShirt.property(
|
10
|
+
:stuff, DataMapper::Property::Flag[:first, :second, :third, :fourth, :fifth])
|
11
|
+
|
12
|
+
@property_klass = DataMapper::Property::Flag
|
13
|
+
end
|
14
|
+
|
15
|
+
it_behaves_like 'A property with flags'
|
16
|
+
|
17
|
+
describe 'when argument matches a value in the flag map' do
|
18
|
+
before :all do
|
19
|
+
@result = @flag.dump(:first)
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'returns flag bit of value' do
|
23
|
+
expect(@result).to eq 1
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe 'when argument matches 2nd value in the flag map' do
|
28
|
+
before :all do
|
29
|
+
@result = @flag.dump(:second)
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'returns flag bit of value' do
|
33
|
+
expect(@result).to eq 2
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe 'when argument matches multiple Symbol values in the flag map' do
|
38
|
+
before :all do
|
39
|
+
@result = @flag.dump([ :second, :fourth ])
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'builds binary flag from key values of all matches' do
|
43
|
+
expect(@result).to eq 10
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe 'when argument matches multiple string values in the flag map' do
|
48
|
+
before :all do
|
49
|
+
@result = @flag.dump(['first', 'second', 'third', 'fourth', 'fifth'])
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'builds binary flag from key values of all matches' do
|
53
|
+
expect(@result).to eq 31
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
describe 'when argument does not match a single value in the flag map' do
|
58
|
+
before :all do
|
59
|
+
@result = @flag.dump(:zero)
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'returns zero' do
|
63
|
+
expect(@result).to eq 0
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
describe 'when argument contains duplicate flags' do
|
68
|
+
before :all do
|
69
|
+
@result = @flag.dump([ :second, :fourth, :second ])
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'behaves the same as if there were no duplicates' do
|
73
|
+
expect(@result).to eq @flag.dump([ :second, :fourth ])
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
describe '.load' do
|
79
|
+
before :all do
|
80
|
+
@flag = DataMapper::TypesFixtures::TShirt.property(:stuff, DataMapper::Property::Flag, :flags => [:uno, :dos, :tres, :cuatro, :cinco])
|
81
|
+
end
|
82
|
+
|
83
|
+
describe 'when argument matches a key in the flag map' do
|
84
|
+
before :all do
|
85
|
+
@result = @flag.load(4)
|
86
|
+
end
|
87
|
+
|
88
|
+
it 'returns array with a single matching element' do
|
89
|
+
expect(@result).to eq [ :tres ]
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
describe 'when argument matches multiple keys in the flag map' do
|
94
|
+
before :all do
|
95
|
+
@result = @flag.load(10)
|
96
|
+
end
|
97
|
+
|
98
|
+
it 'returns array of matching values' do
|
99
|
+
expect(@result).to eq [ :dos, :cuatro ]
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
describe 'when argument does not match a single key in the flag map' do
|
104
|
+
before :all do
|
105
|
+
@result = @flag.load(nil)
|
106
|
+
end
|
107
|
+
|
108
|
+
it 'returns an empty array' do
|
109
|
+
expect(@result).to eq []
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
@@ -0,0 +1,109 @@
|
|
1
|
+
require_relative '../spec_helper'
|
2
|
+
|
3
|
+
require_relative '../fixtures/network_node'
|
4
|
+
|
5
|
+
try_spec do
|
6
|
+
describe DataMapper::Property::IPAddress do
|
7
|
+
before :all do
|
8
|
+
@stored = '81.20.130.1'
|
9
|
+
@input = IPAddr.new(@stored)
|
10
|
+
@property = DataMapper::TypesFixtures::NetworkNode.properties[:ip_address]
|
11
|
+
end
|
12
|
+
|
13
|
+
describe '.dump' do
|
14
|
+
describe 'when argument is an IP address given as Ruby object' do
|
15
|
+
before :all do
|
16
|
+
@result = @property.dump(@input)
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'dumps input into a string' do
|
20
|
+
expect(@result).to eq @stored
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe 'when argument is nil' do
|
25
|
+
before :all do
|
26
|
+
@result = @property.dump(nil)
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'returns nil' do
|
30
|
+
expect(@result).to be_nil
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe 'when input is a blank string' do
|
35
|
+
before :all do
|
36
|
+
@result = @property.dump('')
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'retuns a blank string' do
|
40
|
+
expect(@result).to eq ''
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
describe '.load' do
|
46
|
+
describe 'when argument is a valid IP address as a string' do
|
47
|
+
before :all do
|
48
|
+
@result = @property.load(@stored)
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'returns IPAddr instance from stored value' do
|
52
|
+
expect(@result).to eq @input
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe 'when argument is nil' do
|
57
|
+
before :all do
|
58
|
+
@result = @property.load(nil)
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'returns nil' do
|
62
|
+
expect(@result).to be_nil
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
describe 'when argument is a blank string' do
|
67
|
+
before :all do
|
68
|
+
@result = @property.load('')
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'returns IPAddr instance from stored value' do
|
72
|
+
expect(@result).to eq IPAddr.new('0.0.0.0')
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
describe 'when argument is an Array instance' do
|
77
|
+
before :all do
|
78
|
+
@operation = lambda { @property.load([]) }
|
79
|
+
end
|
80
|
+
|
81
|
+
it 'raises ArgumentError with a meaningful message' do
|
82
|
+
expect { @operation }.to raise_error(ArgumentError, '+value+ must be nil or a String')
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
describe '.typecast' do
|
88
|
+
describe 'when argument is an IpAddr object' do
|
89
|
+
before :all do
|
90
|
+
@result = @property.typecast(@input)
|
91
|
+
end
|
92
|
+
|
93
|
+
it 'does not change the value' do
|
94
|
+
expect(@result).to eq @input
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
describe 'when argument is a valid IP address as a string' do
|
99
|
+
before :all do
|
100
|
+
@result = @property.typecast(@stored)
|
101
|
+
end
|
102
|
+
|
103
|
+
it 'instantiates IPAddr instance' do
|
104
|
+
expect(@result).to eq @input
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
@@ -0,0 +1,126 @@
|
|
1
|
+
require_relative '../spec_helper'
|
2
|
+
require_relative '../shared/identity_function_group'
|
3
|
+
|
4
|
+
try_spec do
|
5
|
+
|
6
|
+
require_relative '../fixtures/person'
|
7
|
+
|
8
|
+
describe DataMapper::Property::Json do
|
9
|
+
before :all do
|
10
|
+
@property = DataMapper::TypesFixtures::Person.properties[:positions]
|
11
|
+
end
|
12
|
+
|
13
|
+
describe '.load' do
|
14
|
+
describe 'when nil is provided' do
|
15
|
+
it 'returns nil' do
|
16
|
+
expect(@property.load(nil)).to be_nil
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe 'when Json encoded primitive string is provided' do
|
21
|
+
it 'returns decoded value as Ruby string' do
|
22
|
+
expect(@property.load(MultiJson.dump(:value => 'JSON encoded string'))).to eq({ 'value' => 'JSON encoded string' })
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe 'when something else is provided' do
|
27
|
+
it 'raises ArgumentError with a meaningful message' do
|
28
|
+
expect {
|
29
|
+
@property.load(:sym)
|
30
|
+
}.to raise_error(ArgumentError, '+value+ of a property of JSON type must be nil or a String')
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe '.dump' do
|
36
|
+
describe 'when nil is provided' do
|
37
|
+
it 'returns nil' do
|
38
|
+
expect(@property.dump(nil)).to be_nil
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe 'when Json encoded primitive string is provided' do
|
43
|
+
it 'does not do double encoding' do
|
44
|
+
expect(@property.dump('Json encoded string')).to eq 'Json encoded string'
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe 'when regular Ruby string is provided' do
|
49
|
+
it 'dumps argument to Json' do
|
50
|
+
expect(@property.dump('dump me (to JSON)')).to eq 'dump me (to JSON)'
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
describe 'when Ruby array is provided' do
|
55
|
+
it 'dumps argument to Json' do
|
56
|
+
expect(@property.dump([1, 2, 3])).to eq '[1,2,3]'
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
describe 'when Ruby hash is provided' do
|
61
|
+
it 'dumps argument to Json' do
|
62
|
+
expect(@property.dump({ :datamapper => 'Data access layer in Ruby' })).to eq '{"datamapper":"Data access layer in Ruby"}'
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
describe '.typecast' do
|
68
|
+
class ::SerializeMe
|
69
|
+
attr_accessor :name
|
70
|
+
end
|
71
|
+
|
72
|
+
describe 'when given instance of a Hash' do
|
73
|
+
before :all do
|
74
|
+
@input = { :library => 'DataMapper' }
|
75
|
+
|
76
|
+
@result = @property.typecast(@input)
|
77
|
+
end
|
78
|
+
|
79
|
+
it_behaves_like 'identity function'
|
80
|
+
end
|
81
|
+
|
82
|
+
describe 'when given instance of an Array' do
|
83
|
+
before :all do
|
84
|
+
@input = %w[ dm-core dm-more ]
|
85
|
+
|
86
|
+
@result = @property.typecast(@input)
|
87
|
+
end
|
88
|
+
|
89
|
+
it_behaves_like 'identity function'
|
90
|
+
end
|
91
|
+
|
92
|
+
describe 'when given nil' do
|
93
|
+
before :all do
|
94
|
+
@input = nil
|
95
|
+
|
96
|
+
@result = @property.typecast(@input)
|
97
|
+
end
|
98
|
+
|
99
|
+
it_behaves_like 'identity function'
|
100
|
+
end
|
101
|
+
|
102
|
+
describe 'when given JSON encoded value' do
|
103
|
+
before :all do
|
104
|
+
@input = '{ "value": 11 }'
|
105
|
+
|
106
|
+
@result = @property.typecast(@input)
|
107
|
+
end
|
108
|
+
|
109
|
+
it 'decodes value from JSON' do
|
110
|
+
expect(@result).to eq({ 'value' => 11 })
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
describe 'when given instance of a custom class' do
|
115
|
+
before :all do
|
116
|
+
@input = SerializeMe.new
|
117
|
+
@input.name = 'Hello!'
|
118
|
+
|
119
|
+
# @result = @property.typecast(@input)
|
120
|
+
end
|
121
|
+
|
122
|
+
it 'attempts to load value from JSON string'
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
@@ -0,0 +1,149 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe DataMapper::Property::ParanoidBoolean do
|
4
|
+
before :all do
|
5
|
+
Object.send(:remove_const, :Blog) if defined?(Blog)
|
6
|
+
|
7
|
+
module ::Blog
|
8
|
+
class Draft
|
9
|
+
include DataMapper::Resource
|
10
|
+
|
11
|
+
property :id, Serial
|
12
|
+
property :deleted, ParanoidBoolean
|
13
|
+
|
14
|
+
before :destroy, :before_destroy
|
15
|
+
|
16
|
+
def before_destroy; end
|
17
|
+
end
|
18
|
+
|
19
|
+
class Article < Draft; end
|
20
|
+
|
21
|
+
class Review < Article; end
|
22
|
+
end
|
23
|
+
|
24
|
+
@model = Blog::Article
|
25
|
+
end
|
26
|
+
|
27
|
+
supported_by :all do
|
28
|
+
describe 'Resource#destroy' do
|
29
|
+
subject { @resource.destroy }
|
30
|
+
|
31
|
+
describe 'with a new resource' do
|
32
|
+
before do
|
33
|
+
@resource = @model.new
|
34
|
+
end
|
35
|
+
|
36
|
+
it { is_expected.to be(false) }
|
37
|
+
|
38
|
+
it 'does not delete the resource from the datastore' do
|
39
|
+
expect { method(:subject) }.not_to change { @model.with_deleted.size }.from(0)
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'does not set the paranoid column' do
|
43
|
+
expect { method(:subject) }.not_to change { @resource.deleted }.from(false)
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'runs the destroy hook' do
|
47
|
+
expect(@resource).to receive(:before_destroy).with(no_args)
|
48
|
+
subject
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
describe 'with a saved resource' do
|
53
|
+
before do
|
54
|
+
@resource = @model.create
|
55
|
+
end
|
56
|
+
|
57
|
+
it { is_expected.to be(true) }
|
58
|
+
|
59
|
+
it 'does not delete the resource from the datastore' do
|
60
|
+
expect { method(:subject) }.not_to change { @model.with_deleted.size }.from(1)
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'sets the paranoid column' do
|
64
|
+
expect { method(:subject) }.to change { @resource.deleted }.from(false).to(true)
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'runs the destroy hook' do
|
68
|
+
expect(@resource).to receive(:before_destroy).with(no_args)
|
69
|
+
subject
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
describe 'Resource#destroy!' do
|
75
|
+
subject { @resource.destroy! }
|
76
|
+
|
77
|
+
describe 'with a new resource' do
|
78
|
+
before do
|
79
|
+
@resource = @model.new
|
80
|
+
end
|
81
|
+
|
82
|
+
it { is_expected.to be(false) }
|
83
|
+
|
84
|
+
it 'does not delete the resource from the datastore' do
|
85
|
+
expect { method(:subject) }.not_to change { @model.with_deleted.size }.from(0)
|
86
|
+
end
|
87
|
+
|
88
|
+
it 'does not set the paranoid column' do
|
89
|
+
expect { method(:subject) }.not_to change { @resource.deleted }.from(false)
|
90
|
+
end
|
91
|
+
|
92
|
+
it 'does not run the destroy hook' do
|
93
|
+
expect(@resource).not_to receive(:before_destroy).with(no_args)
|
94
|
+
subject
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
describe 'with a saved resource' do
|
99
|
+
before do
|
100
|
+
@resource = @model.create
|
101
|
+
end
|
102
|
+
|
103
|
+
it { is_expected.to be(true) }
|
104
|
+
|
105
|
+
it 'deletes the resource from the datastore' do
|
106
|
+
expect { method(:subject) }.to change { @model.with_deleted.size }.from(1).to(0)
|
107
|
+
end
|
108
|
+
|
109
|
+
it 'does not set the paranoid column' do
|
110
|
+
expect { method(:subject) }.not_to change { @resource.deleted }.from(false)
|
111
|
+
end
|
112
|
+
|
113
|
+
it 'does not run the destroy hook' do
|
114
|
+
expect(@resource).not_to receive(:before_destroy).with(no_args)
|
115
|
+
subject
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
describe 'Model#with_deleted' do
|
121
|
+
before do
|
122
|
+
@resource = @model.create
|
123
|
+
@resource.destroy
|
124
|
+
end
|
125
|
+
|
126
|
+
describe 'with a block' do
|
127
|
+
subject { @model.with_deleted { @model.all } }
|
128
|
+
|
129
|
+
it 'scopes the block to return all resources' do
|
130
|
+
expect(subject.map(&:key)).to eq [ @resource.key ]
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
describe 'without a block' do
|
135
|
+
subject { @model.with_deleted }
|
136
|
+
|
137
|
+
it 'returns a collection scoped to return all resources' do
|
138
|
+
expect(subject.map(&:key)).to eq [ @resource.key ]
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
describe 'Model.inherited' do
|
144
|
+
it 'sets @paranoid_properties' do
|
145
|
+
expect(::Blog::Review.instance_variable_get(:@paranoid_properties)).to eq ::Blog::Article.instance_variable_get(:@paranoid_properties)
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
@@ -0,0 +1,153 @@
|
|
1
|
+
require_relative '../spec_helper'
|
2
|
+
|
3
|
+
describe DataMapper::Property::ParanoidDateTime do
|
4
|
+
before :all do
|
5
|
+
Object.send(:remove_const, :Blog) if defined?(Blog)
|
6
|
+
module ::Blog
|
7
|
+
class Draft
|
8
|
+
include DataMapper::Resource
|
9
|
+
|
10
|
+
property :id, Serial
|
11
|
+
property :deleted_at, ParanoidDateTime
|
12
|
+
|
13
|
+
before :destroy, :before_destroy
|
14
|
+
|
15
|
+
def before_destroy; end
|
16
|
+
end
|
17
|
+
|
18
|
+
class Article < Draft; end
|
19
|
+
|
20
|
+
class Review < Article; end
|
21
|
+
end
|
22
|
+
|
23
|
+
@model = Blog::Article
|
24
|
+
end
|
25
|
+
|
26
|
+
supported_by :all do
|
27
|
+
describe 'Resource#destroy' do
|
28
|
+
before do
|
29
|
+
pending 'Does not work with < 1.8.7, see if backports fixes it' if RUBY_VERSION < '1.8.7'
|
30
|
+
end
|
31
|
+
|
32
|
+
subject { @resource.destroy }
|
33
|
+
|
34
|
+
describe 'with a new resource' do
|
35
|
+
before do
|
36
|
+
@resource = @model.new
|
37
|
+
end
|
38
|
+
|
39
|
+
it { is_expected.to be(false) }
|
40
|
+
|
41
|
+
it 'does not delete the resource from the datastore' do
|
42
|
+
expect { method(:subject) }.not_to change { @model.with_deleted.size }.from(0)
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'does not set the paranoid column' do
|
46
|
+
expect { method(:subject) }.not_to change { @resource.deleted_at }.from(nil)
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'runs the destroy hook' do
|
50
|
+
expect(@resource).to receive(:before_destroy).with(no_args)
|
51
|
+
subject
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
describe 'with a saved resource' do
|
56
|
+
before do
|
57
|
+
@resource = @model.create
|
58
|
+
end
|
59
|
+
|
60
|
+
it { is_expected.to be(true) }
|
61
|
+
|
62
|
+
it 'does not delete the resource from the datastore' do
|
63
|
+
expect { method(:subject) }.not_to change { @model.with_deleted.size }.from(1)
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'sets the paranoid column' do
|
67
|
+
expect { method(:subject) }.to change { @resource.deleted_at }.from(nil)
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'runs the destroy hook' do
|
71
|
+
expect(@resource).to receive(:before_destroy).with(no_args)
|
72
|
+
subject
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
describe 'Resource#destroy!' do
|
78
|
+
subject { @resource.destroy! }
|
79
|
+
|
80
|
+
describe 'with a new resource' do
|
81
|
+
before do
|
82
|
+
@resource = @model.new
|
83
|
+
end
|
84
|
+
|
85
|
+
it { is_expected.to be(false) }
|
86
|
+
|
87
|
+
it 'does not delete the resource from the datastore' do
|
88
|
+
expect { method(:subject) }.not_to change { @model.with_deleted.size }.from(0)
|
89
|
+
end
|
90
|
+
|
91
|
+
it 'does not set the paranoid column' do
|
92
|
+
expect { method(:subject) }.not_to change { @resource.deleted_at }.from(nil)
|
93
|
+
end
|
94
|
+
|
95
|
+
it 'does not run the destroy hook' do
|
96
|
+
expect(@resource).not_to receive(:before_destroy).with(no_args)
|
97
|
+
subject
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
describe 'with a saved resource' do
|
102
|
+
before do
|
103
|
+
@resource = @model.create
|
104
|
+
end
|
105
|
+
|
106
|
+
it { is_expected.to be(true) }
|
107
|
+
|
108
|
+
it 'deletes the resource from the datastore' do
|
109
|
+
expect { method(:subject) }.to change { @model.with_deleted.size }.from(1).to(0)
|
110
|
+
end
|
111
|
+
|
112
|
+
it 'does not set the paranoid column' do
|
113
|
+
expect { method(:subject) }.not_to change { @resource.deleted_at }.from(nil)
|
114
|
+
end
|
115
|
+
|
116
|
+
it 'does not run the destroy hook' do
|
117
|
+
expect(@resource).not_to receive(:before_destroy).with(no_args)
|
118
|
+
subject
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
describe 'Model#with_deleted' do
|
124
|
+
before do
|
125
|
+
pending 'Does not work with < 1.8.7, see if backports fixes it' if RUBY_VERSION < '1.8.7'
|
126
|
+
@resource = @model.create
|
127
|
+
@resource.destroy
|
128
|
+
end
|
129
|
+
|
130
|
+
describe 'with a block' do
|
131
|
+
subject { @model.with_deleted { @model.all } }
|
132
|
+
|
133
|
+
it 'scopes the block to return all resources' do
|
134
|
+
expect(subject.map(&:key)).to eq [ @resource.key ]
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
describe 'without a block' do
|
139
|
+
subject { @model.with_deleted }
|
140
|
+
|
141
|
+
it 'returns a collection scoped to return all resources' do
|
142
|
+
expect(subject.map(&:key)).to eq [ @resource.key ]
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
describe 'Model.inherited' do
|
148
|
+
it 'sets @paranoid_properties' do
|
149
|
+
expect(Blog::Review.instance_variable_get(:@paranoid_properties)).to eq Blog::Article.instance_variable_get(:@paranoid_properties)
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require_relative '../spec_helper'
|
2
|
+
|
3
|
+
try_spec do
|
4
|
+
describe DataMapper::Property::Regexp do
|
5
|
+
before :all do
|
6
|
+
class ::User
|
7
|
+
include DataMapper::Resource
|
8
|
+
property :id, Serial
|
9
|
+
property :regexp, Regexp
|
10
|
+
end
|
11
|
+
|
12
|
+
@property = User.properties[:regexp]
|
13
|
+
end
|
14
|
+
|
15
|
+
describe '.load' do
|
16
|
+
describe 'when argument is a string' do
|
17
|
+
before :all do
|
18
|
+
@input = '[a-z]\d+'
|
19
|
+
@result = @property.load(@input)
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'create a regexp instance from argument' do
|
23
|
+
expect(@result).to eq Regexp.new(@input)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe 'when argument is nil' do
|
28
|
+
before :all do
|
29
|
+
@input = nil
|
30
|
+
@result = @property.load(@input)
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'returns nil' do
|
34
|
+
expect(@result).to be_nil
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe '.dump' do
|
40
|
+
describe 'when argument is a regular expression' do
|
41
|
+
before :all do
|
42
|
+
@input = /\d+/
|
43
|
+
@result = @property.dump(@input)
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'escapes the argument' do
|
47
|
+
expect(@result).to eq '\\d+'
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe 'when argument is nil' do
|
52
|
+
before :all do
|
53
|
+
@input = nil
|
54
|
+
@result = @property.dump(@input)
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'returns nil' do
|
58
|
+
expect(@result).to be_nil
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|