|
1 # Copyright (c) 2012 netzguerilla.net <iro@netzguerilla.net> |
|
2 # |
|
3 # This file is part of Iro. |
|
4 # |
|
5 # Permission is hereby granted, free of charge, to any person obtaining a copy of |
|
6 # this software and associated documentation files (the "Software"), to deal in |
|
7 # the Software without restriction, including without limitation the rights to use, |
|
8 # copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
|
9 # #Software, and to permit persons to whom the Software is furnished to do so, |
|
10 # subject to the following conditions: |
|
11 # |
|
12 # The above copyright notice and this permission notice shall be included in |
|
13 # all copies or substantial portions of the Software. |
|
14 # |
|
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
|
16 # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
|
17 # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
|
18 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
|
19 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
|
20 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
21 |
|
22 from twisted.trial import unittest |
|
23 |
|
24 from sqlalchemy import create_engine, pool |
|
25 from tempfile import mkdtemp |
|
26 |
|
27 from shutil import rmtree |
|
28 import atexit |
|
29 from ngdatabase.mysql import Server, createConfig, Database |
|
30 |
|
31 from iro.model import setEngine, setPool |
|
32 from iro.model.utils import WithSession |
|
33 from iro.model.schema import Base |
|
34 |
|
35 from iro.controller.pool import dbPool |
|
36 |
|
37 from .utils import DummyObserver |
|
38 |
|
39 class DBTestCase(unittest.TestCase): |
|
40 '''a TestCase with DB connection |
|
41 you have to set self.session manually in setUp''' |
|
42 def __init__(self,*args,**kwargs): |
|
43 unittest.TestCase.__init__(self,*args,**kwargs) |
|
44 self.engine = md.engine |
|
45 |
|
46 def setUp(self): |
|
47 md.setUp() |
|
48 self.log = DummyObserver() |
|
49 self.log.start() |
|
50 |
|
51 def tearDown(self): |
|
52 self.log.stop() |
|
53 self.__cleanDB() |
|
54 |
|
55 def session(self,autocommit=True): |
|
56 '''returns a WithSession''' |
|
57 return WithSession(self.engine,autocommit) |
|
58 |
|
59 def __cleanDB(self): |
|
60 '''cleaning database''' |
|
61 with self.session() as session: |
|
62 for table in reversed(Base.metadata.sorted_tables): |
|
63 session.execute(table.delete()) |
|
64 |
|
65 |
|
66 class SampleDatabase(Database): |
|
67 def createPassword(self): |
|
68 self.password="test" |
|
69 return self.password |
|
70 |
|
71 class ModuleData(object): |
|
72 def __init__(self): |
|
73 self.create() |
|
74 |
|
75 def close(self): |
|
76 if self.valid: |
|
77 self.server.stop() |
|
78 rmtree(self.tdir) |
|
79 self.valid= False |
|
80 |
|
81 def create(self): |
|
82 self.tdir = mkdtemp(prefix='iro-mysql-') |
|
83 self.server = Server('%s/my.cnf'%self.tdir) |
|
84 self.db = SampleDatabase("iro","test",'%s/my.cnf'%self.tdir) |
|
85 self.dburl = 'mysql://test:test@localhost/iro?unix_socket=%s/socket'%self.tdir |
|
86 self.engine = create_engine(self.dburl, |
|
87 poolclass = pool.SingletonThreadPool, pool_size=dbPool.maxthreads, )#echo=True) |
|
88 self.valid = False |
|
89 |
|
90 def setUp(self): |
|
91 if not self.valid: |
|
92 with open('%s/my.cnf'%self.tdir,'w') as cnf: |
|
93 cnf.write(createConfig(self.tdir)) |
|
94 self.server.create() |
|
95 self.server.start() |
|
96 self.db.create() |
|
97 Base.metadata.create_all(self.engine) |
|
98 setEngine(self.engine) |
|
99 setPool(dbPool) |
|
100 self.valid = True |
|
101 |
|
102 |
|
103 md=ModuleData() |
|
104 atexit.register(md.close) |