[r5631]: projects / tgAuthorization / trunk / tests / databasesetup.py Maximize Restore History

Download this file

databasesetup.py    112 lines (85 with data), 3.0 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright (c) 2007, Agendaless Consulting and Contributors.
# Copyright (c) 2008, Florent Aide <florent.aide@gmail.com> and
# Gustavo Narea <me@gustavonarea.net>
# All Rights Reserved.
#
# This software is subject to the provisions of the BSD-like license at
# http://www.repoze.org/LICENSE.txt. A copy of the license should accompany
# this distribution. THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL
# EXPRESS OR IMPLIED WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND
# FITNESS FOR A PARTICULAR PURPOSE
#
##############################################################################
"""Stuff required to setup the test database."""
import os
import sha
from sqlalchemy import *
from sqlalchemy.orm import *
from cStringIO import StringIO
from cgi import FieldStorage
from fixture.model import init_model, DBSession, metadata, Permission, \
Group, User
engine = create_engine(os.environ.get('DBURL', 'sqlite://'))
def setup_database():
init_model(engine)
teardownDatabase()
metadata.create_all(engine)
# Creating permissions
see_site = Permission()
see_site.permission_name = u'see-site'
DBSession.save(see_site)
edit_site = Permission()
edit_site.permission_name = u'edit-site'
DBSession.save(edit_site)
commit = Permission()
commit.permission_name = u'commit'
DBSession.save(commit)
# Creating groups
admins = Group(u'admins')
admins.permissions.append(edit_site)
DBSession.save(admins)
developers = Group(u'developers')
developers.permissions = [commit, edit_site]
DBSession.save(developers)
trolls = Group(u'trolls')
trolls.permissions.append(see_site)
DBSession.save(trolls)
# Plus a couple of groups with no permissions
php = Group(u'php')
DBSession.save(php)
python = Group(u'python')
DBSession.save(python)
# Creating users
user = User()
user.user_name = u'rms'
user.password = u'freedom'
user.groups.append(admins)
user.groups.append(developers)
DBSession.save(user)
user = User()
user.user_name = u'linus'
user.password = u'linux'
user.groups.append(developers)
DBSession.save(user)
user = User()
user.user_name = u'sballmer'
user.password = u'developers'
user.groups.append(trolls)
DBSession.save(user)
# Plus a couple of users without groups
user = User()
user.user_name = u'guido'
user.password = u'phytonic'
DBSession.save(user)
user = User()
user.user_name = u'rasmus'
user.password = u'php'
DBSession.save(user)
DBSession.commit()
def teardownDatabase():
DBSession.rollback()
metadata.drop_all(engine)