Skip to content

Commit

Permalink
rspec
Browse files Browse the repository at this point in the history
  • Loading branch information
opoudjis committed Jul 6, 2018
1 parent e484d7d commit a789170
Show file tree
Hide file tree
Showing 35 changed files with 23,862 additions and 9 deletions.
3 changes: 3 additions & 0 deletions Gemfile
@@ -1,3 +1,6 @@
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8

source "https://rubygems.org"

# Specify your gem's dependencies in gemspec
Expand Down
43 changes: 37 additions & 6 deletions lib/relaton/relaton.rb
Expand Up @@ -19,11 +19,7 @@ def initialize(global_cache, local_cache)
# @return [String] Relaton XML serialisation of reference
def get(code, year, opts)
stdclass = standard_class(code)
case stdclass
when :isobib then Isobib::IsoBibliography.get(code, year, opts)
else
nil
end
check_bibliocache(code, year, opts, stdclass)
end

def save()
Expand All @@ -37,11 +33,46 @@ def standard_class(code)
%r{^GB Standard }.match? code and return :gbbib
%r{^IETF }.match? code and return :rfcbib
%r{^(ISO|IEC)[ /]|IEV($| )}.match? code and return :isobib
raise(RelatonError,
raise(RelatonError,
"#{code} does not have a recognised prefix: #{PREFIXES.join(', ')}")
nil
end

def std_id(code, year, opts, _stdclass)
ret = code
ret += ":#{year}" if year
ret += " (all parts)" if opts[:all_parts]
ret
end

def check_bibliocache(code, year, opts, stdclass)
id = std_id(code, year, opts, stdclass)
return nil if @bibdb.nil? # signals we will not be using isobib
@bibdb[id] = nil unless is_valid_bibcache_entry?(@bibdb[id], year)
@bibdb[id] ||= new_bibcache_entry(code, year, opts, stdclass)
@local_bibdb[id] = @bibdb[id] if !@local_bibdb.nil? &&
!is_valid_bibcache_entry?(@local_bibdb[id], year)
return @local_bibdb[id]["bib"] unless @local_bibdb.nil?
@bibdb[id]["bib"]
end

# hash uses => , because the hash is imported from JSON
def new_bibcache_entry(code, year, opts, stdclass)
bib = case stdclass
when :isobib then Isobib::IsoBibliography.get(code, year, opts)
else
nil
end
return nil if bib.nil?
{ "fetched" => Date.today, "bib" => bib }
end

# if cached reference is undated, expire it after 60 days
def is_valid_bibcache_entry?(x, year)
x && x.is_a?(Hash) && x&.has_key?("bib") && x&.has_key?("fetched") &&
(year || Date.today - Date.iso8601(x["fetched"]) < 60)
end

def open_cache_biblio(filename)
biblio = {}
if Pathname.new(filename).file?
Expand Down
66 changes: 63 additions & 3 deletions spec/relaton/relaton_spec.rb
Expand Up @@ -4,10 +4,70 @@
it "rejects an illegal reference prefix" do
system "rm testcache.json testcache2.json"
db = Relaton::Db.new("testcache.json", "testcache2.json")
expect(db.get("XYZ XYZ", nil, {})).to raise_error(RelatonError)
expect{db.get("XYZ XYZ", nil, {})}.to raise_error(Relaton::RelatonError)
db.save()
expect(File.exist?("testcache.json")).to be false
expect(File.exist?("testcache2.json")).to be false
expect(File.exist?("testcache.json")).to be true
expect(File.exist?("testcache2.json")).to be true
testcache = File.read "testcache.json"
expect(testcache).to eq "{}"
testcache = File.read "testcache2.json"
expect(testcache).to eq "{}"
end

it "gets an ISO reference and caches it" do
mock_algolia 1
mock_http_net 2
system "rm testcache.json testcache2.json"
db = Relaton::Db.new("testcache.json", "testcache2.json")
bib = db.get("ISO 19115-1", nil, {})
expect(bib).to include "<bibitem type=\"international-standard\" id=\"ISO19115-1\">"
db.save()
expect(File.exist?("testcache.json")).to be true
expect(File.exist?("testcache2.json")).to be true
testcache = File.read "testcache.json"
expect(testcache).to include "<bibitem type=\\\"international-standard\\\" id=\\\"ISO19115-1\\\">"
testcache = File.read "testcache2.json"
expect(testcache).to include "<bibitem type=\\\"international-standard\\\" id=\\\"ISO19115-1\\\">"
end

private

# rubocop:disable Naming/UncommunicativeBlockParamName, Naming/VariableName
# rubocop:disable Metrics/AbcSize
# Mock xhr rquests to Algolia.
def mock_algolia(num)
index = double 'index'
expect(index).to receive(:search) do |text, facetFilters:, page: 0|
expect(text).to be_instance_of String
expect(facetFilters[0]).to eq 'category:standard'
JSON.parse File.read "spec/support/algolia_resp_page_#{page}.json"
end.exactly(num).times
expect(Algolia::Index).to receive(:new).with('all_en').and_return index
end
# rubocop:enable Naming/UncommunicativeBlockParamName, Naming/VariableName
# rubocop:enable Metrics/AbcSize

# Mock http get pages requests.
def mock_http_net(num)
expect(Net::HTTP).to receive(:get_response).with(kind_of(URI)) do |uri|
if uri.path.match? %r{\/contents\/}
# When path is from json response then redirect.
resp = Net::HTTPMovedPermanently.new '1.1', '301', 'Moved Permanently'
resp['location'] = "/standard/#{uri.path.match(/\d+\.html$/)}"
else
# In other case return success response with body.
resp = double_resp uri
end
resp
end.exactly(num).times
end

def double_resp(uri)
resp = double 'resp'
expect(resp).to receive(:body) do
File.read "spec/support/#{uri.path.tr('/', '_')}"
end.at_least :once
expect(resp).to receive(:code).and_return('200').at_most :once
resp
end
end
828 changes: 828 additions & 0 deletions spec/support/_fr_standard_26020.html

Large diffs are not rendered by default.

843 changes: 843 additions & 0 deletions spec/support/_fr_standard_32557.html

Large diffs are not rendered by default.

768 changes: 768 additions & 0 deletions spec/support/_fr_standard_32579.html

Large diffs are not rendered by default.

880 changes: 880 additions & 0 deletions spec/support/_fr_standard_39229.html

Large diffs are not rendered by default.

606 changes: 606 additions & 0 deletions spec/support/_fr_standard_44361.html

Large diffs are not rendered by default.

850 changes: 850 additions & 0 deletions spec/support/_fr_standard_53798.html

Large diffs are not rendered by default.

805 changes: 805 additions & 0 deletions spec/support/_fr_standard_57104.html

Large diffs are not rendered by default.

768 changes: 768 additions & 0 deletions spec/support/_fr_standard_66197.html

Large diffs are not rendered by default.

704 changes: 704 additions & 0 deletions spec/support/_fr_standard_67039.html

Large diffs are not rendered by default.

775 changes: 775 additions & 0 deletions spec/support/_fr_standard_73118.html

Large diffs are not rendered by default.

831 changes: 831 additions & 0 deletions spec/support/_ru_standard_26020.html

Large diffs are not rendered by default.

841 changes: 841 additions & 0 deletions spec/support/_ru_standard_32557.html

Large diffs are not rendered by default.

772 changes: 772 additions & 0 deletions spec/support/_ru_standard_32579.html

Large diffs are not rendered by default.

872 changes: 872 additions & 0 deletions spec/support/_ru_standard_39229.html

Large diffs are not rendered by default.

597 changes: 597 additions & 0 deletions spec/support/_ru_standard_44361.html

Large diffs are not rendered by default.

843 changes: 843 additions & 0 deletions spec/support/_ru_standard_53798.html

Large diffs are not rendered by default.

803 changes: 803 additions & 0 deletions spec/support/_ru_standard_57104.html

Large diffs are not rendered by default.

767 changes: 767 additions & 0 deletions spec/support/_ru_standard_66197.html

Large diffs are not rendered by default.

695 changes: 695 additions & 0 deletions spec/support/_ru_standard_67039.html

Large diffs are not rendered by default.

768 changes: 768 additions & 0 deletions spec/support/_ru_standard_73118.html

Large diffs are not rendered by default.

0 comments on commit a789170

Please sign in to comment.