def test_output_header(self): reader = ReaderWriter() reader.rows = [('aa', 'bb', 'cc', 'dd')] writer = ReaderWriter() m.ExtractMap('b=bb,c=cc', 'a=id').process(reader, writer) self.assertEqual( [('aa', 'dd', 'id')], writer.rows)
def test_ids_are_converted_to_string(self): rw = ReaderWriter() rw.rows = [('id', 'value'), ('1', 'one')] newmap = make_map('value', 'id') newmap.read(rw) self.assertEqual( {('one',): 1}, newmap.values)
def test_content_is_produced_by_process(self): reader = ReaderWriter() reader.rows = [('a', 'b'), (1, 2), (1, 2)] writer = ReaderWriter() t = m.Transformer() t.transform = mock.Mock(t.transform, return_value=sentinel.output) t.process(reader, writer) self.assertEqual([sentinel.output, sentinel.output], writer.rows[1:])
def test_header_is_output_field_names(self): reader = ReaderWriter() reader.rows = [('a', 'b')] writer = ReaderWriter() t = m.Transformer() t.output_field_names = sentinel.output_field_names t.process(reader, writer) self.assertEqual([sentinel.output_field_names], writer.rows)
def test_keep_fields(self): with open('map.csv', 'w') as f: f.write('id,a\n5,a') reader = ReaderWriter() reader.rows = [('a', 'b'), ('a', 'b'), ('c', 'd')] writer = ReaderWriter() m.extract_map(reader, writer, 'map.csv', 'a', 'id', keep_fields=True) self.assertEqual( [('a', 'b', 'id'), ('a', 'b', 5), ('c', 'd', 6)], writer.rows)
def test_existing_map_used(self): with open('map.csv', 'w') as f: f.write('id,a\n5,a') reader = ReaderWriter() reader.rows = [('a', 'b'), ('a', 'b'), ('c', 'd')] writer = ReaderWriter() m.extract_map(reader, writer, 'map.csv', 'a', 'id') self.assertEqual( sorted([('b', 5), ('d', 6)]), sorted(writer.rows[1:]))
def test_changed_map_is_written_out(self): with open('map.csv', 'w') as f: f.write('id,a\n5,a') reader = ReaderWriter() reader.rows = [('a', 'b'), ('a', 'b'), ('c', 'd')] writer = ReaderWriter() m.extract_map(reader, writer, 'map.csv', 'a', 'id') with open('map.csv') as f: items = tuple(csv.reader(f)) self.assertEqual( sorted((('id', 'a'), ('5', 'a'), ('6', 'c'))), map(tuple, sorted(items)))
def test_map_header(self): reader = ReaderWriter() reader.rows = [('aa', 'bb', 'cc', 'dd'), ] writer = ReaderWriter() extract_map = m.ExtractMap('b=bb,c=cc', 'a=id') extract_map.process(reader, writer) map_writer = ReaderWriter() extract_map.map.write(map_writer) # all fields renamed, id comes first - for sorting? self.assertEqual( [('a', 'b', 'c')], map_writer.rows)
def test(self): reader = ReaderWriter() reader.rows = [ ('aa', 'bb', 'cc', 'dd'), (sentinel.aa1, sentinel.bb1, sentinel.cc1, sentinel.dd1), (sentinel.aa2, sentinel.bb2, sentinel.cc2, sentinel.dd2), ] writer = ReaderWriter() m.RemoveFields(['cc', 'aa']).process(reader, writer) self.assertEqual([ ('bb', 'dd'), (sentinel.bb1, sentinel.dd1), (sentinel.bb2, sentinel.dd2), ], writer.rows)
def test(self): reader = ReaderWriter() reader.rows = [ ('aa', 'bb', 'cc', 'dd'), (sentinel.aa1, sentinel.bb1, sentinel.cc1, sentinel.dd1), (sentinel.aa2, sentinel.bb2, sentinel.cc2, sentinel.dd2), ] writer = ReaderWriter() m.RemoveFields(['cc', 'aa']).process(reader, writer) self.assertEqual( [ ('bb', 'dd'), (sentinel.bb1, sentinel.dd1), (sentinel.bb2, sentinel.dd2), ], writer.rows)
def test_output_values(self): reader = ReaderWriter() reader.rows = [ ('aa', 'bb', 'cc', 'dd'), (sentinel.aa1, sentinel.bb1, sentinel.cc1, sentinel.dd1), (sentinel.aa2, sentinel.bb2, sentinel.cc2, sentinel.dd2), (sentinel.aa3, sentinel.bb1, sentinel.cc1, sentinel.dd3), ] writer = ReaderWriter() m.ExtractMap('b=bb,c=cc', 'a=id').process(reader, writer) self.assertEqual( sorted([ (sentinel.aa1, sentinel.dd1, 0), (sentinel.aa2, sentinel.dd2, 1), (sentinel.aa3, sentinel.dd3, 0), ]), sorted(writer.rows[1:]))
def test_keep_fields(self): reader = ReaderWriter() reader.rows = [ ('aa', 'bb', 'cc', 'dd'), (sentinel.aa1, sentinel.bb1, sentinel.cc1, sentinel.dd1), (sentinel.aa2, sentinel.bb2, sentinel.cc2, sentinel.dd2), (sentinel.aa3, sentinel.bb1, sentinel.cc1, sentinel.dd3), ] writer = ReaderWriter() em = m.ExtractMap('b=bb,c=cc', 'a=id', keep_fields=True) em.process(reader, writer) self.assertEqual( [ ('aa', 'bb', 'cc', 'dd', 'id'), (sentinel.aa1, sentinel.bb1, sentinel.cc1, sentinel.dd1, 0), (sentinel.aa2, sentinel.bb2, sentinel.cc2, sentinel.dd2, 1), (sentinel.aa3, sentinel.bb1, sentinel.cc1, sentinel.dd3, 0), ], writer.rows)
def test_map_content(self): reader = ReaderWriter() reader.rows = [ ('aa', 'bb', 'cc', 'dd'), (sentinel.aa1, sentinel.bb1, sentinel.cc1, sentinel.dd1), (sentinel.aa2, sentinel.bb2, sentinel.cc2, sentinel.dd2), (sentinel.aa3, sentinel.bb1, sentinel.cc1, sentinel.dd3), ] writer = ReaderWriter() extract_map = m.ExtractMap('b=bb,c=cc', 'a=id') extract_map.process(reader, writer) map_writer = ReaderWriter() extract_map.map.write(map_writer) # all fields renamed, id comes first - for sorting? self.assertEqual( sorted([ (0, sentinel.bb1, sentinel.cc1), (1, sentinel.bb2, sentinel.cc2), ]), sorted(map_writer.rows[1:]))
def test_calls_bind_before_transform(self): reader = ReaderWriter() reader.rows = [('a', 'b'), (1, 2)] writer = ReaderWriter() BindCheckerTransformer().process(reader, writer)
def test_missing_value_field(self): reader = ReaderWriter() reader.rows = [('id', 'bb')] map = make_map('aa,bb', 'id') self.assertRaises(Exception, lambda: map.read(reader))
def test_valuess_not_unique_dies(self): reader = ReaderWriter() reader.rows = [self.header, (1, 1, 1), (2, 1, 1)] map = make_map('aa,bb', 'id') self.assertRaises(Exception, lambda: map.read(reader))