class TestORMAlumnos(unittest.TestCase): """Test case para el ORM de Alumno.""" DB_NAME = "alumnos.test.db" def setUp(self): self.conn = Connection.get_conn(db_name=self.DB_NAME) self.cur = self.conn.cursor() self.orm = ORMAlumnos(self.conn) def tearDown(self): Connection._reset() try: os.remove(self.DB_NAME) except FileNotFoundError as e: pass def create_alumnos(self): password = hashlib.sha256("UnaPasswordReSegura".encode()).hexdigest() alumno = Alumno(1, "Juancho Pancho", "h", 30, password) self.orm.create(alumno) password = hashlib.sha256("OtraPasswordMuySegura".encode()).hexdigest() alumno = Alumno(2, "Speedy gonzalez", "m", 55, password) self.orm.create(alumno) def test_el_testcase_anda(self): self.assertEqual(1, 1) def test_la_BD_no_esta_vacia(self): alumnos = self.cur.execute("SELECT * FROM Alumno").fetchall() self.assertGreater(len(alumnos), 0) def test_alumno_se_carga_correctamente(self): self.create_alumnos() alumnos = self.cur.execute("SELECT * FROM Alumno").fetchall() self.assertNotEqual(len(alumnos), 0) def test_orm_devuelve_alumno(self): self.create_alumnos() alumno = self.orm.get_by_id(1) self.assertIsInstance(alumno, Alumno) self.assertEqual(alumno.nombre, "Juancho Pancho") def test_orm_actualiza_correctamente(self): self.create_alumnos() alumno = self.orm.get_by_id(1) alumno.nombre = "Pepe Luis" self.orm.update(alumno) alumno_actualizado = self.orm.get_by_id(1) self.assertEqual(alumno_actualizado.nombre, "Pepe Luis")
class TestSignup(unittest.TestCase): """Test case para la creación de un nuevo Alumno.""" DB_NAME = "alumnos.test.db" def setUp(self): self.conn = Connection.get_conn(db_name=self.DB_NAME) self.cur = self.conn.cursor() self.orm = ORMAlumnos(self.conn) def tearDown(self): Connection._reset() try: os.remove(self.DB_NAME) except FileNotFoundError as e: pass def test_anda(self): self.assertEqual(1, 1) def test_no_permite_alumnos_con_legajo_duplicado(self): with patch("signup.get_orm", return_value=self.orm): alumno = Alumno( 1, "Juancho", "m", 21, hashlib.sha256("UnaPasswordMuySegura".encode()).hexdigest()) self.orm.create(alumno) alumno_dict = {"legajo": 1} self.assertTrue(user_exists(alumno_dict)) alumno_dict["legajo"] = 32 self.assertFalse(user_exists(alumno_dict)) def test_devuelve_error_si_alumno_existe(self): with patch("signup.get_orm", return_value=self.orm): alumno = Alumno( 1, "Juancho", "m", 21, hashlib.sha256("UnaPasswordMuySegura".encode()).hexdigest()) self.orm.create(alumno) alumno_dict = {"legajo": 1} errors = check_new_user(alumno_dict) usuario_ya_existe_msg = errors.get("usuario", None) self.assertIsNotNone(usuario_ya_existe_msg) alumno_dict = {"legajo": 322} errors = check_new_user(alumno_dict) usuario_ya_existe_msg = errors.get("usuario", None) self.assertIsNone(usuario_ya_existe_msg)
class TestLogin(unittest.TestCase): """Test case para el script de login.""" DB_NAME = "alumnos.test.db" FAKE_USER = { "legajo": 1, "password": sha256("UnaPasswordMuySegura".encode()).hexdigest() } def setUp(self): self.conn = Connection.get_conn(db_name=self.DB_NAME) self.orm = ORMAlumnos(self.conn) def tearDown(self): Connection._reset() try: os.remove(self.DB_NAME) except FileNotFoundError as e: pass def test_loguea_usuario_valido(self): with patch("login.get_dict_from_fieldstorage", return_value=self.FAKE_USER): with patch("login.get_orm", return_value=self.orm): alumno = Alumno( 1, "Juancho", "m", 21, sha256("UnaPasswordMuySegura".encode()).hexdigest()) self.orm.create(alumno) pq = PyQuery(login.post()) #splitear las lineas por el '\n' con "splitlines()" res = pq("div#form-success").text().lower().splitlines() self.assertEqual(res[0], "pudo ingresar correctamente") def test_no_loguea_usuario_invalido(self): with patch("login.get_dict_from_fieldstorage", return_value=self.FAKE_USER): with patch("login.get_orm", return_value=self.orm): alumno = Alumno(1, "Juancho", "m", 21, sha256("OtraPassword".encode()).hexdigest()) self.orm.create(alumno) pq = PyQuery(login.post()) res = pq("div#form-success").text().lower().splitlines() self.assertListEqual( res, [], msg="No se encontró el mensaje de login exitoso")
def get_orm(): """Devuelve el ORM a utilizar""" from db.db import Connection from db.orm_alumnos import ORMAlumnos return ORMAlumnos(Connection.get_conn())
def setUp(self): self.conn = Connection.get_conn(db_name=self.DB_NAME) self.cur = self.conn.cursor() self.orm = ORMAlumnos(self.conn)