def test_causas_acidentes_ano(self):
        """
        Tests if method causas_acidentes_ano is correctly instanced.

        @brief Local variables
            causas_acidentes_list -
                Receives a list containing the causes of accidents and their
                number per year.
            anos -
                Receives a list of years to be tested.
            descricao_causas_acidentes_ano -
                Receives a list with a better description to the cause of the
                accidents per year.
        """

        self.dao = CausasAcidentesDAO()
        causas_acidentes_ano_list = self.dao.causas_acidentes_ano()

        anos = causas_acidentes_ano_list[0].ano_list
        self.assertEquals([
            2007,
            2008,
            2009,
            2010,
            2011,
            2012,
            2013
        ], anos)

        descricao_causas_acidentes_ano = []
        for acidente in causas_acidentes_ano_list:
            descricao_causas_acidentes_ano.append(acidente.causa)

        self.assertIn("Outras", descricao_causas_acidentes_ano)
def causas_acidentes(request):
    """ Return the render of the page with statistics of causes of accidents.
    @param request context request from view
    @return HTML page with statistics of causes of accidents.
    """

    try:
        # DAO object of causes
        causes_dao = CausasAcidentesDAO()
        # List of causes
        causes_list = causes_dao.causas_acidentes()
        # List of years by causes
        causes_year_list = causes_dao.causas_acidentes_ano()
        # List of statistics by causes
        probability_causes_list = causes_dao.probabilidade_causas_acidentes()
        # List of means
        average_deviation_list = causes_dao.media_desvio_causas_acidentes()

    except (MySQLdb.Error, ResultadoConsultaNuloError):
        # logger.error(str(e))
        erro = "Ocorreu um erro no sistema, tente novamente mais tarde!"
        response = render_to_response(
            "index.html", {
                'erro': erro
            }, context_instance=RequestContext(request)
        )

        return response

    response = render_to_response(
        "causas_acidentes.html", {
            'causas_acidentes_list': causes_list,
            'causas_acidentes_ano_list': causes_year_list,
            'anos': causes_year_list[0].ano_list,
            'probabilidade_causas_acidentes_list': probability_causes_list,
            'causas': causes_list,
            'media_desvio_causas_acidentes_list': average_deviation_list,
        }, context_instance=RequestContext(request)
    )

    return response
    def test_probabilidade_causas_acidentes(self):
        """
        Tests if method probabilidade_causas_acidentes is correctly
        calculationg the probability.

        @brief Local variable
            probabilidade_causas_list -
                Receives a list with the accidents causes.
        """

        self.dao = CausasAcidentesDAO()
        probabilidade_causas_list = self.dao.probabilidade_causas_acidentes()

        for causa in probabilidade_causas_list:
            for probabilidade in causa.probabilidade_por_limite_list:
                self.assertTrue(probabilidade >= 0 and probabilidade <= 100)
    def test_media_desvio_causas_acidentes(self):
        """
        Tests if method media_desvio_causas_acidentes is correctly
        calculationg the standard deviation.

        @brief Local variable
            media_desvio_list -
                Receives a list with the media and standard deviation of
                causes.
        """

        self.dao = CausasAcidentesDAO()
        media_desvio_list = self.dao.media_desvio_causas_acidentes()

        for media_desvio_causas_acidentes in media_desvio_list:
            self.assertTrue(media_desvio_causas_acidentes.media > 0)
            self.assertTrue(media_desvio_causas_acidentes.desvio > 0)
    def test_causas_acidentes(self):
        """
        Tests if method causas_acidentes is correctly instanced.

        @brief Local variables
            causas_acidentes_list -
                Receives a list with the causes of accidents.
            descricao_causas_acidentes -
                Receives a list with a better description to the cause of the
                accident.
        """

        self.dao = CausasAcidentesDAO()
        causas_acidentes_list = self.dao.causas_acidentes()

        descricao_causas_acidentes = []
        for acidente in causas_acidentes_list:
            descricao_causas_acidentes.append(acidente.causa)

        self.assertIn("Outras", descricao_causas_acidentes)
class TestCausasAcidentes(DAO_Tests):

    """docstring for TestUF
    Tests for the class causa_acidente_dao.
    """

    def test_causas_acidentes(self):
        """
        Tests if method causas_acidentes is correctly instanced.

        @brief Local variables
            causas_acidentes_list -
                Receives a list with the causes of accidents.
            descricao_causas_acidentes -
                Receives a list with a better description to the cause of the
                accident.
        """

        self.dao = CausasAcidentesDAO()
        causas_acidentes_list = self.dao.causas_acidentes()

        descricao_causas_acidentes = []
        for acidente in causas_acidentes_list:
            descricao_causas_acidentes.append(acidente.causa)

        self.assertIn("Outras", descricao_causas_acidentes)

    def test_causas_acidentes_ano(self):
        """
        Tests if method causas_acidentes_ano is correctly instanced.

        @brief Local variables
            causas_acidentes_list -
                Receives a list containing the causes of accidents and their
                number per year.
            anos -
                Receives a list of years to be tested.
            descricao_causas_acidentes_ano -
                Receives a list with a better description to the cause of the
                accidents per year.
        """

        self.dao = CausasAcidentesDAO()
        causas_acidentes_ano_list = self.dao.causas_acidentes_ano()

        anos = causas_acidentes_ano_list[0].ano_list
        self.assertEquals([
            2007,
            2008,
            2009,
            2010,
            2011,
            2012,
            2013
        ], anos)

        descricao_causas_acidentes_ano = []
        for acidente in causas_acidentes_ano_list:
            descricao_causas_acidentes_ano.append(acidente.causa)

        self.assertIn("Outras", descricao_causas_acidentes_ano)

    def test_probabilidade_causas_acidentes(self):
        """
        Tests if method probabilidade_causas_acidentes is correctly
        calculationg the probability.

        @brief Local variable
            probabilidade_causas_list -
                Receives a list with the accidents causes.
        """

        self.dao = CausasAcidentesDAO()
        probabilidade_causas_list = self.dao.probabilidade_causas_acidentes()

        for causa in probabilidade_causas_list:
            for probabilidade in causa.probabilidade_por_limite_list:
                self.assertTrue(probabilidade >= 0 and probabilidade <= 100)

    def test_media_desvio_causas_acidentes(self):
        """
        Tests if method media_desvio_causas_acidentes is correctly
        calculationg the standard deviation.

        @brief Local variable
            media_desvio_list -
                Receives a list with the media and standard deviation of
                causes.
        """

        self.dao = CausasAcidentesDAO()
        media_desvio_list = self.dao.media_desvio_causas_acidentes()

        for media_desvio_causas_acidentes in media_desvio_list:
            self.assertTrue(media_desvio_causas_acidentes.media > 0)
            self.assertTrue(media_desvio_causas_acidentes.desvio > 0)