-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathtc_ar_provider.rb
executable file
·196 lines (160 loc) · 6.67 KB
/
tc_ar_provider.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
require 'test_helper_ar_provider'
class ActiveRecordProviderTest < TransactionalTestCase
def test_identify
assert @provider.identify =~ /ActiveRecord Based Provider/
end
def test_metadata_formats
assert_nothing_raised { REXML::Document.new(@provider.list_metadata_formats) }
doc = REXML::Document.new(@provider.list_metadata_formats)
assert doc.elements['/OAI-PMH/ListMetadataFormats/metadataFormat/metadataPrefix'].text == 'oai_dc'
end
def test_metadata_formats_for_record
record_id = DCField.first.id
assert_nothing_raised { REXML::Document.new(@provider.list_metadata_formats(:identifier => "oai:test:#{record_id}")) }
doc = REXML::Document.new(@provider.list_metadata_formats)
assert doc.elements['/OAI-PMH/ListMetadataFormats/metadataFormat/metadataPrefix'].text == 'oai_dc'
end
def test_list_records
assert_nothing_raised do
REXML::Document.new(@provider.list_records(:metadata_prefix => 'oai_dc'))
end
doc = REXML::Document.new(@provider.list_records(
:metadata_prefix => 'oai_dc'))
assert_equal 100, doc.elements['OAI-PMH/ListRecords'].to_a.size
end
def test_list_records_scope
@provider = ARProviderWithScope.new
doc = nil
assert_nothing_raised do
doc = REXML::Document.new(@provider.list_records(:metadata_prefix => 'oai_dc'))
end
expected_count = DCField.where("date < ?", ARProviderWithScope::DATE_LESS_THAN_RESTRICTION).count
assert_equal expected_count, doc.elements['OAI-PMH/ListRecords'].to_a.size
end
def test_get_record_alternate_identifier_column
@provider = ARProviderCustomIdentifierField.new
record_id = DCField.first.send(@provider.class.model.identifier_field)
doc = REXML::Document.new(@provider.get_record(
:identifier => "oai:test:#{record_id}", :metadata_prefix => 'oai_dc'))
assert_equal "oai:test:#{record_id}", doc.elements['OAI-PMH/GetRecord/record/header/identifier'].text
end
def test_list_identifiers
assert_nothing_raised do
REXML::Document.new(@provider.list_identifiers(:metadata_prefix => 'oai_dc'))
end
doc = REXML::Document.new(@provider.list_identifiers(:metadata_prefix => 'oai_dc'))
assert_equal 100, doc.elements['OAI-PMH/ListIdentifiers'].to_a.size
end
def test_get_record
record_id = DCField.first.id
assert_nothing_raised do
REXML::Document.new(@provider.get_record(
:identifier => "oai:test:#{record_id}", :metadata_prefix => 'oai_dc'))
end
doc = REXML::Document.new(@provider.get_record(
:identifier => "#{record_id}", :metadata_prefix => 'oai_dc'))
assert_equal "oai:test:#{record_id}", doc.elements['OAI-PMH/GetRecord/record/header/identifier'].text
end
def test_deleted
record = DCField.first
record.deleted = true;
record.save
doc = REXML::Document.new(@provider.get_record(
:identifier => "oai:test:#{record.id}", :metadata_prefix => 'oai_dc'))
assert_equal "oai:test:#{record.id}", doc.elements['OAI-PMH/GetRecord/record/header/identifier'].text
assert_equal 'deleted', doc.elements['OAI-PMH/GetRecord/record/header'].attributes["status"]
end
def test_from
first_id = DCField.order("id asc").first.id
DCField.where("dc_fields.id < #{first_id + 90}").update_all(updated_at: Time.parse("January 1 2005"))
DCField.where("dc_fields.id < #{first_id + 10}").update_all(updated_at: Time.parse("June 1 2005"))
from_param = Time.parse("January 1 2006").getutc.iso8601
doc = REXML::Document.new(
@provider.list_records(
:metadata_prefix => 'oai_dc', :from => from_param)
)
assert_equal DCField.where(["dc_fields.updated_at >= ?", from_param]).size,
doc.elements['OAI-PMH/ListRecords'].size
doc = REXML::Document.new(
@provider.list_records(
:metadata_prefix => 'oai_dc', :from => Time.parse("May 30 2005").getutc.iso8601)
)
assert_equal 20, doc.elements['OAI-PMH/ListRecords'].to_a.size
end
def test_until
first_id = DCField.order(id: :asc).first.id
DCField.where("dc_fields.id < ?", first_id + 10).update_all(updated_at: Time.parse("June 1 2005"))
doc = REXML::Document.new(
@provider.list_records(
:metadata_prefix => 'oai_dc', :until => Time.parse("June 1 2005"))
)
assert_equal 10, doc.elements['OAI-PMH/ListRecords'].to_a.size
end
def test_from_and_until
first_id = DCField.order(id: :asc).first.id
DCField.update_all(updated_at: Time.parse("June 1 2005"))
DCField.where("dc_fields.id < ?", first_id + 50).update_all(updated_at: Time.parse("June 15 2005"))
DCField.where("dc_fields.id < ?", first_id + 10).update_all(updated_at: Time.parse("June 30 2005"))
doc = REXML::Document.new(
@provider.list_records(
:metadata_prefix => 'oai_dc',
:from => Time.parse("June 3 2005").getutc.iso8601,
:until => Time.parse("June 16 2005").getutc.iso8601)
)
assert_equal 40, doc.elements['OAI-PMH/ListRecords'].to_a.size
end
def test_bad_until_raises_exception
DCField.order(id: :asc).limit(10).update_all(updated_at: 1.year.ago)
DCField.order(id: :desc).limit(10).update_all(updated_at: 1.year.from_now)
badTimes = [
'junk',
'February 92nd, 2015']
badTimes.each do |time|
assert_raise(OAI::ArgumentException) do
@provider.list_records(:metadata_prefix => 'oai_dc', :until => time)
end
end
end
def test_bad_from_raises_exception
DCField.order(id: :asc).limit(10).update_all(updated_at: 1.year.ago)
DCField.order(id: :desc).limit(10).update_all(updated_at: 1.year.from_now)
badTimes = [
'junk',
'February 92nd, 2015']
badTimes.each do |time|
assert_raise(OAI::ArgumentException) do
@provider.list_records(:metadata_prefix => 'oai_dc', :from => time)
end
end
end
def test_handles_empty_collections
DCField.delete_all
assert DCField.count == 0
# Identify and ListMetadataFormats should return normally
test_identify
test_metadata_formats
# ListIdentifiers and ListRecords should return "noRecordsMatch" error code
assert_raises(OAI::NoMatchException) do
REXML::Document.new(@provider.list_identifiers(:metadata_prefix => 'oai_dc'))
end
assert_raises(OAI::NoMatchException) do
REXML::Document.new(@provider.list_records(:metadata_prefix => 'oai_dc'))
end
end
def test_bad_id_raises_exception
badIdentifiers = [
'invalid"id',
'oai:test/5000',
'oai:test/-1',
'oai:test/one',
'oai:test/\\$1\1!']
badIdentifiers.each do |id|
assert_raise(OAI::IdException) do
@provider.get_record(:identifier => id, :metadata_prefix => 'oai_dc')
end
end
end
def setup
@provider = ARProvider.new
end
end