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

Fix Package diameter property #118

Merged
merged 4 commits into from
Mar 9, 2018
Merged

Fix Package diameter property #118

merged 4 commits into from
Mar 9, 2018

Conversation

bratomes
Copy link
Contributor

@bratomes bratomes commented Mar 9, 2018

According to new version of Correios' documentation, the package diameter only needs to be calculate if the object type is equal 003, cylinder, otherwise must return 0, as we can see in the image below.

screenshot from 2018-03-09 09-36-05

('TYPE_BOX', 0),
('TYPE_CYLINDER', 16),
])
def test_package_diameter(package, package_type, diameter):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should add a paremeter with diameter larger than MIN_DIAMETER to test the second condition of max() call.

@bratomes
Copy link
Contributor Author

bratomes commented Mar 9, 2018

done @cacarrara

@@ -331,7 +331,9 @@ def length(self, length):

@property
def diameter(self) -> int:
return max(MIN_DIAMETER, int(math.ceil(self.real_diameter)))
if self.package_type == Package.TYPE_CYLINDER:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inverte a lógica... trate a exceção primeiro e retorne o valor "default" na sequencia:

if self.package_type != Package.TYPE_CYLINDER:
   return 0.0
return max(...)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agora com mais tempo pra explicar:

Em um método que deve retornar um valor calculado "exceto em certos casos" fica mais elegante tratar as "exceções" primeiro para que elas fiquem evidentes no corpo do método.

E se o programador está lendo o método que retorna um diâmetro mas se interessa só pelo retorno "default" os olhos podem ir direto para o fim do método onde ele vai encontrar um comando "return" que retorna o valor que ele quer na maior parte das vezes...

Entenderam o racional? O objetivo dessa mudança é deixar o método (property nesse caso) fácil de ler para quem vai usar ela e fácil de entender para quem vai refatorar/reescrever ela...

('TYPE_CYLINDER', 18, 18),
])
def test_package_diameter(package, package_type, diameter, result):
package.package_type = getattr(package, package_type)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parametrize direto com Package.TYPE_ENVELOPE e tira esse getattr()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pergunta: já não tinha testes para dimensões de pacotes cilindricos? não dava pra adicionar isso lá?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eu criei um novo teste para abordar o diâmetro de todos os tipo de objetos, e não somente do tipo cilindrico.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

beleza... então refatora só pra tirar aquele getattr ali..

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Com mais calma: explícito é melhor do que implícito. 🙂

Quando escrevemos um teste é importante que ele também possa servir como algo próximo de um "documento de uso".

Quando colocamos uma string no parametrize desse teste pode passar a mensagem para a pessoa que não viu a implementação do teste que o parâmetro que passamos para o método é do tipo string, o que não é verdade. Mais uma vez é uma questão de escrever código que transmita informação para quem quer ler ele rapidamente e já saber como usar...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice 👍

In [1]: import this
The Zen of Python, by Tim Peters

Explicit is better than implicit.

@osantana
Copy link
Contributor

osantana commented Mar 9, 2018

assim que passar no CI eu já faço merge...

posso fazer release na sequencia?

@bratomes
Copy link
Contributor Author

bratomes commented Mar 9, 2018

Yes, you can :)

@osantana osantana merged commit f688d25 into master Mar 9, 2018
@osantana osantana deleted the fix-diameter-property branch March 9, 2018 16:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants