|
import unittest |
|
import json |
|
|
|
from rolodex import Rolodex |
|
|
|
|
|
TEST_DATA = [ |
|
'Booker T., Washington, 87360, 373 781 7380, yellow\n', |
|
'Chandler, Kerri, (623)-668-9293, pink, 123123121\n', |
|
'James, Murphy, yellow, 83880, 018 154 6474\n', |
|
'asdfawefawea\n' |
|
] |
|
|
|
TEST_VALID_DATA = [ |
|
{ |
|
'color': ' yellow\n', |
|
'firstname': 'Booker T.', |
|
'lastname': ' Washington', |
|
'phonenumber': ' 373 781 7380', |
|
'zipcode': ' 87360' |
|
}, |
|
{ |
|
'color': ' yellow', |
|
'firstname': 'James', |
|
'lastname': ' Murphy', |
|
'phonenumber': ' 018 154 6474\n', |
|
'zipcode': ' 83880' |
|
} |
|
] |
|
|
|
TEST_CLEAN_DATA = [ |
|
{ |
|
'color': 'yellow', |
|
'firstname': 'Booker T.', |
|
'lastname': 'Washington', |
|
'phonenumber': '373-781-7380', |
|
'zipcode': '87360' |
|
}, |
|
{ |
|
'color': 'yellow', |
|
'firstname': 'James', |
|
'lastname': 'Murphy', |
|
'phonenumber': '018-154-6474', |
|
'zipcode': '83880' |
|
} |
|
] |
|
|
|
|
|
class RolodexTest(unittest.TestCase): |
|
def setUp(self): |
|
self.rolodex = Rolodex() |
|
|
|
def test_open_method_assigns_file_lines_attribute(self): |
|
''' |
|
Tests to ensure the .open() method assigns and returns |
|
the file_lines attribute with correct length |
|
''' |
|
rolodex = Rolodex('test_input.txt') |
|
file_lines = rolodex.open() |
|
actual = len(file_lines) |
|
expected = 4 |
|
self.assertEqual(actual, expected) |
|
|
|
def test_input_file_line_length_validation_passing(self): |
|
''' |
|
Tests to ensure an input line with five comma-delimited |
|
values is considered valid |
|
''' |
|
actual = self.rolodex.validate_length(TEST_DATA[0]) |
|
expected = True |
|
self.assertEqual(actual, expected) |
|
|
|
def test_input_file_line_length_validation_failing(self): |
|
''' |
|
Tests to ensure an input line not with five |
|
comma-delimited values is considered invalid |
|
''' |
|
actual = self.rolodex.validate_length(TEST_DATA[3]) |
|
expected = False |
|
self.assertEqual(actual, expected) |
|
|
|
def test_phone_number_length_validation_passing(self): |
|
''' |
|
Tests that a phone number with 10 digits is considered valid |
|
''' |
|
actual = self.rolodex.validate_phone_number(TEST_DATA[2]) |
|
expected = 4 # index of phone number |
|
self.assertEqual(actual, expected) |
|
|
|
def test_phone_number_length_validation_failing(self): |
|
''' |
|
Tests that a phone number not with 10 digits is considered invalid |
|
''' |
|
actual = self.rolodex.validate_phone_number(TEST_DATA[3]) |
|
expected = None |
|
self.assertEqual(actual, expected) |
|
|
|
def test_zipcode_length_validation_passing(self): |
|
''' |
|
Tests that a zipcode with 5 digits is considered valid |
|
''' |
|
actual = self.rolodex.validate_zipcode(TEST_DATA[2]) |
|
expected = 3 # index of zipcode |
|
self.assertEqual(actual, expected) |
|
|
|
def test_zipcode_length_validation_failing(self): |
|
''' |
|
Tests that a zipcode not with 5 digits is considered invalid |
|
''' |
|
actual = self.rolodex.validate_zipcode(TEST_DATA[1]) |
|
expected = None |
|
self.assertEqual(actual, expected) |
|
|
|
def test_get_line_format_format_one(self): |
|
''' |
|
Tests for correct format when phone index == 2 (third position) |
|
''' |
|
actual = self.rolodex.get_line_format(2) |
|
expected = ['lastname', 'firstname', 'phonenumber', 'color', 'zipcode'] |
|
self.assertEqual(actual, expected) |
|
|
|
def test_get_line_format_format_two(self): |
|
''' |
|
Tests for correct format when phone index == 3 (fourth position) |
|
''' |
|
actual = self.rolodex.get_line_format(3) |
|
expected = ['firstname', 'lastname', 'zipcode', 'phonenumber', 'color'] |
|
self.assertEqual(actual, expected) |
|
|
|
def test_get_line_format_format_three(self): |
|
''' |
|
Tests for correct format when phone index == 4 (fifth position) |
|
''' |
|
actual = self.rolodex.get_line_format(4) |
|
expected = ['firstname', 'lastname', 'color', 'zipcode', 'phonenumber'] |
|
self.assertEqual(actual, expected) |
|
|
|
def test_get_line_format_format_none(self): |
|
''' |
|
Tests for correct format when phone index not 2, 3 or 4 |
|
''' |
|
actual = self.rolodex.get_line_format(0) |
|
expected = None |
|
self.assertEqual(actual, expected) |
|
|
|
def test_line_validation_passing(self): |
|
''' |
|
Tests that a full line is considered valid with correct data |
|
''' |
|
actual = len(self.rolodex.validate_line(TEST_DATA[0], 0)) |
|
expected = 1 # length of valid_data |
|
self.assertEqual(actual, expected) |
|
|
|
def test_line_validation_failing(self): |
|
''' |
|
Tests that a full line is considered valid with correct data |
|
''' |
|
actual = self.rolodex.validate_line(TEST_DATA[3], 3) |
|
expected = [3] # value of errors |
|
self.assertEqual(actual, expected) |
|
|
|
def test_full_validation(self): |
|
''' |
|
Tests that valid_data and errors set correctly on set of lines |
|
''' |
|
self.rolodex.file_lines = TEST_DATA |
|
valid_data, errors = self.rolodex.validate() |
|
actual = len(valid_data) |
|
expected = 2 |
|
self.assertEqual(actual, expected) |
|
actual = len(errors) |
|
expected = 2 |
|
self.assertEqual(actual, expected) |
|
|
|
def test_normalize_firstname(self): |
|
''' |
|
Tests first name normalize logic |
|
''' |
|
value = 'booKer T.' |
|
actual = self.rolodex.normalize_firstname(value) |
|
expected = 'Booker T.' |
|
self.assertEqual(actual, expected) |
|
|
|
def test_normalize_lastname(self): |
|
''' |
|
Tests last name normalize logic |
|
''' |
|
value = 'wAshington' |
|
actual = self.rolodex.normalize_lastname(value) |
|
expected = 'Washington' |
|
self.assertEqual(actual, expected) |
|
|
|
def test_normalize_phonenumber(self): |
|
''' |
|
Tests phone number normalize logic |
|
''' |
|
value = '3737817380' |
|
actual = self.rolodex.normalize_phonenumber(value) |
|
expected = '373-781-7380' |
|
self.assertEqual(actual, expected) |
|
|
|
def test_normalize_color(self): |
|
''' |
|
Tests color normalize logic |
|
''' |
|
value = 'BLUE' |
|
actual = self.rolodex.normalize_color(value) |
|
expected = 'blue' |
|
self.assertEqual(actual, expected) |
|
|
|
def test_normalize_zipcode(self): |
|
''' |
|
Tests zip code normalize logic |
|
''' |
|
value = '123456677788' |
|
actual = self.rolodex.normalize_zipcode(value) |
|
expected = '12345' |
|
self.assertEqual(actual, expected) |
|
|
|
def test_full_normalize(self): |
|
''' |
|
Tests that applying all normalize methods work in sequence |
|
''' |
|
self.rolodex.valid_data = TEST_VALID_DATA |
|
actual = len(self.rolodex.normalize()) |
|
expected = 2 |
|
self.assertEqual(actual, expected) |
|
|
|
def test_transform(self): |
|
''' |
|
Tests that transform returns a JSON-formatted object with |
|
entries sorted by last name, first name |
|
''' |
|
self.rolodex.clean_data = TEST_CLEAN_DATA |
|
self.rolodex.errors = [1, 3] |
|
raw_json = self.rolodex.transform() |
|
json_dict = json.loads(raw_json) |
|
actual = json_dict['entries'][0]['lastname'] |
|
expected = 'Murphy' |
|
self.assertEqual(actual, expected) |
|
actual = json_dict['errors'][0] |
|
expected = 1 |
|
self.assertEqual(actual, expected) |
|
|
|
def test_full_process(self): |
|
''' |
|
Tests that entire process for validating, normalizing and |
|
transforming an input file returns expected data |
|
''' |
|
rolodex = Rolodex('test_input.txt') |
|
raw_json = rolodex.process() |
|
json_dict = json.loads(raw_json) |
|
actual = json_dict['entries'][1]['lastname'] |
|
expected = 'Washington' |
|
self.assertEqual(actual, expected) |
|
actual = json_dict['errors'][1] |
|
expected = 3 |
|
self.assertEqual(actual, expected) |