require 'voruby/votable/1.1/votable'
require 'test/unit'
include VORuby::VOTable::V1_1
module VORuby
module VOTable
module V1_1
module Test
module Methods
def test_construction
obj = self.class_to_test.new(self.inputs)
outputs = self.respond_to?(:expected_outputs) ? self.expected_outputs : self.inputs
outputs.each do |name, value|
result = obj.send(name)
assert_equal result, outputs[name]
end
end
def test_accessors
obj = self.class_to_test.new
outputs = self.respond_to?(:expected_outputs) ? self.expected_outputs : self.inputs
if self.inputs.is_a?(Hash)
self.inputs.each do |name, value|
obj.send("#{name}=", value)
assert_equal obj.send(name), outputs[name]
end
end
end
end
class VOTableTest < ::Test::Unit::TestCase
include Methods
def class_to_test; VOTable end
def inputs
{
:version => '1.1',
:id => 'my_test_votable',
:description => Description.new,
:definitions => Definitions.new,
:coordinate_systems => [Coosys.new],
:params => [Param.new],
:infos => [Info.new],
:resources => [Resource.new]
}
end
def test_to_html
votable = VOTable.new(
:version => '1.1',
:coordinate_systems => [
Coosys.new(:id => 'J2000', :equinox => 'J2000.', :epoch => 'J2000.', :system => 'eq_FK5')
],
:resources => [
Resource.new(
:name => 'myFavouriteGalaxies',
:tables => [
Table.new(
:name => 'results',
:description => Description.new(:text => 'Velocities and Distance estimations'),
:params => [
Param.new(
:name => 'Telescope',
:datatype => 'float',
:ucd => 'phys.size;instr.tel',
:unit => 'm',
:value => '3.6'
)
],
:fields => [
Field.new(:name => 'RA', :id => 'col1', :ucd => 'pos.eq.ra;meta.main',
:ref => 'J2000', :datatype => 'float', :width => 6, :precision => '2', :unit => 'deg'),
Field.new(:name => 'Dec', :id => 'col2', :ucd => 'pos.eq.dec;meta.main',
:ref => 'J2000', :datatype => 'float', :width => 6, :precision => '2', :unit => 'deg'),
Field.new(:name => 'Name', :id => 'col3', :ucd => 'meta.id;meta.main',
:datatype => 'char', :arraysize => '8*'),
Field.new(:name => 'RVel', :id => 'col4', :ucd => 'src.veloc.hc', :datatype => 'int',
:width => 5, :unit => 'km/s'),
Field.new(:name => 'e_RVel', :id => 'col5', :ucd => 'stat.error;src.veloc.hc',
:datatype => 'int', :width => 3, :unit => 'km/s'),
Field.new(:name => 'R', :id => 'col6', :ucd => 'phys.distance', :datatype => 'float',
:width => 4, :precision => '1', :unit => 'Mpc',
:description => Description.new(:text => 'Distance of Galaxy, assuming H=75km/s/Mpc'))
],
:data => Data.new(
:format => TableData.new(
:trs => [
Tr.new(:tds => [
Td.new(:text => '010.68'), Td.new(:text => '+41.27'), Td.new(:text => 'N 224'),
Td.new(:text => '-297'), Td.new(:text => '5'), Td.new(:text => '0.7')
]),
Tr.new(:tds => [
Td.new(:text => '287.43'), Td.new(:text => '-63.85'), Td.new(:text => 'N 6744'),
Td.new(:text => '839'), Td.new(:text => '6'), Td.new(:text => '10.4')
]),
Tr.new(:tds => [
Td.new(:text => '023.48'), Td.new(:text => '+30.66'), Td.new(:text => 'N 598'),
Td.new(:text => '-182'), Td.new(:text => '3'), Td.new(:text => '0.7')
])
]
)
)
)
]
)
]
)
assert_equal(
File.read('test/voruby/votable/1.1/votable.html').strip.gsub(/\n+\s*/, ''),
votable.to_html.strip.gsub(/\n+\s*/, '')
)
end
end
class DescriptionTest < ::Test::Unit::TestCase
include Methods
def class_to_test; Description end
def inputs; {:text => 'A description'} end
end
class DefinitionsTest < ::Test::Unit::TestCase
include Methods
def class_to_test; Definitions end
def inputs
{
:coordinate_systems => [Coosys.new],
:params => [Param.new]
}
end
end
class CoosysTest < ::Test::Unit::TestCase
include Methods
def class_to_test; Coosys end
def inputs
{:id => 'my_coosys', :equinox => 'J2000', :epoch => 'B1950', :system => 'supergalactic'}
end
end
class ParamTest < ::Test::Unit::TestCase
include Methods
def class_to_test; Param end
def inputs
{
:id => 'my_param_test',
:unit => 'm/s',
:datatype => 'float',
:arraysize => '*',
:precision => '3',
:width => 6,
:ref => 'refer_to_1',
:name => 'My param test',
:ucd => 'phys.velocity',
:utype => 'my_utype',
:value => '123.456',
:description => Description.new,
:values => Values.new,
:links => [Link.new]
}
end
def test_casting
param = Param.new(:datatype => 'float', :arraysize => '*')
param.value = [123.4, 21.5]
assert_equal [123.4, 21.5], param.value(true)
end
end
class InfoTest < ::Test::Unit::TestCase
include Methods
def class_to_test; Info end
def inputs
{:id => 'my_info', :name => 'info_lite', :value => '123'}
end
end
class ResourceTest < ::Test::Unit::TestCase
include Methods
def class_to_test; Resource end
def inputs
{
:name => 'Test Resource',
:id => 'my_test_resource',
:utype => 'a_utype',
:type => 'a_type',
:description => Description.new,
:infos => [Info.new],
:coordinate_systems => [Coosys.new],
:params => [Param.new],
:links => [Link.new],
:tables => [Table.new],
:resources => [Resource.new]
}
end
end
class LinkTest < ::Test::Unit::TestCase
include Methods
def class_to_test; Link end
def inputs
{
:id => 'my_link', :content_role => 'hints', :content_type => 'text/html',
:title => 'link title', :value => 'this is value',
:href => 'http://www.google.com/', :gref => 'a gref',
:action => 'http://www.noao.edu/actions/mine'
}
end
def expected_outputs
self.inputs.merge({
:href => URI.parse('http://www.google.com/'),
:action => URI.parse('http://www.noao.edu/actions/mine')
})
end
end
class TableTest < ::Test::Unit::TestCase
include Methods
def class_to_test; Table end
def inputs
{
:id => 'my_table',
:name => 'My Table',
:ref => 'ref1',
:ucd => 'a_ucd',
:utype => 'a_utype',
:nrows => '5',
:description => Description.new,
:data => Data.new(),
:fields => [Field.new],
:params => [Param.new],
:groups => [Group.new],
:links => [Link.new]
}
end
def expected_outputs
self.inputs.merge({:nrows => 5})
end
def test_to_html
table = Table.new(
:id => 'table1',
:description => Description.new(:text => 'A test table'),
:fields => [
Field.new(:name => 'RA', :id => 'col1', :ucd => 'pos.eq.ra;meta.main',
:ref => 'J2000', :datatype => 'float', :width => 6, :precision => '2', :unit => 'deg'),
Field.new(:name => 'Dec', :id => 'col2', :ucd => 'pos.eq.dec;meta.main',
:ref => 'J2000', :datatype => 'float', :width => 6, :precision => '2', :unit => 'deg'),
Field.new(:name => 'Name', :id => 'col3', :ucd => 'meta.id;meta.main',
:datatype => 'char', :arraysize => '8*')
],
:data => Data.new(
:format => TableData.new(
:trs => [
Tr.new(:tds => [Td.new(:text => '123'), Td.new(:text => '456'), Td.new(:text => 'my_obj1')]),
Tr.new(:tds => [Td.new(:text => '789'), Td.new(:text => '112'), Td.new(:text => 'my_obj2')]),
Tr.new(:tds => [Td.new(:text => '145'), Td.new(:text => '178'), Td.new(:text => 'my_obj3')])
]
)
)
)
assert_equal(
'
' +
'A test table' +
'' +
'' +
'' +
'' +
' RA ' +
' pos.eq.ra;meta.main ' +
' deg ' +
' float' +
' ' +
' | ' +
'' +
'' +
' Dec ' +
' pos.eq.dec;meta.main ' +
' deg ' +
' float' +
' ' +
' | ' +
'' +
'' +
' Name ' +
' meta.id;meta.main ' +
' char' +
' 8*' +
' ' +
' | ' +
'
' +
'' +
'' +
'' +
'123 | ' +
'456 | ' +
'my_obj1 | ' +
'
' +
'' +
'789 | ' +
'112 | ' +
'my_obj2 | ' +
'
' +
'' +
'145 | ' +
'178 | ' +
'my_obj3 | ' +
'
' +
'' +
'
',
table.to_html.strip.gsub(/\n+\s*/, '')
)
end
end
class FieldTest < ::Test::Unit::TestCase
include Methods
def class_to_test; Field end
def inputs
{
:id => 'my_field',
:unit => 'm/s',
:datatype => 'float',
:precision => '3',
:width => '6',
:precision => '3',
:ref => 'my_ref',
:name => 'Velocity',
:ucd => 'phys.velocity',
:utype => 'a_utype',
:arraysize => '*',
:type => 'a_type',
:description => Description.new,
:values => [Values.new],
:links => [Link.new]
}
end
def expected_outputs
self.inputs.merge({:width => 6})
end
def test_to_html
field = Field.new(:id => 'col1', :name => 'Name', :ucd => 'meta.id;meta.main', :datatype => 'char', :arraysize => '*')
assert_equal(
'' +
'
Name
' +
'
meta.id;meta.main
' +
'
char' +
'
*' +
'
',
field.to_html.strip.gsub(/\n+\s*/, '')
)
end
end
class ValuesTest < ::Test::Unit::TestCase
include Methods
def class_to_test; Values end
def inputs
{
:id => 'my_values',
:type => 'a_type',
:null => ' ',
:ref => 'values_ref',
:max => Max.new,
:min => Min.new,
:options => [Option.new]
}
end
end
class MinTest < ::Test::Unit::TestCase
include Methods
def class_to_test; Min end
def inputs
{:value => '10.2', :inclusive => 'no'}
end
def test_inclusive
min = Min.new(self.inputs)
assert_equal false, min.inclusive?
end
def test_alternate_inclusives
min = Min.new(:inclusive => false)
assert_equal 'no', min.inclusive
min.inclusive = true
assert_equal 'yes', min.inclusive
end
end
class MaxTest < ::Test::Unit::TestCase
include Methods
def class_to_test; Max end
def inputs
{:value => '10.2', :inclusive => 'yes'}
end
def test_inclusive
max = Max.new(self.inputs)
assert_equal true, max.inclusive?
end
def test_alternate_inclusives
max = Max.new(:inclusive => false)
assert_equal 'no', max.inclusive
max.inclusive = true
assert_equal 'yes', max.inclusive
end
end
class OptionTest < ::Test::Unit::TestCase
include Methods
def class_to_test; Option end
def inputs
{
:name => 'my_option',
:value => 'blahdeblah',
:options => [Option.new]
}
end
end
class GroupTest < ::Test::Unit::TestCase
include Methods
def class_to_test; Group end
def inputs
{
:id => 'my_group',
:name => 'Group1',
:ref => 'group1',
:ucd => 'a_ucd',
:utype => 'a_utype',
:description => Description.new,
:field_refs => [FieldRef.new],
:param_refs => [ParamRef.new],
:params => [Param.new],
:groups => [Group.new]
}
end
end
class FieldRefTest < ::Test::Unit::TestCase
include Methods
def class_to_test; FieldRef end
def inputs; {:ref => 'my_field_ref'} end
def test_field_retrieval
table = Table.new(
:fields => [Field.new(:id => 'col1', :name => 'my column')],
:groups => [
Group.new(
:field_refs => [FieldRef.new(:ref => 'col1')]
)
]
)
assert_equal Field.new(:id => 'col1', :name => 'my column'), table.groups.first.field_refs.first.field
end
end
class ParamRefTest < ::Test::Unit::TestCase
include Methods
def class_to_test; ParamRef end
def inputs; {:ref => 'my_param_ref'} end
def test_param_retrieval
table = Table.new(
:params => [Param.new(:id => 'param1', :name => 'my param')],
:groups => [
Group.new(
:param_refs => [ParamRef.new(:ref => 'param1')]
)
]
)
assert_equal Param.new(:id => 'param1', :name => 'my param'), table.groups.first.param_refs.first.param
end
end
class DataTest < ::Test::Unit::TestCase
include Methods
def class_to_test; Data end
def inputs
{
:format => TableData.new
}
end
def test_alternative_formats
data = Data.new(:format => Binary.new)
assert_equal Binary.new, data.format
data = Data.new(:format => Fits.new)
assert_equal Fits.new, data.format
end
end
class TableDataTest < ::Test::Unit::TestCase
include Methods
def class_to_test; TableData end
def inputs; {:trs => [Tr.new]} end
end
class BinaryTest < ::Test::Unit::TestCase
include Methods
def class_to_test; Binary end
def inputs; {:stream => Stream.new()} end
end
class StreamTest < ::Test::Unit::TestCase
include Methods
def class_to_test; Stream end
def inputs
{
:type => 'other', :href => 'http://www.noao.edu/', :actuate => 'onRequest',
:encoding => 'gzip', :expires => '2007-12-05T08:45:14', :rights => 'none',
:text => 'Some text'
}
end
def expected_outputs
self.inputs.merge({
:href => URI.parse(self.inputs[:href]),
:expires => DateTime.parse(self.inputs[:expires])
})
end
def test_retrieve
stream = Stream.new(:href => 'http://www.noao.edu/index.shtml')
file = stream.retrieve
#assert_kind_of Tempfile, file
assert_match /National Optical Astronomy Observatory/, file.read
stream = Stream.new(:text => 'my text')
file = stream.retrieve
assert_kind_of StringIO, file
assert_equal 'my text', file.read
end
end
class FitsTest < ::Test::Unit::TestCase
include Methods
def class_to_test; Fits end
def inputs
{
:extnum => '2',
:stream => Stream.new
}
end
def expected_outputs
self.inputs.merge({:extnum => 2})
end
# def test_to_rfits
# fits = Fits.new(:stream => Stream.new(:href => 'http://fits.gsfc.nasa.gov/nrao_data/samples/image/swp05569slg.fits'))
# rfits = fits.to_rfits
#
# (VORuby.rfits?) ? assert_kind_of(RFits::File, rfits) : assert_kind_of(Tempfile, rfits)
# end
end
class TrTest < ::Test::Unit::TestCase
include Methods
def class_to_test; Tr end
def inputs; {:tds => [Td.new]} end
def test_to_html
tr = Tr.new(:tds => [Td.new(:text => 'hello'), Td.new(:text => 'world')])
assert_equal("hello | world |
", tr.to_html.strip.gsub(/\n+\s*/, ''))
end
end
class TdTest < ::Test::Unit::TestCase
include Methods
def class_to_test; Td end
def inputs
{
:encoding => 'base64',
:text => 'my value'
}
end
def test_casting
table = Table.new(
:fields => [
Field.new(:name => 'col1', :datatype => 'boolean'),
Field.new(:name => 'col1_2', :datatype => 'boolean', :arraysize => '*'),
Field.new(:name => 'col2', :datatype => 'bit'),
Field.new(:name => 'col2_2', :datatype => 'bit', :arraysize => '*'),
Field.new(:name => 'col3', :datatype => 'unsignedByte'),
Field.new(:name => 'col3_2', :datatype => 'unsignedByte', :arraysize => '*'),
Field.new(:name => 'col4', :datatype => 'short'),
Field.new(:name => 'col4_2', :datatype => 'short', :arraysize => '*'),
Field.new(:name => 'col5', :datatype => 'int'),
Field.new(:name => 'col5_2', :datatype => 'int', :arraysize => '*'),
Field.new(:name => 'col6', :datatype => 'long'),
Field.new(:name => 'col6_2', :datatype => 'long', :arraysize => '*'),
Field.new(:name => 'col7', :datatype => 'char'),
Field.new(:name => 'col7_2', :datatype => 'char', :arraysize => '*'),
Field.new(:name => 'col8', :datatype => 'unicodeChar'),
Field.new(:name => 'col8_2', :datatype => 'unicodeChar', :arraysize => '*'),
Field.new(:name => 'col9', :datatype => 'float'),
Field.new(:name => 'col9_2', :datatype => 'float', :arraysize => '*'),
Field.new(:name => 'col10', :datatype => 'double'),
Field.new(:name => 'col10_2', :datatype => 'double', :arraysize => '*'),
Field.new(:name => 'col11', :datatype => 'floatComplex'),
Field.new(:name => 'col11_2', :datatype => 'floatComplex', :arraysize => '*'),
Field.new(:name => 'col12', :datatype => 'doubleComplex'),
Field.new(:name => 'col12_2', :datatype => 'doubleComplex', :arraysize => '*')
],
:data => Data.new(
:format => TableData.new(
:trs => [
Tr.new(:tds => [
Td.new(:text => 't'),
Td.new(:text => 't f 1 true false 0'),
Td.new(:text => '1'),
Td.new(:text => '1 1 0 1 1'),
Td.new(:text => 'a'),
Td.new(:text => 'a b c d'),
Td.new(:text => '34'),
Td.new(:text => '34 12 4 1'),
Td.new(:text => '100'),
Td.new(:text => '100 101 102'),
Td.new(:text => '1000'),
Td.new(:text => '1000 1001 1002'),
Td.new(:text => 'a'),
Td.new(:text => 'hello'),
Td.new(:text => 'b'),
Td.new(:text => 'world'),
Td.new(:text => '12.1'),
Td.new(:text => '1.1 2.2 3.3'),
Td.new(:text => '1000'),
Td.new(:text => '100.1, 200.2, 300.3'),
Td.new(:text => '7.2 5.5'),
Td.new(:text => '1.1 10.1 3.2 2.2 9.0 7.1')
])
]
)
)
)
tr = table.data.format.trs.first
# Retrieval...
assert_equal true, tr.tds[0].value
assert_equal [true, false, true, true, false, false], tr.tds[1].value
assert_equal 1, tr.tds[2].value
assert_equal [1, 1, 0, 1, 1], tr.tds[3].value
assert_equal 97, tr.tds[4].value
assert_equal [97, 98, 99, 100], tr.tds[5].value
assert_equal 34, tr.tds[6].value
assert_equal [34, 12, 4, 1], tr.tds[7].value
assert_equal 100, tr.tds[8].value
assert_equal [100, 101, 102], tr.tds[9].value
assert_equal 1000, tr.tds[10].value
assert_equal [1000, 1001, 1002], tr.tds[11].value
assert_equal 'a', tr.tds[12].value
assert_equal 'hello', tr.tds[13].value
assert_equal 'b', tr.tds[14].value
assert_equal 'world', tr.tds[15].value
assert_equal 12.1, tr.tds[16].value
assert_equal [1.1, 2.2, 3.3], tr.tds[17].value
assert_equal 1000, tr.tds[18].value
assert_equal [100.1, 200.2, 300.3], tr.tds[19].value
assert_equal Complex.new(7.2, 5.5), tr.tds[20].value
assert_equal [Complex.new(1.1, 10.1), Complex.new(3.2, 2.2), Complex.new(9.0, 7.1)], tr.tds[21].value
# Setters
tr.tds[0].value = true
assert_equal true, tr.tds[0].value
tr.tds[2].value = 1
assert_equal 1, tr.tds[2].value
tr.tds[4].value = 'a'
assert_equal 97, tr.tds[4].value
tr.tds[6].value = 1000
assert_equal 1000, tr.tds[6].value
tr.tds[8].value = 10000
assert_equal 10000, tr.tds[8].value
tr.tds[10].value = 1000000
assert_equal 1000000, tr.tds[10].value
tr.tds[12].value = 'b'
assert_equal 'b', tr.tds[12].value
tr.tds[14].value = 'B'
assert_equal 'B', tr.tds[14].value
tr.tds[16].value = 1.1
assert_equal 1.1, tr.tds[16].value
tr.tds[18].value = 10001.1
assert_equal 10001.1, tr.tds[18].value
tr.tds[20].value = Complex.new(1.1, 2.2)
assert_equal Complex.new(1.1, 2.2), tr.tds[20].value
# This should be an array...
assert_raise RuntimeError do
tr.tds[1].value = 1
end
# And this should be a scalar...
assert_raise RuntimeError do
tr.tds[0].value = [1, 0, 1, 1]
end
end
def test_to_html
td = Td.new(:text => '1 1 0 1 1')
assert_equal("1 1 0 1 1 | ", td.to_html.strip.gsub(/\n+\s*/, ''))
end
end
class NSVOTableTest < ::Test::Unit::TestCase
def test_ns
votable = VOTable.new(File.open('test/voruby/votable/1.1/votable.ns.xml'))
table = votable.resources.first.tables.first
assert_equal 'ra', table.fields[-3].name
assert_equal 'double', table.fields[-3].datatype
assert_equal 10, table.fields[-3].width
assert_equal -14.0986, table.data.format.trs.first.tds[3].value
assert_equal 1.0456679, table.data.format.trs[1].tds[-2].value
end
end
end
end
end
end