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

735 #736

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open

735 #736

Changes from 1 commit
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
Prev Previous commit
Next Next commit
Aula de implementação de conexão, Sessão e usuário
Carloshbfreire committed Sep 21, 2022
commit 23a0cff69bb839a6af16b83f8dc1c3de55d74360
Empty file added Libpythonpro/spam/__init__.py
Empty file.
25 changes: 25 additions & 0 deletions Libpythonpro/spam/db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Sessao:
contador = 0
usuarios = []

def salvar(self, usuario):
Sessao.contador += 1
usuario.id = Sessao.contador
self.usuarios.append(usuario)

def listar(self):
return self.usuarios

def roll_back(self):
pass

def fechar(self):
pass


class Conexao:
def gerar_sessao(self):
return Sessao()

def fechar(self):
pass
4 changes: 4 additions & 0 deletions Libpythonpro/spam/modelos.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Usuario:
def __init__(self, nome):
self.nome = nome
self.id = None
Empty file.
9 changes: 9 additions & 0 deletions Libpythonpro/spam/test_spam/enviador_de_email.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Enviador:
def enviar(self, remetente, destinatário, assunto, corpo):
if '@' not in remetente:
raise EmailInvalido(f'Email de Remetente Inválido:{remetente}')
return remetente


class EmailInvalido(Exception):
pass
25 changes: 25 additions & 0 deletions Libpythonpro/spam/test_spam/testes_usuarios.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from Libpythonpro.spam.db import Conexao
from Libpythonpro.spam.modelos import Usuario


def test_salvar_usuario():
conexao = Conexao()
sessao = conexao.gerar_sessao()
usuario= Usuario(nome='Carlos')
sessao.salvar(usuario)
assert isinstance(usuario.id, int)
sessao.roll_back()
sessao.fechar()
conexao.fechar()


def test_listar_usuario():
conexao = Conexao()
sessao = conexao.gerar_sessao()
usuarios = [Usuario(nome='Carlos'), Usuario(nome='Givani')]
for usuario in usuarios:
sessao.salvar(usuario)
assert usuarios == sessao.listar()
sessao.roll_back()
sessao.fechar()
conexao.fechar()
Empty file.
38 changes: 38 additions & 0 deletions Libpythonpro/tests/test_spam/test_enviador_de_email.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import pytest

from Libpythonpro.spam.test_spam.enviador_de_email import Enviador, EmailInvalido


def test_criar_enviador_de_email():
enviador= Enviador()
assert enviador is not None

@pytest.mark.parametrize(
'destinatario',
['[email protected]','[email protected]']
)
def test_remetente(destinatario):
enviador = Enviador()
destinatario
resultado = enviador.enviar(
destinatario,
'[email protected]',
'Cursos Python Pro',
'Primeira Turma Guido Von Rossum aberta.')
assert destinatario in resultado


@pytest.mark.parametrize(
'remetente',
['','renzo']
)
def test_remetente_invalido(remetente):
enviador = Enviador()
with pytest.raises(EmailInvalido):
enviador.enviar(
remetente,
'[email protected]',
'Cursos Python Pro',
'Primeira Turma Guido Von Rossum aberta.'
)