Exemplo n.º 1
0
    def test_runner_execution(self, mocker, response, cnpj_batch):
        """Tests runner execution.

        The runner keeps trying to get the data when a timeout or any other
        exceptions occurs. We will issue a list of CNPJs to handle and make
        it return an error, a timeout and success for each of them.
        """

        # The first batch: all Timeout errors.
        # The second batch: all generic errors.
        # The third batch: one error, all other success.
        # The fourth batch: the last success.
        returns = []

        # First and second
        returns.extend([Timeout() for cnpj in cnpj_batch])
        returns.extend([Exception() for cnpj in cnpj_batch])

        # Third and fourth
        returns.extend([Timeout()])
        returns.extend([response(cnpj) for cnpj in cnpj_batch[1:]])
        returns.extend([response(cnpj_batch[0])])

        mocker.patch("requests.get", side_effect=returns)

        # Execute
        runner = Runner(cnpj_batch)
        data = runner.run()

        # Check results
        for cnpj in cnpj_batch:
            assert cnpj in data
            assert data[cnpj] == json.loads(response(cnpj).content, encoding="utf-8")
Exemplo n.º 2
0
    def run(self):
        """Reads data from CNPJ list and write results to output directory."""
        self._assure_output_dir(self.output)
        companies = self.read()
        print '%s CNPJs found' % len(companies)

        pbar = ProgressBar(
            widgets=[Counter(), ' ',
                     Percentage(), ' ',
                     Bar(), ' ',
                     Timer()],
            maxval=len(companies)).start()

        resolved = 0
        runner = Runner(companies, self.days, self.token)

        try:
            for data in runner:
                self.write(data)
                resolved = resolved + 1
                pbar.update(resolved)
        except KeyboardInterrupt:
            print '\naborted: waiting current requests to finish.'
            runner.stop()
            return

        pbar.finish()
Exemplo n.º 3
0
    def test_runner_execution(self, mocker, response, cnpj_batch):
        """Tests runner execution.

        The runner keeps trying to get the data when a timeout or any other
        exceptions occurs. We will issue a list of CNPJs to handle and make
        it return an error, a timeout and success for each of them.
        """

        # The first batch: all Timeout errors.
        # The second batch: all generic errors.
        # The third batch: one error, all other success.
        # The fourth batch: the last success.
        returns = []

        # First and second
        returns.extend([Timeout() for cnpj in cnpj_batch])
        returns.extend([Exception() for cnpj in cnpj_batch])

        # Third and fourth
        returns.extend([Timeout()])
        returns.extend([response(cnpj) for cnpj in cnpj_batch[1:]])
        returns.extend([response(cnpj_batch[0])])

        mocker.patch('requests.get', side_effect=returns)

        # Execute
        runner = Runner(cnpj_batch)
        data = runner.run()

        # Check results
        for cnpj in cnpj_batch:
            assert cnpj in data
            assert data[cnpj] == json.loads(response(cnpj).content,
                                            encoding='utf-8')
Exemplo n.º 4
0
    def run(self):
        """Reads data from CNPJ list and write results to output directory."""
        self._assure_output_dir(self.output)
        companies = self.read()
        print '%s CNPJs found' % len(companies)

        pbar = ProgressBar(
            widgets=[Counter(), ' ', Percentage(), ' ', Bar(), ' ', Timer()],
            maxval=len(companies)).start()

        resolved = 0
        runner = Runner(companies, self.days, self.token)

        try:
            for data in runner:
                self.write(data)
                resolved = resolved + 1
                pbar.update(resolved)
        except KeyboardInterrupt:
            print '\naborted: waiting current requests to finish.'
            runner.stop()
            return

        pbar.finish()
Exemplo n.º 5
0
 def run(self):
     """Reads data from CNPJ list and write results to output directory."""
     runner = Runner(self.read())
     self.write(runner.run())
Exemplo n.º 6
0
 def run(self):
     """Reads data from CNPJ list and write results to output directory."""
     runner = Runner(self.read())
     self.write(runner.run())