test_inheritance.py :  » Database » SQLObject » SQLObject-0.12.4 » sqlobject » inheritance » tests » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Database » SQLObject 
SQLObject » SQLObject 0.12.4 » sqlobject » inheritance » tests » test_inheritance.py
from py.test import raises
from sqlobject import *
from sqlobject.tests.dbtest import *
from sqlobject.inheritance import InheritableSQLObject

########################################
## Inheritance
########################################

class InheritablePerson(InheritableSQLObject):
    firstName = StringCol()
    lastName = StringCol(alternateID=True, length=255)

class Employee(InheritablePerson):
    _inheritable = False
    position = StringCol()

def setup():
    setupClass(InheritablePerson)
    setupClass(Employee)

    Employee(firstName='Project', lastName='Leader', position='Project leader')
    InheritablePerson(firstName='Oneof', lastName='Authors')

def test_creation_fail():
    setup()
    kwargs ={'firstName':'John', 'lastname':'Doe'}
    raises(TypeError, Employee, **kwargs)
    persons = InheritablePerson.select(InheritablePerson.q.firstName == 'John')
    assert persons.count() == 0

def test_inheritance():
    setup()

    persons = InheritablePerson.select() # all
    for person in persons:
        assert isinstance(person, InheritablePerson)
        if isinstance(person, Employee):
            assert not hasattr(person, "childName")
        else:
            assert hasattr(person, "childName")
            assert not person.childName

def test_inheritance_select():
    setup()

    persons = InheritablePerson.select(InheritablePerson.q.firstName <> None)
    assert persons.count() == 2

    persons = InheritablePerson.select(InheritablePerson.q.firstName == "phd")
    assert persons.count() == 0

    employees = Employee.select(Employee.q.firstName <> None)
    assert employees.count() == 1

    employees = Employee.select(Employee.q.firstName == "phd")
    assert employees.count() == 0

    employees = Employee.select(Employee.q.position <> None)
    assert employees.count() == 1

    persons = InheritablePerson.selectBy(firstName="Project")
    assert persons.count() == 1
    assert isinstance(persons[0], Employee)

    persons = Employee.selectBy(firstName="Project")
    assert persons.count() == 1

    try:
        person = InheritablePerson.byLastName("Oneof")
    except:
        pass
    else:
        raise RuntimeError, "unknown person %s" % person

    person = InheritablePerson.byLastName("Leader")
    assert person.firstName == "Project"

    person = Employee.byLastName("Leader")
    assert person.firstName == "Project"

    persons = list(InheritablePerson.select(orderBy=InheritablePerson.q.lastName))
    assert len(persons) == 2

    persons = list(InheritablePerson.select(orderBy=(InheritablePerson.q.lastName, InheritablePerson.q.firstName)))
    assert len(persons) == 2

    persons = list(Employee.select(orderBy=Employee.q.lastName))
    assert len(persons) == 1

    persons = list(Employee.select(orderBy=(Employee.q.lastName, Employee.q.firstName)))
    assert len(persons) == 1

    persons = list(Employee.select(orderBy=Employee.q.position))
    assert len(persons) == 1

    persons = list(Employee.select(orderBy=(Employee.q.position, Employee.q.lastName)))
    assert len(persons) == 1

def test_addDelColumn():
    setup()

    assert hasattr(InheritablePerson, "firstName")
    assert hasattr(Employee, "firstName")
    assert hasattr(InheritablePerson.q, "firstName")
    assert hasattr(Employee.q, "firstName")

    Employee.sqlmeta.addColumn(IntCol('runtime', default=None))

    assert not hasattr(InheritablePerson, 'runtime')
    assert hasattr(Employee, 'runtime')
    assert not hasattr(InheritablePerson.q, 'runtime')
    assert hasattr(Employee.q, 'runtime')

    InheritablePerson.sqlmeta.addColumn(IntCol('runtime2', default=None))

    assert hasattr(InheritablePerson, 'runtime2')
    assert hasattr(Employee, 'runtime2')
    assert hasattr(InheritablePerson.q, 'runtime2')
    assert hasattr(Employee.q, 'runtime2')

    Employee.sqlmeta.delColumn('runtime')

    assert not hasattr(InheritablePerson, 'runtime')
    assert not hasattr(Employee, 'runtime')
    assert not hasattr(InheritablePerson.q, 'runtime')
    assert not hasattr(Employee.q, 'runtime')

    InheritablePerson.sqlmeta.delColumn('runtime2')

    assert not hasattr(InheritablePerson, 'runtime2')
    assert not hasattr(Employee, 'runtime2')
    assert not hasattr(InheritablePerson.q, 'runtime2')
    assert not hasattr(Employee.q, 'runtime2')
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.