Skip to content

Commit

Permalink
Refactor easy-to-move table tests into modules. wireservice#482
Browse files Browse the repository at this point in the history
  • Loading branch information
nbedi committed Mar 11, 2016
1 parent c152cc0 commit 2993152
Show file tree
Hide file tree
Showing 13 changed files with 2,689 additions and 2,599 deletions.
2,599 changes: 0 additions & 2,599 deletions tests/test_table.py

This file was deleted.

1,278 changes: 1,278 additions & 0 deletions tests/test_table/__init__.py

Large diffs are not rendered by default.

39 changes: 39 additions & 0 deletions tests/test_table/test_aggregate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env python
# -*- coding: utf8 -*-

from agate import Table
from agate.aggregations import Count, Sum
from agate.data_types import *
from agate.testcase import AgateTestCase


class TestAggregate(AgateTestCase):
def setUp(self):
self.rows = (
(1, 4, 'a'),
(2, 3, 'b'),
(None, 2, u'👍')
)

self.number_type = Number()
self.text_type = Text()

self.column_names = ['one', 'two', 'three']
self.column_types = [self.number_type, self.number_type, self.text_type]

self.table = Table(self.rows, self.column_names, self.column_types)

def test_count(self):
self.assertEqual(self.table.aggregate(Count()), 3)

def test_sum(self):
self.assertEqual(self.table.aggregate(Sum('two')), 9)

def test_multiple(self):
self.assertSequenceEqual(
self.table.aggregate([
Count(),
Sum('two')
]),
[3, 9]
)
121 changes: 121 additions & 0 deletions tests/test_table/test_bins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#!/usr/bin/env python
# -*- coding: utf8 -*-

try:
from cdecimal import Decimal
except ImportError: # pragma: no cover
from decimal import Decimal

from agate import Table
from agate.data_types import *
from agate.testcase import AgateTestCase


class TestBins(AgateTestCase):
def setUp(self):
self.number_type = Number()
self.column_names = ['number']
self.column_types = [self.number_type]

def test_bins(self):
rows = []

for i in range(0, 100):
rows.append([i]),

new_table = Table(rows, self.column_names, self.column_types).bins('number')

self.assertColumnNames(new_table, ['number', 'Count'])
self.assertColumnTypes(new_table, [Text, Number])

self.assertSequenceEqual(new_table.rows[0], ['[0 - 10)', 10])
self.assertSequenceEqual(new_table.rows[3], ['[30 - 40)', 10])
self.assertSequenceEqual(new_table.rows[9], ['[90 - 100]', 10])

self.assertRowNames(new_table, [
'[0 - 10)',
'[10 - 20)',
'[20 - 30)',
'[30 - 40)',
'[40 - 50)',
'[50 - 60)',
'[60 - 70)',
'[70 - 80)',
'[80 - 90)',
'[90 - 100]',
])

def test_bins_negative(self):
rows = []

for i in range(0, -100, -1):
rows.append([i])

new_table = Table(rows, self.column_names, self.column_types).bins('number', 10, -100, 0)

self.assertColumnNames(new_table, ['number', 'Count'])
self.assertColumnTypes(new_table, [Text, Number])

self.assertSequenceEqual(new_table.rows[0], ['[-100 - -90)', 9])
self.assertSequenceEqual(new_table.rows[3], ['[-70 - -60)', 10])
self.assertSequenceEqual(new_table.rows[9], ['[-10 - 0]', 11])

def test_bins_mixed_signs(self):
rows = []

for i in range(0, -100, -1):
rows.append([i + 50])

new_table = Table(rows, self.column_names, self.column_types).bins('number')

self.assertColumnNames(new_table, ['number', 'Count'])
self.assertColumnTypes(new_table, [Text, Number])

self.assertSequenceEqual(new_table.rows[0], ['[-50 - -40)', 9])
self.assertSequenceEqual(new_table.rows[3], ['[-20 - -10)', 10])
self.assertSequenceEqual(new_table.rows[9], ['[40 - 50]', 11])

def test_bins_small_numbers(self):
rows = []

for i in range(0, 100):
rows.append([Decimal(i) / Decimal('10')])

new_table = Table(rows, self.column_names, self.column_types).bins('number')

self.assertSequenceEqual(new_table.rows[0], ['[0 - 1)', 10])
self.assertSequenceEqual(new_table.rows[3], ['[3 - 4)', 10])
self.assertSequenceEqual(new_table.rows[9], ['[9 - 10]', 10])

def test_bins_decimals(self):
rows = []

for i in range(0, 100):
rows.append([Decimal(i) / Decimal('100')])

new_table = Table(rows, self.column_names, self.column_types).bins('number')

self.assertColumnNames(new_table, ['number', 'Count'])
self.assertColumnTypes(new_table, [Text, Number])

self.assertSequenceEqual(new_table.rows[0], ['[0.0 - 0.1)', 10])
self.assertSequenceEqual(new_table.rows[3], ['[0.3 - 0.4)', 10])
self.assertSequenceEqual(new_table.rows[9], ['[0.9 - 1.0]', 10])

def test_bins_nulls(self):
rows = []

for i in range(0, 100):
rows.append([Decimal(i) / Decimal('100')])

rows.append([None])

new_table = Table(rows, self.column_names, self.column_types).bins('number')

self.assertColumnNames(new_table, ['number', 'Count'])
self.assertColumnTypes(new_table, [Text, Number])

self.assertSequenceEqual(new_table.rows[0], ['[0.0 - 0.1)', 10])
self.assertSequenceEqual(new_table.rows[3], ['[0.3 - 0.4)', 10])
self.assertSequenceEqual(new_table.rows[9], ['[0.9 - 1.0]', 10])
self.assertSequenceEqual(new_table.rows[10], [None, 1])
67 changes: 67 additions & 0 deletions tests/test_table/test_compute.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/usr/bin/env python
# -*- coding: utf8 -*-

import six

from agate import Table
from agate.data_types import *
from agate.computations import Formula
from agate.testcase import AgateTestCase


class TestCompute(AgateTestCase):
def setUp(self):
self.rows = (
('a', 2, 3, 4),
(None, 3, 5, None),
('a', 2, 4, None),
('b', 3, 6, None)
)

self.number_type = Number()
self.text_type = Text()

self.column_names = [
'one', 'two', 'three', 'four'
]
self.column_types = [
self.text_type, self.number_type, self.number_type, self.number_type
]

self.table = Table(self.rows, self.column_names, self.column_types)

def test_compute(self):
new_table = self.table.compute([
('test', Formula(self.number_type, lambda r: r['two'] + r['three']))
])

self.assertIsNot(new_table, self.table)
self.assertColumnNames(new_table, ['one', 'two', 'three', 'four', 'test'])
self.assertColumnTypes(new_table, [Text, Number, Number, Number, Number])

self.assertSequenceEqual(new_table.rows[0], ('a', 2, 3, 4, 5))
self.assertSequenceEqual(new_table.columns['test'], (5, 8, 6, 9))

def test_compute_multiple(self):
new_table = self.table.compute([
('number', Formula(self.number_type, lambda r: r['two'] + r['three'])),
('text', Formula(self.text_type, lambda r: (r['one'] or '-') + six.text_type(r['three'])))
])

self.assertIsNot(new_table, self.table)
self.assertColumnNames(new_table, ['one', 'two', 'three', 'four', 'number', 'text'])
self.assertColumnTypes(new_table, [Text, Number, Number, Number, Number, Text])

self.assertSequenceEqual(new_table.rows[0], ('a', 2, 3, 4, 5, 'a3'))
self.assertSequenceEqual(new_table.columns['number'], (5, 8, 6, 9))
self.assertSequenceEqual(new_table.columns['text'], ('a3', '-5', 'a4', 'b6'))

def test_compute_with_row_names(self):
table = Table(self.rows, self.column_names, self.column_types, row_names='three')

new_table = table.compute([
('number', Formula(self.number_type, lambda r: r['two'] + r['three'])),
('text', Formula(self.text_type, lambda r: (r['one'] or '-') + six.text_type(r['three'])))
])

self.assertRowNames(new_table, [3, 5, 4, 6])
109 changes: 109 additions & 0 deletions tests/test_table/test_denormalize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#!/usr/bin/env python
# -*- coding: utf8 -*-

from agate import Table
from agate.data_types import *
from agate.testcase import AgateTestCase


class TestDenormalize(AgateTestCase):
def setUp(self):
self.rows = (
('Jane', 'Code', 'gender', 'female'),
('Jane', 'Code', 'age', '27'),
('Jim', 'Program', 'gender', 'male'),
('Jim', 'Bytes', 'age', '24')
)

self.text_type = Text()

self.column_names = ['first_name', 'last_name', 'property', 'value']
self.column_types = [self.text_type, self.text_type, self.text_type, self.text_type]

def test_denormalize(self):
table = Table(self.rows, self.column_names, self.column_types)

normalized_table = table.denormalize('first_name', 'property', 'value')

normal_rows = (
('Jane', 'female', 27),
('Jim', 'male', 24),
)

self.assertRows(normalized_table, normal_rows)
self.assertColumnNames(normalized_table, ['first_name', 'gender', 'age'])
self.assertColumnTypes(normalized_table, [Text, Text, Number])
self.assertRowNames(normalized_table, ['Jane', 'Jim'])

def test_denormalize_no_key(self):
table = Table(self.rows, self.column_names, self.column_types)

normalized_table = table.denormalize(None, 'property', 'value')

# NB: value has been overwritten
normal_rows = (
('male', 24),
)

self.assertRows(normalized_table, normal_rows)
self.assertColumnNames(normalized_table, ['gender', 'age'])
self.assertColumnTypes(normalized_table, [Text, Number])

def test_denormalize_multiple_keys(self):
table = Table(self.rows, self.column_names, self.column_types)

normalized_table = table.denormalize(['first_name', 'last_name'], 'property', 'value')

normal_rows = (
('Jane', 'Code', 'female', 27),
('Jim', 'Program', 'male', None),
('Jim', 'Bytes', None, 24),
)

self.assertRows(normalized_table, normal_rows)
self.assertColumnNames(normalized_table, ['first_name', 'last_name', 'gender', 'age'])
self.assertColumnTypes(normalized_table, [Text, Text, Text, Number])
self.assertRowNames(normalized_table, [('Jane', 'Code'), ('Jim', 'Program'), ('Jim', 'Bytes')])

def test_denormalize_default_value(self):
table = Table(self.rows, self.column_names, self.column_types)

normalized_table = table.denormalize(['first_name', 'last_name'], 'property', 'value', default_value='hello')

normal_rows = (
('Jane', 'Code', 'female', '27'),
('Jim', 'Program', 'male', 'hello'),
('Jim', 'Bytes', 'hello', '24'),
)

self.assertRows(normalized_table, normal_rows)
self.assertColumnNames(normalized_table, ['first_name', 'last_name', 'gender', 'age'])
self.assertColumnTypes(normalized_table, [Text, Text, Text, Text])

def test_denormalize_column_types(self):
table = Table(self.rows, self.column_names, self.column_types)

normalized_table = table.denormalize(None, 'property', 'value', column_types=[Text(), Number()])

# NB: value has been overwritten
normal_rows = (
('male', 24),
)

self.assertRows(normalized_table, normal_rows)
self.assertColumnNames(normalized_table, ['gender', 'age'])
self.assertColumnTypes(normalized_table, [Text, Number])

def test_denormalize_column_type_tester(self):
table = Table(self.rows, self.column_names, self.column_types)

normalized_table = table.denormalize(None, 'property', 'value', column_types=TypeTester(force={'gender': Text()}))

# NB: value has been overwritten
normal_rows = (
('male', 24),
)

self.assertRows(normalized_table, normal_rows)
self.assertColumnNames(normalized_table, ['gender', 'age'])
self.assertColumnTypes(normalized_table, [Text, Number])
Loading

0 comments on commit 2993152

Please sign in to comment.