def test_partition(): t = [('fruit', 'city', 'sales'), ('orange', 'London', 12), ('banana', 'London', 42), ('orange', 'Paris', 31), ('banana', 'Amsterdam', 74), ('kiwi', 'Berlin', 55)] p = partition('fruit') fn1 = NamedTemporaryFile().name fn2 = NamedTemporaryFile().name p.pipe('orange', tocsv(fn1)) p.pipe('banana', tocsv(fn2)) p.push(t) oranges_expected = [('fruit', 'city', 'sales'), ('orange', 'London', '12'), ('orange', 'Paris', '31')] bananas_expected = [('fruit', 'city', 'sales'), ('banana', 'London', '42'), ('banana', 'Amsterdam', '74')] oranges_actual = fromcsv(fn1) bananas_actual = fromcsv(fn2) ieq(oranges_expected, oranges_actual) ieq(bananas_expected, bananas_actual) # alternative syntax p = partition('fruit') p | ('orange', tocsv(fn1)) p | ('banana', tocsv(fn2)) p.push(t) ieq(oranges_expected, oranges_actual) ieq(bananas_expected, bananas_actual) # test with callable discriminator p = partition(lambda row: row['sales'] > 40) p | (True, tocsv(fn1)) p | (False, tocsv(fn2)) p.push(t) high_expected = [('fruit', 'city', 'sales'), ('banana', 'London', '42'), ('banana', 'Amsterdam', '74'), ('kiwi', 'Berlin', '55')] low_expected = [('fruit', 'city', 'sales'), ('orange', 'London', '12'), ('orange', 'Paris', '31')] high_actual = fromcsv(fn1) low_actual = fromcsv(fn2) ieq(high_expected, high_actual) ieq(low_expected, low_actual)
def test_partition(): t = [ ("fruit", "city", "sales"), ("orange", "London", 12), ("banana", "London", 42), ("orange", "Paris", 31), ("banana", "Amsterdam", 74), ("kiwi", "Berlin", 55), ] p = partition("fruit") fn1 = NamedTemporaryFile().name fn2 = NamedTemporaryFile().name p.pipe("orange", tocsv(fn1)) p.pipe("banana", tocsv(fn2)) p.push(t) oranges_expected = [("fruit", "city", "sales"), ("orange", "London", "12"), ("orange", "Paris", "31")] bananas_expected = [("fruit", "city", "sales"), ("banana", "London", "42"), ("banana", "Amsterdam", "74")] oranges_actual = fromcsv(fn1) bananas_actual = fromcsv(fn2) ieq(oranges_expected, oranges_actual) ieq(bananas_expected, bananas_actual) # alternative syntax p = partition("fruit") p | ("orange", tocsv(fn1)) p | ("banana", tocsv(fn2)) p.push(t) ieq(oranges_expected, oranges_actual) ieq(bananas_expected, bananas_actual) # test with callable discriminator p = partition(lambda row: row["sales"] > 40) p | (True, tocsv(fn1)) p | (False, tocsv(fn2)) p.push(t) high_expected = [ ("fruit", "city", "sales"), ("banana", "London", "42"), ("banana", "Amsterdam", "74"), ("kiwi", "Berlin", "55"), ] low_expected = [("fruit", "city", "sales"), ("orange", "London", "12"), ("orange", "Paris", "31")] high_actual = fromcsv(fn1) low_actual = fromcsv(fn2) ieq(high_expected, high_actual) ieq(low_expected, low_actual)
def test_partition(): t = [('fruit', 'city', 'sales'), ('orange', 'London', 12), ('banana', 'London', 42), ('orange', 'Paris', 31), ('banana', 'Amsterdam', 74), ('kiwi', 'Berlin', 55)] p = partition('fruit') p.pipe('orange', tocsv('oranges.csv')) p.pipe('banana', tocsv('bananas.csv')) p.push(t) oranges_expected = [('fruit', 'city', 'sales'), ('orange', 'London', '12'), ('orange', 'Paris', '31')] bananas_expected = [('fruit', 'city', 'sales'), ('banana', 'London', '42'), ('banana', 'Amsterdam', '74')] oranges_actual = fromcsv('oranges.csv') bananas_actual = fromcsv('bananas.csv') iassertequal(oranges_expected, oranges_actual) iassertequal(bananas_expected, bananas_actual) # alternative syntax p = partition('fruit') p | ('orange', tocsv('oranges.csv')) p | ('banana', tocsv('bananas.csv')) p.push(t) iassertequal(oranges_expected, oranges_actual) iassertequal(bananas_expected, bananas_actual) # test with callable discriminator p = partition(lambda row: row['sales'] > 40) p | (True, tocsv('high.csv')) p | (False, tocsv('low.csv')) p.push(t) high_expected = [('fruit', 'city', 'sales'), ('banana', 'London', '42'), ('banana', 'Amsterdam', '74'), ('kiwi', 'Berlin', '55')] low_expected = [('fruit', 'city', 'sales'), ('orange', 'London', '12'), ('orange', 'Paris', '31')] high_actual = fromcsv('high.csv') low_actual = fromcsv('low.csv') iassertequal(high_expected, high_actual) iassertequal(low_expected, low_actual)
def test_tocsv_pipe(): t = [('fruit', 'city', 'sales'), ('orange', 'London', '12'), ('banana', 'London', '42'), ('orange', 'Paris', '31'), ('banana', 'Amsterdam', '74'), ('kiwi', 'Berlin', '55')] f1 = NamedTemporaryFile(delete=False) f2 = NamedTemporaryFile(delete=False) p = tocsv(f1.name) p.pipe(tocsv(f2.name)) p.push(t) ieq(t, fromcsv(f1.name)) ieq(t, fromcsv(f2.name))
def test_tocsv_pipe(): t = [('fruit', 'city', 'sales'), ('orange', 'London', '12'), ('banana', 'London', '42'), ('orange', 'Paris', '31'), ('banana', 'Amsterdam', '74'), ('kiwi', 'Berlin', '55')] f1 = NamedTemporaryFile(delete=False) f2 = NamedTemporaryFile(delete=False) p = tocsv(f1.name) p.pipe(tocsv(f2.name)) p.push(t) iassertequal(t, fromcsv(f1.name)) iassertequal(t, fromcsv(f2.name))
def test_tocsv_pipe(): t = [('fruit', 'city', 'sales'), ('orange', 'London', '12'), ('banana', 'London', '42'), ('orange', 'Paris', '31'), ('banana', 'Amsterdam', '74'), ('kiwi', 'Berlin', '55')] fn1 = NamedTemporaryFile().name fn2 = NamedTemporaryFile().name p = tocsv(fn1) p.pipe(tocsv(fn2)) p.push(t) ieq(t, fromcsv(fn1)) ieq(t, fromcsv(fn2))
def test_tocsv_pipe(): t = [ ("fruit", "city", "sales"), ("orange", "London", "12"), ("banana", "London", "42"), ("orange", "Paris", "31"), ("banana", "Amsterdam", "74"), ("kiwi", "Berlin", "55"), ] f1 = NamedTemporaryFile(delete=False) f2 = NamedTemporaryFile(delete=False) p = tocsv(f1.name) p.pipe(tocsv(f2.name)) p.push(t) ieq(t, fromcsv(f1.name)) ieq(t, fromcsv(f2.name))
def test_tocsv_pipe(): t = [ ("fruit", "city", "sales"), ("orange", "London", "12"), ("banana", "London", "42"), ("orange", "Paris", "31"), ("banana", "Amsterdam", "74"), ("kiwi", "Berlin", "55"), ] fn1 = NamedTemporaryFile().name fn2 = NamedTemporaryFile().name p = tocsv(fn1) p.pipe(tocsv(fn2)) p.push(t) ieq(t, fromcsv(fn1)) ieq(t, fromcsv(fn2))
def test_tocsv(): t = [('fruit', 'city', 'sales'), ('orange', 'London', '12'), ('banana', 'London', '42'), ('orange', 'Paris', '31'), ('banana', 'Amsterdam', '74'), ('kiwi', 'Berlin', '55')] f = NamedTemporaryFile(delete=False) p = tocsv(f.name) p.push(t) iassertequal(t, fromcsv(f.name))