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)
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)
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))))