Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/cliente validar name #30

Merged
merged 14 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions app/migrations/0015_merge_20240604_2040.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Generated by Django 5.0.4 on 2024-06-04 23:40

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('app', '0012_alter_product_price'),
('app', '0014_merge_20240603_0914'),
]

operations = [
]
14 changes: 14 additions & 0 deletions app/migrations/0020_merge_20240605_1018.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Generated by Django 5.0.4 on 2024-06-05 13:18

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('app', '0015_merge_20240604_2040'),
('app', '0019_merge_20240605_0942'),
]

operations = [
]
4 changes: 4 additions & 0 deletions app/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re # Importa el módulo de expresiones regulares
from datetime import datetime

from django.core.exceptions import ValidationError
Expand All @@ -24,6 +25,8 @@ def validate_client(data):

if name == "":
errors["name"] = "Por favor ingrese un nombre"
elif not re.match(r'^[a-zA-ZáéíóúÁÉÍÓÚñÑ\s]+$', name):
errors["name"] = "El nombre debe contener solo letras y espacios"

if phone == "":
errors["phone"] = "Por favor ingrese un teléfono"
Expand Down Expand Up @@ -93,6 +96,7 @@ def update_client(self, client_data):
if len(errors) > 0:
return False, errors


self.name = client_data.get("name", "") or self.name
self.email = client_data.get("email", "") or self.email
self.phone = client_data.get("phone", "") or self.phone
Expand Down
1 change: 1 addition & 0 deletions app/templates/clients/form.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ <h1>Nuevo Cliente</h1>
<input type="text"
id="name"
name="name"
pattern="^[A-Za-z\s]+$"
value="{{client.name}}"
class="form-control"
required/>
Expand Down
65 changes: 64 additions & 1 deletion app/tests_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ def test_edit_user_with_valid_data_test(self):
"phone": "221456789",
"email": "[email protected]",
"city": "Berisso",

},
)

Expand Down Expand Up @@ -161,6 +160,70 @@ def test_edit_user_with_invalid_data_test_city(self):
editedClient = Client.objects.get(pk=client.id)
self.assertEqual(editedClient.city, "La Plata")

def test_validation_invalid_name_client(self):
"""
Prueba si se muestra un error de validación cuando se ingresa un nombre inválido.
"""
response = self.client.post(
reverse("clients_form"),
data={
"name": "Juan Sebastian Veron 11",
"phone": "221555232",
"address": "13 y 44",
"email": "[email protected]",
},
)

self.assertContains(response, "El nombre debe contener solo letras y espacios")

def test_user_cant_edit_client_with_empty_name(self):
"""
Prueba que un usuario no pueda editar un cliente con un nombre vacío.
"""
client=Client.objects.create(
name="Juan Sebastian Veron",
phone="221555232",
city="La Plata",
email="[email protected]",
)

response = self.client.post(
reverse("clients_form"),
data={
"id":client.id,
"name":"",
"phone":client.phone,
"city":client.city,
"email":client.email,
},
)

self.assertContains(response, "Por favor ingrese un nombre")

def test_user_cant_edit_client_with_incorrect_name(self):
"""
Prueba que un usuario no pueda editar un cliente con un nombre incorrecto.
"""
client=Client.objects.create(
name="Juan Sebastian Veron",
phone="221555232",
city="La Plata",
email="[email protected]",
)

response = self.client.post(
reverse("clients_form"),
data={
"id":client.id,
"name":"Juan Sebastian Veron 11",
"phone":client.phone,
"city":client.city,
"email":client.email,
},
)

self.assertContains(response, "El nombre debe contener solo letras y espacios")

class MedicineIntegrationTest(TestCase):
def test_can_create_medicine(self):
"""
Expand Down
93 changes: 92 additions & 1 deletion app/tests_unit.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
from django.test import TestCase
from django.utils import timezone

from app.models import Client, Medicine, Pet, Product, Provider, validate_pet
from app.models import (
Client,
Medicine,
Pet,
Product,
Provider,
validate_client,
validate_pet,
)


class ClientModelTest(TestCase):
Expand Down Expand Up @@ -41,13 +49,16 @@ def test_can_update_client(self):
)
client = Client.objects.get(pk=1)

self.assertEqual(client.phone, "221555232")

client.update_client( {
"name": "Juan Sebastian Veron",
"phone": "221555232",
"email": "[email protected]",
"city": "Berisso",
},)


client_updated = Client.objects.get(pk=1)

self.assertEqual(client_updated.phone, "221555232")
Expand Down Expand Up @@ -77,6 +88,39 @@ def test_update_client_with_error(self):

self.assertEqual(client_updated.phone, "221555232")


def test_validate_client_incorrect_name(self):
"""
Prueba que verifica que si un nombre es ingresado con algún caracter que no sean letras minúsculas, mayúsculas o espacios devuelva el error
"""

data = {
"name": "Juan Sebastian Veron 11",
"phone": "221555232",
"city": "La Plata",
"email": "[email protected]",
}

result = validate_client(data)

self.assertIn("El nombre debe contener solo letras y espacios", result.values())

def test_validate_client_with_empty_name(self):
"""
Prueba que verifica que no se pueda crear un cliente con el campo nombre vacío
"""
data = {
"name": "",
"phone": "221555232",
"city": "La Plata",
"email": "[email protected]",
}

errors = validate_client(data)

self.assertIn("Por favor ingrese un nombre", errors.values())


def test_update_client_with_email_null(self): #nuevo test verificando que no pueda hacer update con email nulo
"""
Esta funcion testea el cliente acutalizado con un email nulo
Expand All @@ -98,6 +142,53 @@ def test_update_client_with_email_null(self): #nuevo test verificando que no pue
client_updated = Client.objects.get(pk=1)

self.assertEqual(client_updated.email, "[email protected]")

def test_update_client_with_empty_name(self):
"""
Prueba que verifica si se produce un error al intentar actualizar un cliente con un campo de nombre vacío.
"""
Client.save_client(
{
"name": "Juan Sebastian Veron",
"phone": "221555232",
"city": "La Plata",
"email": "[email protected]",
},
)
client = Client.objects.get(pk=1)

self.assertEqual(client.name, "Juan Sebastian Veron")

client.update_client({"name": ""})
client_updated = Client.objects.get(pk=1)

self.assertEqual(client_updated.name, "Juan Sebastian Veron")

def test_update_client_with_incorrect_name(self):
"""
Prueba que verifica si se produce un error al intentar actualizar un cliente con un campo de nombre incorrecto.
"""
Client.save_client(
{
"name": "Juan Sebastian Veron",
"phone": "221555232",
"city": "La Plata",
"email": "[email protected]",
},
)
client = Client.objects.get(pk=1)

self.assertEqual(client.name, "Juan Sebastian Veron")

client.update_client({
"name": "Juan Sebastian Veron 11",
"phone": "221555232",
"city": "La Plata",
"email": "[email protected]",})
client_updated = Client.objects.get(pk=1)

self.assertEqual(client_updated.name, "Juan Sebastian Veron")

class MedicineModelTest(TestCase):

def test_can_create_medicine_with_valid_dose(self):
Expand Down
19 changes: 10 additions & 9 deletions functional_tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ def test_should_show_clients_data(self):

expect(self.page.get_by_text("Juan Sebastián Veron")).to_be_visible()
expect(self.page.get_by_text("La Plata")).to_be_visible()

expect(self.page.get_by_text("221555232")).to_be_visible()
expect(self.page.get_by_text("[email protected]")).to_be_visible()

Expand All @@ -140,7 +141,7 @@ def test_should_show_client_edit_action(self):
"""
client = Client.objects.create(
name="Juan Sebastián Veron",
city="13 y 44",
city="La Plata",
phone="221555232",
email="[email protected]",
)
Expand All @@ -158,7 +159,7 @@ def test_should_show_client_delete_action(self):
"""
client = Client.objects.create(
name="Juan Sebastián Veron",
city="13 y 44",
city="La Plata",
phone="221555232",
email="[email protected]",
)
Expand All @@ -181,15 +182,15 @@ def test_should_can_be_able_to_delete_a_client(self):
Esta función verifica que un cliente pueda ser eliminado correctamente a través de una solicitud POST al servidor.
"""
Client.objects.create(
name="Juan Sebastián Veron",
city="13 y 44",
name="Juan Sebastian Veron",
city="La Plata",
phone="221555232",
email="[email protected]",
)

self.page.goto(f"{self.live_server_url}{reverse('clients_repo')}")

expect(self.page.get_by_text("Juan Sebastián Veron")).to_be_visible()
expect(self.page.get_by_text("Juan Sebastian Veron")).to_be_visible()

def is_delete_response(response):
"""
Expand All @@ -204,7 +205,7 @@ def is_delete_response(response):
response = response_info.value
self.assertTrue(response.status < 400)

expect(self.page.get_by_text("Juan Sebastián Veron")).not_to_be_visible()
expect(self.page.get_by_text("Juan Sebastian Veron")).not_to_be_visible()


class ClientCreateEditTestCase(PlaywrightTestCase):
Expand All @@ -216,15 +217,15 @@ def test_should_be_able_to_create_a_new_client(self):

expect(self.page.get_by_role("form")).to_be_visible()

self.page.get_by_label("Nombre").fill("Juan Sebastián Veron")
self.page.get_by_label("Nombre").fill("Juan Sebastian Veron")
self.page.get_by_label("Teléfono").fill("221555232")
self.page.get_by_label("Email").fill("[email protected]")
self.page.get_by_label("Ciudad").select_option("La Plata")


self.page.get_by_role("button", name="Guardar").click()

expect(self.page.get_by_text("Juan Sebastián Veron")).to_be_visible()
expect(self.page.get_by_text("Juan Sebastian Veron")).to_be_visible()
expect(self.page.get_by_text("221555232")).to_be_visible()
expect(self.page.get_by_text("[email protected]")).to_be_visible()
expect(self.page.get_by_text("La Plata")).to_be_visible()
Expand All @@ -245,7 +246,7 @@ def test_should_view_errors_if_form_is_invalid(self):
expect(self.page.get_by_text("Por favor ingrese un teléfono")).to_be_visible()
expect(self.page.get_by_text("Por favor ingrese un email")).to_be_visible()

self.page.get_by_label("Nombre").fill("Juan Sebastián Veron")
self.page.get_by_label("Nombre").fill("Juan Sebastian Veron")
self.page.get_by_label("Teléfono").fill("221555232")
self.page.get_by_label("Email").fill("brujita75")
self.page.get_by_label("Ciudad").select_option("La Plata")
Expand Down