common_repository_model 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -27,4 +27,5 @@ Gem::Specification.new do |gem|
27
27
  gem.add_runtime_dependency "minitest"
28
28
  gem.add_runtime_dependency "minitest-matchers"
29
29
  gem.add_runtime_dependency "active-fedora"
30
+ gem.add_runtime_dependency "factory_girl"
30
31
  end
@@ -10,9 +10,14 @@ module CommonRepositoryModel
10
10
  )
11
11
 
12
12
  has_metadata(name: "properties",type: ActiveFedora::SimpleDatastream) do |m|
13
- m.field 'area_name', :string
13
+ m.field 'name', :string
14
14
  end
15
15
 
16
- delegate_to 'properties', [:area_name], unique: true
16
+ validates :name, presence: true
17
+
18
+ delegate_to 'properties', [:name], unique: true
19
+
20
+ # We shouldn't be calling these
21
+ protected :save, :save!
17
22
  end
18
23
  end
@@ -3,6 +3,20 @@ require_relative './area'
3
3
  require_relative './data'
4
4
  require 'set'
5
5
 
6
+ class PersistedValidator < ActiveModel::EachValidator
7
+ def validate_each(record, attribute, value)
8
+ is_valid = true
9
+ is_valid = false if value.nil?
10
+ begin
11
+ is_valid = false unless value.persisted?
12
+ rescue NoMethodError
13
+ is_valid = false
14
+ end
15
+
16
+ record.errors.add(attribute, 'must be persisted') unless is_valid
17
+ is_valid
18
+ end
19
+ end
6
20
  module CommonRepositoryModel
7
21
  class MembershipRegistry
8
22
  def initialize
@@ -68,6 +82,8 @@ module CommonRepositoryModel
68
82
  property: :is_member_of_area
69
83
  )
70
84
 
85
+ validates :area, presence: true, persisted: true
86
+
71
87
  has_many(
72
88
  :child_collections,
73
89
  class_name: 'CommonRepositoryModel::Collection',
@@ -1,3 +1,3 @@
1
1
  module CommonRepositoryModel
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -3,29 +3,46 @@ require 'common_repository_model/area'
3
3
  require 'common_repository_model/collection'
4
4
 
5
5
  describe CommonRepositoryModel::Area do
6
+ describe 'meta test' do
7
+ it {
8
+ FactoryGirl.create(:area).persisted?.must_equal true
9
+ }
10
+ end
6
11
 
7
- subject { CommonRepositoryModel::Area.new(area_name: area_name) }
8
- let(:area_name) { 'My Area Name'}
12
+ describe 'without persisting' do
13
+ subject { FactoryGirl.build(:area, name: name) }
14
+ let(:name) { 'My Area Name'}
9
15
 
10
- it 'should have #area_name' do
11
- subject.area_name.must_equal area_name
12
- end
16
+ it 'should find or create by #name' do
17
+ lambda { subject.save }.must_raise(NoMethodError)
18
+ lambda { subject.save! }.must_raise(NoMethodError)
19
+ end
20
+
21
+ it 'should require a #name' do
22
+ subject.name = nil
23
+ subject.valid?.must_equal false
24
+ subject.name = name
25
+ subject.valid?.must_equal true
26
+ end
27
+
28
+ it 'should have #name' do
29
+ subject.name.must_equal name
30
+ end
13
31
 
14
- describe 'has_many #collections' do
15
32
  it 'should build #collections' do
16
33
  subject.collections.build.
17
34
  must_be_kind_of(CommonRepositoryModel::Collection)
18
35
  end
19
36
  end
20
37
 
21
- describe 'integration' do
22
- let(:collection) { CommonRepositoryModel::Collection.new }
38
+ describe 'integration (with persistence)' do
39
+ subject { FactoryGirl.create(:area) }
40
+ let(:collection) { FactoryGirl.build(:collection) }
23
41
  it 'should save' do
24
42
  # Before we can add a collection, the containing object
25
43
  # must be saved
26
- subject.save.must_equal true
27
44
  subject.collections << collection
28
- subject.save.must_equal true
45
+ subject.send(:save).must_equal true
29
46
  new_subject = subject.class.find(subject.pid)
30
47
 
31
48
  new_subject.collections.size.must_equal 1
@@ -3,7 +3,14 @@ require 'common_repository_model/collection'
3
3
 
4
4
  describe CommonRepositoryModel::Collection do
5
5
 
6
- subject { CommonRepositoryModel::Collection.new }
6
+ subject { FactoryGirl.build(:collection) }
7
+
8
+ it 'should require an area' do
9
+ subject.area.must_be_kind_of(CommonRepositoryModel::Area)
10
+ subject.valid?.must_equal true
11
+ subject.area = nil
12
+ subject.valid?.must_equal false
13
+ end
7
14
 
8
15
  it 'should belong_to #area' do
9
16
  subject.must_respond_to(:area)
@@ -46,7 +53,7 @@ describe CommonRepositoryModel::Collection do
46
53
 
47
54
 
48
55
  describe 'integration' do
49
- let(:child_collection) { CommonRepositoryModel::Collection.new }
56
+ let(:child_collection) { FactoryGirl.build(:collection) }
50
57
  it 'should handle parent/child collection' do
51
58
  # Before we can add a collection, the containing object
52
59
  # must be saved
@@ -3,15 +3,10 @@ require 'common_repository_model/data'
3
3
 
4
4
  describe CommonRepositoryModel::Data do
5
5
 
6
- subject {
7
- CommonRepositoryModel::Data.new(
8
- slot_name: '1234',
9
- md5_checksum: 'abcdefg'
10
- )
11
- }
6
+ subject { FactoryGirl.build(:data) }
12
7
 
13
8
  describe 'integration' do
14
- let(:collection) { CommonRepositoryModel::Collection.new }
9
+ let(:collection) { FactoryGirl.build(:collection) }
15
10
  let(:file_1) { File.new(__FILE__) }
16
11
  let(:file_2) {
17
12
  File.new(File.join(File.dirname(__FILE__), '../spec_helper.rb'))
@@ -53,15 +53,15 @@ class Person < CommonRepositoryModel::Collection
53
53
  end
54
54
 
55
55
  describe CommonRepositoryModel::Collection do
56
-
57
- let(:family) { Family.new }
58
- let(:lawyer) { Job.new }
59
- let(:doctor) { Job.new }
60
- let(:heathcliff) { Person.new }
61
- let(:claire) { Person.new }
62
- let(:theo) { Person.new }
63
- let(:vanessa) { Person.new }
64
- let(:rudy) { Person.new }
56
+ let(:area) { FactoryGirl.create(:area) }
57
+ let(:family) { Family.new(area: area) }
58
+ let(:lawyer) { Job.new(area: area) }
59
+ let(:doctor) { Job.new(area: area) }
60
+ let(:heathcliff) { Person.new(area: area) }
61
+ let(:claire) { Person.new(area: area) }
62
+ let(:theo) { Person.new(area: area) }
63
+ let(:vanessa) { Person.new(area: area) }
64
+ let(:rudy) { Person.new(area: area) }
65
65
  let(:dress) { Clothing.new(slot_name: 'outerwear') }
66
66
 
67
67
  before(:all) do
@@ -107,14 +107,14 @@ describe CommonRepositoryModel::Collection do
107
107
  end
108
108
 
109
109
  it 'verifies complicated relationships' do
110
- # verify_initial_relations_for_family
111
- # verify_initial_relations_for_theo
112
- # verify_initial_relations_for_claire
113
- # verify_initial_relations_for_dress
110
+ verify_initial_relations_for_family
111
+ verify_initial_relations_for_theo
112
+ verify_initial_relations_for_claire
113
+ verify_initial_relations_for_dress
114
114
 
115
- # verify_claire_adding_a_child
115
+ verify_claire_adding_a_child
116
116
 
117
- # verify_claire_losing_a_child
117
+ verify_claire_losing_a_child
118
118
 
119
119
  verify_rudy_losing_a_parent
120
120
  end
@@ -182,7 +182,7 @@ describe CommonRepositoryModel::Collection do
182
182
  end
183
183
 
184
184
  def verify_claire_adding_a_child
185
- sandra = Person.new
185
+ sandra = Person.new(area: area)
186
186
  sandra.save
187
187
  @claire.children << sandra
188
188
  @claire.save
@@ -0,0 +1,17 @@
1
+ require 'common_repository_model/area'
2
+ require 'common_repository_model/collection'
3
+ require 'common_repository_model/data'
4
+
5
+ FactoryGirl.define do
6
+ factory :area, class: CommonRepositoryModel::Area do
7
+ sequence(:name) { |n| "Area #{n}" }
8
+ to_create { |instance| instance.send(:save!) }
9
+ end
10
+ factory :collection, class: CommonRepositoryModel::Collection do
11
+ area { CommonRepositoryModel::Area.all.first }
12
+ end
13
+ factory :data, class: CommonRepositoryModel::Data do
14
+ sequence(:slot_name) { |n| "Slot Name #{n}" }
15
+ sequence(:md5_checksum) { |n| "#{n}abc"}
16
+ end
17
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  $:.unshift File.expand_path('../../lib', __FILE__)
2
2
  require 'minitest/autorun'
3
3
  require 'active_fedora'
4
+ require 'factory_girl'
5
+ FactoryGirl.find_definitions
4
6
  ActiveFedora.init(
5
7
  fedora_config_path: File.join(File.dirname(__FILE__),"config/fedora.yml"),
6
8
  solr_config_path: File.join(File.dirname(__FILE__),"config/solr.yml"),
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: common_repository_model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-14 00:00:00.000000000 Z
12
+ date: 2012-11-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -107,6 +107,22 @@ dependencies:
107
107
  - - ! '>='
108
108
  - !ruby/object:Gem::Version
109
109
  version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: factory_girl
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
110
126
  description: Notre Dame's Common Fedora Repository Model
111
127
  email:
112
128
  - jeremy.n.friesen@gmail.com
@@ -141,6 +157,7 @@ files:
141
157
  - spec/config/fedora.yml
142
158
  - spec/config/predicate_mappings.yml
143
159
  - spec/config/solr.yml
160
+ - spec/factories/common_repository_model_factories.rb
144
161
  - spec/spec_helper.rb
145
162
  homepage: https://github.com/ndlib/common_repository_model
146
163
  licenses: []
@@ -176,4 +193,5 @@ test_files:
176
193
  - spec/config/fedora.yml
177
194
  - spec/config/predicate_mappings.yml
178
195
  - spec/config/solr.yml
196
+ - spec/factories/common_repository_model_factories.rb
179
197
  - spec/spec_helper.rb