require 'test/unit'
require 'voruby/adql/parser'
include VORuby::ADQL
class ScalarExpressionTest < Test::Unit::TestCase
def setup
@exp = ScalarExpression.new('A string')
end
def test_to_adqls
assert_equal('A string', @exp.to_adqls)
end
end
class ClosedExprTest < Test::Unit::TestCase
def setup
@exp = ClosedExpr.new(Atom.new(12.5))
end
def test_to_adqls
assert_equal('(12.5)', @exp.to_adqls)
end
end
class BinaryOperatorTest < Test::Unit::TestCase
def test_construct
assert_raise(RuntimeError) {
op = BinaryOperator.new('^')
}
assert_nothing_raised {
op = BinaryOperator.new('+')
}
assert_nothing_raised {
op = BinaryOperator.new('//', ['//', '+', '-'])
}
assert_raise(RuntimeError){
op = BinaryOperator.new('\\', ['//', '+', '-'])
}
end
def test_to_adqls
op = BinaryOperator.new('*')
assert_equal('*', op.to_adqls)
end
end
class BinaryExprTest < Test::Unit::TestCase
def test_construct
assert_nothing_raised {
expr = BinaryExpr.new('a', '+', 'b')
}
assert_raise(RuntimeError){
expr = BinaryExpr.new('a', '++', 'b')
}
end
def test_to_adqls
expr = BinaryExpr.new('a', '+', 'b')
assert_equal('a + b', expr.to_adqls)
end
end
class UnaryOperatorTest < Test::Unit::TestCase
def test_construct
assert_raise(RuntimeError) {
op = UnaryOperator.new('++')
}
assert_nothing_raised {
op = UnaryOperator.new('+')
}
assert_nothing_raised {
op = BinaryOperator.new('++', ['++', '--'])
}
assert_raise(RuntimeError){
op = BinaryOperator.new('==', ['++', '--'])
}
end
def test_to_adqls
op = BinaryOperator.new('+')
assert_equal('+', op.to_adqls)
end
end
class UnaryExprTest < Test::Unit::TestCase
def test_construct
assert_nothing_raised {
expr = UnaryExpr.new('-', 'a.ra')
}
assert_raise(RuntimeError){
expr = UnaryExpr.new('++', 'a.ra')
}
end
def test_to_adqls
assert_equal('-a.ra', UnaryExpr.new('-', 'a.ra').to_adqls)
end
end
class ColumnReferenceTest < Test::Unit::TestCase
def setup
@ref = ColumnReference.new('o', 'objid')
end
def test_to_adqls
assert_equal('o.objid', @ref.to_adqls)
end
end
class AtomTest < Test::Unit::TestCase
def setup
@real_literal = 1.0
@int_literal = 2
@string_literal = 'a'
end
def test_construct
assert_nothing_raised {
atom = Atom.new(@real_literal, 'cm')
atom = Atom.new(@int_literal)
atom = Atom.new(@string_literal)
}
end
def test_to_adqls
assert_equal('5cm', Atom.new(5, 'cm').to_adqls)
assert_equal('100.0', Atom.new(100.0).to_adqls)
assert_equal('\'a\'', Atom.new('a').to_adqls)
end
end
class LiteralTypeTest < Test::Unit::TestCase
def setup
real_xml = ''
int_xml = ''
string_xml = ''
@real_doc = REXML::Document.new(real_xml)
@int_doc = REXML::Document.new(int_xml)
@string_doc = REXML::Document.new(string_xml)
end
def test_from_xml
assert_nothing_raised {
literal = LiteralType.from_xml(@real_doc.root)
literal = LiteralType.from_xml(@int_doc.root)
literal = LiteralType.from_xml(@string_doc.root)
}
end
def test_to_adql
assert_equal('10.0', LiteralType.from_xml(@real_doc.root).to_adqls)
assert_equal('10', LiteralType.from_xml(@int_doc.root).to_adqls)
assert_equal('ten', LiteralType.from_xml(@string_doc.root).to_adqls)
end
end
class RealTypeTest < Test::Unit::TestCase
def setup
@float = RealType.new(3.14159)
end
def test_construct
assert_raise(VORuby::VOTables::VOTable::Misc::TypeException){
float = RealType.new('5')
}
end
def test_equal
assert_equal(3.14159, @float.value)
end
def test_to_adqls
assert_equal('3.14159', @float.to_adqls)
end
end
class IntegerTypeTest < Test::Unit::TestCase
def setup
@int = IntegerType.new(7)
end
def test_construct
assert_raise(VORuby::VOTables::VOTable::Misc::TypeException){
int = IntegerType.new(5.0)
}
end
def test_equal
assert_equal(7, @int.value)
end
def test_to_adqls
assert_equal('7', @int.to_adqls)
end
end
class StringTypeTest < Test::Unit::TestCase
def setup
@string = StringType.new('test string')
end
def test_construct
assert_raise(VORuby::VOTables::VOTable::Misc::TypeException){
string = StringType.new(3.1)
}
end
def test_equal
assert_equal('test string', @string.value)
end
def test_to_adqls
assert_equal('test string', @string.to_adqls)
end
end
class SelectionOptionTest < Test::Unit::TestCase
def test_construct
assert_nothing_raised {
sel_op = SelectionOption.new('ALL')
}
assert_raise(RuntimeError){
sel_op = SelectionOption.new('NEW')
}
end
def test_to_adqls
assert_equal('DISTINCT', SelectionOption.new('DISTINCT').to_adqls)
end
end
class AllOrDistinctTest < Test::Unit::TestCase
def test_construct
assert_nothing_raised {
all_dis = AllOrDistinct.new('ALL')
all_dis = AllOrDistinct.new('NEW', ['ALL', 'DISTINCT', 'NEW'])
}
assert_raise(RuntimeError){
all_dis = AllOrDistinct.new('NEW')
}
end
def test_to_adqls
assert_equal('DISTINCT', AllOrDistinct.new('DISTINCT').to_adqls)
end
end
class TrigonometricFunctionNameTest < Test::Unit::TestCase
def test_construct
assert_nothing_raised {
trig = TrigonometricFunctionName.new('COT')
trig = TrigonometricFunctionName.new('ISIN', ['SIN', 'ASIN', 'ISIN'])
}
assert_raise(RuntimeError){
trig = TrigonometricFunctionName.new('ISIN')
}
end
def test_to_adqls
assert_equal('COT', TrigonometricFunctionName.new('COT').to_adqls)
end
end
class TrigonometricFunctionTest < Test::Unit::TestCase
def setup
@arg = ColumnReference.new('o', 'objid')
end
def test_construct
assert_nothing_raised {
trig_func = TrigonometricFunction.new('ASIN', @arg)
}
assert_raise(RuntimeError){
trig_func = TrigonometricFunction.new('NEW', @arg)
}
end
def test_to_adqls
assert_equal('ASIN(o.objid)', TrigonometricFunction.new('ASIN', @arg).to_adqls)
end
end
class MathFunctionNameTest < Test::Unit::TestCase
def test_construct
assert_nothing_raised {
math = MathFunctionName.new('ABS')
math = MathFunctionName.new('GRAD', ['ABS', 'CEILING', 'GRAD'])
}
assert_raise(RuntimeError){
math = MathFunctionName.new('GRAD')
}
end
def test_to_adqls
assert_equal('ABS', MathFunctionName.new('ABS').to_adqls)
end
end
#class MathFunctionTest < Test::Unit::TestCase
#def test_construct
# assert_nothing_raised {
# math_func = MathFunction.new('ABS')
# }
# assert_raise (RuntimeError){
# math_func = TrigonometricFunction.new('NEW')
# }
#end
#def test_to_adqls
# assert_equal('ABS', MathFunction.new('ABS').to_adqls)
#end
#end
class AggregateFunctionNameTest < Test::Unit::TestCase
def test_construct
assert_nothing_raised {
agg = AggregateFunctionName.new('MAX')
agg = AggregateFunctionName.new('DISP', ['AVG', 'DISP', 'MIN'])
}
assert_raise(RuntimeError){
agg = AggregateFunctionName.new('DISP')
}
end
def test_to_adqls
assert_equal('COUNT', AggregateFunctionName.new('COUNT').to_adqls)
end
end
# class AggregateFunctionTest < Test::Unit::TestCase
# def test_construct
# assert_nothing_raised {
# agg_func = AggregateFunction.new('MAX')
# }
# assert_raise (RuntimeError){
# agg_func = AggregateFunction.new('NEW')
# }
# end
# def test_to_adqls
# assert_equal('SUM', AggregateFunction.new('SUM').to_adqls)
# end
# end
class AliasSelectionItemTest < Test::Unit::TestCase
def setup
@alias_sel1 = AliasSelectionItem.new('o.objid', 'ObjId')
@alias_sel2 = AliasSelectionItem.new('o.objid')
end
def test_to_adqls
assert_equal('o.objid AS ObjId', @alias_sel1.to_adqls)
assert_equal('o.objid', @alias_sel2.to_adqls)
end
end
class ComparisonTest < Test::Unit::TestCase
def test_construct
assert_nothing_raised {
comp = Comparison.new('<>')
comp = Comparison.new('()', ['<>', '=', '()'])
}
assert_raise(RuntimeError){
comp = Comparison.new('!')
}
end
def test_to_adqls
assert_equal('<>', Comparison.new('<>').to_adqls)
end
end
class ArchiveTableTest < Test::Unit::TestCase
def setup
@archive1 = ArchiveTable.new('SDSS', 'PhotoPrimary', 'o')
@archive2 = ArchiveTable.new('SDSS', 'PhotoPrimary')
end
def test_to_adqls
assert_equal('SDSS:PhotoPrimary o', @archive1.to_adqls)
assert_equal('SDSS:PhotoPrimary', @archive2.to_adqls)
end
end
class IncludeTableTest < Test::Unit::TestCase
def setup
@table = IncludeTable.new('o')
end
def test_to_adqls
assert_equal('o', @table.to_adqls)
end
end
class DropTableTest < Test::Unit::TestCase
def setup
@table = DropTable.new('o')
end
def test_to_adqls
assert_equal('!o', @table.to_adqls)
end
end
class IntersectionSearchTest < Test::Unit::TestCase
def setup
@tables = [IncludeTable.new('o'),
IncludeTable.new('t'),
DropTable.new('z')]
@cond1 = XMatch.new(@tables, '>=', 0.5)
@cond2 = XMatch.new(@tables, '<=', 1.1)
end
def test_construct
assert_nothing_raised {
search = IntersectionSearch.new(@cond1, @cond2)
}
end
def test_to_adqls
search = IntersectionSearch.new(@cond1, @cond2)
assert_equal("XMATCH(o, t, !z) >= 0.5 AND XMATCH(o, t, !z) <= 1.1",
search.to_adqls)
end
end
class UnionSearchTest < Test::Unit::TestCase
def setup
@tables = [IncludeTable.new('o'),
IncludeTable.new('t'),
DropTable.new('z')]
@cond1 = XMatch.new(@tables, '>=', 0.5)
@cond2 = XMatch.new(@tables, '<=', 1.1)
end
def test_construct
assert_nothing_raised {
search = UnionSearch.new(@cond1, @cond2)
}
end
def test_to_adqls
search = UnionSearch.new(@cond1, @cond2)
assert_equal("XMATCH(o, t, !z) >= 0.5 OR XMATCH(o, t, !z) <= 1.1",
search.to_adqls)
end
end
class XMatchTest < Test::Unit::TestCase
def setup
@tables = [IncludeTable.new('o'),
IncludeTable.new('t'),
DropTable.new('z')]
end
def test_construct
assert_nothing_raised {
xmatch = XMatch.new(@tables, '<=', 3.5)
xmatch = XMatch.new(@tables,
Comparison.new('<='),
RealType.new(3.5))
}
end
def test_to_adqls
xmatch = XMatch.new(@tables, '<=', 3.5)
assert_equal("XMATCH(o, t, !z) <= 3.5", xmatch.to_adqls)
end
end
class LikePredTest < Test::Unit::TestCase
def test_construct
assert_nothing_raised {
like = LikePred.new('name', '%my_name%')
}
end
def test_to_adqls
assert_equal('name LIKE \'%my_name%\'',
LikePred.new('name', '%my_name%').to_adqls)
end
end
class NotLikePredTest < Test::Unit::TestCase
def test_construct
assert_nothing_raised {
like = NotLikePred.new('name', '%my_name%')
}
end
def test_to_adqls
assert_equal('name NOT LIKE \'%my_name%\'',
NotLikePred.new('name', '%my_name%').to_adqls)
end
end
#class ExclusiveSearchTest < Test::Unit::TestCase; end
#class SubQuerySetTest < Test::Unit::TestCase; end
class ClosedSearchTest < Test::Unit::TestCase
def setup
@tables = [IncludeTable.new('o'),
IncludeTable.new('t'),
DropTable.new('z')]
@search = XMatch.new(@tables, '<=', 3.5)
end
def test_construct
assert_nothing_raised {
cs = ClosedSearch.new(@search)
}
end
def test_to_adqls
assert_equal('(XMATCH(o, t, !z) <= 3.5)',
ClosedSearch.new(@search).to_adqls)
end
end
class ComparisonPredTest < Test::Unit::TestCase
def setup
@cond1 = ColumnReference.new('PhotoPrimary', 'objid')
@cond2 = BinaryExpr.new('a', '+', 'b')
end
def test_construct
assert_nothing_raised {
cp = ComparisonPred.new(@cond1, '<>', @cond2)
}
end
def test_to_adqls
assert_equal('PhotoPrimary.objid <> a + b',
ComparisonPred.new(@cond1, '<>', @cond2).to_adqls)
end
end
class NotBetweenPredTest < Test::Unit::TestCase
def setup
@arg1 = ColumnReference.new('PhotoPrimary', 'objid')
@arg2 = UnaryExpr.new('-', 2)
@arg3 = UnaryExpr.new('+', 3)
end
def test_construct
assert_nothing_raised {
bp = NotBetweenPred.new(@arg1, @arg2, @arg3)
}
end
def test_to_adqls
assert_equal('PhotoPrimary.objid NOT BETWEEN -2 AND +3',
NotBetweenPred.new(@arg1, @arg2, @arg3).to_adqls)
end
end
class RegionSearchTest < Test::Unit::TestCase
def test_construct
assert_nothing_raised {
rs = RegionSearch.new(Circle.new(182.5, -0.89, 8), 'overlaps')
}
end
def test_to_adqls
assert_equal('Region(\'CIRCLE J2000 182.5 -0.89 8.0\')',
RegionSearch.new(Circle.new(182.5, -0.89, 8), 'overlaps').to_adqls)
end
end
class HavingTest < Test::Unit::TestCase
def setup
@cond = RegionSearch.new(Circle.new(182.5, -0.89, 8.0), 'overlaps')
end
def test_construct
assert_nothing_raised {
h = Having.new(@cond)
}
end
def test_to_adqls
assert_equal('HAVING Region(\'CIRCLE J2000 182.5 -0.89 8.0\')',
Having.new(@cond).to_adqls)
end
def test_from_xml
xml = '' +
'' +
'' +
'' +
'' +
'' +
'' +
''
doc = REXML::Document.new(xml)
assert_equal("HAVING opt.org LIKE '%esa%'",
Having.from_xml(doc.root).to_adqls)
end
end
class GroupByTest < Test::Unit::TestCase
def setup
@columns = [ColumnReference.new('o', 'objid'),
ColumnReference.new('o', 'type'),
ColumnReference.new('t', 'objid')]
end
def test_construct
assert_nothing_raised {
gb = GroupBy.new(@columns)
}
end
def test_to_adqls
assert_equal('GROUP BY o.objid, o.type, t.objid',
GroupBy.new(@columns).to_adqls)
end
def test_from_xml
xml = '' +
'' +
'' +
'' +
''
doc = REXML::Document.new(xml)
assert_equal('GROUP BY o.recid, o.ra, o.dec',
GroupBy.from_xml(doc.root).to_adqls)
end
end
class WhereTest < Test::Unit::TestCase
def setup
@tables = [IncludeTable.new('o'), IncludeTable.new('t')]
@xmatch = XMatch.new(@tables, '<', 3.5)
@region = RegionSearch.new(Circle.new(182.5, -0.89, 8.5), 'overlaps')
@comp = ComparisonPred.new(
ColumnReference.new('o', 'type'),
'=',
ScalarExpression.new(3))
@cond1 = IntersectionSearch.new(@xmatch, @region)
@condition = UnionSearch.new(@cond1, @comp)
end
def test_construct
assert_nothing_raised {
w = Where.new(@condition)
}
end
def test_to_adql
assert_equal('WHERE XMATCH(o, t) < 3.5 AND Region(\'CIRCLE J2000 182.5 -0.89 8.5\') OR o.type = 3',
Where.new(@condition).to_adqls)
end
def test_from_xml
xml = '' +
'' +
'' +
'' +
'' +
'' +
'' +
''
doc = REXML::Document.new(xml)
assert_equal("WHERE opt.org LIKE '%esa%'",
Where.from_xml(doc.root).to_adqls)
end
end
class FromTest < Test::Unit::TestCase
def setup
@tables = [ArchiveTable.new('SDSS', 'PhotoPrimary', 'o'),
ArchiveTable.new('2MASS', 'PhotoPrimary', 't')]
end
def test_construct
assert_nothing_raised {
from = From.new(@tables)
}
end
def test_to_adql
assert_equal("FROM SDSS:PhotoPrimary o, 2MASS:PhotoPrimary t",
From.new(@tables).to_adqls)
end
def test_from_xml
xml = '' +
'' +
'' +
''
doc = REXML::Document.new(xml)
assert_equal('FROM SDSS:PhotoPrimary o, TWOMASS:PhotoPrimary a',
From.from_xml(doc.root).to_adqls)
end
end
class SelectionListTest < Test::Unit::TestCase
def setup
@items = [ColumnReference.new('o', 'objid'),
ColumnReference.new('o', 'ra'),
ColumnReference.new('o', 'dec')]
end
def test_construct
assert_nothing_raised {
list = SelectionList.new(@items)
}
end
def test_to_adql
assert_equal("o.objid, o.ra, o.dec",
SelectionList.new(@items).to_adqls)
end
def test_from_xml
xml = '' +
' ' +
' ' +
' ' +
''
doc = REXML::Document.new(xml)
assert_equal('o.recid, o.ra, o.dec',
SelectionList.from_xml(doc.root).to_adqls)
end
end
class SelectionLimitTest < Test::Unit::TestCase
def test_construct
assert_nothing_raised {
limit = SelectionLimit.new(10)
}
assert_raise(RuntimeError){
limit = SelectionLimit.new(-1)
limit = SelectionLimit.new(10.2)
}
end
def test_to_adqls
assert_equal("TOP 10", SelectionLimit.new(10).to_adqls)
end
def test_from_xml
xml = ''
doc = REXML::Document.new(xml)
assert_equal('TOP 10', SelectionLimit.from_xml(doc.root).to_adqls)
end
end
class IntoTest < Test::Unit::TestCase
def test_construct
assert_nothing_raised {
into = Into.new('my_table')
}
end
def test_to_adqls
assert_equal('INTO my_table', Into.new('my_table').to_adqls)
end
def test_from_xml
xml = 'my_table'
doc = REXML::Document.new(xml)
assert_equal('INTO my_table',
Into.from_xml(doc.root).to_adqls)
end
end
class OrderDirectionTest < Test::Unit::TestCase
def test_construct
assert_nothing_raised {
dir = OrderDirection.new('ASC')
dir = OrderDirection.new('NEW', ['ASC', 'DESC', 'NEW'])
}
assert_raise(RuntimeError){
dir = OrderDirection.new('NEW')
}
end
def test_to_adqls
assert_equal('ASC', OrderDirection.new('ASC').to_adqls)
end
end
class OrderOptionTest < Test::Unit::TestCase
def test_construct
assert_nothing_raised {
order = OrderOption.new('ASC')
}
end
def test_to_adqls
assert_equal('ASC', OrderOption.new('ASC').to_adqls)
end
end
class OrderTest < Test::Unit::TestCase
def setup
@expression = ColumnReference.new('o', 'objid')
end
def test_construct
assert_nothing_raised {
order = Order.new(@expression)
}
end
def test_to_adqls
assert_equal('o.objid DESC', Order.new(@expression, 'DESC').to_adqls)
end
end
class OrderExpressionTest < Test::Unit::TestCase
def setup
@orders = [Order.new(ColumnReference.new('o', 'objid')),
Order.new(ColumnReference.new('o', 'ra'), 'DESC'),
Order.new(ColumnReference.new('o', 'dec'), 'ASC')]
end
def test_construct
assert_nothing_raised {
ord_expr = OrderExpression.new(@orders)
}
end
def test_to_adqls
assert_equal('ORDER BY o.objid, o.ra DESC, o.dec ASC',
OrderExpression.new(@orders).to_adqls)
end
end
class InclusiveSearchTest < Test::Unit::TestCase
def setup
@expression = ColumnReference.new('o', 'objid')
@set = ConstantListSet.new(['IBM', 'Microsoft', 'Apple'])
end
def test_construct
assert_nothing_raised {
isearch = InclusiveSearch.new(@expression, @set)
}
end
def test_to_adqls
assert_equal("o.objid IN (\"IBM\", \"Microsoft\", \"Apple\")",
InclusiveSearch.new(@expression, @set).to_adqls)
end
end
class SelectTest < Test::Unit::TestCase
def setup
@allow = SelectionOption.new('DISTINCT')
@restrict = SelectionLimit.new(10)
@columns = [ColumnReference.new('o', 'objid'),
ColumnReference.new('o', 'ra'),
ColumnReference.new('o', 'dec'),
ColumnReference.new('o', 'type'),
ColumnReference.new('t', 'objid'),
ColumnReference.new('t', 'ra'),
ColumnReference.new('t', 'dec'),
ColumnReference.new('t', 'j_m'),
ColumnReference.new('o', 'i')]
@sel_list = SelectionList.new(@columns)
@into = Into.new('myTable')
@from_list = [ArchiveTable.new('SDSS', 'PhotoPrimary', 'o'),
ArchiveTable.new('TWOMASS', 'PhotoPrimary', 't')]
@from = From.new(@from_list)
@xmatch_tables = [IncludeTable.new('o'),
IncludeTable.new('t')]
@xmatch = XMatch.new(@xmatch_tables, '<', 3.5)
@region = RegionSearch.new(Circle.new(182.5, -0.89, 8), 'overlaps')
@xmatchregion = IntersectionSearch.new(@xmatch, @region)
@otype = ComparisonPred.new(
ColumnReference.new('o', 'type'),
'=',
Atom.new(3))
@xmatchregion_otype = IntersectionSearch.new(@xmatchregion, @otype)
@oi = ComparisonPred.new(
ColumnReference.new('o', 'i'),
'<',
Atom.new(21))
@xmatchregion_otype_oi =
IntersectionSearch.new(@xmatchregion_otype, @oi)
@tjm = ComparisonPred.new(
ColumnReference.new('t', 'j_m'),
'<',
Atom.new(18))
@xmatchregion_otype_oi_tjm =
IntersectionSearch.new(@xmatchregion_otype_oi, @tjm)
@oitjmdiff = ComparisonPred.new(
ClosedExpr.new(
BinaryExpr.new(
ColumnReference.new('o', 'i'),
'-',
ColumnReference.new('t', 'j_m')
)
),
'>',
Atom.new(2))
@conditions = IntersectionSearch.new(
@xmatchregion_otype_oi_tjm,
@oitjmdiff)
@where = Where.new(@conditions)
@group_by = GroupBy.new(
[ColumnReference.new('o', 'ra'),
ColumnReference.new('o', 'dec')])
@having = Having.new(
ComparisonPred.new(
ColumnReference.new('o', 'objid'),
'=',
Atom.new('1234')
)
)
@order_by = OrderExpression.new(
[Order.new(ColumnReference.new('o', 'objid')),
Order.new(ColumnReference.new('o', 'ra'), 'DESC')]
)
end
def test_construct
assert_nothing_raised {
select = Select.new(@sel_list)
select = Select.new(@sel_list, @allow)
select = Select.new(@sel_list, @allow, @restrict)
select = Select.new(@sel_list, @allow, @restrict, @into)
select = Select.new(@sel_list, @allow, @restrict, @into, @from)
select = Select.new(@sel_list, nil, nil, nil,
@from, @where)
select = Select.new(@sel_list, nil, nil, nil,
@from, @where, @group_by)
select = Select.new(@sel_list, nil, nil, nil,
@from, @where, @group_by, @having, @order_by)
}
end
def test_to_adqls
adqls = 'SELECT o.objid, o.ra, o.dec, o.type, t.objid, t.ra, t.dec, t.j_m, o.i'
assert_equal(adqls, Select.new(@sel_list).to_adqls)
adqls =
'SELECT DISTINCT ' +
'o.objid, o.ra, o.dec, o.type, t.objid, t.ra, t.dec, t.j_m, o.i'
assert_equal(adqls, Select.new(@sel_list, @allow).to_adqls)
adqls = 'SELECT DISTINCT TOP 10 o.objid, o.ra, o.dec, o.type, t.objid, t.ra, t.dec, t.j_m, o.i'
assert_equal(adqls, Select.new(@sel_list, @allow, @restrict).to_adqls)
adqls =
'SELECT DISTINCT TOP 10 ' +
'o.objid, o.ra, o.dec, o.type, t.objid, t.ra, t.dec, t.j_m, o.i ' +
'INTO myTable'
assert_equal(adqls, Select.new(@sel_list, @allow, @restrict, @into).to_adqls)
adqls =
'SELECT DISTINCT TOP 10 ' +
'o.objid, o.ra, o.dec, o.type, t.objid, t.ra, t.dec, t.j_m, o.i ' +
'INTO myTable ' +
'FROM SDSS:PhotoPrimary o, TWOMASS:PhotoPrimary t'
assert_equal(adqls, Select.new(@sel_list, @allow, @restrict, @into, @from).to_adqls)
adqls =
'SELECT ' +
'o.objid, o.ra, o.dec, o.type, t.objid, t.ra, t.dec, t.j_m, o.i ' +
'FROM SDSS:PhotoPrimary o, TWOMASS:PhotoPrimary t ' +
'WHERE ' +
'XMATCH(o, t) < 3.5 AND ' +
'Region(\'CIRCLE J2000 182.5 -0.89 8.0\') AND ' +
'o.type = 3 AND ' +
'o.i < 21 AND ' +
't.j_m < 18 AND ' +
'(o.i - t.j_m) > 2 ' +
'GROUP BY o.ra, o.dec ' +
'HAVING o.objid = \'1234\' ' +
'ORDER BY o.objid, o.ra DESC'
assert_equal(adqls,
Select.new(@sel_list, nil, nil, nil, @from,
@where, @group_by, @having, @order_by).to_adqls)
end
end
class UserDefinedFunctionTest < Test::Unit::TestCase
def setup
@params = [ColumnReference.new('o', 'ra'), ColumnReference.new('o', 'dec')]
end
def test_construct
assert_nothing_raised {
udf = UserDefinedFunction.new('my_func', @params)
}
end
def test_to_adqls
assert_equal('my_func(o.ra, o.dec)',
UserDefinedFunction.new('my_func', @params).to_adqls)
end
end
class JointTableQualifierTest < Test::Unit::TestCase
def test_construct
assert_nothing_raised {
qual = JointTableQualifier.new('INNER')
qual = JointTableQualifier.new('FAKE', ['INNER', 'FAKE', 'CROSS'])
}
assert_raise(RuntimeError){
qual = JointTableQualifier.new('FAKE')
}
end
def test_to_adqls
assert_equal('INNER', JointTableQualifier.new('INNER').to_adqls)
end
end
class JoinTableTest < Test::Unit::TestCase
def setup
@tables = ArrayOfFromTable.new(
[ArchiveTable.new('SDSS', 'PhotoPrimary', 'o'),
ArchiveTable.new('TWOMASS', 'PhotoPrimary', 't')]
)
@comp = ComparisonPred.new(
ColumnReference.new('o', 'objid'),
'=',
ColumnReference.new('t', 'objid')
)
end
def test_construct
assert_nothing_raised {
jt = JoinTable.new('INNER', @tables, @comp)
}
end
def test_to_adqls
assert_equal('INNER SDSS:PhotoPrimary o, TWOMASS:PhotoPrimary t ON o.objid = t.objid',
JoinTable.new('INNER', @tables, @comp).to_adqls)
end
end
class ArrayOfFromTableTest < Test::Unit::TestCase
def setup
@froms =
[ArchiveTable.new('SDSS', 'PhotoPrimary', 'o'),
ArchiveTable.new('TWOMASS', 'PhotoPrimary', 't')]
end
def test_construct
assert_nothing_raised {
aoft = ArrayOfFromTable.new(@froms)
}
end
def test_to_adqls
assert_equal('SDSS:PhotoPrimary o, TWOMASS:PhotoPrimary t',
ArrayOfFromTable.new(@froms).to_adqls)
end
end
class ArgTest < Test::Unit::TestCase
def setup
xml1 = ''
xml2 = '' +
'' +
''
xml3 = '' +
'' +
'' +
''
xml4 = '' +
'' +
''
xml5 = '' +
'' +
''
xml6 = '' +
'' +
'' +
''
xml7 = '' +
'' +
'' +
''
xml8 = '' +
'' +
''
xml9 = '' +
'MyFunc' +
'' +
''
xml10 = '' +
'' +
''
@doc1 = REXML::Document.new(xml1)
@doc2 = REXML::Document.new(xml2)
@doc3 = REXML::Document.new(xml3)
@doc4 = REXML::Document.new(xml4)
@doc5 = REXML::Document.new(xml5)
@doc6 = REXML::Document.new(xml6)
@doc7 = REXML::Document.new(xml7)
@doc8 = REXML::Document.new(xml8)
@doc9 = REXML::Document.new(xml9)
@doc10 = REXML::Document.new(xml10)
end
def test_from_xml
assert_nothing_raised {
arg = Arg.from_xml(@doc1.root)
arg = Arg.from_xml(@doc2.root)
arg = Arg.from_xml(@doc3.root)
arg = Arg.from_xml(@doc4.root)
arg = Arg.from_xml(@doc5.root)
arg = Arg.from_xml(@doc6.root)
arg = Arg.from_xml(@doc7.root)
arg = Arg.from_xml(@doc8.root)
arg = Arg.from_xml(@doc9.root)
arg = Arg.from_xml(@doc10.root)
}
end
def test_to_adql
assert_equal('o.objid', Arg.from_xml(@doc1.root).to_adqls)
assert_equal("'CDS'", Arg.from_xml(@doc2.root).to_adqls)
assert_equal('opt.R - opt.B', Arg.from_xml(@doc3.root).to_adqls)
assert_equal('-opt.R', Arg.from_xml(@doc4.root).to_adqls)
assert_equal('(opt.R)', Arg.from_xml(@doc5.root).to_adqls)
assert_equal('SIN(DISTINCT opt.R)', Arg.from_xml(@doc6.root).to_adqls)
assert_equal('ABS(ALL opt.R)', Arg.from_xml(@doc7.root).to_adqls)
assert_equal('AVG(opt.R)', Arg.from_xml(@doc8.root).to_adqls)
assert_equal('MyFunc(opt.R)', Arg.from_xml(@doc9.root).to_adqls)
assert_equal('opt.R AS myCol', Arg.from_xml(@doc10.root).to_adqls)
end
end
class LiteralTest < Test::Unit::TestCase
def setup
@doc1 = REXML::Document.new('')
@doc2 = REXML::Document.new('')
@doc3 = REXML::Document.new('')
end
def test_from_xml
assert_nothing_raised {
lit = LiteralType.from_xml(@doc1.root)
lit = LiteralType.from_xml(@doc2.root)
lit = LiteralType.from_xml(@doc3.root)
}
end
def test_to_adql
assert_equal("10.0", LiteralType.from_xml(@doc1.root).to_adqls)
assert_equal("10", LiteralType.from_xml(@doc2.root).to_adqls)
assert_equal('ten', LiteralType.from_xml(@doc3.root).to_adqls)
end
end
class UnitTest < Test::Unit::TestCase
def setup
@doc = REXML::Document.new('cm')
end
def text_from_xml
assert_nothing_raised {
unit = Unit.from_xml(@doc.root)
}
end
def test_to_adqls
assert_equal('cm', Unit.from_xml(@doc.root).to_adqls)
end
end
class AllowTest < Test::Unit::TestCase
def setup
@doc = REXML::Document.new('')
end
def test_from_xml
assert_nothing_raised {
allow = Allow.from_xml(@doc.root)
}
end
def test_to_adqls
assert_equal('DISTINCT', Allow.from_xml(@doc.root).to_adqls)
end
end
class ExpressionTest < Test::Unit::TestCase
def setup
@doc = REXML::Document.new('')
end
def test_from_xml
assert_nothing_raised {
expr = Expression.from_xml(@doc.root)
}
end
def test_to_adqls
assert_equal('opt.R', Expression.from_xml(@doc.root).to_adqls)
end
end
class ConditionTest < Test::Unit::TestCase
def setup
@xml1 = '' +
'' +
'' +
'' +
'' +
''
@doc1 = REXML::Document.new(@xml1)
@xml2 = '' +
'' +
'' +
'' +
'' +
''
@doc2 = REXML::Document.new(@xml2)
@xml3 = '' +
@xml1 +
''
@doc3 = REXML::Document.new(@xml3)
@xml4 = '' +
'' +
'' +
'' +
'' +
''
@doc4 = REXML::Document.new(@xml4)
@xml5 = '' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
''
@doc5 = REXML::Document.new(@xml5)
@xml6 = '' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
''
@doc6 = REXML::Document.new(@xml6)
@xml7 = '' +
@xml4 +
@xml5 +
''
@doc7 = REXML::Document.new(@xml7)
@xml8 = '' +
@xml4 +
@xml5 +
''
@doc8 = REXML::Document.new(@xml8)
@xml9 = '' +
'' +
'' +
'' +
'' +
'' +
''
@doc9 = REXML::Document.new(@xml9)
@xml10 = '' +
'' +
'181.3 -0.76' +
'6.5' +
'' +
''
@doc10 = REXML::Document.new(@xml10)
@xml11 = '' +
@xml4 +
''
@doc11 = REXML::Document.new(@xml11)
@xml12 = '' +
'' +
'' +
'' +
'' +
''
@doc12 = REXML::Document.new(@xml12)
@xml13 = '' +
'' +
'' +
'' +
'' +
''
@doc13 = REXML::Document.new(@xml13)
end
def test_from_xml
assert_nothing_raised {
cond = Condition.from_xml(@doc1.root)
cond = Condition.from_xml(@doc2.root)
cond = Condition.from_xml(@doc3.root)
cond = Condition.from_xml(@doc4.root)
cond = Condition.from_xml(@doc5.root)
cond = Condition.from_xml(@doc6.root)
cond = Condition.from_xml(@doc7.root)
cond = Condition.from_xml(@doc8.root)
cond = Condition.from_xml(@doc9.root)
cond = Condition.from_xml(@doc10.root)
cond = Condition.from_xml(@doc11.root)
cond = Condition.from_xml(@doc12.root)
cond = Condition.from_xml(@doc13.root)
}
end
def test_to_adqls
assert_equal("opt.org LIKE '%esa%'",
Condition.from_xml(@doc1.root).to_adqls)
assert_equal("opt.org NOT LIKE '%esa%'",
Condition.from_xml(@doc2.root).to_adqls)
assert_equal("(opt.org LIKE '%esa%')",
Condition.from_xml(@doc3.root).to_adqls)
assert_equal("opt.publisher <> 'CDS'",
Condition.from_xml(@doc4.root).to_adqls)
assert_equal("opt.R BETWEEN 1.2 AND 1.6",
Condition.from_xml(@doc5.root).to_adqls)
assert_equal("opt.R NOT BETWEEN 1.2 AND 1.6",
Condition.from_xml(@doc6.root).to_adqls)
assert_equal("opt.publisher <> 'CDS' AND opt.R BETWEEN 1.2 AND 1.6",
Condition.from_xml(@doc7.root).to_adqls)
assert_equal("opt.publisher <> 'CDS' OR opt.R BETWEEN 1.2 AND 1.6",
Condition.from_xml(@doc8.root).to_adqls)
assert_equal('XMATCH(opt, apt, !drop_me) < 0.5',
Condition.from_xml(@doc9.root).to_adqls)
assert_equal("Region('CIRCLE J2000 181.3 -0.76 6.5')",
Condition.from_xml(@doc10.root).to_adqls)
assert_equal("NOT opt.publisher <> 'CDS'",
Condition.from_xml(@doc11.root).to_adqls)
assert_equal('opt.R IN (SELECT opt.R, opt.V, opt.ra, opt.dec)',
Condition.from_xml(@doc12.root).to_adqls)
assert_equal('opt.R NOT IN (SELECT opt.R, opt.V, opt.ra, opt.dec)',
Condition.from_xml(@doc13.root).to_adqls)
end
end
class TableTest < Test::Unit::TestCase
def setup
@xml1 = '
'
@doc1 = REXML::Document.new(@xml1)
@xml2 = ''
@doc2 = REXML::Document.new(@xml2)
@xml3 = ''
@doc3 = REXML::Document.new(@xml3)
@xml4 = '' +
'INNER' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'
'
@doc4 = REXML::Document.new(@xml4)
@table1 = Table.new('PhotoPrimary', 'o')
@table2 = Table.new('PhotoPrimary')
end
def test_from_xml
assert_nothing_raised {
tbl = Table.from_xml(@doc1.root)
tbl = Table.from_xml(@doc2.root)
tbl = Table.from_xml(@doc3.root)
tbl = Table.from_xml(@doc4.root)
}
end
def test_to_adqls
assert_equal('my_table', Table.from_xml(@doc1.root).to_adqls)
assert_equal('!my_table', Table.from_xml(@doc2.root).to_adqls)
assert_equal('SDSS:PhotoPrimary o', Table.from_xml(@doc3.root).to_adqls)
assert_equal("INNER SDSS:PhotoPrimary o, TWOMASS:PhotoPrimary t ON o.publisher = 'CDS'",
Table.from_xml(@doc4.root).to_adqls)
assert_equal('PhotoPrimary o', @table1.to_adqls)
assert_equal('PhotoPrimary', @table2.to_adqls)
end
end
class ConstantListSetTest < Test::Unit::TestCase
def setup
@xml = '' +
' ' +
' ' +
' ' +
''
@doc = REXML::Document.new(@xml)
end
def test_from_xml
assert_nothing_raised {
set = ConstantListSet.from_xml(@doc.root)
}
end
def test_to_adqls
assert_equal('10.1, "hello", 5',
ConstantListSet.from_xml(@doc.root).to_adqls)
end
end
class ColumnTest < Test::Unit::TestCase
def setup
@xml = ''
@doc = REXML::Document.new(@xml)
end
def test_from_xml
assert_nothing_raised {
col = Column.from_xml(@doc.root)
}
end
def test_to_adqls
assert_equal('o.publisher', Column.from_xml(@doc.root).to_adqls)
xml = ''
doc = REXML::Document.new(xml)
assert_equal('o.ra', Column.from_xml(doc.root).to_adqls)
end
end
class ItemTest < Test::Unit::TestCase
def setup
@xml1 = '- ' +
'' +
'' +
'
'
@doc1 = REXML::Document.new(@xml1)
end
def test_from_xml
assert_nothing_raised {
item = Item.from_xml(@doc1.root)
}
end
def test_from_adqls
assert_equal('o.ra ASC', Item.from_xml(@doc1.root).to_adqls)
end
end
class OrderByTest < Test::Unit::TestCase
def setup
xml = '' +
'- ' +
'' +
'' +
'
' +
'- ' +
'' +
'' +
'
' +
''
@doc = REXML::Document.new(xml)
end
def test_from_xml
assert_equal('ORDER BY o.ra ASC, o.dec DESC',
OrderBy.from_xml(@doc.root).to_adqls)
end
end
class ParamsTest < Test::Unit::TestCase
def setup
xml = '' +
'' +
'' +
''
@doc = REXML::Document.new(xml)
end
def test_from_xml
assert_equal('o.R - o.B',
Params.from_xml(@doc.root).to_adqls)
end
end
class QualifierTest < Test::Unit::TestCase
def setup
xml = 'INNER'
@doc = REXML::Document.new(xml)
end
def test_from_xml
assert_equal('INNER', Qualifier.from_xml(@doc.root).to_adqls)
end
end
class TablesTest < Test::Unit::TestCase
def setup
xml = '' +
'' +
'' +
''
@doc = REXML::Document.new(xml)
end
def test_from_xml
assert_equal('SDSS:PhotoPrimary o, TWOMASS:PhotoPrimary t',
Tables.from_xml(@doc.root).to_adqls)
end
end
class RealWorldTests < Test::Unit::TestCase
def setup
@test1_doc = REXML::Document.new(File.new('test/adql/test1.adql'))
@test2_doc = REXML::Document.new(File.new('test/adql/test2.adql'))
@test3_doc = REXML::Document.new(File.new('test/adql/test3.adql'))
@test4_doc = REXML::Document.new(File.new('test/adql/test4.adql'))
@test5_doc = REXML::Document.new(File.new('test/adql/test5.adql'))
@test6_doc = REXML::Document.new(File.new('test/adql/test6.adql'))
@test7_doc = REXML::Document.new(File.new('test/adql/test7.adql'))
end
def test_test1
assert_equal('SELECT o.objid, o.ra, o.dec, o.type, t.objid, t.ra, t.dec, t.j_m, o.i ' +
'FROM SDSS:PhotoPrimary o, TWOMASS:PhotoPrimary t, ' +
'INNER SDSS:PhotoPrimary o, TWOMASS:PhotoPrimary t ON o.objid = t.objid ' +
"WHERE Region('CIRCLE J2000 182.5 -0.89 8.0')",
Select.from_xml(@test1_doc.root).to_adqls)
end
def test_test2
assert_equal('SELECT o.objid, o.ra, o.dec, o.r, o.type, t.objid, t.ra, t.dec ' +
'FROM SDSS:PhotoPrimary o, TWOMASS:PhotoPrimary t ' +
'WHERE ' +
'XMATCH(o, t) < 3.5 AND ' +
"Region('CIRCLE J2000 181.3 -0.76 6.5') AND " +
'o.type = 3',
Select.from_xml(@test2_doc.root).to_adqls)
end
def test_test3
assert_equal('SELECT o.objid, o.ra, o.dec, o.type, t.objid, t.ra, t.dec, t.j_m, o.i ' +
'FROM SDSS:PhotoPrimary o, TWOMASS:PhotoPrimary t ' +
'WHERE ' +
'XMATCH(o, t) < 3.5 AND ' +
"Region('CIRCLE J2000 182.5 -0.89 8.0') AND " +
'o.type = 3 AND ' +
'o.i < 21 AND ' +
't.j_m < 18 AND ' +
'(o.i - t.j_m) > 2',
Select.from_xml(@test3_doc.root).to_adqls)
end
def test_test4
assert_equal('SELECT o.objid, o.ra, o.dec, t.ra, t.dec, t.objid, o.type ' +
'FROM SDSS:PhotoPrimary o, TWOMASS:PhotoPrimary t, USNOB:PhotoPrimary p ' +
'WHERE ' +
'XMATCH(o, t, !p) < 3.5 AND ' +
"Region('CIRCLE J2000 182.5 -0.89 8.0') AND " +
'o.type = 3',
Select.from_xml(@test4_doc.root).to_adqls)
end
def test_test5
assert_equal('SELECT o.objid, o.ra, o.dec, o.type, t.objid, t.j_m, o.z ' +
'FROM SDSSDR2:PhotoPrimary o, TWOMASS:PhotoPrimary t ' +
'WHERE ' +
'XMATCH(o, t) < 2.5 AND ' +
"Region('CIRCLE J2000 16.031 -0.891 30.0') AND " +
'(o.z - t.j_m) > 2',
Select.from_xml(@test5_doc.root).to_adqls)
end
def test_test6
assert_equal('SELECT TOP 10 s.* FROM sdss:photoobjall s',
Select.from_xml(@test6_doc.root).to_adqls)
end
def test_test7
assert_equal('SELECT b.objid, b.ra, b.dec, b.u, s.z AS sdssRS ' +
'FROM sdss:photoprimary b, sdss:specobjall s ' +
'WHERE ' +
's.specobjid = b.specobjid AND ' +
's.z > 1.8 AND ' +
"Region('CIRCLE J2000 181.3 -0.76 30.0')",
Select.from_xml(@test7_doc.root).to_adqls)
end
end
class ParserTest < Test::Unit::TestCase
def test_construct
assert_nothing_raised {
select = Parser.new(File.new('test/adql/test3.adql')).parse
select_s = Parser.convert_to_adqls(File.new('test/adql/test3.adql'))
}
end
end