Skip to content

Commit

Permalink
TestChangeRecords.test_simple() almost works
Browse files Browse the repository at this point in the history
  • Loading branch information
stroeder committed Jul 17, 2016
1 parent d70170c commit 0c7977c
Showing 1 changed file with 57 additions and 48 deletions.
105 changes: 57 additions & 48 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.17 2016/07/17 16:25:46 stroeder Exp $
$Id: t_ldif.py,v 1.18 2016/07/17 17:43:39 stroeder Exp $
"""

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


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 `records'
"""
ldif_file = StringIO()
ldif_writer = ldif.LDIFWriter(ldif_file)
for dn, entry in records:
ldif_writer.unparse(dn, entry)
return ldif_file.getvalue()


class TestLDIFParser(unittest.TestCase):
"""
Various LDIF test cases
"""

def _parse_records(
self,
ldif_string,
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' % self.record_type
)
parser_method()
if self.record_type == 'entry':
return ldif_parser.all_records
elif self.record_type == 'change':
return ldif_parser.all_modify_changes

def _unparse_records(self, records):
"""
Returns LDIF string with entry records from list `records'
"""
ldif_file = StringIO()
ldif_writer = ldif.LDIFWriter(ldif_file)
if self.record_type == 'entry':
for dn, entry in records:
ldif_writer.unparse(dn, entry)
elif self.record_type == 'change':
for dn, modops, controls in records:
ldif_writer.unparse(dn, modops)
return ldif_file.getvalue()

def check_records(
self,
ldif_string,
records,
record_type='entry',
ignored_attr_types=None,
max_entries=0
):
Expand All @@ -72,15 +76,13 @@ def check_records(
and matches list of unparsed `records'.
"""
ldif_string = textwrap.dedent(ldif_string).lstrip() + '\n'
parsed_records = parse_records(
parsed_records = self._parse_records(
ldif_string,
record_type=record_type,
ignored_attr_types=ignored_attr_types,
max_entries=max_entries,
)
parsed_records2 = parse_records(
unparse_records(records),
record_type=record_type,
parsed_records2 = self._parse_records(
self._unparse_records(records),
ignored_attr_types=ignored_attr_types,
max_entries=max_entries,
)
Expand All @@ -92,11 +94,13 @@ class TestEntryRecords(TestLDIFParser):
"""
Various LDIF test cases
"""
record_type='entry'

def test_empty(self):
self.check_records(
"""
version: 1
""",
[]
)
Expand Down Expand Up @@ -188,7 +192,7 @@ def test_folded(self):
]
)

def test_empty(self):
def test_empty_attr_values(self):
self.check_records(
"""
dn: cn=x,cn=y,cn=z
Expand Down Expand Up @@ -401,14 +405,14 @@ class TestChangeRecords(TestLDIFParser):
"""
Various LDIF test cases
"""
record_type='change'

def test_empty(self):
self.check_records(
"""
version: 1
""",
[],
record_type='change',
)

def test_simple(self):
Expand All @@ -433,12 +437,17 @@ def test_simple(self):
-
""",
[
(ldif.MOD_OP_INTEGER['replace'], 'attrib', [b'value', b'value2']),
(ldif.MOD_OP_INTEGER['add'], 'attrib2', [b'value', b'value2']),
(ldif.MOD_OP_INTEGER['delete'], 'attrib3', [b'value']),
(ldif.MOD_OP_INTEGER['delete'], 'attrib4', None),
(
'cn=x,cn=y,cn=z',
[
(ldif.MOD_OP_INTEGER['replace'], 'attrib', [b'value', b'value2']),
(ldif.MOD_OP_INTEGER['add'], 'attrib2', [b'value', b'value2']),
(ldif.MOD_OP_INTEGER['delete'], 'attrib3', [b'value']),
(ldif.MOD_OP_INTEGER['delete'], 'attrib4', None),
],
[],
),
],
record_type='change',
)

if __name__ == '__main__':
Expand Down

0 comments on commit 0c7977c

Please sign in to comment.