|
19 | 19 | before(:all) do |
20 | 20 | class Foo < ActiveFedora::Base |
21 | 21 | property :title, predicate: ::RDF::Vocab::DC.title, multiple: false |
| 22 | + |
| 23 | + def to_solr |
| 24 | + super.tap do |solr_doc| |
| 25 | + solr_doc["title_uniq_si"] = title.downcase.gsub(/\s+/,'') if title.present? |
| 26 | + end |
| 27 | + end |
22 | 28 | end |
23 | 29 | end |
24 | 30 | after(:all) { Object.send(:remove_const, :Foo) } |
25 | 31 |
|
26 | | - let(:solr_field) {"title_uniq_si"} |
27 | | - let(:validator) {UniquenessValidator.new({:attributes => [:title], :solr_name => solr_field})} |
28 | | - let(:record) {Foo.new} |
| 32 | + let(:field) { 'title' } |
| 33 | + let(:solr_field) { "title_uniq_si" } |
| 34 | + let(:validator) { UniquenessValidator.new({ :attributes => [field], :solr_name => solr_field }) } |
| 35 | + let(:record) { Foo.new(title: title) } |
| 36 | + let(:title) { 'new_title' } |
29 | 37 |
|
30 | 38 | it "should raise an exception if solr_name option is missing" do |
31 | | - expect{UniquenessValidator.new({attributes: [:title]})}.to raise_error ArgumentError |
| 39 | + expect { UniquenessValidator.new({ attributes: [:title] }) }.to raise_error ArgumentError |
32 | 40 | end |
33 | 41 |
|
34 | 42 | it "should not return errors when field is unique" do |
35 | | - allow(validator).to receive("find_doc").and_return(nil) |
36 | 43 | expect(record).not_to receive('errors') |
37 | | - validator.validate_each(record, "title", "new_title") |
| 44 | + validator.validate_each(record, field, record.attributes[field]) |
38 | 45 | end |
39 | 46 |
|
40 | 47 | it "should not return errors when field is unique but record is the same" do |
41 | | - doc = double(id: record.id) |
42 | | - allow(validator).to receive("find_doc").and_return(doc) |
| 48 | + record.save! |
43 | 49 | expect(record.errors).not_to receive('add') |
44 | | - validator.validate_each(record, "title", "new_title") |
| 50 | + validator.validate_each(record, field, record.attributes[field]) |
45 | 51 | end |
46 | 52 |
|
47 | 53 | it "should return errors when field is not unique" do |
48 | | - doc = double(id: 'different-id') |
49 | | - allow(validator).to receive("find_doc").and_return(doc) |
50 | | - # expect(record.errors).to receive('add') |
51 | | - validator.validate_each(record, "title", "old_title") |
| 54 | + Foo.create(id: 'different-id', title: 'new_title') |
| 55 | + validator.validate_each(record, field, record.attributes[field]) |
52 | 56 | expect(record.errors).to_not be_empty |
53 | 57 | end |
54 | | - |
55 | | - describe "#find_doc" do |
56 | | - let (:klass) {record.class} |
57 | | - let (:value) {"old_title"} |
58 | | - |
59 | | - it "should use the solr field name and supplied values" do |
60 | | - relation = double() |
61 | | - allow(relation).to receive("first") |
62 | | - expect(klass).to receive(:where).once.with(solr_field => value).and_return(relation) |
63 | | - validator.find_doc(klass, value) |
64 | | - end |
65 | | - it "should return one record when present" do |
66 | | - doc = Foo.new |
67 | | - relation = double() |
68 | | - allow(relation).to receive("first").and_return(doc) |
69 | | - allow(klass).to receive("where").and_return(relation) |
70 | | - expect(validator.find_doc(klass, value)).to be_an_instance_of klass |
71 | | - end |
72 | | - it "should return nil when not present" do |
73 | | - relation = double() |
74 | | - allow(relation).to receive("first").and_return(nil) |
75 | | - allow(klass).to receive("where").and_return(relation) |
76 | | - expect(validator.find_doc(klass, value)).to be_nil |
77 | | - end |
78 | | - end |
79 | 58 | end |
0 commit comments