class FullDictionaryTest(DictionaryTest): """Base class for testing a full database instance.""" def setUp(self): DictionaryTest.setUp(self) # build dictionary dataPath = [util.getDataPath(), os.path.join('.', 'test'), os.path.join('.', 'test', 'downloads')] self.builder = DatabaseBuilder(quiet=True, dbConnectInst=self.db, dataPath=dataPath, rebuildExisting=True, noFail=False) self.builder.build(self.DICTIONARY) self.dictionary = self.dictionaryClass(dbConnectInst=self.db) def tearDown(self): self.builder.remove(self.DICTIONARY)
def setUp(self): DictionaryTest.setUp(self) # build dictionary dataPath = [ util.getDataPath(), os.path.join('.', 'test'), os.path.join('.', 'test', 'downloads') ] self.builder = DatabaseBuilder(quiet=True, dbConnectInst=self.db, dataPath=dataPath, rebuildExisting=True, noFail=False) self.builder.build(self.DICTIONARY) self.dictionary = self.dictionaryClass(dbConnectInst=self.db)
def setUp(self): DictionaryTest.setUp(self) builderClasses = DatabaseBuilder.getTableBuilderClasses(quiet=True) dictionaryBuilder = [cls for cls in builderClasses if cls.PROVIDES == self.table][0] contentBuilder = types.ClassType("SimpleDictBuilder", (DictionaryResultTest._ContentGenerator, dictionaryBuilder), {'content': self.INSTALL_CONTENT}) self.builder = DatabaseBuilder(quiet=True, dbConnectInst=self.db, additionalBuilders=[contentBuilder], prefer=["SimpleDictBuilder"], rebuildExisting=True, noFail=False) self.builder.build(self.DICTIONARY) assert self.db.mainHasTable(self.DICTIONARY) self.dictionary = self.dictionaryClass(dbConnectInst=self.db, **self.DICTIONARY_OPTIONS)
def setUp(self): DictionaryTest.setUp(self) # build dictionary dataPath = [util.getDataPath(), os.path.join('.', 'test'), os.path.join('.', 'test', 'downloads')] self.builder = DatabaseBuilder(quiet=True, dbConnectInst=self.db, dataPath=dataPath, rebuildExisting=True, noFail=False) self.builder.build(self.DICTIONARY) self.dictionary = self.dictionaryClass(dbConnectInst=self.db)
class FullDictionaryTest(DictionaryTest): """Base class for testing a full database instance.""" def setUp(self): DictionaryTest.setUp(self) # build dictionary dataPath = [ util.getDataPath(), os.path.join('.', 'test'), os.path.join('.', 'test', 'downloads') ] self.builder = DatabaseBuilder(quiet=True, dbConnectInst=self.db, dataPath=dataPath, rebuildExisting=True, noFail=False) self.builder.build(self.DICTIONARY) self.dictionary = self.dictionaryClass(dbConnectInst=self.db) def tearDown(self): self.builder.remove(self.DICTIONARY)
def setUp(self): DictionaryTest.setUp(self) builderClasses = DatabaseBuilder.getTableBuilderClasses(quiet=True) dictionaryBuilder = [ cls for cls in builderClasses if cls.PROVIDES == self.table ][0] contentBuilder = new.classobj( "SimpleDictBuilder", (DictionaryResultTest._ContentGenerator, dictionaryBuilder), {'content': self.INSTALL_CONTENT}) self.builder = DatabaseBuilder(quiet=True, dbConnectInst=self.db, additionalBuilders=[contentBuilder], prefer=["SimpleDictBuilder"], rebuildExisting=True, noFail=False) self.builder.build(self.DICTIONARY) assert self.db.mainHasTable(self.DICTIONARY) self.dictionary = self.dictionaryClass(dbConnectInst=self.db, **self.DICTIONARY_OPTIONS)
def setUp(self): prefer = [self.BUILDER] prefer.extend(self.PREFER_BUILDERS) self.dataPath = self.EXTERNAL_DATA_PATHS[:] self.dataPath.append(util.getDataPath()) self.dataPath.append(os.path.join('.', 'test')) self.dataPath.append(os.path.join('.', 'test', 'downloads')) self.dbInstances = {} for databasePath in self.DATABASES: self.dbInstances[databasePath] = DatabaseBuilder(quiet=True, databaseUrl=databasePath, dataPath=self.dataPath, prefer=prefer, rebuildExisting=True, noFail=False) # make sure we don't get a production db instance tables = self.dbInstances[databasePath].db.engine.table_names() assert len(tables) == 0, "Database is not empty: '%s'" \ % "', '".join(tables)
BUILDER = builder.CEDICTBuilder OPTIONS = [{'enableFTS3': False}, {'filePath': './test/downloads/CEDICT', 'fileType': '.gz'}] class CEDICTGRBuilderTest(TableBuilderTest, unittest.TestCase): BUILDER = builder.CEDICTGRBuilder OPTIONS = [{'enableFTS3': False}, {'filePath': './test/downloads/CEDICTGR', 'fileType': '.zip'}] class HanDeDictBuilderTest(TableBuilderTest, unittest.TestCase): BUILDER = builder.HanDeDictBuilder OPTIONS = [{'enableFTS3': False}, {'filePath': './test/downloads/HanDeDict', 'fileType': '.tar.bz2'}] class CFDICTBuilderTest(TableBuilderTest, unittest.TestCase): BUILDER = builder.CFDICTBuilder OPTIONS = [{'enableFTS3': False}, {'filePath': './test/downloads/CFDICT', 'fileType': '.zip'}] # Generate default test classes for TableBuilder without special definitions for builderClass in DatabaseBuilder.getTableBuilderClasses( resolveConflicts=False): testClassName = '%sTest' % builderClass.__name__ if testClassName not in globals(): globals()[testClassName] = types.ClassType(testClassName, (TableBuilderTest, unittest.TestCase), {'BUILDER': builderClass}) del testClassName
class DictionaryResultTest(DictionaryTest): """Base class for testing of dictionary return values.""" INSTALL_CONTENT = None """ Content the dictionary mock will include. Should follow the dictionary's actual format. """ ACCESS_RESULTS = [] """List of query/result tuples.""" DICTIONARY_OPTIONS = {} """Options for the dictionary instance passed when constructing object.""" class _ContentGenerator(object): def getGenerator(self): for line in self.content: yield line def setUp(self): DictionaryTest.setUp(self) builderClasses = DatabaseBuilder.getTableBuilderClasses(quiet=True) dictionaryBuilder = [ cls for cls in builderClasses if cls.PROVIDES == self.table ][0] contentBuilder = new.classobj( "SimpleDictBuilder", (DictionaryResultTest._ContentGenerator, dictionaryBuilder), {'content': self.INSTALL_CONTENT}) self.builder = DatabaseBuilder(quiet=True, dbConnectInst=self.db, additionalBuilders=[contentBuilder], prefer=["SimpleDictBuilder"], rebuildExisting=True, noFail=False) self.builder.build(self.DICTIONARY) assert self.db.mainHasTable(self.DICTIONARY) self.dictionary = self.dictionaryClass(dbConnectInst=self.db, **self.DICTIONARY_OPTIONS) def tearDown(self): self.builder.remove(self.DICTIONARY) assert not self.db.mainHasTable(self.DICTIONARY) @util.cachedproperty def resultIndexMap(self): content = self.INSTALL_CONTENT[:] content = map(list, content) for strategy in self.dictionary._formatStrategies: content = map(strategy.format, content) content = map(tuple, content) return dict((row, idx) for idx, row in enumerate(content)) def testResults(self): """Test results for access methods ``getFor...``.""" def resultPrettyPrint(indices): return '[' + "\n".join( repr(self.INSTALL_CONTENT[index]) for index in sorted(indices)) + ']' for methodName, options, requests in self.ACCESS_RESULTS: options = dict(options) or {} method = getattr(self.dictionary, methodName) for request, targetResultIndices in requests: results = method(request, **options) resultIndices = [ self.resultIndexMap[tuple(e)] for e in results ] self.assertEquals( set(resultIndices), set(targetResultIndices), ("Mismatch for method %s and string %s (options %s)\n" % (repr(methodName), repr(request), repr(options)) + "Should be\n%s\nbut is\n%s\n" % (resultPrettyPrint(targetResultIndices), resultPrettyPrint(resultIndices))))
class DictionaryResultTest(DictionaryTest): """Base class for testing of dictionary return values.""" INSTALL_CONTENT = None """ Content the dictionary mock will include. Should follow the dictionary's actual format. """ ACCESS_RESULTS = [] """List of query/result tuples.""" DICTIONARY_OPTIONS = {} """Options for the dictionary instance passed when constructing object.""" class _ContentGenerator(object): def getGenerator(self): for line in self.content: yield line def setUp(self): DictionaryTest.setUp(self) builderClasses = DatabaseBuilder.getTableBuilderClasses(quiet=True) dictionaryBuilder = [cls for cls in builderClasses if cls.PROVIDES == self.table][0] contentBuilder = types.ClassType("SimpleDictBuilder", (DictionaryResultTest._ContentGenerator, dictionaryBuilder), {'content': self.INSTALL_CONTENT}) self.builder = DatabaseBuilder(quiet=True, dbConnectInst=self.db, additionalBuilders=[contentBuilder], prefer=["SimpleDictBuilder"], rebuildExisting=True, noFail=False) self.builder.build(self.DICTIONARY) assert self.db.mainHasTable(self.DICTIONARY) self.dictionary = self.dictionaryClass(dbConnectInst=self.db, **self.DICTIONARY_OPTIONS) def tearDown(self): self.builder.remove(self.DICTIONARY) assert not self.db.mainHasTable(self.DICTIONARY) @util.cachedproperty def resultIndexMap(self): content = self.INSTALL_CONTENT[:] content = map(list, content) for strategy in self.dictionary._formatStrategies: content = map(strategy.format, content) content = map(tuple, content) return dict((row, idx) for idx, row in enumerate(content)) def testResults(self): """Test results for access methods ``getFor...``.""" def resultPrettyPrint(indices): return '[' + "\n".join(repr(self.INSTALL_CONTENT[index]) for index in sorted(indices)) + ']' for methodName, options, requests in self.ACCESS_RESULTS: options = dict(options) or {} method = getattr(self.dictionary, methodName) for request, targetResultIndices in requests: results = method(request, **options) resultIndices = [self.resultIndexMap[tuple(e)] for e in results] self.assertEquals(set(resultIndices), set(targetResultIndices), ("Mismatch for method %s and string %s (options %s)\n" % (repr(methodName), repr(request), repr(options)) + "Should be\n%s\nbut is\n%s\n" % (resultPrettyPrint(targetResultIndices), resultPrettyPrint(resultIndices))))