From 319b8124be57fada18c1d3e785a97a8cf18438ad Mon Sep 17 00:00:00 2001 From: Ilya Kreymer Date: Mon, 22 Sep 2014 21:12:25 -0700 Subject: [PATCH] cdxobject: add ability to create empty CDXObject(), add tests for CDXObject/IDXObject checking for supported and unsupported number of fields --- pywb/cdx/cdxobject.py | 8 +++++++- pywb/cdx/test/test_cdxobject.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 pywb/cdx/test/test_cdxobject.py diff --git a/pywb/cdx/cdxobject.py b/pywb/cdx/cdxobject.py index b64b0861..7a190be4 100644 --- a/pywb/cdx/cdxobject.py +++ b/pywb/cdx/cdxobject.py @@ -46,10 +46,16 @@ class CDXObject(OrderedDict): "orig.length", "orig.offset", "orig.filename"] ] - def __init__(self, cdxline): + def __init__(self, cdxline=''): OrderedDict.__init__(self) cdxline = cdxline.rstrip() + + # Allows for filling the fields later or in a custom way + if not cdxline: + self.cdxline = cdxline + return + fields = cdxline.split(' ') cdxformat = None diff --git a/pywb/cdx/test/test_cdxobject.py b/pywb/cdx/test/test_cdxobject.py new file mode 100644 index 00000000..13e242a7 --- /dev/null +++ b/pywb/cdx/test/test_cdxobject.py @@ -0,0 +1,32 @@ +from pywb.cdx.cdxobject import CDXObject, IDXObject, CDXException +from pytest import raises + +def test_empty_cdxobject(): + x = CDXObject('') + assert len(x) == 0 + +def test_invalid_cdx_format(): + with raises(CDXException): + x = CDXObject('a b c') + + +def _make_line(fields): + line = ' '.join(['-'] * fields) + x = CDXObject(line) + assert len(x) == fields + assert str(x) == line + +def test_valid_cdx_formats(): + # Currently supported cdx formats, 9, 11, 12, 14 field + # See CDXObject for more details + _make_line(9) + _make_line(12) + + _make_line(11) + _make_line(14) + +def test_invalid_idx_format(): + with raises(CDXException): + x = IDXObject('a b c') + +