""" Tests for STC-X parsing and generation. """ #c Copyright 2008-2025, the GAVO project #c #c This program is free software, covered by the GNU GPL. See the #c COPYING file in the source distribution. import datetime import os from gavo.helpers import testhelpers from gavo import stc from gavo.stc import stcxgen from gavo.stc import dm def assertEqualWithoutIds(withId, template, desc=""): """raises an AssertionError if withId and template are different after all ids (and similar) in withId are blanked. We want this since the ids given to our elements are essentially random. """ # etree-based serialization has blanks in empty elements; we don't # want to fail on templates for that withoutId = testhelpers.cleanXML(withId) template = testhelpers.cleanXML(template) if withoutId!=template: matchLen = len(os.path.commonprefix([template, withoutId])) raise AssertionError("Didn't get expected STC XML for example '%s';" " non-matching part: %s"%(desc, withoutId[matchLen:])) class SpaceFrameTest(testhelpers.VerboseTest): def testSimple(self): assertEqualWithoutIds(stcxgen._nodeToStan(dm.SpaceFrame( flavor="CARTESIAN", nDim=2, refFrame="ICRS", name="rotten", refPos=dm.RefPos(standardOrigin="GEOCENTER")), None).render( prefixForEmpty="stc"), 'rotten' '') def testParsing(self): ast = stc.parseSTCX( '<' '/AstroCoordSystem>12.2<' 'C2>0.3')[0][1] self.assertEqual(ast.astroSystem.spaceFrame.refFrame, "GALACTIC_II") class TimeFrameTest(testhelpers.VerboseTest): def testSimple(self): ast = stc.parseSTCX('TT' '')[0][1] self.assertEqual(ast.astroSystem.timeFrame.refPos.standardOrigin, "TOPOCENTER") self.assertEqual(ast.astroSystem.timeFrame.timeScale, "TT") class V(stc.STC.STCElement): """A spartan STC-X root for test purposes. """ class STCMappingTest(testhelpers.VerboseTest): def assertMapsto(self, stcsLiteral, stcxExpected): ast = stc.parseSTCS(stcsLiteral) stcxResult = stcxgen.astToStan(ast, V).render(prefixForEmpty="stc") assertEqualWithoutIds(stcxResult, stcxExpected, stcsLiteral) class OtherCoordTest(STCMappingTest): def testSimple(self): self.assertMapsto("Time TT 2009-03-10T09:56:10.015625", 'TT') def testComplex(self): self.assertMapsto("Time TT 2009-03-10T09:56:10.015625 unit s" " Error 0.0001 0.0002 Resolution 0.0001 PixSize 2", 'TT') def testRedSpect(self): self.assertMapsto("Spectral NEPTUNE 12 unit Angstrom Error 4 3" " Redshift TOPOCENTER VELOCITY RELATIVISTIC 0.1", 'RELATIVISTIC12.04.03.00.1') def testTimeParse(self): ast = stc.parseSTCX(""" TT """)[0][1] self.assertEqual(ast.time.frame.timeScale, "TT") self.assertEqual(ast.time.value, datetime.datetime(2000, 1, 1, 0, 0)) self.assertEqual(ast.time.unit, None) class SpaceCoordTest(STCMappingTest): def testSimple(self): self.assertMapsto("Position ICRS 12.3 14.2", '12.314.2') def testComplex(self): self.assertMapsto("Position ICRS Epoch J200.3 12000.3 14000 unit arcsec" " Error 0.1 0.14 Resolution 0.5 0.55 Size 1 1.1 4.8 2.3 PixSize 0.2 0.2", '12000.314000.00.10.140.50.551.01.14.82.30.2200.3') class OtherCoordIntervalTest(STCMappingTest): def testSome(self): self.assertMapsto("TimeInterval TT 2009-03-10T09:56:10.015625" " SpectralInterval 1e10 1e11 unit Hz" " RedshiftInterval VELOCITY 1000 7500 unit km/s", 'TTOPTICAL2009-03-10T09:56:10.01562510000000000.0100000000000.01000.07500.0') def testTimeWithError(self): self.assertMapsto("TimeInterval TT 2009-03-10T09:56:10 2009-03-10T09:57:10 Error" " 4 Resolution 1 2 PixSize 1", 'TT2009-03-10T09:56:102009-03-10T09:57:10') def testSpectralWithError(self): self.assertMapsto("SpectralInterval 1e10 1e11 unit Hz Error 1 2", '1.02.010000000000.0100000000000.0') class SpaceCoordIntervalTest(STCMappingTest): def test1D(self): self.assertMapsto("PositionInterval UNKNOWNFrame CART1 1 2 unit mm", '1.02.0') def test2D(self): self.assertMapsto("PositionInterval ICRS 12 13 14 15", '12.013.014.015.0') def test2DWithSize(self): self.assertMapsto("PositionInterval ICRS 12 13 14 15 Size 1 1.5 1.75 2", '1.01.51.752.012.013.014.015.0') def test3D(self): self.assertMapsto("Time TT 2000-01-01T12:00:00" " PositionInterval ECLIPTIC CART3 12 13 10 14 15 9", 'TTJ2000.012.013.010.014.015.09.0') class RegionTest(STCMappingTest): def assertRaisesValueError(self, stcsExpr): self.assertRaises(stc.STCValueError, lambda s:stcxgen.astToStan(stc.parseSTCS(s), V), stcsExpr) def testCircles(self): self.assertRaisesValueError("Circle UNKNOWNFrame CART1 12 4") self.assertMapsto("Circle ICRS 12 13 1", '
12.013.0
1.0
') self.assertMapsto("Circle UNKNOWNFrame CART3 12 13 1 4", '4.0
12.013.01.0
') def testEllipse(self): self.assertMapsto("Ellipse ICRS 12 13 1 0.75 0", '
12.013.0
1.00.750.0
') def testBox(self): self.assertMapsto("Box fillfactor 0.1 ICRS 70 190 23 18", '
70.0190.0
23.018.0
') self.assertRaisesValueError("Box UNKNOWNFrame CART1 1 1") def testPolygon(self): self.assertMapsto("Polygon ICRS 70 190 23 18 12 45 30 -10", '70.0190.023.018.012.045.030.0-10.0') self.assertRaisesValueError("Polygon UNKNOWNFrame CART1 1 2 3 4") def testConvex(self): self.assertMapsto("Convex ICRS 70 190 23 0.125 12 45 30 -0.25", '70.0190.023.00.12512.045.030.0-0.25') class CompoundTest(STCMappingTest): def testSimpleUnion(self): self.assertMapsto("Union ICRS (Circle 10 10 2 Box 11 11 2 3)", '
10.010.0
2.0
11.011.0
2.03.0
') def testSimpleIntersection(self): self.assertMapsto("Intersection ICRS (Polygon 10 2 2 10 10 10" " Ellipse 11 11 2 3 30)", '10.02.02.010.010.010.0
11.011.0
2.03.030.0
') def testUnionWithNot(self): self.assertMapsto("Union ICRS (Circle 10 10 2 Not (Box 11 11 2 3))", '
10.010.0
2.0
11.011.0
2.03.0
') def testSimpleDifference(self): self.assertMapsto("Difference ICRS (Circle 10 10 2 Not (Box 11 11 2 3))", '
10.010.0
2.0
11.011.0
2.03.0
') def testInsaneRegion(self): self.assertMapsto("Difference ICRS (AllSky Union (Circle 10 10 2" " Intersection (Polygon 10 2 2 10 10 10 Ellipse 11 11 2 3 30" " Not (Difference (Circle 12 12 3 Box 11 11 2 3)))))", '
10.010.0
2.0
10.02.02.010.010.010.0
11.011.0
2.03.030.0
12.012.0
3.0
11.011.0
2.03.0
') class STCSRoundtripTest(testhelpers.VerboseTest, metaclass=testhelpers.SamplesBasedAutoTest): def _runTest(self, sample): src, expected = sample if expected is None: expected = src ast = stc.parseSTCS(src) xml = stc.getSTCXProfile(ast) ast2 = stc.parseSTCX(xml)[0][1] stcs = stc.getSTCS(ast2) self.assertEqual(stcs, expected) samples = [ ("AllSky ICRS", None), ("Box ICRS 12 1 15 3", 'Box ICRS 12.0 1.0 15.0 3.0'), ] if __name__=="__main__": testhelpers.main(STCSRoundtripTest)