Test.py :  » Web-Frameworks » Webware » Webware-1.0.2 » UserKit » 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 » Web Frameworks » Webware 
Webware » Webware 1.0.2 » UserKit » Tests » Test.py
"""
Tests various functions of Users and Roles

To run these tests:
  cd Webware
  python AllTests.py UserKit.Tests.Test
"""

import os
import unittest

import UserKit

TEST_CODE_DIR = os.path.dirname(__file__) # e.g. ".../Webware/UserKit/Tests"


class BasicRoleTest(unittest.TestCase):

  def roleClasses(self):
    """Return a list of all Role classes for testing."""
    from UserKit.Role import Role
    from UserKit.HierRole import HierRole
    return [Role, HierRole]

  def testA_RoleBasics(self):
    """Invoke testRole() with each class returned by roleClasses."""
    for roleClass in self.roleClasses():
      self.checkRoleClass(roleClass)

  def checkRoleClass(self, roleClass):
    role = roleClass('foo', 'bar')
    assert role.name() == 'foo'
    assert role.description() == 'bar'
    assert str(role) == 'foo'

    role.setName('x')
    assert role.name() == 'x'

    role.setDescription('y')
    assert role.description() == 'y'

    assert role.playsRole(role)


class HierRoleTest(unittest.TestCase):

  def testHierRole(self):
    from UserKit.HierRole import HierRole
    animal    = hr('animal')
    eggLayer  = hr('eggLayer', None, [animal])
    furry     = hr('furry', None, [animal])
    snake     = hr('snake', None, [eggLayer])
    dog       = hr('dog', None, [furry])
    platypus  = hr('platypus', None, [eggLayer, furry])
    vegetable = hr('vegetable')

    roles = locals()
    del roles['hr']
    del roles['self']

    # The tests below are one per line.
    # The first word is the role name.
    # The rest of the words are all the roles it plays
    # (besides itself).
    tests = '''\
      eggLayer, animal
      furry, animal
      snake, eggLayer, animal
      dog, furry, animal
      platypus, eggLayer, furry, animal'''

    tests = tests.split('\n')
    tests = [test.split(', ') for test in tests]

    # Strip names
    # Can we use a compounded/nested list comprehension for this?
    oldTest = tests
    tests = []
    for test in oldTest:
      test = [name.strip() for name in test]
      tests.append(test)

    # Now let's actually do some testing...
    for test in tests:
      role = roles[test[0]]
      assert role.playsRole(role)

      # Test that the role plays all the roles listed
      for name in test[1:]:
        playsRole = roles[name]
        assert role.playsRole(playsRole)

      # Now test that the role does NOT play any of the other
      # roles not listed
      otherRoles = roles.copy()
      for name in test:
        del otherRoles[name]
      for name in otherRoles.keys():
        assert not role.playsRole(roles[name])
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.