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

Rend le code des tutos plus pythonique #4772

Merged
merged 5 commits into from
Nov 21, 2017

Conversation

artragis
Copy link
Member

@artragis artragis commented Nov 2, 2017

En reprenant mon travail sur les API je me suis rendu compte que les tests étaient vraiment moches,
en regardant sur codacy, j'ai vu qu'en effet ça causait pas mal de problèmes donc j'ai décidé de commencer à refactorer.

Pour éviter que les changements ne soient trop grands, je n'ai pour l'instant fait que le tearDown mais par la suite je compte ajouter des méthodes utiles à la création de tests.

Notons que j'en ai profité pour fixer #4759

zds/__init__.py Outdated
import simplejson as json_reader
except ImportError:
import json as json_reader
import json as json_writer
Copy link
Contributor

@motet-a motet-a Nov 2, 2017

Choose a reason for hiding this comment

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

Je ne vois pas l’intérêt d’avoir un json_reader et un json_writer qui font exactement la même chose.

Il n’y a aucun risque d’avoir des conflits avec le module json de la bibliothèque standard parce qu’on fait partout un from zds.

Copy link
Member Author

Choose a reason for hiding this comment

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

je ne l'ai jamais compris non plus mais comme ceux qui on mis en place cette distinction depuis casi le début du code ne sont plus actifs sur le dev, on se les traine sans trop se poser de questions.

Copy link
Contributor

Choose a reason for hiding this comment

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

C’est vrai. On changera ça plus tard, dans une autre PR au pire.

Copy link
Contributor

Choose a reason for hiding this comment

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

Bon, du coup c’est pas grave mais cette PR ne corrige pas complètement #4759 (à mon avis).

Copy link
Member Author

Choose a reason for hiding this comment

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

en fait c'est surtout que je pense que créer un fichier pour ça ne sert à rien. Je pense clairement que mon fix est plus logique. Après si tu as des arguments pour tenir la méthodes que tu préconises dans le ticket, je prends.

Copy link
Contributor

@motet-a motet-a Nov 2, 2017

Choose a reason for hiding this comment

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

Alors oui, je n'ai pas pensé à mettre ça dans __init__.py et j'avoue que c'est mieux 🙃 Je parlais de la différence entre json_reader et json_writer qui est complètement inutile à mon avis (je pense qu'on pourrait rassembler et renommer tout ça json).

Copy link
Member Author

Choose a reason for hiding this comment

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

j'ai nommé ça en json_handler comme ça pas de confusion possible.

title = factory.Sequence(lambda n: 'Mon contenu No{0}'.format(n))
description = factory.Sequence(lambda n: 'Description du contenu No{0}'.format(n))
title = factory.Sequence('Mon contenu No{0}'.format)
description = factory.Sequence('Description du contenu No{0}'.format)
Copy link
Contributor

Choose a reason for hiding this comment

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

Attention avec le nombre de paramètres quand-même. Je ne connais pas trop Python, mais en JS, [1, 2, 3].map(someFunction) n’est pas l’équivalent de [1, 2, 3].map(n => someFunction(n)) (et c’est source de bugs bien traîtres).

Copy link
Member Author

Choose a reason for hiding this comment

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

sauf que contrairement à js, s'il y avait deux arguments passés via Sequence python aurait crashé car il n'y a pas de "trou noir undefined" si tu passes deux arguments à une fonction qui n'en accepte qu'un ça crash.

Copy link
Contributor

Choose a reason for hiding this comment

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

Pas faux. En fait, je me suis dit qu’il y avait peut-être un truc vu qu’ils utilisent une lambda dans la doc de factory-boy. Mais c’était peut-être simplement pour essayer de faire comprendre aux gens ce qu’il se passait.

def setUp(self):

self.overriden_zds_app = overridden_zds_app
Copy link
Contributor

Choose a reason for hiding this comment

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

s/overriden/overridden

Copy link
Member Author

Choose a reason for hiding this comment

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

je fix


def setUp(self):

settings.overriden_zds_app = overridden_zds_app
Copy link
Contributor

Choose a reason for hiding this comment

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

s/overriden/overridden

Copy link
Contributor

@motet-a motet-a left a comment

Choose a reason for hiding this comment

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

Ce n’est peut-être pas faisable immédiatement, mais je me demande si il n’y aurait pas une manière plus propre et plus simple de gérer ces overridden_zds_settings.

@motet-a motet-a added C-Back Concerne le back-end Django S-Refactorisation Améliore le code existant sans forcément ajouter de nouvelle fonctionnalité labels Nov 2, 2017
shutil.rmtree(settings.MEDIA_ROOT)
if os.path.isdir(overridden_zds_app['content']['extra_content_watchdog_dir']):
shutil.rmtree(overridden_zds_app['content']['extra_content_watchdog_dir'])
super().tearDown()
Copy link
Contributor

Choose a reason for hiding this comment

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

Si je ne dis pas de bêtises, ceci va casser si la classe du test hérite de plus d’un mixin qui possède un attribut tearDown, non ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Non, s'il y a plus d'un tearDown, il exécutera les teardown de toutes les classes : https://stackoverflow.com/questions/24166225/python3-behaviour-of-super-on-multi-inheritance

@artragis artragis force-pushed the fix_code_pythonicite branch 4 times, most recently from 1cdf2e0 to 86718d2 Compare November 2, 2017 09:56
@zestedesavoir zestedesavoir deleted a comment from coveralls Nov 2, 2017
@zestedesavoir zestedesavoir deleted a comment from coveralls Nov 2, 2017
@zestedesavoir zestedesavoir deleted a comment from coveralls Nov 2, 2017
et j'en profite pour régler le problème de l'import json.

Pour ce qui est des tests, j'ai commencé à travailler à les factoriser un peu pour qu'ils soient plus lisibles et donc plus maintenables et fiables. Pour l'instant j'ai factorisé que le tearDown mais j'ajouterai des choses après.
@Anto59290
Copy link
Contributor

Belle simplication du code 👍

@zestedesavoir zestedesavoir deleted a comment from coveralls Nov 4, 2017
@zestedesavoir zestedesavoir deleted a comment from coveralls Nov 4, 2017
@motet-a
Copy link
Contributor

motet-a commented Nov 4, 2017

Pas mal 🙃 Mais encore une fois, je pense qu’on pourrait très bien renommer json_handler en json.


class TutorialTestMixin:
def clean_media_dir(self):
if os.path.isdir(self.overridden_zds_app['content']['repo_private_path']):
Copy link
Contributor

@motet-a motet-a Nov 4, 2017

Choose a reason for hiding this comment

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

En fait on peut enlever tous ces if et utiliser shutil.rmtree(path, ignore_errors=True). C’est un poil plus sûr parce que c’est atomique, et c’est même plus rapide, ça évite de stat(2) pour rien.

Copy link
Member Author

Choose a reason for hiding this comment

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

anéfé.

Copy link
Member Author

Choose a reason for hiding this comment

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

C'est modifié, je vous laisse squash mes commits.

@artragis
Copy link
Member Author

artragis commented Nov 5, 2017

@motet-a j'aime pas l'idée de faire d'un côté un travail d'abstraction et d'un autre lui donner le nom d'un des modules qu'on abstrait. Peut être mon côté java/C# qui sort trop.

@zestedesavoir zestedesavoir deleted a comment from coveralls Nov 5, 2017
@zestedesavoir zestedesavoir deleted a comment from coveralls Nov 5, 2017
@Situphen Situphen removed the QA svp label Nov 11, 2017
@zestedesavoir zestedesavoir deleted a comment from coveralls Nov 11, 2017
@zestedesavoir zestedesavoir deleted a comment from coveralls Nov 11, 2017
@coveralls
Copy link

coveralls commented Nov 21, 2017

Coverage Status

Coverage increased (+0.03%) to 89.794% when pulling 60c86db on artragis:fix_code_pythonicite into b978c14 on zestedesavoir:dev.

@zestedesavoir zestedesavoir deleted a comment from coveralls Nov 21, 2017
@zestedesavoir zestedesavoir deleted a comment from coveralls Nov 21, 2017
@motet-a motet-a added this to the Version de développement milestone Nov 21, 2017
Copy link
Member

@gllmc gllmc left a comment

Choose a reason for hiding this comment

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

👍

@gllmc gllmc merged commit 4b606e8 into zestedesavoir:dev Nov 21, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-Back Concerne le back-end Django S-Refactorisation Améliore le code existant sans forcément ajouter de nouvelle fonctionnalité
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants