Beispiel #1
0
class BOWBuilderTest(unittest.TestCase): 
    
    def setUp(self):
        self.parserInstance=TextLinklistParserMock()
        self.outputQueue=Queue()
        outputCondition=Condition()
        self.bOWBuilderSimple=BOWBuilderSimple(None,self.outputQueue,None,outputCondition,parserInstance=self.parserInstance)
        self.bOWBuilderSimple.settings=SettingsDummy()
        self.bOWBuilderSimple.debug=DebugMock()
    
    def tearDown(self):
        del self.bOWBuilderSimple
        del self.parserInstance

    def test_BOWBuilderSimple_consume(self):
        """ BOWBuilderSimple.consume() returns an instance of a SmartURL. """
        result=self.bOWBuilderSimple.consume()
        self.assertTrue(isinstance(result,SmartURL))

    def test_BOWBuilderSimple_verify_incorrect_input(self):
        """ BOWBuilderSimple.verifySemiProduct() doesn't accept simple string or another wrong input. """
        self.assertFalse(self.bOWBuilderSimple.verifySemiProduct("http://www.example.com/page88.html"))
        self.assertFalse(self.bOWBuilderSimple.verifySemiProduct(None))
        self.assertFalse(self.bOWBuilderSimple.verifySemiProduct('c'))
        self.assertFalse(self.bOWBuilderSimple.verifySemiProduct(88))
        
    def test_BOWBuilderSimple_verify_correct_input(self):
        """ BOWBuilderSimple.verifySemiProduct() returns true if called with a SmartURL instance."""
        url=SmartURL("http://www.example.com/page88.html")
        self.assertTrue(self.bOWBuilderSimple.verifySemiProduct(url))
    
    def test_BOWBuilderSimple_produce_data(self):
        """ Into counters goes the result of parser.words() """
        url=SmartURL("http://www.kosek.cz/clanky/kosek/swn-xml/ar02s17.html")
        expectation={"kosek":2,"cz":1,"clanky":1,"swn":1,"xml":1,"ar02s17":1}
        # 026 Tests also that "http:","","www","html" are left out.
        # 026 Tests also that kosek is count twice.
        self.bOWBuilderSimple.produce(url)
        result=self.bOWBuilderSimple.counters.counters
        self.assertDictEqual(result,expectation)
        
        
    def test_BOWBuilderSimple_produce_incremental(self):
        """ BOWBuilderSimple.produce() is incrementally stuffed with data, while at some places are performed asserts. """
        while self.parserInstance.cx<205:
            semiProduct = self.bOWBuilderSimple.consume()
            if self.parserInstance.cx >= 204: self.parserInstance._linklistEOF=True
            if self.bOWBuilderSimple.verifySemiProduct(semiProduct):
                # 026 Verification requires due to async in incrementing.) 
                self.bOWBuilderSimple.produce(semiProduct)
            if self.parserInstance.cx == 73: self.assertEqual(self.bOWBuilderSimple.counters.NumberOfCounters, 73+1)
            # 027 The formula 73+1 means 73 dynamicly changed counters plus one static of value 73.
            if self.parserInstance.cx == 99: self.assertTrue(self.outputQueue.empty())
            if self.parserInstance.cx == 101: self.assertFalse(self.outputQueue.empty())
            if self.parserInstance.cx == 101: self.assertEqual(self.bOWBuilderSimple.counters.NumberOfCounters, 1+1)
            if self.parserInstance.cx == 173: self.assertEqual(self.bOWBuilderSimple.counters.NumberOfCounters, 73+1)
            if self.parserInstance.cx == 199: self.assertEqual(self.outputQueue.qsize(),1)
            if self.parserInstance.cx == 201: self.assertEqual(self.outputQueue.qsize(),2)
            if self.parserInstance.cx == 201: self.assertEqual(self.bOWBuilderSimple.counters.NumberOfCounters, 1+1)
            if self.parserInstance.cx == 204: self.assertEqual(self.outputQueue.qsize(),3)
        # Test if item in queue is correct:
        item=self.outputQueue.get()
        self.assertTrue(isinstance(item,CounterManager))
        self.assertEqual(item.title, "word")
    
    def test_BOWBuilderSimple_initializeCounters(self):
        """ initializeCounters just add attribute counters into BOWBuilderSimple (which is of type CounterManager). """
        # 027 The problem to test this is, that is already added BOWBuilderSimple in init().
        self.bOWBuilderSimple.counters=None
        self.bOWBuilderSimple.initializeCounters()
        self.assertIsInstance(self.bOWBuilderSimple.counters, CounterManager)
        
    def test_BOWBuilderSimple_finalize(self):
        """ Finalize just puts actual counters into output queue without respect how many has accumulated. """
        outputQueueSizeInitial=self.outputQueue.qsize()
        self.bOWBuilderSimple.finalize()
        self.assertEqual(outputQueueSizeInitial+1, self.outputQueue.qsize())