""" Tests for base.Structure-s. This is for sort-of generic functionality of structs. Tests for specific types of attributes should go to attrdeftest. """ #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. from io import StringIO import re import unittest from gavo.helpers import testhelpers from gavo import base from gavo.base import structure from gavo.base import xmlstruct from structscaffold import (Color, Foo, Foos, Bla, Palette, Image, Root) class SimpleStructureTest(unittest.TestCase): """tests for very basic structures. """ def _getStruct(self): class Bla(structure.ParseableStructure): _att1 = base.UnicodeAttribute("att1", default="default") return Bla def testSimpleAttSetting(self): class Bla(structure.ParseableStructure): _att1 = base.UnicodeAttribute("att1") s1 = Bla(None) self.assertEqual(s1.att1, None) s1._att1.feed(None, s1, "value") self.assertEqual(s1.att1, "value") def testDefaultAtt(self): Bla = self._getStruct() s1 = Bla(None) self.assertEqual(s1.att1, "default") def testNoInterference(self): Bla = self._getStruct() s1, s2 = Bla(None), Bla(None) s2._att1.feed(None, s2, "Foo") self.assertEqual(s1.att1, "default") self.assertEqual(s2.att1, "Foo") def testDefault(self): Bla = self._getStruct() s1 = Bla(None, att1="no default") self.assertEqual(s1.att1, "no default") class AtomListStructureTest(unittest.TestCase): """tests for structures with lists. """ def testBasic(self): s1 = Bla(None) self.assertEqual(s1.items, []) s1._att1.feed(None, s1, "2") self.assertEqual(s1.items, [2]) s1._att1.feedObject(s1, 1) self.assertEqual(s1.items, [2, 1]) def testNoInterference(self): s1, s2 = Bla(None), Bla(None) s1._att1.feedObject(s1, 3) s2._att1.feedObject(s2, 1) self.assertEqual(s1.items, [3]) self.assertEqual(s2.items, [1]) def testRaises(self): s1 = Bla(None) try: s1._att1.feed(None, s1, "x4") except base.LiteralParseError as msg: self.assertEqual(msg.attName, "item") self.assertEqual(msg.literal, "x4") else: self.fail("Bad integer literal doesn't raise correct exc. in list") def _feedInto(baseStruct, eventList): ep = xmlstruct.EventProcessor(baseStruct, base.ParseContext()) for ev in eventList: ep.feed(*ev) return ep.result class AtomicFeedTest(testhelpers.VerboseTest): """tests for feeding into atomic structures. """ def _getPlainStructure(self): class Bla(structure.ParseableStructure): name_ = "bla" _foo = base.UnicodeAttribute("foo", default="default") _bar = base.IntAttribute("bar", 0) return Bla def testSomeAtoms(self): Bla = self._getPlainStructure() s1 = _feedInto(Bla, [ ("start", "bla"), ("start", "foo"), ("value", "content_", "no default"), ("end", "foo"), ("value", "bar", 3),]) self.assertEqual(s1.foo, "no default") self.assertEqual(s1.bar, 3) def testRaising(self): Bla = self._getPlainStructure() self.assertRaisesWithMsg(base.StructureError, "bla elements have no xxx attributes or children.", _feedInto, (Bla, [("start", "bla"), ("start", "xxx")])) class XMLParseTest(testhelpers.VerboseTest): """tests for building structures out of XML documents. """ def testSimpleParse(self): f = xmlstruct.parseFromStream(Foo, StringIO(""" """)) self.assertEqual(f.color.r, 255) self.assertEqual(f.color.g, 0) self.assertEqual(f.color.b, 0) self.assertEqual(f.name, "red") def testIdSet(self): f = xmlstruct.parseFromStream(Foos, StringIO(""" """)) self.assertEqual(f.plups[0].id, "x") self.assertEqual(f.plups[1].color.id, "y") def testWrongRootElement(self): self.assertRaisesWithMsg(base.StructureError, 'At IO:\' \', (1, 0):' " Expected root element color, found foo", xmlstruct.parseFromStream, (Color, StringIO(""" """))) def testMsgBadAtt(self): self.assertRaisesWithMsg(base.StructureError, 'At IO:\'\', (1, 0):' " color elements have no noAtt attributes or children.", xmlstruct.parseFromString, (Color, '')) def testMsgBadChild(self): self.assertRaisesWithMsg(base.StructureError, "At IO:'30', (1, 7):" " color elements have no noAtt attributes or children.", xmlstruct.parseFromString, (Color, '30')) def testAtomListParse(self): f = xmlstruct.parseFromStream(Bla, StringIO('1' '2')) self.assertEqual(f.items, [0, 1, 2]) def testStructContent(self): f = xmlstruct.parseFromString(Foo, 'Some content') self.assertEqual(f.content_, "Some content") self.assertRaisesWithMsg(base.StructureError, "At IO:'Some content', (1, 19):" " color elements must not have character data content.", xmlstruct.parseFromString, (Color, 'Some content')) def testNoAttrsOnAttrs(self): self.assertRaisesWithMsg( base.StructureError, "At IO:'xy', (1, 5):" " name is atomic and thus has no attribute attr.", xmlstruct.parseFromString, (Foo, 'xy')) def testNoChildrenOnAttrs(self): self.assertRaisesWithMsg( base.StructureError, "At IO:'outxy', (1, 11):" " name elements cannot have child children", xmlstruct.parseFromString, (Foo, 'outxy')) class CopyTest(testhelpers.VerboseTest): """tests for copying structures. """ def testCopyStruct(self): f = xmlstruct.parseFromStream(Foo, StringIO(""" """)) f2 = f.copy(None) f.color.g = 27 self.assertEqual(f2.color.g, 0) def testCopyAtomList(self): f = xmlstruct.parseFromStream(Bla, StringIO('1' '2')) f2 = f.copy(None) f2.items.append(3) self.assertEqual(f.items, [0, 1, 2]) self.assertEqual(f2.items, [0, 1, 2, 3]) def testCopyStructList(self): f = xmlstruct.parseFromStream(Palette, StringIO('' '')) f2 = f.copy(None) f2.colors.append(Color(f2, r=0)) self.assertEqual(len(f.colors), 2) self.assertEqual(len(f2.colors), 3) def testOriginal(self): f = xmlstruct.parseFromStream(Palette, StringIO('' '7')) self.assertEqual(f.colors[0].g, 255) self.assertEqual(f.colors[1].g, 0) f.colors[1].r = 8 self.assertEqual(f.colors[1].r, 8) self.assertEqual(f.colors[0].r, 7) def testTypecheck(self): self.assertRaisesWithMsg(base.StructureError, 'At IO:\'\', (1, 28):' " Reference to 'xy' yielded object of type Foo, but expected a(n) Color" " instance.", xmlstruct.parseFromString, (Palette, '' '')) def testElementRefusal(self): self.assertRaisesWithMsg(base.StructureError, 'At IO:\'xy' 'xy')) def testBadId(self): self.assertRaisesWithMsg(base.NotFoundError, "Element with id 'xy' could not be located in parse context", xmlstruct.parseFromString, (Palette, '' '')) class FeedFromTest(testhelpers.VerboseTest): """tests for working of feedFrom hackery. """ def testBasic(self): f = xmlstruct.parseFromString(Palette, 'content') f2 = xmlstruct.parseFromString(Palette, '') f2.feedFrom(f) self.assertEqual(f2.foos[0].content_, "content", "Copyable attribute was not copied") self.assertEqual(f2.id, "pal2", "Non-copyable attribute was copied") class BeforeTest(testhelpers.VerboseTest): """tests for sorting of children. """ def testWorkingSort(self): class Foo(base.Structure): _att1 = base.UnicodeAttribute("b", before="a") _att2 = base.UnicodeAttribute("a") self.assertEqual([a.name_ for a in Foo.attrSeq], ["b", "a", "id"]) def testBailOnCycle(self): def defineBadStruct(): class Foo(base.Structure): _att1 = base.UnicodeAttribute("b", before="a") _att2 = base.UnicodeAttribute("a", before="c") _att3 = base.UnicodeAttribute("c", before="b") self.assertRaises(ValueError, defineBadStruct) class FeedingTest(testhelpers.VerboseTest): def testMixedParse(self): f = _feedInto(Foo, [ ("start", "foo"), ("start", "color"), ("value", "r", "10"), ("start", "g"), ("value", "content_", "20"), ("end", "g"), ("end", "color"), ("start", "name"), ("value", "content_", "blue"), ("end", "name"),]) self.assertEqual(f.color.r, 10) self.assertEqual(f.color.g, 20) self.assertEqual(f.color.b, 255) self.assertEqual(f.name, "blue") class IterEventsTest(testhelpers.VerboseTest): def testPlainAttribute(self): s = base.parseFromString(Color, '') self.assertEqual(list(s.iterEvents()), [('value', 'g', '7'), ('value', 'r', '5')]) def testNamedRef(self): # see testImmediateRef for the counterpart where there's not # actually a reference to be filled stuff = base.parseFromString(Root, '' '' '') self.assertTrue(('value', 'pal', 'pal1') in list(stuff.iterEvents())) # See commented-out parsecontext.ReferenceAttribute.iterEvents # def testImmediateRef(self): # stuff = base.parseFromString(Root, '' # '') # # make sure the pal element is present (rather than a bogus reference) # self.failUnless(('start', 'pal', None) in # list(stuff.iterEvents())) def testNamedRefCopied(self): stuff = base.parseFromString(Root, '' '' '') self.assertEqual( list(stuff.iterEvents()).count(('value', 'pal', 'pal1')), 2) def testAtomList(self): stuff = base.parseFromString(Image, 'abcdef') evs = list(stuff.iterEvents()) self.assertEqual(len(evs), 6) self.assertEqual(evs[1], ('value', 'content_', 'abc')) self.assertEqual(evs[2], ('end', 'calib', None)) class AttachedPositionsTest(testhelpers.VerboseTest): sampleXML = ('\n' '\n\n' '\n\n' '') def testPositionAdded(self): root = base.parseFromString(Root, self.sampleXML) self.assertEqual(root.getSourcePosition(), 'IO:\'