From 0e9404ae9332c2e821247352689df9d8fe6eb2ea Mon Sep 17 00:00:00 2001 From: Valentin Samir Date: Mon, 24 Feb 2014 21:49:23 +0100 Subject: [PATCH] =?UTF-8?q?[test.py]=20utilisation=20de=20contexts=20pour?= =?UTF-8?q?=20les=20=C3=A9critures=20de=20la=20base=20ldap?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test.py | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/test.py b/test.py index 4c07515..e3d05b9 100755 --- a/test.py +++ b/test.py @@ -79,7 +79,8 @@ def keys_of_list_of_dict(l, type=''): keys = keys.union(item.attrs.keys()) try: if item.mode in ['w', 'rw']: - item.save() + with item: + item.save() except Exception as error: anime.reinit() print ERREUR @@ -124,9 +125,9 @@ def tests_machines(parent_dn, realm_list, ipsec=False): mldif = borne_ldif else: mldif = machine_ldif - machine = conn.newMachine(parent_dn, realm, mldif) - if ipsec: machine['ipsec'] = u'auto' - machine.create() + with conn.newMachine(parent_dn, realm, mldif) as machine: + if ipsec: machine['ipsec'] = u'auto' + machine.create() except Exception as error: print ERREUR if show_traceback: print traceback.format_exc() @@ -135,9 +136,9 @@ def tests_machines(parent_dn, realm_list, ipsec=False): print OK anim("Suppression d'une machines %s" % realm) try: - machine = conn.search(u'mid=%s' % machine['mid'][0], mode='rw')[0] - machine.delete() - del(machine) + with conn.search(u'mid=%s' % machine['mid'][0], mode='rw')[0] as machine: + machine.delete() + del(machine) except Exception as error: print ERREUR if show_traceback: print traceback.format_exc() @@ -196,8 +197,8 @@ print "Instanciation" anim("Creation d'un adherent") try: - adherent = conn.newAdherent(adherent_ldif) - adherent.create() + with conn.newAdherent(adherent_ldif) as adherent: + adherent.create() except Exception: print ERREUR print traceback.format_exc() @@ -223,9 +224,9 @@ tests_machines(variables.base_dn, ["bornes"]) anim("Creation d'un club") try: - club = conn.newClub(club_ldif) - club['responsable'] = unicode(adherent['aid'][0]) - club.create() + with conn.newClub(club_ldif) as club: + club['responsable'] = unicode(adherent['aid'][0]) + club.create() except Exception: print ERREUR print traceback.format_exc() @@ -236,8 +237,8 @@ else: anim("Suppression d'un club") try: - club = conn.search(u'cid=%s' % club['cid'][0], mode='rw')[0] - club.delete() + with conn.search(u'cid=%s' % club['cid'][0], mode='rw')[0] as club: + club.delete() except Exception: print ERREUR print traceback.format_exc() @@ -250,8 +251,8 @@ else: anim("Creation d'une facture") try: - facture = conn.newFacture(adherent.dn, facture_ldif) - facture.create() + with conn.newFacture(adherent.dn, facture_ldif) as facture: + facture.create() except Exception as error: print ERREUR if show_traceback: print traceback.format_exc() @@ -260,8 +261,8 @@ else: print OK anim("Suppression d'une facture") try: - facture = conn.search(u'fid=%s' % facture['fid'][0], mode='rw')[0] - facture.delete() + with conn.search(u'fid=%s' % facture['fid'][0], mode='rw')[0] as facture: + facture.delete() except Exception: print ERREUR print traceback.format_exc() @@ -275,8 +276,8 @@ else: if adherent: anim("Suppression d'un adherent") try: - adherent = conn.search(u'aid=%s' % adherent['aid'][0], mode='rw')[0] - adherent.delete() + with conn.search(u'aid=%s' % adherent['aid'][0], mode='rw')[0] as adherent: + adherent.delete() except Exception: print ERREUR print traceback.format_exc()