-
Notifications
You must be signed in to change notification settings - Fork 38
Tentativa de resposta ao projeto II #25
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| from Livro import Livro | ||
|
|
||
| class Biblioteca: | ||
| def __init__(self): | ||
| self.livros = [] | ||
|
|
||
| def adicionar_livro(self, livro: Livro): | ||
| if (not isinstance(livro, Livro)): | ||
| raise TypeError(f'Esperado Livro obtido valor {livro} do tipo {type(livro)}.') | ||
| self.livros.append(livro) | ||
|
|
||
| def exibir_livros(self): | ||
| return self.livros | ||
|
|
||
| def emprestar_livro(self, livro: Livro): | ||
| if livro.emprestado == True: | ||
| return(f'O livro não está disponível') | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Aqui vc está indo no caminho certo! |
||
| elif livro.emprestado: | ||
| return(f'Este livro está disponível.') | ||
| else: | ||
| raise TypeError ('Não temos este livro') | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| class Livro: | ||
| def __init__(self, nome, autor, emprestado = False, disponivel = True): | ||
| self.nome = nome | ||
| self.autor = autor | ||
| self.emprestado = False | ||
| self.disponivel = True | ||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| from unittest import TestCase | ||
| from Biblioteca import Biblioteca | ||
| from Livro import Livro | ||
|
|
||
| class TestBiblioteca(TestCase): | ||
|
|
||
| def setUp(self): | ||
| self.biblioteca = Biblioteca() | ||
|
|
||
| def test_init_deve_passar(self): | ||
| # Arrange / Act | ||
| biblioteca = Biblioteca() | ||
|
|
||
| # Assert | ||
| self.assertIsInstance(biblioteca.livros, list) | ||
|
|
||
| def test_adicionarlivro_deve_passar(self): | ||
| # Arrange | ||
| biblioteca = Biblioteca() | ||
| nome_livro = 'O Mito da Beleza' | ||
| autor_livro = 'Naomi Wolf' | ||
| livro = Livro(nome_livro, autor_livro) | ||
|
|
||
| # Act | ||
| self.biblioteca.adicionar_livro(Livro) | ||
|
|
||
| # Assert | ||
| self.assertEqual(1, len(biblioteca.livros)) | ||
|
|
||
| def test_adicionar_livro_não_aceita_num(self): | ||
| # Arrange | ||
| biblioteca = Biblioteca() | ||
| livro = 1988 | ||
|
|
||
| # Act / Assert | ||
| with self.assertRaises(TypeError): | ||
| biblioteca.adicionar_livro(livro) | ||
|
|
||
| def test_exibir_livros(self.biblioteca): | ||
| #Arrange | ||
| lista = Biblioteca() | ||
| #Act | ||
| return lista | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Aqui nesse teste está te retornando um falso positivo, quando não tem erros, e mais, quando não tem nenhum assert ele passa (mistérios do python) Aqui está faltando chamar a ação, o método lista.exibir_livros() |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| from unittest import TestCase | ||
| from Livro import Livro | ||
|
|
||
| class TestLivro(TestCase): | ||
| def test_init_deve_passar(self): | ||
| # Arrange | ||
| nome = 'O caminho' | ||
| autor = 'Peter Mallouk' | ||
|
|
||
| # Act | ||
| livro = Livro(nome, autor) | ||
|
|
||
| # Assert | ||
| self.assertEqual(nome, livro.nome) | ||
| self.assertEqual(autor, livro.autor) | ||
| self.assertEqual(False, livro.emprestado) | ||
|
|
||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| raise - estoura uma exceção | ||
|
|
||
| duas formas de importar: | ||
| 1. import tananana | ||
| 2. from tanana import tanana | ||
|
|
||
| entender o conceito de self pelo amor de deus? | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Muito bacana ter criado um arquivo para fazer as anotações da aula! |
||
|
|
||
| quando rodamos testes não é o python que está usando é a biblioteca que vai rodar os testes | ||
| então ao invés do run vamos em | ||
|
|
||
| -m unittest testBiblioteca.py | ||
| -m unittest testLivro.py | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Esse método tem alguns ajustes a serem feitos na lógica também
raiselivro.emprestado == Trueelivro.emprestado:são formas diferentes de fazer a mesma verificação, por isso o seu elif nunca vai ser alcançadoUm site que ajuda muito a entender a lógica é o https://pythontutor.com/
Nesse site vc consegue fazer teste de mesa e ir debugando o código