def test_distant_stats(self):

        os.mkdir(mirror)
        url = 'http://example.com/mirror/daily/2008-11-18.bz2'
        stats = ApacheDistantLocalStats(mirror)

        self.assertEquals(list(stats.read_stats(url)), [])

        # let's build the stats
        local_stats = ApacheLocalStats()
        local_stats.build_monthly_stats(2008,
                                        11,
                                        log_sample,
                                        bz2_file,
                                        compression='bz2')

        # now patching url so it return the built stats
        import urllib2
        old_open = urllib2.urlopen

        def _open(url):
            class FakeUrl(object):
                def read(self):
                    return open(bz2_file).read()

            return FakeUrl()

        urllib2.urlopen = _open

        read = stats.read_stats(url)
        first_entry = read.next()

        self.assertEquals(first_entry['count'], '1')
        self.assertEquals(first_entry['packagename'], 'appwsgi')

        # checking that the cache is filled
        self.assert_('2008-11-18.bz2' in os.listdir(mirror))

        # removing the urlopen patch
        urllib2.urlopen = old_open

        # the cache should be activated now
        read = stats.read_stats(url)
        first_entry = read.next()
        self.assertEquals(first_entry['count'], '1')
        self.assertEquals(first_entry['packagename'], 'appwsgi')
    def test_distant_stats(self):

        os.mkdir(mirror)
        url = 'http://example.com/mirror/daily/2008-11-18.bz2'
        stats = ApacheDistantLocalStats(mirror)
        
        self.assertEquals(list(stats.read_stats(url)), [])
       
        # let's build the stats
        local_stats = ApacheLocalStats()    
        local_stats.build_monthly_stats(2008, 11, log_sample, 
                                  bz2_file, compression='bz2') 
        
        # now patching url so it return the built stats
        import urllib2
        old_open = urllib2.urlopen
        def _open(url):
            class FakeUrl(object):
                def read(self):
                    return open(bz2_file).read()
            return FakeUrl()
        urllib2.urlopen = _open

        read = stats.read_stats(url)
        first_entry = read.next()

        self.assertEquals(first_entry['count'], '1')
        self.assertEquals(first_entry['packagename'], 'appwsgi')
        
        # checking that the cache is filled
        self.assert_('2008-11-18.bz2' in os.listdir(mirror))

        # removing the urlopen patch
        urllib2.urlopen = old_open
       
        # the cache should be activated now
        read = stats.read_stats(url) 
        first_entry = read.next()
        self.assertEquals(first_entry['count'], '1')
        self.assertEquals(first_entry['packagename'], 'appwsgi')
 def read_distant_stats(mirror, filename):
     mirror_domain = urlparse.urlparse(mirror[0])[1]
     mirror_domain = os.path.join(mirrors, mirror_domain)
     distant_reader = ApacheDistantLocalStats(mirror_domain)
     stat_file_url = '%s/%s/%s' % (mirror[0], mirror[3], filename)
     return distant_reader.read_stats(stat_file_url)