No tienes acceso a esta clase

隆Contin煤a aprendiendo! 脷nete y comienza a potenciar tu carrera

Aprende todo un fin de semana sin pagar una suscripci贸n 馃敟

Aprende todo un fin de semana sin pagar una suscripci贸n 馃敟

Reg铆strate

Comienza en:

4D
0H
23M
1S

Ajustando detalles en los tests para IndexView

7/17
Recursos

Aportes 44

Preguntas 7

Ordenar por:

驴Quieres ver m谩s aportes, preguntas y respuestas de la comunidad?

o inicia sesi贸n.

Simplemente verifica que el queryset sea igual a una lista vacia, se supone que no debe haber nada.

    def test_two_future_questions(self):
        future_question1 = crear_pregunta(30)
        future_question1 = crear_pregunta(40)
        
        response = self.client.get(reverse("encuestas:index"))
        
        self.assertQuerysetEqual(
            response.context["latest_question_list"],
            []
        )

Mi soluci贸n es con verificar que el Queryset siga vacio.

Soluci贸n al reto:

def test_two_future_questions(self):
	"""
	The question index page may not displayed multiple questions.
	"""
	create_question(question_text="Future question 1", days=30)
	create_question(question_text="Future question 2", days=40)
	response = self.client.get(reverse("polls:index"))
	
self.assertQuerysetEqual(response.context["latest_question_list"], [])
        

EL RETO

    def two_future_questions(self):
        """
        the quesions index page may display multiple questions
        """
        future_question1 = create_question("future question",days=30)
        future_question2 = create_question("future question",days=50)
        response = self.client.get(reverse("polls:index"))
        self.assertQuerysetEqual(response.context["latest_question_list"],[])

En un principio cuando hicimos test de preguntas quedaba algo confuso, pero despu茅s de esta clase, he comprendido como realizarlas

Con una pregunta del pasado y una del futuro solo debe haber un registro en latest_question_list

self.assertEquals(len(response.context["latest_question_list"]), 1)

Con dos preguntas del pasado debe haber dos registros en latest_question_list

self.assertEquals(len(response.context["latest_question_list"]), 2)

Reto:

def test_two_future_questions(self):
        """ The questions arent displayed in the index view"""
        future_question1 = create_question("Future Question 1", 25)
        future_question2 = create_question("Future Question 2", 15)
        response = self.client.get(reverse("polls:index"))
        self.assertContains(response, "No polls are available.")
        self.assertQuerysetEqual(
            response.context["latest_question_list"], [])

Al colocar ordered=False, no es necesario que las preguntas aparezcan en el mismo orden, solo basta con que aparezcan

self.assertQuerysetEqual(response.context["latest_question_list"] , [past_question2, past_question1],ordered=False)

My Solution

def test_two_future_questions(self):
        """if Multiple future questions exist, none must be displayed
        """
        create_question("future question1", 30)
        create_question("future question2", 40)
        response = self.client.get(reverse("polls:index"))
        self.assertQuerysetEqual(response.context["latest_question_list"], [])

Aqu铆 el text cuando dos preguntas en el futura son publicadas. 馃槂

<def test_two_future_questions(self):
        "if question with pub_date in the future are published, both will not be displayed on the index page."

        future_question1 = create_question(question_text="future question 1", days= 89)
        future_question2 = create_question(question_text="future question 2", days= 9)

        response = self.client.get(reverse("polls:home"))   
        self.assertQuerysetEqual(response.context["lasted_question_list"], [])
        self.assertContains(response, "No polls are available")> 

Mi solucion

def test_tqo_future_question(self):
        """not show future questions"""
        future_question1 = create_question("Pregunta del futuro 1", days=30)
        future_question2 = create_question("Pregunta del futuro 2", days=30)
        response = self.client.get(reverse("polls:index"))
        self.assertQuerysetEqual(
            response.context["latest_question_list"],
            []
        )
    def test_two_past_questionS(self):
        """
        The questions index page may display multiple questions.
        """
        question_set = []
        question_set.append(create_question(question_text="past question 1", days=-20))
        question_set.append(create_question(question_text="past question 2", days=-7))

        response = self.client.get(reverse("polls:index"))
        context = response.context['lates_question_list']

        self.assertQuerysetEqual(
            context,
            question_set
        )

    def test_two_future_questionS(self):
        """
        The questions index page may display multiple questions.
        """
        question_set = []
        question_set.append(create_question(question_text="future question 1", days=20))
        question_set.append(create_question(question_text="future question 2", days=7))

        response = self.client.get(reverse("polls:index"))
        context = response.context['lates_question_list']

        self.assertQuerysetEqual(
            context,
            []
        )

Mi aporte al c贸digo, verificando vi, que solo con que latess_question_list retorne una lista vacia est谩 bien, pero le agregu茅 que tenga la respuesta No p帽olls available

 def test_two_future_questions(self):
        """
        The questions index may display multiple questions
        """
        response = self.client.get(reverse("polls:index"))
        self.assertContains(response, "No polls are available.")
        self.assertQuerysetEqual(
            response.context["latest_question_list"], [])
        
    def test_two_future_question(self):
        """
        The question index page may display multiple question.
        """
        future_question1 = create_question(question_text='Future question 1', days=30)
        future_question2 = create_question(question_text='Future question 2', days=40)
        response =  self.client.get(reverse('polls:index'))
        self.assertQuerysetEqual(
            response.context['latest_question_list'],
            []
        )

Aqui dejo mi aporte al desafio:

def test_two_future_question(self):
        """ The questions index page may display multiple question"""
        future_question1 = create_question(question_text="Future question1", days= 30)
        future_question2 = create_question(question_text="Future question2", days= 40)
        response = self.client.get(reverse("polls:index"))
        self.assertQuerysetEqual(
            response.context["latest_question_list"],
            []
        )

Mi aporte al reto: fijarse en las pregutas pasadas, que son las que deben de estar publicadas, que el orden en la lista importa. Es decir como se publican de las m谩s recientes a las m谩s viejas, si las colocamos al rev茅s en el assert, nos env铆a error.

Soluci贸n al reto:

    def test_two_future_questions(self):
        """The questions index page may display multiple questions."""
        future_question1 = create_question(question_text="Future question 1", days=30)
        future_question2 = create_question(question_text="Future question 2", days=40)
        response = self.client.get(reverse("polls:index"))
        self.assertQuerysetEqual(
            response.context["latest_question_list"], 
            [])

Easy challenge:

def test_two_future_questions(self):
	"""
	if two future question was created, the response context must be empty
	"""
	_ = create_question(question_text="future1 question", days=30)
	_ = create_question(question_text="future2 question", days=30)
	response = self.client.get(reverse("polls:index"))
	self.assertQuerysetEqual(
		response.context["latest_question_list"],
		[]
	)

coloque un _ para indicar que no se va utilizar el resultado de la funci贸n create_question(...)

Creo que el reto esta bastante facil.
Igual que para las 2 del pasado pero el contexto tiene que venir vac铆o.

    def test_two_future_questions(self):
        """ If there are 2 question from the future no one will be displayed"""
        question_future_1 = create_question(question_text= "Question from past", days=10)
        question_future_2 = create_question(question_text= "Question from past", days=8)
        response = self.client.get(reverse("polls:index"))
        self.assertQuerysetEqual(
            response.context["latest_question_list"],
            []
        )

def test_two_question_future(self):
""鈥渢his method should not show if two questions are posted in the future鈥""
question_future1 = create_question(鈥榩regunta 1 publicada en el futuro鈥, days = 30)
question_future2 = create_question(鈥榩regunta 2 publicada en el futuro鈥, days = 40)
response = self.client.get(reverse(鈥減olls:index鈥))
self.assertQuerysetEqual(response.context[鈥渓ista_de_preguntas鈥漖,[ ])

La respuesta al reto:

    def test_two_future_questions(self):
        """
        The questions index do not display any questions.
        """
        future_question1 = create_question("Future question 1?", days=30)
        future_question2 = create_question("Future question 2?", days=40)
        response = self.client.get(reverse("polls:index"))
        self.assertQuerysetEqual(response.context["latest_question_list"], [])

El test del reto:

def test_two_future_questions(self):
        """
        The future questions aren't display.
        """
        future_question1 = create_question(
            question_text="Future question 1", days=30)
        future_question2 = create_question(
            question_text="Future question 2", days=40)
        response = self.client.get(reverse("polls:index"))
        self.assertQuerysetEqual(response.context["latest_question_list"],[])

Mi propuesta con 2 asserts en el test:

def test_two_future_questions(self):
        """
        This questions won't be displayed on the index page.
        """
        future_question1 = create_question(
            question_text='Future question 1', days=30)
        future_question2 = create_question(
            question_text='Future question 2', days=40)
        response = self.client.get(reverse('polls:index'))
        self.assertNotContains(response, [future_question1, future_question2])
        self.assertQuerysetEqual(response.context['latest_question_list'], [])

esta es mi soluci贸n:
def test_two_future_questions(self):
""鈥
The questions indez page aren鈥檛 may display multiple questions whit pub_date in the future.
鈥""
future_question1 = create_question(question_text=鈥渇uture question 1鈥, days=30)
future_question2 = create_question(question_text=鈥渇uture question 2鈥, days=40)
response = self.client.get(reverse(鈥減olls:index鈥))
self.assertQuerysetEqual(response.context[鈥渓atest_question_list鈥漖,[])

Aqui mi solucion al reto:

def test_two_future_questions(self):
        """
        The questions index page may display multiple questions.
        """
        future_question1=create_question(question_text="Future question 1.", days=30)
        future_question2=create_question(question_text="Future question 2.", days=5)
        response = self.client.get(reverse('polls:index'))
        self.assertQuerysetEqual(response.context['latest_question_list'],[])

Como puedes ver como las 2 preguntas estan en el futuro pues no debe devolver nada.

Dejo la documentaci贸n de Coverage.py.

def test_two_future_question(self):
""鈥渋f table Question contain a two future question鈥""
create_question(鈥淔uture question 1鈥, 30)
create_question(鈥淔uture question 2鈥, 55)
response = self.client.get(reverse(鈥減olls:index鈥))
self.assertContains(response, 鈥淣o polls are available.鈥)
self.assertQuerysetEqual(response.context[鈥渓atest_question_list鈥漖,[])

Creo que encontr茅 un bug en el c贸digo del programa.

A la hora de crear las variables past_question 1 y 2 el profesor Facundo lo hace de la siguiente forma:

past_question1 = create_question("Past Question 1", days=-30) # Fecha de hoy MENOS 30 d铆as
past_question2 = create_question("Past Question 2", days=-45) # Fecha de hoy MENOS 45 d铆as

La prueba pasa OK, pero, 驴Y si la segunda pregunta se creo apenas hace 15 d铆as?

past_question1 = create_question("Past Question 1", days=-30) # Fecha de hoy MENOS 30 d铆as
past_question2 = create_question("Past Question 2", days=-15) # Fecha de hoy MENOS 15 d铆as

Al lanzar nuestro set de pruebas se obtiene el siguiente error.


Esto significa que la prueba mas reciente (past_question2, -15 d铆as) est谩 siendo colocada en el 铆ndice 0 de nuestro lista latest_question_list, con lo cual se invalida la evaluaci贸n que realiza el profesor en la siguiente l铆nea.

self.assertQuerysetEqual(response.context['latest_question_list'], [past_question1, past_question2])

驴Porqu茅 sucede esto? Bueno, es debido al order_by en el m茅todo get_queryset de la clase IndexView. 驴Como podemos evitar este comportamiento err谩tico? Una forma de atacarlo ser铆a mediante el uso de count() del objeto QuerySet y hacer uso de assertEquals().

self.assertEquals(response.context['latest_question_list'].count(), 2)

De esta forma nuestra prueba ser谩 exitosa 馃槂. 隆Saludos!

RETO

    def test_two_past_questions(self):
        """The questions index page may display multiple questions."""
        create_question(question_text= "future question1", days= 30)
        create_question(question_text= "future question2", days= 40)
        response = self.client.get(reverse("polls:index"))
        self.assertContains(response, "No polls are available.")
        self.assertQuerysetEqual(
            response.context["latest_question_list"],
            []
        )
    def test_two_future_question(self):
        """ 
        test para index.view la pagina funcione de manera correcta cuando el usuario publique dos preguntas en el futuro
        """
        future_question1 = create_question(question_text="Future question 1", days = 30)
        future_question2 = create_question(question_text="Future question 1", days = 60)
        response = self.client.get(reverse("polls:index"))
        self.assertQuerysetEqual(
            response.context["latest_question_list"], []
        )```
def test_two_future_questions(self):
        ''' 
        Questions with a pub_date in the future aren't displayed on the index page.
        '''

        future_question1 = create_question(question_text="Past question1", days=30)
        future_question2 = create_question(question_text="Past question2", days=40)
        response = self.client.get(reverse("polls:index"))
        self.assertContains(response, "No polls are available.")
        self.assertQuerysetEqual(response.context["latest_question_list"], [])
def test_two_future_questions(self):
        """
        The question index page can't display multiple questions.
        """
        future_question1 = create_question("future question 1", 10)
        future_question2 = create_question("future question 2", 19)
        response = self.client.get(reverse("polls:index"))
        self.assertQuerysetEqual(
            response.context["latest_question_list"],
            []
        )

Como resolvi el reto:

la lista debe estar vacia porque en teoria si hay mensajes del futuro no se dbeen puiblicar por lo cual no muestra ningun mensaje del futuro

    def test_two_future_question(self):
        """ 
        The question index page may display multiple questions.
        """
        future_question1 = create_question(question_text="future question1", days=31)
        future_question2 = create_question(question_text="future question2", days=40)
        responses = self.client.get(reverse('polls:index'))
        self.assertQuerysetEqual(responses.context["latest_question_list"],[])

Aqu铆 me soluci贸n al desafio:

    def test_two_future_questions(self):
        "The questions index page may display multiple questions."
        future_question1 = create_question(question_text="future question 1", days=20)
        future_question2 = create_question(question_text="future question 2", days=40)
        response = self.client.get(reverse("polls:index"))
        self.assertQuerysetEqual(
            response.context["latest_question_list"], []
        )

Mic贸digo tambi茅n compara el contexto con una lista vac铆a.

def test_two_futurequestions(self):
        """The question index page may not display multiple question with pub_date in the future."""
        future_question_1 = create_question(question_text = 'Pregunta del futuro N掳 1.', days = 45)
        future_question_2 = create_question(question_text = 'Pregunta del futuro N掳 2.', days = 30)
        response = self.client.get(reverse('polls:home'))
        self.assertQuerysetEqual(response.context['latest_question_list'], [])

Buenas noches colegas.

Mi c贸digo para publicar: 2 preguntas en el futuro
驴Aplica? Faceback por fa 馃槈

    def test_two_future_questions(self):
        """
        The questions index page cannot display multiple questions.
        """
        future_question1 = create_question("Future question 1.", days=30)
        future_question2 = create_question("Future question 2.", days=5)
        response = self.client.get(reverse('polls:index'))
        self.assertContains(response, "No polls are available.")
        self.assertQuerysetEqual(response.context['latest_question_list'], [])

Aqu铆 el c贸digo de mi reto:

def test_two_future_question(self):
    """
    The questions index page must not display any future questions
    """
    future_question_1 = create_question(question_text='Future question 1', days=30)
    future_question_2 = create_question(question_text='Future question 2', days=40)
    response = self.client.get(reverse('polls:index'))
    self.assertQuerysetEqual(
        response.context['latest_question_list'],
        []
    )

Asi hice el reto

def test_two_furure_questions(self):
        """
        The question index page don't may display any future questions
        """
        future_question_1 = create_question(question_text= "My future question example #1", days= 10)
        future_question_2 = create_question(question_text= "My future question example #2", days= 2)
        response = self.client.get(reverse("polls:index"))
        self.assertQuerysetEqual(
            response.context["latest_question_list"], [] #basicamente esto verifica que no se haya guardado nada
        )
    def test_two_future_questions(self):
        """ The question index page may not display future questions """
        future_question1= create_question("Future question 1", 20)
        future_question2= create_question("Future question 2", 30)
        response= self.client.get(reverse("polls:index"))
        self.assertEqual(response.status_code, 200)
        self.assertQuerysetEqual(response.context["latest_question_list"],[])

Asi el mio, los hice utilizando
self.assertNotIn
aunque son dos lineas en lugar de una

def test_two_future_question(self):
    """The questions index page may display multiple questions."""
    future_q_1 = create_question(days = 1, hours = -1, minutes = -30)
    future_q_2 = create_question(hours = 1, minutes = 30)
    response = self.client.get(reverse('polls:index'))
    self.assertNotIn(future_q_1, response.context['latest_question_list'])
    self.assertNotIn(future_q_2, response.context['latest_question_list'])
    def test_tow_future_questions(self):
        """If futures questions exist, only past question are displayed"""
        future_question1 = create_question(question_text="Future Question 1", days=30)
        future_question2 = create_question(question_text="Future Question 2", days=40)
        response = self.client.get(reverse("polls:index"))
        self.assertQuerysetEqual(
            response.context["latest_question_list"],
            []
        )

Aqu铆 esta mi soluci贸n al reto, solo debemos asegurarnos que devuelva una lista vac铆a.

def test_two_future_questions(self):
        """
        future questions should not be displayed on the index page
        """

        future_question1 = create_question(question_text="Future question1", days=15)
        future_question2 = create_question(question_text="Future question2", days=15)
        response = self.client.get(reverse("polls:index"))
        self.assertQuerysetEqual(
            response.context["latest_question_list"],[]
        )