Exemplo n.º 1
0
class SimpleTestCase(unittest.TestCase):
    def setUp(self):
        """Call before every test case."""
        self.foo = Foo()

    def testGettingOne(self):
        assert self.foo.getTheNumber1(
        ) == 1, "getTheNumber1() is not calculating values correctly"

    def testGettingTwo(self):
        assert self.foo.getTheNumber2(
        ) == 2, "getTheNumber2() is not calculating values correctly"

    def testGettingThree(self):
        assert self.foo.getTheNumber3(
        ) == 3, "getTheNumber3() is not calculating values correctly"

    def testGettingAString(self):
        assert self.foo.getTheStringHelloWorld(
        ) == "Hello World", "Getting hello world is not working"

    def testAssertingTwoObjects(self):
        foo1 = Foo()
        foo2 = Foo()
        foo1.doNothing()
        assert foo1 == foo2, "Uh Oh the foos aren't equal"
Exemplo n.º 2
0
class SimpleTestCaseTwo(unittest.TestCase):
    def setUp(self):
        """Call before every test case."""
        self.foo = Foo()

    def testGettingFour(self):
        assert self.foo.getTheNumber4(
        ) == 4, "getTheNumber4() is not calculating values correctly"

    def testGettingFive(self):
        assert self.foo.getTheNumber5(
        ) == 5, "getTheNumber2() is not calculating values correctly"
Exemplo n.º 3
0
def test(store):
    from Foo import Foo
    from webware.MiscUtils.DataTable import DataTable

    dataSource = '''
b:int,i:int,l:long,f:float,s:string,x:int
0,0,0,0,0,0
0,0,0,0.0,0.0,0
1,1,1,1,a,1
0,-1,8589934592,-3.14,'x',3
'''

    data = DataTable()
    data.readString(dataSource)

    for values in data:
        print(values)

        t = Foo()
        for attr in list('bilfsx'):
            t.setValueForKey(attr, values[attr])

        store.addObject(t)
        store.saveChanges()

        # Try an immediate fetch
        results = store.fetchObjectsOfClass(Foo)
        assert len(results) == 1
        # This tests the uniquing feature of MiddleKit:
        assert id(results[0]) == id(t)

        # Clear the store's in memory objects and try a fetch again
        store.clear()
        results = store.fetchObjectsOfClass(Foo)
        assert len(results) == 1
        assert results[0].allAttrs() == t.allAttrs()

        # Make sure what we got from the store is what we put in
        for attr in list('bilsx'):
            assert results[0].valueForKey(attr) == values[attr]

        different = 0.000001  # @@ 2000-11-25 ce: more work needed on floats
        assert abs(results[0].valueForKey('f') - values['f']) < different

        # Insert the fetched attributes
        t2 = Foo()
        for attr in list('bilfsx'):
            t2.setValueForKey(attr, results[0].valueForKey(attr))
        store.addObject(t2)
        store.saveChanges()
        results = store.fetchObjectsOfClass(Foo)
        assert len(results) == 2, 'len=%r' % len(results)
        assert results[0].allAttrs() == results[1].allAttrs()

        # Reset
        store.clear()
        store.executeSQLTransaction('delete from Foo;')
Exemplo n.º 4
0
def setupListTest(store, klass):
    # Create a Foo and a Bar, with the Foo pointing to the Bar
    bar = Bar()
    bar.setX(42)
    foo = Foo()
    foo.setBar(bar)
    store.addObject(foo)
    store.addObject(bar)
    store.saveChanges()

    # create an instance of klass and put it into the list in foo
    obj = klass()
    getattr(foo, 'addToListOf%s' % klass.__name__)(obj)
    store.saveChanges()

    return obj, foo, bar
Exemplo n.º 5
0
def setupListTest(store, klass):
    # Create a Foo and a Bar, with the Foo pointing to the Bar
    bar = Bar()
    bar.setX(42)
    foo = Foo()
    foo.setBar(bar)
    store.addObject(foo)
    store.addObject(bar)
    store.saveChanges()

    # create an instance of klass and put it into the list in foo
    obj = klass()
    getattr(foo, 'addToListOf%s' % klass.__name__)(obj)
    store.saveChanges()

    return obj, foo, bar
Exemplo n.º 6
0
def test(store):
    """Bug report from Stefan Karlsson <*****@*****.**> on Dec 3 2001.

    Obj refs can be incorrectly stored to the database as zeroes for newly created objects.
    """

    from Bar import Bar
    from Foo import Foo
    from BarReq import BarReq

    # Since we're the second empty test, double check that the db is really empty
    assert len(store.fetchObjectsOfClass(Bar)) == 0
    assert len(store.fetchObjectsOfClass(Foo)) == 0

    bar = Bar()
    foo = Foo()
    store.addObject(bar)
    store.addObject(foo)
    bar.setFoo(foo)

    store.saveChanges()

    bars = store.fetchObjectsOfClass(Bar)
    assert len(bars) == 1
    bar2 = bars[0]
    assert bar2 is bar
    assert bar.foo() is not None  # the sign of the bug in question
    assert bar.foo() is foo  # what we should expect

    store.clear()
    bar = store.fetchObjectsOfClass(Bar)[0]
    assert bar.foo() is not None
Exemplo n.º 7
0
def testAddToBars(store):
    # Test 1: Use addToBars()
    f = Foo()
    store.addObject(f)

    b = Bar()
    f.addToBars(b)
    b.dumpAttrs()
    store.saveChanges()

    store.clear()
    f = store.fetchObjectsOfClass(Foo)[0]
    bars = f.bars()
    assert len(bars) == 1, 'bars=%r' % bars
    assert bars[0].foo() == f
    reset(store)
Exemplo n.º 8
0
def test(store):
    from Foo import Foo
    from Bar import Bar
    from MiscUtils.DataTable import DataTable
    import sys

    class Blarg:
        pass

    blarg = Blarg()  # a dummy object, incompatible with everything

    f = Foo()
    typeErrors(f, blarg)
    typeErrors(f, sys.stdout)
    typeErrors(f, 1.0, ['f'])
    typeErrors(f, 1, 'b i l f'.split())

    # ValueErrors and others
    try:
        f.setB(5)
    except ValueError:
        pass
    else:
        raise Exception, 'expecting ValueError for bad bool argument'

    try:
        f.setI(2L**32)
    except OverflowError:
        pass
    else:
        raise Exception, 'expecting OverflowError for large int argument'

    try:
        f.setE('c')
    except ValueError:
        pass
    else:
        raise Exception, 'expecting ValueError for invalid enum'

    # Numerics that pass
    f.setI(1L)  # ints can take longs that fit in the int range
    f.setL(1)  # longs can take ints
    f.setF(1)  # floats can take ints
    f.setF(1L)  # floats can take longs
Exemplo n.º 9
0
def test(store):
    from Foo import Foo
    from MiscUtils.DataTable import DataTable

    dataSource = '''
b:int,i:int,l:long,f:float,s:string,x:int
0,0,0,0,0,0
0,0,0,0.0,0.0,0
1,1,1,1,a,1
0,-1,8589934592,-3.14,'x',3
'''

    data = DataTable()
    data.readString(dataSource)

    for values in data:
        print values

        t = Foo()
        for attr in list('bilfsx'):
            t.setValueForKey(attr, values[attr])

        store.addObject(t)
        store.saveChanges()

        # Try an immediate fetch
        results = store.fetchObjectsOfClass(Foo)
        assert len(results) == 1
        # This tests the uniquing feature of MiddleKit:
        assert id(results[0]) == id(t)

        # Clear the store's in memory objects and try a fetch again
        store.clear()
        results = store.fetchObjectsOfClass(Foo)
        assert len(results) == 1
        assert results[0].allAttrs() == t.allAttrs()

        # Make sure what we got from the store is what we put in
        for attr in list('bilsx'):
            assert results[0].valueForKey(attr) == values[attr]

        different = 0.000001 # @@ 2000-11-25 ce: more work needed on floats
        assert abs(results[0].valueForKey('f')-values['f']) < different

        # Insert the fetched attributes
        t2 = Foo()
        for attr in list('bilfsx'):
            t2.setValueForKey(attr, results[0].valueForKey(attr))
        store.addObject(t2)
        store.saveChanges()
        results = store.fetchObjectsOfClass(Foo)
        assert len(results) == 2, 'len=%r' % len(results)
        assert results[0].allAttrs() == results[1].allAttrs()

        # Reset
        store.clear()
        store.executeSQLTransaction('delete from Foo;')
Exemplo n.º 10
0
def testNone(store):
    print 'Testing None.'

    store.executeSQLTransaction('delete from Foo;')

    f = Foo()
    f.setD(None)
    f.setT(None)
    f.setDt(None)

    store.addObject(f)
    store.saveChanges()
    store.clear()

    results = store.fetchObjectsOfClass(Foo)
    assert len(results) == 1
    f = results[0]
    assert f.d() is None
    assert f.t() is None
    assert f.dt() is None
Exemplo n.º 11
0
def setupListTest(store, klass):
    """
	Setup 3 objects: one of the specified klass, pointing to a Foo, pointing to a Bar.
	Returns tuple (object of specified klass, foo, bar).
	"""
    # Create a Foo and a Bar, with the Foo pointing to the Bar
    bar = Bar()
    bar.setX(42)
    foo = Foo()
    foo.setBar(bar)
    store.addObject(foo)
    store.addObject(bar)
    store.saveChanges()

    # create an instance of klass and put it into the list in foo
    object = klass()
    getattr(foo, 'addToListOf%s' % klass.__name__)(object)
    store.saveChanges()

    return object, foo, bar
Exemplo n.º 12
0
def setupTest(store, klass):
    """
	Setup 3 objects: one of the specified klass, pointing to a Foo, pointing to a Bar.
	Returns tuple (object of specified klass, foo, bar).
	"""
    # Create a Foo and a Bar, with the Foo pointing to the Bar
    bar = Bar()
    bar.setX(42)
    foo = Foo()
    foo.setBar(bar)
    store.addObject(foo)
    store.addObject(bar)
    store.saveChanges()

    # create an instance of klass pointing to Foo
    object = klass()
    object.setFoo(foo)
    store.addObject(object)
    store.saveChanges()

    return object, foo, bar
Exemplo n.º 13
0
def setupListTest(store, klass):
    """Setup list test.

    Setup 3 objects: one of the specified klass, pointing to a Foo,
    pointing to a Bar. Returns tuple (object of specified klass, foo, bar).
    """
    # Create a Foo and a Bar, with the Foo pointing to the Bar
    bar = Bar()
    bar.setX(42)
    foo = Foo()
    foo.setBar(bar)
    store.addObject(foo)
    store.addObject(bar)
    store.saveChanges()

    # create an instance of klass and put it into the list in foo
    obj = klass()
    getattr(foo, 'addToListOf%s' % klass.__name__)(obj)
    store.saveChanges()

    return obj, foo, bar
Exemplo n.º 14
0
def test(store):
    from Foo import Foo
    from MiscUtils.DataTable import DataTable

    thing = store.model().klass('Thing')
    assert thing.hasAttr('a')
    assert thing.hasAttr('b')
    assert not thing.hasAttr('i')

    f = Foo()
    f.setA('a')
    f.setB('b')
    f.setX(1)

    store.addObject(f)
    store.saveChanges()

    store.clear()
    f = store.fetchObjectsOfClass('Foo')[0]
    assert f.a() == 'a'
    assert f.b() == 'b'
    assert f.x() == 1
Exemplo n.º 15
0
def setupTest(store, klass):
    """Setup test.

    Setup 3 objects: one of the specified klass, pointing to a Foo,
    pointing to a Bar. Returns tuple (object of specified klass, foo, bar).
    """
    # Create a Foo and a Bar, with the Foo pointing to the Bar
    bar = Bar()
    bar.setX(42)
    foo = Foo()
    foo.setBar(bar)
    store.addObject(foo)
    store.addObject(bar)
    store.saveChanges()

    # create an instance of klass pointing to Foo
    obj = klass()
    obj.setFoo(foo)
    store.addObject(obj)
    store.saveChanges()

    return obj, foo, bar
Exemplo n.º 16
0
def test(store):
    from Foo import Foo

    f = Foo()

    # legal sets:
    f.setRi(1)
    f.setNi(2)
    f.setRs('a')
    f.setNs('b')
    f.setNi(None)
    f.setNs(None)

    # illegal sets:
    errMsg = 'Set None for required attribute, but no exception was raised.'
    try:
        f.setRi(None)
    except:
        pass
    else:
        raise Exception, errMsg

    try:
        f.setRs(None)
    except:
        pass
    else:
        raise Exception, errMsg

    store.addObject(f)
    store.saveChanges()
    store.clear()

    results = store.fetchObjectsOfClass(Foo)
    assert len(results) == 1
    f = results[0]
    assert f.ri() == 1
    assert f.ni() == None
    assert f.rs() == 'a'
    assert f.ns() == None

    return

    from MiscUtils.DataTable import DataTable

    dataSource = '''
b:int,i:int,l:long,f:float,s:string
0,0,0,0,0
0,0,0,0.0,0.0
1,1,1,1,a
0,-1,8589934592,-3.14,'x'
'''

    data = DataTable()
    data.readString(dataSource)

    for values in data:
        print values

        t = Thing()
        t.setB(values['b'])
        t.setI(values['i'])
        t.setL(values['l'])
        t.setF(values['f'])
        t.setS(values['s'])

        store.addObject(t)
        store.saveChanges()

        # Try an immediate fetch
        results = store.fetchObjectsOfClass(Thing)
        assert len(results) == 1
        # This tests the uniquing feature of MiddleKit:
        assert id(results[0]) == id(t)

        # Clear the store's in memory objects and try a fetch again
        store.clear()
        results = store.fetchObjectsOfClass(Thing)
        assert len(results) == 1
        assert results[0].allAttrs() == t.allAttrs()

        # Make sure what we got from the store is what we put in
        assert t.b() == values['b']
        assert t.i() == values['i']
        assert t.l() == values['l']
        assert t.f() == values['f']
        assert t.s() == values['s']

        # Reset
        store.clear()
        store.executeSQL('delete from Thing;')
        del t
Exemplo n.º 17
0
from Foo import Foo

# print(Foo.pole)
# obiekt = Foo()
#
# print(obiekt.pole)
#
# obiekt.pole = 5
#
# print()
# print(obiekt.pole)
# print(Foo.pole)

obiekt = Foo()
obiekt.a = -11
print(obiekt.a)
Exemplo n.º 18
0
 def __init__(self, editor):
     Signal.__init__(self)
     from Foo import Foo
     Foo(self, editor)
Exemplo n.º 19
0
def testStrings(store):
    print('Testing with strings.')

    f = Foo()
    f.setD('2001-06-07')
    f.setT('12:42')
    f.setDt('2001-06-07 12:42')

    storeFoo(store, f)

    f.setD('2002-11-11')
    f.setT('16:04')
    f.setDt('2002-11-11 16:04')

    store.saveChanges()
Exemplo n.º 20
0
def testNone(store):
    print('Testing None.')

    store.executeSQLTransaction('delete from Foo;')

    f = Foo()
    f.setD(None)
    f.setT(None)
    f.setDt(None)

    store.addObject(f)
    store.saveChanges()
    store.clear()

    results = store.fetchObjectsOfClass(Foo)
    assert len(results) == 1
    f = results[0]
    assert f.d() is None
    assert f.t() is None
    assert f.dt() is None
Exemplo n.º 21
0
def test(store):
    # We invoke testAddToBars twice on purpose, just to see that
    # the second time around, things are stable enough to pass again
    testAddToBars(store)
    testAddToBars(store)

    # Test 2: do not use addToBars()
    # @@ 2001-02-11 ce: this is probably not a valid test in the long run
    f = Foo()
    f.setX(
        0
    )  # @@ 2000-11-25 ce: take out after fixing default value bug in gen py code
    store.addObject(f)
    b = Bar()
    b.setFoo(f)
    b.setX(7)
    store.addObject(b)
    store.saveChanges()
    store.clear()

    f = store.fetchObjectsOfClass(Foo)[0]
    assert f._mk_store
    assert f._mk_inStore
    bars = f.bars()
    assert type(bars) is ListType
    assert len(bars) == 1, 'bars=%r' % bars
    assert bars[0].x() == 7

    # Test addToXYZ() method
    bar = Bar()
    bar.setX(7)
    f.addToBars(bar)
    assert bar.foo() == f
    store.saveChanges()
    store.clear()

    f = store.fetchObjectsOfClass(Foo)[0]
    bars = f.bars()
    assert type(bars) is ListType
    assert len(bars) == 2, 'bars=%r' % bars
    assert bars[0].x() == 7
    assert bars[1].x() == 7

    # Test the assertion checking in addToXYZ()
    try:
        f.addToBars(None)
    except:
        pass
    else:
        NoException('f.addToBars(None) # None not allowed')

    try:
        f.addToBars(5)
    except:
        pass
    else:
        NoException('f.addToBars(5) # not an object')

    try:
        f.addToBars(f)
    except:
        pass
    else:
        NoException('f.addToBars(f) # wrong class')

    try:
        f.addToBars(bar)
    except:
        pass
    else:
        NoException('f.addToBars(bar) # already added')
Exemplo n.º 22
0
def test(store):
    from Foo import Foo

    a100 = 'a' * 100
    b500 = 'b' * 500
    #c70000 = 'c'*70000
    c70000 = 'c' * 60000  #get rid of fb2 limit

    f = Foo()
    f.setMax100(a100)
    f.setMax500(b500)
    f.setMax70000(c70000)
    store.addObject(f)
    store.saveChanges()

    store.clear()
    results = store.fetchObjectsOfClass(Foo)
    f = results[0]
    assert f.max100() == a100
    assert f.max500() == b500
    assert f.max70000() == c70000

    # use only ascii so this should work with all db encodings
    difficultString = ''.join([chr(i) for i in range(1, 128)])
    f = Foo()
    f.setMax500(difficultString)
    store.addObject(f)
    store.saveChanges()
    serialNum = f.serialNum()
    store.clear()
    result = store.fetchObject(Foo, serialNum)
    assert result.max500() == difficultString  #problem with backslash??
Exemplo n.º 23
0
def test(store):
    from Foo import Foo
    from Bar import Bar
    from BarReq import BarReq

    # Create a Foo and a Bar that refers to it
    f = Foo()
    f.setX(3)

    store.addObject(f)
    store.saveChanges()

    b = Bar()
    b.setFoo(f)
    store.addObject(b)
    store.saveChanges()

    # Test fetching
    store.clear()
    results = store.fetchObjectsOfClass(Bar)
    assert len(results)
    b = results[0]
    f1 = b.foo()
    f2 = b.foo()
    assert f1 is f2  # test uniqueness
    assert b.foo().x() == 3

    # Fetch in reverse order
    store.clear()
    f = store.fetchObjectsOfClass(Foo)[0]
    b = store.fetchObjectsOfClass(Bar)[0]
    assert b.foo() is f

    # Test None, set, save and retrieve
    b.setFoo(None)
    store.saveChanges()
    store.clear()
    b = store.fetchObjectsOfClass(Bar)[0]
    assert b.foo() is None

    # Test the assertions in setFoo()
    b = BarReq()
    try:
        b.setFoo(None)
    except:
        pass
    else:
        NoException('b.setFoo(None) # None not allowed')

    try:
        b.setFoo('x')
    except:
        pass
    else:
        NoException('b.setFoo("x") # wrong type not allowed')

    try:
        b.setFoo(Bar())
    except:
        pass
    else:
        NoException('b.setFoo(Bar()) # wrong class not allowed')
Exemplo n.º 24
0
def testObjects(store):
    import mx
    from mx.DateTime import DateTimeFrom, TimeFrom
    print 'Testing with DateTime module.'

    d = DateTimeFrom('2001-06-07')
    t = TimeFrom('12:42')
    dt = DateTimeFrom('2001-06-07 12:42')

    f = Foo()
    f.setD(d)
    f.setT(t)
    f.setDt(dt)

    storeFoo(store, f)

    d = DateTimeFrom('2002-11-11')
    t = TimeFrom('16:04')
    dt = DateTimeFrom('2002-11-11 16:04')

    f.setD(d)
    f.setT(t)
    f.setDt(dt)

    store.saveChanges()
Exemplo n.º 25
0
 def onModuleLoad(self):
     f = Foo()
     b = Bar()
Exemplo n.º 26
0
 def testAssertingTwoObjects(self):
     foo1 = Foo()
     foo2 = Foo()
     foo1.doNothing()
     assert foo1 == foo2, "Uh Oh the foos aren't equal"
Exemplo n.º 27
0
 def test_computation(self):
     f = Foo(2, 3)
     self.assertEqual(6, f.get_product())
Exemplo n.º 28
0
def test(store):
    from Foo import Foo

    a100 = 'a' * 100
    b500 = 'b' * 500
    c70000 = 'c' * 70000

    f = Foo()
    f.setMax100(a100)
    f.setMax500(b500)
    f.setMax70000(c70000)
    store.addObject(f)
    store.saveChanges()

    store.clear()
    results = store.fetchObjectsOfClass(Foo)
    f = results[0]
    assert f.max100() == a100
    assert f.max500() == b500
    assert f.max70000() == c70000

    difficultString = ''.join([chr(i) for i in range(256)])
    f = Foo()
    f.setMax500(difficultString)
    store.addObject(f)
    store.saveChanges()
    serialNum = f.serialNum()
    store.clear()
    result = store.fetchObject(Foo, serialNum)
    assert result.max500() == difficultString
Exemplo n.º 29
0
def test(store):
    from Foo import Foo
    from webware.MiscUtils.DataTable import DataTable

    thing = store.model().klass('Thing')
    assert thing.hasAttr('a')
    assert thing.hasAttr('b')
    assert not thing.hasAttr('i')

    f = Foo()
    f.setA('a')
    f.setB('b')
    f.setX(1)

    store.addObject(f)
    store.saveChanges()

    store.clear()
    f = store.fetchObjectsOfClass('Foo')[0]
    assert f.a() == 'a'
    assert f.b() == 'b'
    assert f.x() == 1
Exemplo n.º 30
0
def testStrings(store):
    print 'Testing with strings.'

    f = Foo()
    f.setD('2001-06-07')
    f.setT('12:42')
    f.setDt('2001-06-07 12:42')

    storeFoo(store, f)

    f.setD('2002-11-11')
    f.setT('16:04')
    f.setDt('2002-11-11 16:04')

    store.saveChanges()
Exemplo n.º 31
0
def test(store):
    from Foo import Foo

    f = Foo()

    # legal sets:
    f.setRi(1)
    f.setNi(2)
    f.setRs('a')
    f.setNs('b')
    f.setNi(None)
    f.setNs(None)

    # illegal sets:
    errMsg = 'Set None for required attribute, but no exception was raised.'
    try:
        f.setRi(None)
    except Exception:
        pass
    else:
        raise Exception(errMsg)

    try:
        f.setRs(None)
    except Exception:
        pass
    else:
        raise Exception(errMsg)

    store.addObject(f)
    store.saveChanges()
    store.clear()

    results = store.fetchObjectsOfClass(Foo)
    assert len(results) == 1
    f = results[0]
    assert f.ri() == 1
    assert f.ni() is None
    assert f.rs() == 'a'
    assert f.ns() is None

    return

    from MiscUtils.DataTable import DataTable

    dataSource = '''
b:int,i:int,l:long,f:float,s:string
0,0,0,0,0
0,0,0,0.0,0.0
1,1,1,1,a
0,-1,8589934592,-3.14,'x'
'''

    data = DataTable()
    data.readString(dataSource)

    for values in data:
        print values

        t = Thing()
        t.setB(values['b'])
        t.setI(values['i'])
        t.setL(values['l'])
        t.setF(values['f'])
        t.setS(values['s'])

        store.addObject(t)
        store.saveChanges()

        # Try an immediate fetch
        results = store.fetchObjectsOfClass(Thing)
        assert len(results) == 1
        # This tests the uniquing feature of MiddleKit:
        assert id(results[0]) == id(t)

        # Clear the store's in memory objects and try a fetch again
        store.clear()
        results = store.fetchObjectsOfClass(Thing)
        assert len(results) == 1
        assert results[0].allAttrs() == t.allAttrs()

        # Make sure what we got from the store is what we put in
        assert t.b() == values['b']
        assert t.i() == values['i']
        assert t.l() == values['l']
        assert t.f() == values['f']
        assert t.s() == values['s']

        # Reset
        store.clear()
        store.executeSQLTransaction('delete from Thing;')
        del t
Exemplo n.º 32
0
 def setUp(self):
     """Call before every test case."""
     self.foo = Foo()
Exemplo n.º 33
0
def testDateTime(store):
    print 'Testing with the datetime module.'

    d = datetime.date(2001, 6, 7)
    t = datetime.time(12, 42)
    dt = datetime.datetime(2001, 6, 7, 12, 42)

    f = Foo()
    f.setD(d)
    f.setT(t)
    f.setDt(dt)

    storeFoo(store, f)

    d = datetime.date(2002, 11, 11)
    t = datetime.time(16, 04)
    dt = datetime.datetime(2002, 11, 11, 16, 0)

    f.setD(d)
    f.setT(t)
    f.setDt(dt)

    store.saveChanges()
Exemplo n.º 34
0
def test(store):
    # We invoke testAddToBars twice on purpose, just to see that
    # the second time around, things are stable enough to pass again
    testAddToBars(store)
    testAddToBars(store)

    # Test 2: do not use addToBars()
    f = Foo()
    store.addObject(f)
    b = Bar()
    b.setFoo(f)
    b.setX(7)
    store.addObject(b)
    store.saveChanges()
    store.clear()

    f = store.fetchObjectsOfClass(Foo)[0]
    assert f._mk_store
    assert f._mk_inStore
    bars = f.bars()
    assert isinstance(bars, list)
    assert len(bars) == 1, 'bars=%r' % bars
    assert bars[0].x() == 7

    # Test addToXYZ() method
    bar = Bar()
    bar.setX(42)
    f.addToBars(bar)
    assert bar.foo() == f
    store.saveChanges()
    store.clear()

    f = store.fetchObjectsOfClass(Foo)[0]
    bars = f.bars()
    assert isinstance(bars, list)
    assert len(bars) == 2, 'bars=%r' % bars
    assert bars[0].x() == 7
    assert bars[1].x() == 42

    # Test the assertion checking in addToXYZ()
    try:
        f.addToBars(None)
    except Exception:
        pass
    else:
        NoException('f.addToBars(None)  # None not allowed')

    try:
        f.addToBars(5)
    except Exception:
        pass
    else:
        NoException('f.addToBars(5)  # not an object')

    try:
        f.addToBars(f)
    except Exception:
        pass
    else:
        NoException('f.addToBars(f)  # wrong class')

    try:
        f.addToBars(bar)
    except Exception:
        pass
    else:
        NoException('f.addToBars(bar)  # already added')

    # Test delFromXYZ() method
    bar = bars[1]
    f.delFromBars(bar)
    assert len(bars) == 1
    assert bar.foo() is None
    store.saveChanges()
    store.clear()

    f = store.fetchObjectsOfClass(Foo)[0]
    bars = f.bars()
    assert isinstance(bars, list)
    assert len(bars) == 1, 'bars=%r' % bars
    assert bars[0].x() == 7

    # Test the assertion checking in delFromXYZ()
    try:
        f.delFromBars(None)
    except Exception:
        pass
    else:
        NoException('f.delFromBars(None)  # None not allowed')

    try:
        f.delFromBars(5)
    except Exception:
        pass
    else:
        NoException('f.delFromBars(5)  # not an object')

    try:
        f.delFromBars(f)
    except Exception:
        pass
    else:
        NoException('f.delFromBars(f)  # wrong class')

    try:
        f.delFromBars(bar)
    except Exception:
        pass
    else:
        NoException('f.delFromBars(bar)  # already deleted')
Exemplo n.º 35
0
def test(store):
    from Foo import Foo

    f = Foo()

    # Test defaults
    assert f.b() == 1
    assert f.i() == 2
    assert f.l() == 3
    assert f.f() == 4
    assert f.s() == '5'
    assert f.e() == 'x'

    # Test min max
    # These should pass
    for x in range(10):
        f.setI(int(x))
        f.setL(long(x))
        f.setF(float(x))
    for x in range(6):
        s = '.'*(x+5)
        f.setS(s)

    # Test min max
    # These should throw exceptions
    if 0:
        for x in [-1, 11]:
            try:
                f.setI(int(x))
            except Exception:
                pass
            else:
                raise NoException

    # We'd like to test that the SQL code has the correct
    # DEFAULTs. Testing the sample values can't do this
    # because the SQL generated for inserting samples
    # uses the defaults specified in the object model.
    # So we use some direct SQL here:
    if getattr(store, 'executeSQL'):
        store.executeSQLTransaction('insert into Foo (i) values (42);')
        foo = store.fetchObjectsOfClass(Foo, clauses='where i=42')[0]
        assert foo.valueForKey('b') == 1, foo.valueForKey('b')
        assert foo.valueForKey('l') == 3, foo.valueForKey('l')
        assert foo.valueForKey('f') == 4, foo.valueForKey('f')
        assert foo.valueForKey('s') == '5', foo.valueForKey('s')

        store.executeSQLTransaction('insert into Foo (s) values (42);')
        foo = store.fetchObjectsOfClass(Foo, clauses="where s='42'")[0]
        assert foo.valueForKey('i') == 2
Exemplo n.º 36
0
def test(store):
    import sys
    from MiscUtils.DataTable import DataTable
    from Foo import Foo
    from Bar import Bar

    class Blarg(object):
        pass
    blarg = Blarg() # a dummy object, incompatible with everything

    f = Foo()
    typeErrors(f, blarg)
    typeErrors(f, sys.stdout)
    typeErrors(f, 1.0, ['f'])
    typeErrors(f, 1, 'b i l f'.split())

    # ValueErrors and others
    try:
        f.setB(5)
    except ValueError:
        pass
    else:
        raise Exception('expecting ValueError for bad bool argument')

    try:
        f.setI(2L**32)
    except OverflowError:
        pass
    else:
        raise Exception('expecting OverflowError for large int argument')

    try:
        f.setE('c')
    except ValueError:
        pass
    else:
        raise Exception('expecting ValueError for invalid enum')

    # Numerics that pass
    f.setI(1L) # ints can take longs that fit in the int range
    f.setL(1) # longs can take ints
    f.setF(1) # floats can take ints
    f.setF(1L) # floats can take longs
Exemplo n.º 37
0
 def onModuleLoad(self):
     from Foo import Foo, Bar
     f = Foo()
     b = Bar()
Exemplo n.º 38
0
def testDateTime(store):
    print('Testing with the datetime module.')

    d = datetime.date(2001, 6, 7)
    t = datetime.time(12, 42)
    dt = datetime.datetime(2001, 6, 7, 12, 42)

    f = Foo()
    f.setD(d)
    f.setT(t)
    f.setDt(dt)

    storeFoo(store, f)

    d = datetime.date(2002, 11, 11)
    t = datetime.time(16, 0o4)
    dt = datetime.datetime(2002, 11, 11, 16, 0)

    f.setD(d)
    f.setT(t)
    f.setDt(dt)

    store.saveChanges()
Exemplo n.º 39
0
def test(store):
    from Foo import Foo
    from Bar import Bar
    from BarReq import BarReq

    # Create a Foo and a Bar that refers to it
    f = Foo()
    f.setX(3)

    store.addObject(f)
    store.saveChanges()

    b = Bar()
    b.setFoo(f)
    store.addObject(b)
    store.saveChanges()

    # Test fetching
    store.clear()
    results = store.fetchObjectsOfClass(Bar)
    assert len(results)
    b = results[0]
    f1 = b.foo()
    f2 = b.foo()
    assert f1 is not None, 'got None instead of a Foo'
    assert f1 is f2 # test uniqueness
    assert b.foo().x() == 3

    # Fetch in reverse order
    store.clear()
    f = store.fetchObjectsOfClass(Foo)[0]
    b = store.fetchObjectsOfClass(Bar)[0]
    assert b.foo() is f

    # Test None, set, save and retrieve
    b.setFoo(None)
    store.saveChanges()
    store.clear()
    b = store.fetchObjectsOfClass(Bar)[0]
    assert b.foo() is None

    # Test the assertions in setFoo()
    b = BarReq()
    try:
        b.setFoo(None)
    except Exception:
        pass
    else:
        NoException('b.setFoo(None) # None not allowed')

    try:
        b.setFoo('x')
    except Exception:
        pass
    else:
        NoException('b.setFoo("x") # wrong type not allowed')

    try:
        b.setFoo(Bar())
    except Exception:
        pass
    else:
        NoException('b.setFoo(Bar()) # wrong class not allowed')
Exemplo n.º 40
0
 def test_dataset(self):
     print("in test_dataset")
     for x, y, z, in FooTest.data:
         with self.subTest():
             self.assertEqual(z, Foo(x, y).get_product())
Exemplo n.º 41
0
def test(store):
    from Foo import Foo

    f = Foo()
    f.setE('red')
    f.setE('green')
    store.addObject(f)
    store.saveChanges()

    results = store.fetchObjectsOfClass(Foo)
    assert len(results) == 1, 'len=%s, results=%s' % (len(results), results)

    assert f.e() == 'green' or f.e() == 1, f.e()

    f = None
    store.clear()

    results = store.fetchObjectsOfClass(Foo)
    assert len(results) == 1, 'len=%s, results=%s' % (len(results), results)
    f = results[0]

    assert f.e() == 'green' or f.e() == 1, f.e()

    f.setE(None)
    store.saveChanges()

    f = None
    store.clear()
    f = store.fetchObjectsOfClass(Foo)[0]
    assert f.e() is None, f.e()

    try:
        f.setE('wrong')
    except ValueError:
        pass
    except Exception:
        assert 0, 'expecting a ValueError for invalid enums'
    else:
        assert 0, 'expecting a ValueError for invalid enums'
Exemplo n.º 42
0
def test(store):
    from Foo import Foo

    f = Foo()

    # Test defaults
    assert f.b() == 1
    assert f.i() == 2
    assert f.l() == 3
    assert f.f() == 4
    assert f.s() == '5'
    assert f.e() == 'x'

    # Test min max
    # These should pass
    for x in range(10):
        f.setI(int(x))
        f.setL(long(x))
        f.setF(float(x))
    for x in range(6):
        s = '.' * (x + 5)
        f.setS(s)

    # Test min max
    # These should throw exceptions
    if 0:
        for x in [-1, 11]:
            try:
                f.setI(int(x))
            except Exception:
                pass
            else:
                raise NoException

    # We'd like to test that the SQL code has the correct
    # DEFAULTs. Testing the sample values can't do this
    # because the SQL generated for inserting samples
    # uses the defaults specified in the object model.
    # So we use some direct SQL here:
    if getattr(store, 'executeSQL'):
        store.executeSQLTransaction('insert into Foo (i) values (42);')
        foo = store.fetchObjectsOfClass(Foo, clauses='where i=42')[0]
        assert foo.valueForKey('b') == 1, foo.valueForKey('b')
        assert foo.valueForKey('l') == 3, foo.valueForKey('l')
        assert foo.valueForKey('f') == 4, foo.valueForKey('f')
        assert foo.valueForKey('s') == '5', foo.valueForKey('s')

        store.executeSQLTransaction('insert into Foo (s) values (42);')
        foo = store.fetchObjectsOfClass(Foo, clauses="where s='42'")[0]
        assert foo.valueForKey('i') == 2
Exemplo n.º 43
0
def test(store):
    from Foo import Foo

    a100 = 'a'*100
    b500 = 'b'*500
    c70000 = 'c'*70000

    f = Foo()
    f.setMax100(a100)
    f.setMax500(b500)
    f.setMax70000(c70000)
    store.addObject(f)
    store.saveChanges()

    store.clear()
    results = store.fetchObjectsOfClass(Foo)
    f = results[0]
    assert f.max100() == a100
    assert f.max500() == b500
    assert f.max70000() == c70000

    # use only ascii so this should work with all db encodings
    difficultString = ''.join([chr(i) for i in range(1, 128)])
    f = Foo()
    f.setMax500(difficultString)
    store.addObject(f)
    store.saveChanges()
    serialNum = f.serialNum()
    store.clear()
    result = store.fetchObject(Foo, serialNum)
    assert result.max500() == difficultString