Skip to content

Commit

Permalink
style
Browse files Browse the repository at this point in the history
  • Loading branch information
stroeder committed Feb 29, 2016
1 parent 1dff225 commit 2d57f9d
Showing 1 changed file with 46 additions and 36 deletions.
82 changes: 46 additions & 36 deletions Tests/t_ldif.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
See http://www.python-ldap.org/ for details.
$Id: t_ldif.py,v 1.13 2016/02/10 22:11:04 stroeder Exp $
$Id: t_ldif.py,v 1.14 2016/02/29 18:55:59 stroeder Exp $
"""

# from Python's standard lib
Expand All @@ -20,35 +20,44 @@
import ldif


class TestEntryRecords(unittest.TestCase):
def parse_records(
ldif_string,
record_type='entry',
ignored_attr_types=None,
max_entries=0,
):
"""
Parse LDIF data in `ldif_string' into list of records
"""
ldif_file = StringIO(ldif_string)
ldif_parser = ldif.LDIFRecordList(
ldif_file,
ignored_attr_types=ignored_attr_types,
max_entries=max_entries,
)
parser_method = getattr(
ldif_parser,
'parse_%s_records' % record_type
)
parser_method()
return ldif_parser.all_records


def unparse_records(records):
"""
Returns LDIF string with entry records from list `entry_records'
"""
ldif_file = StringIO()
ldif_writer = ldif.LDIFWriter(ldif_file)
for dn, entry in records:
ldif_writer.unparse(dn, entry)
return ldif_file.getvalue()

def _parse_entry_records(
self,
ldif_string,
ignored_attr_types=None,
max_entries=0,
):
"""
Parse LDIF data in `ldif_string' into list of entry records
"""
ldif_file = StringIO(ldif_string)
ldif_parser = ldif.LDIFRecordList(
ldif_file,
ignored_attr_types=ignored_attr_types,
max_entries=max_entries,
)
ldif_parser.parse_entry_records()
return ldif_parser.all_records

def _unparse_entry_records(self, entry_records):
"""
Returns LDIF string with entry records from list `entry_records'
"""
ldif_file = StringIO()
ldif_writer = ldif.LDIFWriter(ldif_file)
for dn, entry in entry_records:
ldif_writer.unparse(dn, entry)
return ldif_file.getvalue()
class TestEntryRecords(unittest.TestCase):
"""
Various LDIF test cases
"""

def check_entry_records(
self,
Expand All @@ -62,13 +71,13 @@ def check_entry_records(
and matches list of unparsed `entry_records'.
"""
ldif_string = textwrap.dedent(ldif_string).lstrip() + '\n'
parsed_entry_records = self._parse_entry_records(
parsed_entry_records = parse_records(
ldif_string,
ignored_attr_types=ignored_attr_types,
max_entries=max_entries,
)
parsed_entry_records2 = self._parse_entry_records(
self._unparse_entry_records(entry_records),
parsed_entry_records2 = parse_records(
unparse_records(entry_records),
ignored_attr_types=ignored_attr_types,
max_entries=max_entries,
)
Expand Down Expand Up @@ -146,16 +155,17 @@ def test_folded(self):
self.check_entry_records(
"""
dn: cn=x,cn=y,cn=z
attrib: very
attrib: very\x20
long
value
line-folded\x20
value
attrib2: %s
""" % ('asdf.' * 20), [
""" % (b'asdf.'*20), [
(
'cn=x,cn=y,cn=z',
{
'attrib': [b'verylong value'],
'attrib2': [b'asdf.' * 20],
'attrib': [b'very long line-folded value'],
'attrib2': [b'asdf.'*20],
}
),
]
Expand Down

0 comments on commit 2d57f9d

Please sign in to comment.