def test_alternate_quotes(self): p1 = CsvSingleQuotedProtocol() # old style p2 = CsvProtocol(quotechar="'") # new style line = "'foo', 'bar', 'baz', 1, 2, 3.333" res = [u'foo', u'bar', u'baz', u'1', u'2', u'3.333'] self.assertEqual(p1.read(line), (None, res)) self.assertEqual(p2.read(line), (None, res))
def test_format_strings(self): p = CsvProtocol() io = [('Benoit Mandelbrot', u'"Benoit Mandelbrot"'), (u'Paul Erdős', u'"Paul Erdős"'), (101, u'101'), (2.718281, u'2.718281')] for t in io: self.assertEqual(p.fmt(t[0]), t[1])
def test_alternate_delimiter(self): p = CsvProtocol(delimiter='|') line = "foo|bar|baz|1|2|3.333\n" res = [u'foo', u'bar', u'baz', u'1', u'2', u'3.333'] self.assertEqual(p.read(line), (None, res))
def test_write_line(self): p = CsvProtocol() data = ['foo', 'bar', 'baz', 1, 2, 3.333] res = u"\"foo\",\"bar\",\"baz\",1,2,3.333" self.assertEqual(p.write(None, data), res)
def test_trailing_separator(self): p = CsvProtocol() line = '"foo", "bar", "baz", 1, 2, 3.333, ' res = [u'foo', u'bar', u'baz', u'1', u'2', u'3.333', u''] self.assertEqual(p.read(line), (None, res))
def test_read_unicode(self): p = CsvProtocol() line = '"Paul", "Erdős", "foo", 1, 2, 3.333' res = [u'Paul', u'Erdős', u'foo', u'1', u'2', u'3.333'] self.assertEqual(p.read(line), (None, res))
def test_drop_quotes(self): p = CsvProtocol() line = "\"foo\", \"bar\", \"baz\", 1, 2, 3.333" res = [u'foo', u'bar', u'baz', u'1', u'2', u'3.333'] self.assertEqual(p.read(line), (None, res))
def test_read_one_line(self): p = CsvProtocol() line = "foo, bar, baz, 1, 2, 3.333\n" res = [u'foo', u'bar', u'baz', u'1', u'2', u'3.333'] self.assertEqual(p.read(line), (None, res))
def test_write_line(self): p = CsvProtocol() data = ['foo', 'bar', 'baz', 1, 2, 3.333] res = u"\"foo\",\"bar\",\"baz\",1,2,3.333" self.assertEqual(p.write(None, data), six.b(res))