Package couchdbkit :: Package ext :: Package pylons :: Module test
[hide private]
[frames] | no frames]

Source Code for Module couchdbkit.ext.pylons.test

 1  # -*- coding: utf-8 - 
 2  # 
 3  # This file is part of couchdbkit released under the MIT license.  
 4  # See the NOTICE for more information. 
 5   
 6  from __future__ import with_statement 
 7   
 8  import os 
 9  import unittest 
10   
11  from ... import BaseDocsLoader, ResourceNotFound 
12  from .db import init_db, sync_design, default_design_path 
13  from ...utils import json 
14   
15 -class FixtureLoader(BaseDocsLoader):
16 - def __init__(self, directory):
17 self._directory = directory
18
19 - def get_docs(self):
20 docs = [] 21 for fixture in os.listdir(self._directory): 22 fixture_path = os.path.join(self._directory, fixture) 23 if not os.path.isfile(fixture_path): 24 raise Exception("Fixture path %s not found" % fixture_path) 25 with open(fixture_path, "r") as fp: 26 for doc in json.load(fp): 27 docs.append(doc) 28 return docs
29
30 -class TestCase(unittest.TestCase):
31 """ 32 Basic test class that will be default load all fixtures specified in the 33 fixtures attribute. 34 """
35 - def __init__(self, *args, **kwargs):
36 self._config = kwargs['config'] 37 del kwargs['config'] 38 unittest.TestCase.__init__(self, *args, **kwargs)
39
40 - def setUp(self):
41 dbname = self._config['couchdb.db'].dbname 42 43 # Set the directory to the fixtures. 44 try: 45 self._config['couchdb.db'].server.delete_db(dbname) 46 except ResourceNotFound: 47 pass 48 49 self._config['couchdb.db'] = init_db(self._config['couchdb.uri'], dbname) 50 sync_design(self._config['couchdb.db'], default_design_path(self._config)) 51 52 if hasattr(self, 'fixtures'): 53 fixtures_dir = self._config['couchdb.fixtures'] 54 if not os.path.isdir(fixtures_dir): 55 raise Exception("Fixtures dir %s not found" % fixtures_dir) 56 FixtureLoader(fixtures_dir).sync(self._config['couchdb.db'])
57