Skip to content

Commit

Permalink
Merge branch 'master' of github.com:GlobalNamesArchitecture/dwc-archive
Browse files Browse the repository at this point in the history
  • Loading branch information
dimus committed Dec 30, 2013
2 parents 15839e0 + 41de4ee commit 915be98
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
0.9.7 Refactoring and tests improvements

0.9.6 Added support for GNUB DwCA files

0.9.4 Gem dependencies updated, added travis support
Expand Down
2 changes: 1 addition & 1 deletion lib/dwc-archive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
class DarwinCore

VERSION = DarwinCore::VERSION
DEFAULT_TMP_DIR = "/tmp"

attr_reader :archive, :core, :metadata, :extensions,
:classification_normalizer
alias :eml :metadata

DEFAULT_TMP_DIR = "/tmp"

def self.nil_field?(field)
return true if [nil, '', '/N'].include?(field)
Expand Down
2 changes: 1 addition & 1 deletion lib/dwc-archive/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class DarwinCore
VERSION = "0.9.6"
VERSION = "0.9.10"
end
21 changes: 21 additions & 0 deletions spec/lib/generator_eml_xml_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require_relative '../spec_helper'

describe DarwinCore::Generator::EmlXml do
subject(:eml) { DarwinCore::Generator::EmlXml.new(data, path) }
let(:data) { EML_DATA }
let(:path) { DarwinCore::DEFAULT_TMP_DIR }

describe '.new' do
it 'initializes generator' do
expect(eml).to be_kind_of DarwinCore::Generator::EmlXml
end
end

describe '#create' do
it 'should create eml xml' do
eml.create
eml_xml = File.read(File.join(path, 'eml.xml'))
expect(eml_xml).to match /Test Classification/
end
end
end
21 changes: 21 additions & 0 deletions spec/lib/generator_meta_xml_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require_relative '../spec_helper'

describe DarwinCore::Generator::MetaXml do
subject(:meta) { DarwinCore::Generator::MetaXml.new(data, path) }
let(:data) { META_DATA }
let(:path) { DarwinCore::DEFAULT_TMP_DIR }

describe '.new' do
it 'initializes' do
expect(meta).to be_kind_of DarwinCore::Generator::MetaXml
end
end

describe '#create' do
it 'creates metadata file' do
meta.create
meta = File.read(File.join(path, 'meta.xml'))
expect(meta).to match %r|<location>core.csv</location>|
end
end
end
116 changes: 116 additions & 0 deletions spec/lib/generator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# encoding: utf-8
require_relative '../spec_helper'

describe DarwinCore::Generator do
subject(:gen) { DarwinCore::Generator.new(dwc_path, tmp_dir) }
let(:tmp_dir) { DarwinCore::DEFAULT_TMP_DIR }
let(:dwc_path) { File.join(tmp_dir, 'spec_dwca.tar.gz') }

describe '.new' do
it 'initializes empty DwCA' do
expect(gen).to be_kind_of DarwinCore::Generator
end
end

describe '#add_core' do
it 'adds core to DwCA instance' do
gen.add_core(CORE_DATA.dup, 'core.csv', true)
core = File.read(File.join(gen.path, 'core.csv'))
expect(core).to match /taxonID,parentNameUsageID,scientificName/
end

context 'urls are not given in header' do
it 'raises error' do
data = CORE_DATA.dup
data[0] = data[0].map { |f| f.split('/')[-1] }
expect { gen.add_core(data, 'core.csv', true) }.
to raise_error DarwinCore::GeneratorError
end
end
end

describe '#add_extension' do
it 'adds extension to DwCA instance' do
gen.add_extension(EXTENSION_DATA.dup,
'vern.csv',
true,
'http://rs.gbif.org/terms/1.0/VernacularName')
extension = File.read(File.join(gen.path, 'vern.csv'))

expect(extension).to match /Береза/
end
end

describe '#add_meta_xml' do
it 'creates metadata for DwCA' do
gen.add_core(CORE_DATA.dup, 'core.csv', true)
gen.add_extension(EXTENSION_DATA.dup,
'vern.csv',
true,
'http://rs.gbif.org/terms/1.0/VernacularName')

gen.add_meta_xml
meta = File.read(File.join(gen.path, 'meta.xml'))
expect(meta).to match %r|<location>core.csv</location>|
end
end

describe '#add_eml_data' do
it 'adds eml data' do
gen.add_eml_xml(EML_DATA)
eml = File.read(File.join(gen.path, 'eml.xml'))
expect(eml).to match /jdoe@example.com/
end
end

describe '#path' do
it 'returns temporary path for assembling DwCA' do
expect(gen.path).to match /dwc_\d+$/
end
end

describe '#files' do
it 'returns created files' do
gen.add_core(CORE_DATA.dup, 'core.csv', true)
gen.add_extension(EXTENSION_DATA.dup,
'vern.csv',
true,
'http://rs.gbif.org/terms/1.0/VernacularName')

gen.add_meta_xml
expect(gen.files).to match_array ['core.csv', 'meta.xml', 'vern.csv']
end
end

describe '#pack' do
it 'creates final DwCA file' do
FileUtils.rm dwc_path if File.exists?(dwc_path)
gen.add_core(CORE_DATA.dup, 'core.csv', true)
gen.add_extension(EXTENSION_DATA.dup,
'vern.csv',
true,
'http://rs.gbif.org/terms/1.0/VernacularName')

gen.add_meta_xml
gen.add_eml_xml(EML_DATA)
gen.pack
expect(File.exists?(dwc_path)).to be_true
end
end

describe '#clean' do
it 'removes temporary directory for DwCA' do
gen.add_eml_xml(EML_DATA)
expect(File.exists?(gen.path)).to be true
gen.clean
expect(File.exists?(gen.path)).to be false
end
end

describe '#eml_xml_data' do
it 'returns current eml data' do
expect(gen.eml_xml_data).to be_kind_of Hash
end
end

end
64 changes: 64 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# encoding: utf-8
# HACK: to suppress warnings
$VERBOSE = nil

Expand All @@ -12,3 +13,66 @@
RSpec.configure do |config|
end

unless defined?(SPEC_CONSTANTS)
EML_DATA = {
id: '1234',
license: 'http://creativecommons.org/licenses/by-sa/3.0/',
title: 'Test Classification',
authors: [
{ first_name: 'John',
last_name: 'Doe',
email: 'jdoe@example.com',
organization: 'Example',
position: 'Assistant Professor',
url: 'http://example.org' },
{ first_name: 'Jane',
last_name: 'Doe',
email: 'jane@example.com' }
],
metadata_providers: [
{ first_name: 'Jim',
last_name: 'Doe',
email: 'jimdoe@example.com',
url: 'http://aggregator.example.org' }],
abstract: 'test classification',
citation:
'Test classification: Doe John, Doe Jane, Taxnonmy, 10, 1, 2010',
url: 'http://example.com'
}
META_DATA = {
extensions: [
{fields: [
'http://rs.tdwg.org/dwc/terms/TaxonID',
'http://rs.tdwg.org/dwc/terms/vernacularName'],
ignoreHeaderLines: 1,
location: 'vern.csv',
rowType: 'http://rs.gbif.org/terms/1.0/VernacularName'}],
core: {
fields: [
'http://rs.tdwg.org/dwc/terms/taxonID',
'http://rs.tdwg.org/dwc/terms/parentNameUsageID',
'http://rs.tdwg.org/dwc/terms/scientificName',
'http://rs.tdwg.org/dwc/terms/taxonRank'],
ignoreHeaderLines: 1,
location: 'core.csv'}}
CORE_DATA = [
["http://rs.tdwg.org/dwc/terms/taxonID",
"http://rs.tdwg.org/dwc/terms/parentNameUsageID",
"http://rs.tdwg.org/dwc/terms/scientificName",
"http://rs.tdwg.org/dwc/terms/taxonRank"],
[1, 0, "Plantae", "kingdom"],
[2, 1, "Betula", "genus"],
[3, 2, "Betula verucosa", "species"]
]
EXTENSION_DATA = [
["http://rs.tdwg.org/dwc/terms/TaxonID",
"http://rs.tdwg.org/dwc/terms/vernacularName"],
[1, "Plants"],
[1, "Растения"],
[2, "Birch"],
[2, "Береза"],
[3, "Wheeping Birch"],
[3, "Береза плакучая"]
]
SPEC_CONSTANTS = true
end

0 comments on commit 915be98

Please sign in to comment.