generated from reprograma/onX-sx-temaX
-
Notifications
You must be signed in to change notification settings - Fork 38
Projeto Guiado II - Cris Pereira - semana 08 - curso Python #23
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
namucris
wants to merge
1
commit into
reprograma:main
Choose a base branch
from
namucris: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
40 changes: 40 additions & 0 deletions
40
exercicios/para-casa/projetoGuiado_CrisPereira/Biblioteca.py
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,40 @@ | ||
| # Exercício Cris Pereira - semana 08 | ||
| 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) | ||
|
|
||
| # Novas funcionalidades abaixo | ||
| def exibir_livros(self): | ||
| return self.livros | ||
|
|
||
| def emprestar_livro(self, titulo): | ||
| for livro in self.livros: | ||
| if livro.nome == titulo and not livro.esta_emprestado: | ||
| livro.esta_emprestado = True | ||
|
|
||
| def remover_livro(self, titulo): | ||
| for livro in self.livros: | ||
| if livro.nome == titulo: | ||
| self.livros.remove(livro) | ||
|
|
||
| def buscar_livro(self, titulo): | ||
| mensagem = "Livro não encontrado." | ||
| for livro in self.livros: | ||
| if livro.nome == titulo: | ||
| mensagem = livro | ||
| return mensagem | ||
|
|
||
| def devolver_livro(self, titulo): | ||
| mensagem = "Livro não encontrado." | ||
| for livro in self.livros: | ||
| if livro.nome == titulo: | ||
| livro.esta_emprestado = False | ||
| mensagem = f"Livro devolvido: {livro}" | ||
| return mensagem |
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 @@ | ||
| # Exercício Cris Pereira - semana 08 | ||
| class Livro: | ||
| def __init__(self, nome, autor): | ||
| self.nome = nome | ||
| self.autor = autor | ||
| self.esta_emprestado = False |
110 changes: 110 additions & 0 deletions
110
exercicios/para-casa/projetoGuiado_CrisPereira/testBiblioteca.py
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,110 @@ | ||
| # Testes Cris Pereira - semana 08 | ||
| from unittest import TestCase | ||
| from Biblioteca import Biblioteca | ||
| from Livro import Livro | ||
|
|
||
| class TestBiblioteca(TestCase): | ||
| def setUp(self): | ||
| self.biblioteca = Biblioteca() | ||
| self.livro1 = Livro("Sister Outsider", "Audre Lorde") | ||
| self.livro2 = Livro("Memórias da Plantação", "Grada Kilomba") | ||
|
|
||
| def test_init_deve_passar(self): | ||
| # Arrange / Act feitos no setup() | ||
| # Assert | ||
| self.assertIsInstance(self.biblioteca.livros, list) | ||
|
|
||
| def test_adicionar_livro_deve_passar(self): | ||
| # Arrange | ||
| 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(self.biblioteca.livros)) | ||
|
|
||
| def test_adicionar_livro_nao_deve_inserir_numero(self): | ||
| # Arrange | ||
| livro = 1988 | ||
|
|
||
| # Act / Assert | ||
| with self.assertRaises(TypeError): | ||
| self.biblioteca.adicionar_livro(livro) | ||
|
|
||
| # Novos testes abaixo | ||
| def test_exibir_livros(self): | ||
| #Arrange | ||
| self.biblioteca.adicionar_livro(self.livro1) | ||
| self.biblioteca.adicionar_livro(self.livro2) | ||
| lista_biblioteca = [self.livro1, self.livro2] | ||
|
|
||
| #Act / Assert | ||
| self.assertEqual(self.biblioteca.exibir_livros(), lista_biblioteca) | ||
|
|
||
| def test_emprestar_livros(self): | ||
| #Arrange | ||
| self.biblioteca.adicionar_livro(self.livro1) | ||
| self.biblioteca.adicionar_livro(self.livro2) | ||
|
|
||
| #Act | ||
| self.biblioteca.emprestar_livro("Sister Outsider") | ||
| self.biblioteca.emprestar_livro("Memórias da Plantação") | ||
|
|
||
| #Assert | ||
| self.assertTrue(self.livro1.esta_emprestado) | ||
| self.assertTrue(self.livro2.esta_emprestado) | ||
|
|
||
| def test_remover_livro(self): | ||
| #Arrange | ||
| self.biblioteca.adicionar_livro(self.livro1) | ||
| self.biblioteca.adicionar_livro(self.livro2) | ||
|
|
||
| #Act | ||
| self.biblioteca.remover_livro("Sister Outsider") | ||
|
|
||
| #Assert | ||
| self.assertIsNot(self.biblioteca.livros, self.livro1) | ||
|
|
||
|
|
||
| def test_buscar_livro_existente(self): | ||
| #Arrange | ||
| self.biblioteca.adicionar_livro(self.livro1) | ||
| self.biblioteca.adicionar_livro(self.livro2) | ||
|
|
||
| #Act | ||
| livro_buscado = self.biblioteca.buscar_livro("Sister Outsider") | ||
|
|
||
| #Assert | ||
| self.assertEqual(livro_buscado, self.livro1) | ||
|
|
||
| def test_buscar_livro_inexistente(self): | ||
| #Arrange | ||
| self.biblioteca.adicionar_livro(self.livro1) | ||
|
|
||
| #Act | ||
| livro_buscado = self.biblioteca.buscar_livro("Memórias da Plantação") | ||
|
|
||
| #Assert | ||
| self.assertEqual(livro_buscado, "Livro não encontrado.") | ||
|
|
||
| def test_devolver_livro(self): | ||
| #Arrange | ||
| self.biblioteca.adicionar_livro(self.livro1) | ||
| self.biblioteca.adicionar_livro(self.livro2) | ||
| self.biblioteca.emprestar_livro("Sister Outsider") | ||
| self.biblioteca.emprestar_livro("Memórias da Plantação") | ||
|
|
||
| #Act | ||
| self.biblioteca.devolver_livro("Memórias da Plantação") | ||
|
|
||
| #Assert | ||
| self.assertFalse(self.livro2.esta_emprestado) | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
17 changes: 17 additions & 0 deletions
17
exercicios/para-casa/projetoGuiado_CrisPereira/testLivro.py
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,17 @@ | ||
| # Exercício Cris Pereira - semana 08 | ||
| from unittest import TestCase | ||
| from Livro import Livro | ||
|
|
||
| class TestLivro(TestCase): | ||
| def test_init_deve_passar(self): | ||
| # Arrange | ||
| nome = "Calibã e a bruxa" | ||
| autor = "Silvia Federici" | ||
|
|
||
| # Act | ||
| livro = Livro(nome, autor) | ||
|
|
||
| # Assert | ||
| self.assertEqual(nome, livro.nome) | ||
| self.assertEqual(autor, livro.autor) | ||
| self.assertEqual(False, livro.esta_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.
Aqui só tem uma pequena correção:
Vc está verificando o objeto livro, porém o seu alvo de teste é a biblioteca e
onde x e y são as posições dos seus livros dentro da lista livros da biblioteca
E agora a dúvida: por que funcionou dessa forma?
parabéns você acaba de conhecer mais uma mania do Python!!
quando a gente cria um objeto e atribui esse objeto a outro eles guardam a mesma referencia, então quando altera um o outro também é alterado.
para se aprofundar nesse assunto aqui um artigo Python - A diferença entre “Deep Copy e Shallow Copy”