generated from reprograma/onX-sx-temaX
-
Notifications
You must be signed in to change notification settings - Fork 38
Projeto 2 - Aline Prado #17
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
Open
linieprado
wants to merge
1
commit into
reprograma:main
Choose a base branch
from
linieprado:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| 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, nome_livro): | ||
| for livro in self.livros: | ||
| if livro.nome == nome_livro: | ||
| livro.emprestado = True | ||
| return True # Se verdadeiro o livro foi emprestado com sucesso | ||
| return False # Se falso o livro não foi encontrado na biblioteca | ||
|
|
||
| def remover_livro(self, nome_livro): | ||
| for livro in self.livros: | ||
| if livro.nome == nome_livro and not livro.emprestado: | ||
| self.livros.remove(livro) | ||
| return True # Se verdadeiro o livro foi removido com sucesso | ||
| return False # Se falso o livro não foi encontrado na biblioteca ou está emprestado | ||
|
|
||
| def buscar_livro(self, nome_livro): | ||
| for livro in self.livros: | ||
| if livro.nome == nome_livro: | ||
| status = "Disponível" if not livro.emprestado else "Emprestado" | ||
| return f"Nome: {livro.nome}, Autor: {livro.autor}, Status: {status}" | ||
| return "Livro não encontrado" | ||
|
|
||
| def devolver_livro(self, nome_livro): | ||
| for livro in self.livros: | ||
| if livro.nome == nome_livro: | ||
| livro.emprestado = False | ||
| return # Livro foi devolvido com sucesso | ||
| return "Livro não encontrado" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| class Livro: | ||
| def __init__(self, nome, autor): | ||
| self.nome = nome | ||
| self.autor = autor | ||
| self.emprestado = False | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| 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 | ||
| # Assert | ||
| self.assertIsInstance(self.biblioteca.livros, list) | ||
|
|
||
| def test_adicionar_livro_deve_passar(self): | ||
| # Arrange | ||
| nome_livro = "Anne de Green Gables" | ||
| autor_livro = "L. M. Montgomery" | ||
| livro = Livro(nome_livro, autor_livro) | ||
|
|
||
| # Act | ||
| self.biblioteca.adicionar_livro(livro) | ||
|
|
||
| # Assert | ||
| self.assertEqual(1, len(self.biblioteca.livros)) | ||
|
|
||
| def test_adicionar_livro_nao_deve_inserir_numero(self): | ||
| # Arrange | ||
| livro = 1988 | ||
|
|
||
| # Assert / act | ||
| with self.assertRaises(TypeError): | ||
| self.biblioteca.adicionar_livro(livro) | ||
|
|
||
| # Teste associado ao método Exibir Livros | ||
|
|
||
| def test_exibir_livros_deve_retornar_lista_de_livros(self): | ||
| # Arrange | ||
| nome_livro1 = "Anne de Green Gables" | ||
| autor_livro1 = "L. M. Montgomery" | ||
| livro1 = Livro(nome_livro1, autor_livro1) | ||
|
|
||
| nome_livro2 = "Dom Quixote" | ||
| autor_livro2 = "Miguel de Cervantes" | ||
| livro2 = Livro(nome_livro2, autor_livro2) | ||
|
|
||
| self.biblioteca.adicionar_livro(livro1) | ||
| self.biblioteca.adicionar_livro(livro2) | ||
|
|
||
| # Act | ||
| lista_de_livros = self.biblioteca.exibir_livros() | ||
|
|
||
| # Assert | ||
| self.assertEqual(len(lista_de_livros), 2) | ||
| self.assertIn(livro1, lista_de_livros) | ||
| self.assertIn(livro2, lista_de_livros) | ||
|
|
||
| # Teste associado ao método Emprestar Livro | ||
|
|
||
| def test_emprestar_livro_deve_marcar_livro_como_emprestado(self): | ||
| # Arrange | ||
| nome_livro = "Alice no País das Maravilhas" | ||
| autor_livro = "Lewis Carrol" | ||
| livro = Livro(nome_livro, autor_livro) | ||
| self.biblioteca.adicionar_livro(livro) | ||
|
|
||
| # Act | ||
| emprestado = self.biblioteca.emprestar_livro(nome_livro) | ||
|
|
||
| # Assert | ||
| self.assertTrue(emprestado) # Verifica se o livro foi emprestado com sucesso | ||
| self.assertTrue(livro.emprestado) # Verifica se o atributo emprestado do livro está como True | ||
|
|
||
| # Teste associado ao método Remover Livro (Extra) | ||
|
|
||
| def test_remover_livro_deve_remover_livro_nao_emprestado(self): | ||
| # Arrange | ||
| nome_livro = "Romeu e Julieta" | ||
| autor_livro = "William Shakespeare" | ||
| livro = Livro(nome_livro, autor_livro) | ||
| self.biblioteca.adicionar_livro(livro) | ||
|
|
||
| # Act | ||
| removido = self.biblioteca.remover_livro(nome_livro) | ||
|
|
||
| # Assert | ||
| self.assertTrue(removido) # Verifica se o livro foi removido com sucesso | ||
| self.assertNotIn(livro, self.biblioteca.livros) # Verifica se o livro não está mais na biblioteca | ||
|
|
||
| # Teste associado ao método Buscar Livro (Extra) | ||
|
|
||
| def test_buscar_livro_deve_retornar_informacoes_do_livro(self): | ||
| # Arrange | ||
| nome_livro = "Morro dos Ventos Uivantes" | ||
| autor_livro = "Emilly Bronte" | ||
| livro = Livro(nome_livro, autor_livro) | ||
| self.biblioteca.adicionar_livro(livro) | ||
|
|
||
| # Act | ||
| informacoes = self.biblioteca.buscar_livro(nome_livro) | ||
|
|
||
| # Assert | ||
| self.assertEqual(informacoes, f"Nome: {nome_livro}, Autor: {autor_livro}, Status: Disponível") | ||
|
|
||
| # Teste associado ao método Devolver Livro (Extra) | ||
|
|
||
| def test_devolver_livro_deve_marcar_livro_como_nao_emprestado(self): | ||
| # Arrange | ||
| nome_livro = "Emma" | ||
| autor_livro = "Jane Austen" | ||
| livro = Livro(nome_livro, autor_livro) | ||
| self.biblioteca.adicionar_livro(livro) | ||
| self.biblioteca.emprestar_livro(nome_livro) | ||
|
|
||
| # Act | ||
| devolvido = self.biblioteca.devolver_livro(nome_livro) | ||
|
|
||
| # Assert | ||
| self.assertIsNone(devolvido) # Verifica se o livro foi devolvido com sucesso | ||
| self.assertFalse(livro.emprestado) # Verifica se o atributo emprestado do livro está como False | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| from unittest import TestCase | ||
| from Livro import Livro | ||
|
|
||
| class TestLivro(TestCase): | ||
| def test_init_deve_passar(self): | ||
| # Arrange | ||
| nome = "Orgulho e Preconceito" | ||
| autor = "Jane Austen" | ||
|
|
||
| # Act | ||
| livro = Livro(nome, autor) | ||
|
|
||
| # Assert | ||
| self.assertEqual(nome, livro.nome) | ||
| self.assertEqual(autor, livro.autor) | ||
| self.assertEqual(False, livro.emprestado) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
O teste está muito coerente, deu para notar como vc compreendeu o conteúdo e aplicou o TDD na implementação.
Só nesse ponto é um erro muito sutil que todas nós que iniciamos em Python passamos.
Vc está comparando o objeto livro no seu teste ao invés do seu alvo de teste, que é o livro de dentro da lista self.biblioteca.livros
O teste funcionou pq o python faz uma cópia superficial do objeto auxiliar (livro) e o objeto que vc atribuiu esse valor (self.biblioteca.livros[x].livro), então quando altera um o outro também é alterado.
Não se preocupe, é apenas um detalhe de como a linguagem funciona, do ponto de vista da entrega está ótimo, é apenas para informar que o python funciona dessa forma. E para se aprofundar aqui está um artigo: Python - A diferença entre “Deep Copy e Shallow Copy”