require File.join(File.dirname(__FILE__), "test_helper")
# spec sketch is here:
# http://mulube.unfuddle.com/projects/9384/notebooks/2319/pages/102977/latest
#TODO: split this into separate files, testing the xml parsing, the xml generation, etc.
class TestEnvironment < Test::Unit::TestCase
include LibXMLTestHelper
#TODO: this is mostly testing eeml V005 parser, not environment generally
test "creating Env obj from unparseable XML" do
assert_raises BadXML do
environment = Environment.new_from_eeml("this is bad xml")
end
assert_raises BadXML do
environment = Environment.new_from_eeml("12, 14, 99, a csv!, 99")
end
end
# Create an object from an eeml document
test 'creating Env obj - attrs' do
env = create_env_from_xml_file_one
# simple text-nodes available as attributes
assert_equal('title here', env.title)
assert_equal('description here', env.description)
assert_equal('http://example.com/api/1247.xml', env.feed_url)
assert_equal('http://example.com/studio/', env.website)
assert_equal('frozen', env.status)
assert_equal('someone@example.com', env.email)
assert_equal('http://example.com/some/icon.gif', env.icon)
#env attrs
assert_equal('1247', env.identifier)
assert_equal('2009-02-11T10:56:56Z', env.updated.strftime("%Y-%m-%dT%H:%M:%SZ"))
assert_equal('http://example.com/creator/', env.creator)
end
test "creating minimal Env object from some 0.5.2 eeml" do
env = create_env_from_eeml_052
# simple text nodes available
assert_equal("Pachube Office environment", env.title)
end
test "creating complete Env object from some 0.5.2 eeml" do
env = create_complete_env_from_eeml_052
# simple text nodes available
assert_equal("title here", env.title)
# new attributes
assert_equal("http://www.pachube.com", env.publisher)
assert_equal("http://www.pachube.com/users/joe", env.owner)
end
test 'location parses ok' do
env = create_env_from_xml_file_one
#
# Up on the roof (somewhere)
# 50.1
# 48.7
# 1.34
#
assert_not_nil env.location
location = env.location
assert_equal "Up on the roof (somewhere)", location.name
assert_equal "50.1", location.latitude
assert_equal "48.7", location.longitude
assert_equal "1.34", location.elevation
assert_equal "physical", location.domain
assert_equal "outdoor", location.exposure
assert_equal "mobile", location.disposition
end
test "datastreams parse ok" do
env = create_env_from_xml_file_one
assert_equal 3, env.datastreams.size
#the expected values for the datastreams in the test doc
expectations_list = [
#id, tag array, min, max, value, unit_symbol, unit_type, unit_value
['0', ['tagD0'], '-9999.0', '1022.0', '0', 'C', 'basicSI', 'Celsius'],
['1', [], '0.0', '1023.0', '33', nil, nil, nil],
['2', ['tagD2a', 'tagD2b', 'tagD2c'], '23.4', '1021.0', '42.1', nil, nil, nil]
]
env.datastreams.each_with_index do |ds, i|
e_identifier, e_tags, e_min_value, e_max_value, e_value, e_unit_symbol, e_unit_type, e_unit_value = expectations_list[i]
assert_equal e_identifier, ds.identifier
assert_equal e_tags.size, ds.tags.size, "should have right num of tags"
e_tags.each do |expected_tag|
ds.tags.member? expected_tag
end
assert_equal e_min_value, ds.min_value
assert_equal e_max_value, ds.max_value
assert_equal e_value, ds.value
assert_equal e_unit_symbol, ds.unit_symbol
assert_equal e_unit_type, ds.unit_type
assert_equal e_unit_value, ds.unit_value
end
end
implement "should not try to parse huge xml string" do
#TODO: arguably, fetcher should also have rejected huge string
end
test "passes if no datastreams" do
original_xml = File.read('test/data/doc_1.xml')
test_xml = remove_nodes_called('data', original_xml)
environment = Environment.new_from_eeml(test_xml)
assert_equal('title here', environment.title)
assert_equal([], environment.datastreams)
end
test "fail if any datastream has no value" do
test_xml = remove_first_node_called('value', read_xml_file_one)
assert_raises(DataMissingValue) do
Environment.new_from_eeml(test_xml)
end
end
test "fail if any datastream has two values" do
new_value_snippet=<<-END
10
14
END
original_xml = read_xml_file_one
test_xml = replace_first_node_called('value', original_xml, new_value_snippet)
assert_raises(DataHasMultipleValues) { Environment.new_from_eeml(test_xml) }
end
test "fail if any datastream has two units" do
new_unit_snippet =<<-END
Celsius
Celsius
END
original_xml = read_xml_file_one
test_xml = replace_first_node_called('unit', original_xml, new_unit_snippet)
assert_raises(DataHasMultipleUnits) { Environment.new_from_eeml(test_xml) }
end
test "ok if no location" do
original_xml = read_xml_file_one
test_xml = remove_first_node_called('location', original_xml)
env = Environment.new_from_eeml(test_xml)
# all other attributes still accessible
assert_equal('title here', env.title)
assert_equal('description here', env.description)
assert_equal('http://example.com/api/1247.xml', env.feed_url)
assert_equal('http://example.com/studio/', env.website)
assert_equal('frozen', env.status)
assert_equal('someone@example.com', env.email)
assert_equal('http://example.com/some/icon.gif', env.icon)
#env attrs
assert_equal('1247', env.identifier)
assert_equal('2009-02-11T10:56:56Z', env.updated.utc.iso8601)
assert_equal('http://example.com/creator/', env.creator)
end
test "ok parsing most minimal eeml file" do
env = create_env_from_xml_file('minimal.xml')
assert_equal 1, env.datastreams.size
datastream = env.datastreams[0]
assert_equal "0", datastream.identifier
# this value.value is a bit ugly
assert_equal "36.2", datastream.value
end
test "ok parsing fully namespaced eeml file" do
env = create_env_from_xml_file('complete_namespaced.xml')
assert_equal 2, env.datastreams.size
datastream = env.datastreams[0]
assert_equal "36.2", datastream.value
end
test "fails if no default namespace provided" do
assert_raise(MissingNamespace) {env = create_env_from_xml_file('no_namespace.xml')}
end
test "captures the `private` attribute if present" do
env = create_env_from_xml_file("doc_1_private.xml")
assert_equal true, env.private
end
test "captures the `private` attribute as false if present and false" do
env = create_env_from_xml_file("doc_1_non_private.xml")
assert_equal false, env.private
end
test "ok parsing oddly tagged eeml file" do
env = create_env_from_xml_file('difficult_tag.xml')
assert_equal 2, env.datastreams.size
d = env.datastreams[0]
assert_equal 2, d.tags.size
assert_equal '10.F59894010800', d.tags[0]
assert_equal 'Living Room, S wall', d.tags[1]
d2 = env.datastreams[1]
assert_equal 2, d2.tags.size
assert_equal '28.3616F1010000', d2.tags[0]
assert_equal 'Living Room, .7m above wood stove', d2.tags[1]
assert_equal '24.1875', d.value
assert_equal '24.4375', d2.value
assert_equal 'celsius', d.unit_value
end
test "parses environment with stub datastreams v051 xml" do
env = create_env_from_xml_file('environment_minimal_051.xml')
assert_equal 2, env.datastreams.size
end
test "parses environment level tags from v051 xml" do
env = create_env_from_xml_file('environment_tags.xml')
assert_equal 2, env.tags.size
assert_equal 'Cambodia', env.tags[0]
assert_equal 'House', env.tags[1]
assert_equal 1, env.datastreams.size
end
test "creates multiple environments from list xml" do
xml = File.read('test/data/list.xml')
list = Environment.new_list_from_eeml(xml)
assert 3, list.size
list.each do |obj|
assert obj.is_a? Environment
end
titles = list.map {|e| e.title}.sort
expected_titles = ["Badgerpower", "CurrentCost for house", "Power and Temperature"]
assert_equal expected_titles, titles
#TODO: check the environments fully (that no aliasing or other confusion has occurred in their parsing)
end
test "creates zero environments from list xml when has none" do
original_xml = File.read('test/data/list.xml')
xml = remove_nodes_called('environment', original_xml)
list = Environment.new_list_from_eeml(xml)
assert list.empty?, "should be empty list"
end
def read_xml_file_one
return File.read('test/data/doc_1.xml')
end
test "parses_all_previously_acceptable_feeds" do
passed_filenames = []
dir_name = 'test/data/real_xmls/v005/oks/'
#check dir exists:
filenames = Dir.entries(dir_name).select{|name| name =~ /.xml/}.sort
assert filenames.size > 1, "should be at least one file found in #{dir_name}"
filenames.each do |filename|
begin
xml = File.read(File.join(dir_name, filename))
env = Environment.new_from_eeml(xml)
passed_filenames << filename
rescue
#FileUtils.mv(File.join(dir_name, filename), 'test/real_xmls/v005/questioned')
raise "While parsing previously acceptable xml in file #{filename} got exception: #{$!.class}: #{$!}." +
"\nSTART OF XML SNIPPET from #{filename}\n#{xml[0..100]}\nEND OF XML SNIPPET" +
"\n Prior to error, successfully parsing #{passed_filenames.size} previously-ok xmls"
end
end
puts "passed all %d files" % passed_filenames.size
end
implement "throw detailed exception if missing mandatory node" do
original_xml = read_xml_file_one
#we'll test for
%w(status email).each do |missing_node_name|
test_xml = remove_first_node_called(missing_node_name, original_xml) #make test document from generic one
#should throw an exception with the right details
begin
Environment.new_from_eeml(test_xml)
flunk "should've raised an exception"
rescue MissingNode => e
assert_equal missing_node_name, e.sought_description, 'should have correct missing node name'
end
end
end
test "ignore unexpected attributes on title node" do
original_xml = read_xml_file_one
test_xml = original_xml.sub(/
/, '')
assert test_xml =~ /myattr/, 'substitution should have placed unexpected "myattr"'
env = Environment.new_from_eeml(test_xml)
assert_equal 'title here', env.title
end
test "handle missing optional nodes" do
original_xml = read_xml_file_one
#we'll remove the following
['title', 'website', ['feed', 'feed_url']].each do |missing_node_name, accessor_name|
accessor_name = missing_node_name if accessor_name.nil?
test_xml = remove_first_node_called(missing_node_name, original_xml) #make test document from generic one
env = Environment.new_from_eeml(test_xml)
assert_nil env.send((accessor_name).to_sym)
end
end
# implement "fail if location missing" do
# original_xml = read_xml_file_one
# test_xml = remove_first_node_called('location', original_xml)
# #should throw an exception with the right details
# begin
# Environment.new_from_eeml(test_xml)
# flunk "should've raised an exception for missing location"
# rescue MissingNode => e
# assert_equal 'location', e.sought_description, 'should have correct missing node name'
# end
# end
# --- ----------------------------------------------------
# --- construction stuff ----------------------------------------------------
# --- ----------------------------------------------------
test "env constructor uses params" do
env = Environment.new(:identifier => 'whatever', :creator => 'http://www.example.com/ourstudio/',
:feed_url => 'http://www.example.com/ourstudio/feeds/house.xml')
assert_equal 'whatever', env.identifier
assert_equal 'http://www.example.com/ourstudio/', env.creator
assert_equal 'http://www.example.com/ourstudio/feeds/house.xml', env.feed_url
end
# --- ------------------------------------------------------
# --- json input stuff ------------------------------------------------------
# --- ------------------------------------------------------
test "creating Env obj from a JSON string" do
env = create_env_from_json_file_one
assert_not_nil env
assert_instance_of Environment, env
assert_equal('title here', env.title)
assert_equal('description here', env.description)
assert_equal('http://example.com/api/1247.xml', env.feed_url)
assert_equal('http://example.com/studio/', env.website)
assert_equal('frozen', env.status)
assert_equal('someone@example.com', env.email)
assert_equal('http://example.com/some/icon.gif', env.icon)
#env attrs
assert_equal('1247', env.identifier)
assert_equal('2009-02-11T10:56:56Z', env.updated.utc.iso8601)
end
test "location parses ok from json" do
env = create_env_from_json_file_one
assert_not_nil env.location
location = env.location
assert_equal "Up on the roof (somewhere)", location.name
assert_equal "50.1", location.latitude
assert_equal "48.7", location.longitude
assert_equal "1.34", location.elevation
assert_equal "physical", location.domain
assert_equal "outdoor", location.exposure
assert_equal "mobile", location.disposition
end
test "datastreams parse ok from json" do
env = create_env_from_json_file_one
assert_equal 3, env.datastreams.size
#the expected values for the datastreams in the test doc
expectations_list = [
#id, tag array, min, max, value, unit_symbol, unit_type, unit_value
['0', ['tagD0'], '-9999.0', '1022.0', '0', 'C', 'basicSI', 'Celsius'],
['1', [], '0.0', '1023.0', '33', nil, nil, nil],
['2', ['tagD2a', 'tagD2b', 'tagD2c'], '23.4', '1021.0', '42.1', nil, nil, nil]
]
env.datastreams.each_with_index do |ds, i|
e_identifier, e_tags, e_min_value, e_max_value, e_value, e_unit_symbol, e_unit_type, e_unit_value = expectations_list[i]
assert_equal e_identifier, ds.identifier
assert_equal e_tags.size, ds.tags.size, "should have right num of tags"
e_tags.each do |expected_tag|
ds.tags.member? expected_tag
end
assert_equal e_min_value, ds.min_value
assert_equal e_max_value, ds.max_value
assert_equal e_value, ds.value
assert_equal e_unit_symbol, ds.unit_symbol
assert_equal e_unit_type, ds.unit_type
assert_equal e_unit_value, ds.unit_value
end
end
test "passes if no datastreams from json" do
original_hash = JSON.parse(File.read('test/data/doc_1.json'))
original_hash.delete('datastreams')
test_json = original_hash.to_json
environment = Environment.new_from_json(test_json)
assert_equal('title here', environment.title)
assert_equal([], environment.datastreams)
end
test "parses a json with version missing and raises appropriate error" do
#original_hash = JSON.parse(File.read('test/data/doc_1.json'))
#original_hash.delete('version')
test_json = '[0,0,0,"9.44","83.68","1013.64"]'
assert_raises(JSON::ParserError) do
environment = Environment.new_from_json(test_json)
end
end
test "parses a v1.0.0 input file ok" do
env = create_env_from_json_v100_file_one
assert_not_nil env
assert_instance_of Environment, env
assert_equal('title here', env.title)
assert_equal('description here', env.description)
assert_equal('http://example.com/api/1247.xml', env.feed_url)
assert_equal('http://example.com/studio/', env.website)
assert_equal('frozen', env.status)
assert_equal('someone@example.com', env.email)
assert_equal('http://example.com/some/icon.gif', env.icon)
#env attrs
assert_equal('1247', env.identifier)
assert_equal('2010-02-11T10:56:56Z', env.updated.utc.iso8601)
# new stuff
assert_equal("http://www.pachube.com", env.publisher)
assert_equal("http://www.pachube.com/users/joe", env.owner)
end
test "location parses ok from json when parsing a v1.0.0 file" do
env = create_env_from_json_v100_file_one
assert_not_nil env.location
location = env.location
assert_equal "Up on the roof (somewhere)", location.name
assert_equal "50.1", location.latitude
assert_equal "48.7", location.longitude
assert_equal "1.34", location.elevation
assert_equal "physical", location.domain
assert_equal "outdoor", location.exposure
assert_equal "mobile", location.disposition
end
test "datastreams parse ok from v1.0.0 json" do
env = create_env_from_json_v100_file_one
assert_equal 3, env.datastreams.size
#the expected values for the datastreams in the test doc
expectations_list = [
#id, tag array, min, max, value, unit_symbol, unit_type, unit_value
['0', ['tagD0'], '-9999.0', '1022.0', '0', 'C', 'basicSI', 'Celsius', '2009-02-11T10:56:56Z'],
['1', [], '0.0', '1023.0', '33', nil, nil, nil, '2009-02-11T10:56:55Z'],
['2', ['tagD2a', 'tagD2b', 'tagD2c'], '23.4', '1021.0', '42.1', nil, nil, nil, '2009-02-11T10:55:10Z']
]
env.datastreams.each_with_index do |ds, i|
e_identifier, e_tags, e_min_value, e_max_value, e_value, e_unit_symbol, e_unit_type, e_unit_value, e_value_recorded_at = expectations_list[i]
assert_equal e_identifier, ds.identifier
assert_equal e_tags.size, ds.tags.size, "should have right num of tags"
e_tags.each do |expected_tag|
ds.tags.member? expected_tag
end
assert_equal e_value_recorded_at, ds.values.last.recorded_at
assert_equal e_min_value, ds.min_value
assert_equal e_max_value, ds.max_value
assert_equal e_value, ds.value
assert_equal e_unit_symbol, ds.unit_symbol
assert_equal e_unit_type, ds.unit_type
assert_equal e_unit_value, ds.unit_value
end
end
test "passes in v1.0.0 if no datastreams from json" do
original_hash = JSON.parse(File.read('test/data/doc_1_v1-0-0.json'))
original_hash.delete('datastreams')
test_json = original_hash.to_json
environment = Environment.new_from_json(test_json)
assert_equal('title here', environment.title)
assert_equal([], environment.datastreams)
end
test "parses environment level tags from v1.0.0 json" do
env = create_env_from_json_file('test/data/environment_tags.json')
assert_equal 3, env.datastreams.size
assert_equal 2, env.tags.size
assert_equal env.tags[0], 'tagE0'
assert_equal env.tags[1], 'tagE1'
end
test "parses a v6 input file ok" do
env = create_env_from_json_v6_file_one
assert_not_nil env
assert_instance_of Environment, env
assert_equal('title here', env.title)
assert_equal('description here', env.description)
assert_equal('http://example.com/api/1247.xml', env.feed_url)
assert_equal('http://example.com/studio/', env.website)
assert_equal('frozen', env.status)
assert_equal('someone@example.com', env.email)
assert_equal('http://example.com/some/icon.gif', env.icon)
#env attrs
assert_equal('1247', env.identifier)
assert_equal('2009-02-11T10:56:56Z', env.updated.utc.iso8601)
end
test "location parses ok from json when parsing a v6 file" do
env = create_env_from_json_v6_file_one
assert_not_nil env.location
location = env.location
assert_equal "Up on the roof (somewhere)", location.name
assert_equal "50.1", location.latitude
assert_equal "48.7", location.longitude
assert_equal "1.34", location.elevation
assert_equal "physical", location.domain
assert_equal "outdoor", location.exposure
assert_equal "mobile", location.disposition
end
test "datastreams parse ok from v6 json" do
env = create_env_from_json_v6_file_one
assert_equal 3, env.datastreams.size
#the expected values for the datastreams in the test doc
expectations_list = [
#id, tag array, min, max, value, unit_symbol, unit_type, unit_value
['0', ['tagD0'], '-9999.0', '1022.0', '0', 'C', 'basicSI', 'Celsius'],
['1', [], '0.0', '1023.0', '33', nil, nil, nil],
['2', ['tagD2a', 'tagD2b', 'tagD2c'], '23.4', '1021.0', '42.1', nil, nil, nil]
]
env.datastreams.each_with_index do |ds, i|
e_identifier, e_tags, e_min_value, e_max_value, e_value, e_unit_symbol, e_unit_type, e_unit_value = expectations_list[i]
assert_equal e_identifier, ds.identifier
assert_equal e_tags.size, ds.tags.size, "should have right num of tags"
e_tags.each do |expected_tag|
ds.tags.member? expected_tag
end
assert_equal e_min_value, ds.min_value
assert_equal e_max_value, ds.max_value
assert_equal e_value, ds.value
assert_equal e_unit_symbol, ds.unit_symbol
assert_equal e_unit_type, ds.unit_type
assert_equal e_unit_value, ds.unit_value
end
end
test "passes in v6 if no datastreams from json" do
original_hash = JSON.parse(File.read('test/data/doc_1_v6.json'))
original_hash.delete('datastreams')
test_json = original_hash.to_json
environment = Environment.new_from_json(test_json)
assert_equal('title here', environment.title)
assert_equal([], environment.datastreams)
end
test "captures `private` attribute when parsing old json" do
env = create_env_from_json_file("test/data/doc_1_private.json")
assert_not_nil env
assert_equal true, env.private
end
test "captures the `private` attribute as false if present and false when parsing old json" do
env = create_env_from_json_file("test/data/doc_1_non_private.json")
assert_equal false, env.private
end
test "captures `private` attribute when parsing v6 json" do
env = create_env_from_json_file("test/data/doc_1_v6_private.json")
assert_not_nil env
assert_equal true, env.private
end
test "captures `private` attribute when parsing 1.0.0 json" do
env = create_env_from_json_file("test/data/doc_1_v1-0-0.json")
assert_not_nil env
assert_equal false, env.private
end
test "captures the `private` attribute as false if present and false when parsing v6 json" do
env = create_env_from_json_file("test/data/doc_1_v6_non_private.json")
assert_not_nil env
assert_equal false, env.private
end
# --- -------------------------------------------------------
# --- csv input stuff -------------------------------------------------------
# --- -------------------------------------------------------
def create_env_with_datastream_values(values)
e = Environment.new
values.each do |v|
e.add_datastream DataStream.new(:value => v)
end
assert_equal values.size, e.datastreams.size
return e
end
test "update from csv values" do
#add a helper method to get an environments datastream values (ignoring min, max) as a simple array
#TODO: note, can't unload the class before other tests, so be careful with this approach to conveniences.
class Eeml::Environment
def values_quick
datastreams.map {|ds| ds.values.last.value}
end
end
values = ['11.01', '2', 'thirdval', '40'].freeze
#this env has NO datastreams, so each value will add a ds.
env = Environment.new
env.update_datastreams_from_csv_values!(values)
assert_equal values, env.values_quick
#When env has FEWER datastreams than the csv
#it should update the ones it has, AND add extra datastreams for the excess in the csv
#TODO: check this assumption:
# ALL datastreams should take their new value from csv, keeping previous min, max values.
env = create_env_with_datastream_values(%w(e1 e2 e3))
env.update_datastreams_from_csv_values!(values)
assert_equal values, env.values_quick
#When env has MORE datastreams than the csv
#it "should remove extra datastreams if less values are published to the csv feed" do
env = create_env_with_datastream_values(%w(e1 e2 e3 e4 e5))
env.update_datastreams_from_csv_values!(values)
assert_equal values, env.values_quick
#WHEN env has SAME number of ds as the csv
env = create_env_with_datastream_values(%w(e1 e2 e3 e4))
env.update_datastreams_from_csv_values!(values)
assert_equal values, env.values_quick
end
test "creating via csv should accept version v1" do
environment = Environment.new_from_csv("1,2,3,4", :v1)
assert_equal 4, environment.datastreams.length
end
test "creating via csv should accept version v2" do
csv = <<-CSV
1,2
3,4
CSV
environment = Environment.new_from_csv(csv, :v2)
assert_equal 2, environment.datastreams.length
assert_equal ['1', '3'], environment.datastreams.map(&:identifier).sort
end
test "creating via csv should attempt to detect version" do
csv = <<-CSV
1,2
3,4
CSV
environment = Environment.new_from_csv(csv)
assert_equal 2, environment.datastreams.length
assert_equal ['1', '3'], environment.datastreams.map(&:identifier).sort
end
test "creating via csv should assume v1 if one row and one entry" do
environment = Environment.new_from_csv("nought")
assert_equal 1, environment.datastreams.length
assert_equal "nought", environment.datastreams.first.values.first.value
end
test "creating via csv should assume v1 if one row and 3+ entry" do
environment = Environment.new_from_csv("nought,two,free")
assert_equal 3, environment.datastreams.length
assert_equal ["free", "nought", "two"], environment.datastreams.collect(&:values).flatten.map(&:value).sort
end
test "creating via csv should raise an exception if it cannot detect the version" do
exception = assert_raises CsvEncodingError do
Environment.new_from_csv("1,2")
end
assert_equal "CSV version could not be detected", exception.message
end
# --- Other Unit Tests -------------------------------------------------------
# --- -------------------------------------------------------
# --- Stuff that gets used in lots of places ---------------------------------
test "environment#add_datastreams should add a group of datastreams" do
e = Environment.new
values = %w(e1 e2 e3)
e.add_datastreams values.collect {|v| DataStream.new(:value => v)}
assert_equal values.size, e.datastreams.size
end
test "environment#add_datastreams should raise an exception if passed duplicate datastreams" do
e = Environment.new
stream_ids = %w(ds1 ds2 ds1 ds3)
assert_raise Exceptions::DuplicateDataStreams, "Duplicate Datastream ID: ds1" do
e.add_datastreams stream_ids.collect {|id| DataStream.new(:identifier => id)}
end
end
test "environment#add_datastreams should raise an exception if datastream ids conflict with existing stream ids" do
e = Environment.new
stream_ids = %w(ds1 ds2 ds3)
e.add_datastreams stream_ids.collect {|id| DataStream.new(:identifier => id)}
assert_raise Exceptions::DuplicateDataStreams, "Duplicate Datastream ID: ds2" do
e.add_datastreams [DataStream.new(:identifier => "ds2")]
end
end
test "environment#add_datastream should raise an exception if datastream ids conflict with existing stream ids" do
e = Environment.new
stream_ids = %w(ds1 ds2 ds3)
e.add_datastreams stream_ids.collect {|id| DataStream.new(:identifier => id)}
assert_raise Exceptions::DuplicateDataStreams, "Duplicate Datastream ID: ds3" do
e.add_datastream DataStream.new(:identifier => "ds3")
end
end
# --- convenience stuff - don't put tests under here ------------------------------
def create_env_from_xml_string(string)
environment = Environment.new_from_eeml(string)
assert_not_nil environment
assert_equal(Environment, environment.class)
return environment
end
def create_env_from_xml_file_one
return create_env_from_xml_file('doc_1.xml')
end
def create_env_from_xml_file(test_file)
filename = 'test/data/' + test_file
eeml_doc = File.read(filename)
assert_not_nil eeml_doc, "content of test xml file shouldn't be nil for file: #{filename}"
return create_env_from_xml_string(eeml_doc)
end
def create_env_from_json_file_one
return create_env_from_json_file('test/data/doc_1.json')
end
def create_env_from_json_v6_file_one
return create_env_from_json_file('test/data/doc_1_v6.json')
end
def create_env_from_json_v100_file_one
return create_env_from_json_file('test/data/doc_1_v1-0-0.json')
end
def create_env_from_json_file(filename)
json = File.read(filename)
return Environment.new_from_json(json)
end
def prep_json_for_reading(json)
return nil if json.nil?
json.gsub(/","/, '"' + "\n" + '"').sort
end
def create_env_from_eeml_052
return create_env_from_xml_file("environment_minimal_052.xml")
end
def create_complete_env_from_eeml_052
return create_env_from_xml_file("eeml_datastream_052.xml")
end
end