-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#40 add PacketBuffer Filter and Component-instance-lookup in Chain to…
… help Unit test result inspection/assertions
- Loading branch information
Showing
5 changed files
with
185 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Packet buffering. | ||
# | ||
# Author:Just van den Broecke | ||
|
||
import copy | ||
from stetl.util import Util | ||
from stetl.filter import Filter | ||
from stetl.packet import FORMAT | ||
|
||
log = Util.get_log("packetbuffer") | ||
|
||
class PacketBuffer(Filter): | ||
""" | ||
Buffers all incoming Packets, main use is unit-testing to inspect Packets after ETL is done. | ||
""" | ||
|
||
# Constructor | ||
def __init__(self, configdict, section): | ||
Filter.__init__(self, configdict, section, consumes=FORMAT.any, produces=FORMAT.any) | ||
self.packet_list = [] | ||
|
||
def invoke(self, packet): | ||
# Buffer Packet and pass-through, we need a deep copy as Packets may be cleared/reused | ||
self.packet_list.append(copy.copy(packet)) | ||
return packet |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Config file for unit testing XmlAssembler. | ||
|
||
[etl] | ||
# chains = input_glob_file|parse_xml_file_filter|xml_assembler|output_std | ||
chains = parse_xml_file_input|xml_assembler|packet_buffer|output_std | ||
|
||
[input_glob_file] | ||
class = inputs.fileinput.GlobFileInput | ||
file_path = tests/data/dummy.gml | ||
|
||
# The source input file producing XML elements | ||
[parse_xml_file_filter] | ||
class = filters.xmlelementreader.XmlElementReader | ||
element_tags = FeatureMember | ||
|
||
[parse_xml_file_input] | ||
class = inputs.fileinput.XmlElementStreamerFileInput | ||
element_tags = FeatureMember | ||
file_path = tests/data/dummy.gml | ||
|
||
# Assembles etree docs gml:featureMember elements, each with "max_elements" elements | ||
[xml_assembler] | ||
class = filters.xmlassembler.XmlAssembler | ||
max_elements = 2 | ||
container_doc = <?xml version="1.0" encoding="UTF-8"?> | ||
<gml:FeatureCollectionT10NL | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns:top10nl="http://www.kadaster.nl/schemas/imbrt/top10nl/1.2" | ||
xmlns:brt="http://www.kadaster.nl/schemas/imbrt/brt-alg/1.0" | ||
xmlns:gml="http://www.opengis.net/gml/3.2" | ||
xsi:schemaLocation="http://www.kadaster.nl/schemas/imbrt/top10nl/1.2 http://www.kadaster.nl/schemas/top10nl/vyyyymmdd/TOP10NL_1_2.xsd"> | ||
</gml:FeatureCollectionT10NL > | ||
element_container_tag = FeatureCollectionT10NL | ||
|
||
[packet_buffer] | ||
class = filters.packetbuffer.PacketBuffer | ||
|
||
[output_std] | ||
class = outputs.standardoutput.StandardOutput |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import os | ||
#import re | ||
import sys | ||
|
||
from stetl.etl import ETL | ||
from stetl.filters.xmlassembler import XmlAssembler | ||
from stetl.filters.packetbuffer import PacketBuffer | ||
from tests.stetl_test_case import StetlTestCase | ||
|
||
class XmlAssemblerTest(StetlTestCase): | ||
"""Unit tests for XmlAssembler""" | ||
|
||
def setUp(self): | ||
super(XmlAssemblerTest, self).setUp() | ||
|
||
# Initialize Stetl | ||
curr_dir = os.path.dirname(os.path.realpath(__file__)) | ||
cfg_dict = {'config_file': os.path.join(curr_dir, 'configs/xmlassembler.cfg')} | ||
self.etl = ETL(cfg_dict) | ||
|
||
def test_class(self): | ||
chain = StetlTestCase.get_chain(self.etl) | ||
section = StetlTestCase.get_section(chain, 1) | ||
class_name = self.etl.configdict.get(section, 'class') | ||
|
||
self.assertEqual('filters.xmlassembler.XmlAssembler', class_name) | ||
|
||
def test_instance(self): | ||
chain = StetlTestCase.get_chain(self.etl) | ||
|
||
self.assertTrue(isinstance(chain.get_by_index(1), XmlAssembler)) | ||
|
||
def test_execute(self): | ||
chain = StetlTestCase.get_chain(self.etl) | ||
chain.run() | ||
|
||
buffer_filter = chain.get_by_class(PacketBuffer) | ||
packet_list = buffer_filter.packet_list | ||
|
||
# most Packets are empty, but we need to find 2 filled with etree docs | ||
doc_packet_list = [] | ||
for packet in packet_list: | ||
if packet.data: | ||
doc_packet_list.append(packet) | ||
|
||
# Assertion: we need to see 2 documents | ||
self.assertEqual(len(doc_packet_list), 2) | ||
namespaces={'gml': 'http://www.opengis.net/gml/3.2', 'top10nl': 'http://register.geostandaarden.nl/gmlapplicatieschema/top10nl/1.2.0'} | ||
|
||
# Assertion: first doc has two FeatureMember elements with proper Namespaces | ||
xml_doc1 = doc_packet_list[0].data | ||
feature_elms = xml_doc1.xpath('/gml:FeatureCollectionT10NL/top10nl:FeatureMember', namespaces=namespaces) | ||
self.assertEqual(len(feature_elms), 2) | ||
|
||
# Assertion: second doc has one FeatureMember with proper Namespaces | ||
xml_doc2 = doc_packet_list[1].data | ||
feature_elms = xml_doc2.xpath('/gml:FeatureCollectionT10NL/top10nl:FeatureMember', namespaces=namespaces) | ||
self.assertEqual(len(feature_elms), 1) | ||
|
||
# Assertion: first doc has end_of_doc but not end_of_stream set | ||
self.assertTrue(doc_packet_list[0].end_of_doc, msg='doc1: end_of_doc if False') | ||
self.assertFalse(doc_packet_list[0].end_of_stream, msg='doc1: end_of_stream is True') | ||
|
||
# Assertion: second doc has end_of_doc and end_of_stream set | ||
self.assertTrue(doc_packet_list[1].end_of_doc, msg='doc2: end_of_doc if False') | ||
self.assertTrue(doc_packet_list[1].end_of_stream, msg='doc2: end_of_stream if False') |