Skip to content

Commit

Permalink
add tv show search row builder
Browse files Browse the repository at this point in the history
  • Loading branch information
jaebradley committed Jun 2, 2017
1 parent 8d49ddd commit 8ea9fbe
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
7 changes: 5 additions & 2 deletions tables/rows/builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ def __init__(self):
self.rating_formatter = RottenTomatoesScoreFormatter()

def build(self, tv_show):
return [self.name(name=tv_show.name), self.rating_formatter.format(rating=tv_show.rotten_tomatoes_score),
self.format_years(start_year=tv_show.start_year, end_year=tv_show.end_year)]
return [
self.name(name=tv_show.name),
self.rating_formatter.format(rating=tv_show.rotten_tomatoes_score),
self.format_years(start_year=tv_show.start_year, end_year=tv_show.end_year)
]

def format_years(self, start_year, end_year):
end_year_value = "" if end_year is None else end_year
Expand Down
74 changes: 74 additions & 0 deletions tests/test_tvShowSearchRowBuilder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
from unittest import TestCase

from mock import patch, Mock

from tables.rows.builders import TvShowSearchRowBuilder


class MockTvShow:
def __init__(self, name, rotten_tomatoes_score, start_year, end_year):
self.name = name
self.rotten_tomatoes_score = rotten_tomatoes_score
self.start_year = start_year
self.end_year = end_year


class TestName(TestCase):
row_builder = TvShowSearchRowBuilder()

@patch("tables.rows.builders.convert_to_ascii")
@patch("tables.rows.builders.colored")
@patch("tables.rows.builders.wrap")
def test_name(self, mock_wrap, mock_colored, mock_ascii_conversion):
name_parts = ["jae", "baebae"]
mock_ascii_conversion.return_value = "converted to ascii"
mock_colored.return_value = "colored"
mock_wrap.return_value = name_parts
expected = "colored\ncolored"

self.assertEqual(expected, self.row_builder.name(name="name"))
mock_ascii_conversion.assert_called_once_with(text="name")
mock_wrap.assert_called_once_with(text="converted to ascii", width=30)
mock_colored.assert_any_call("jae", attrs=["bold"])
mock_colored.assert_any_call("baebae", attrs=["bold"])


class TestFormatYears(TestCase):
row_builder = TvShowSearchRowBuilder()
start_year = "start year"

def test_when_end_year_is_none(self):
expected = "start year-"
self.assertEqual(expected, self.row_builder.format_years(start_year=self.start_year, end_year=None))

def test_when_end_year_is_defined(self):
end_year = "end year"
expected = "start year-end year"
self.assertEqual(expected, self.row_builder.format_years(start_year=self.start_year, end_year=end_year))


class TestBuild(TestCase):
row_builder = TvShowSearchRowBuilder()

def test_build(self):
name = "name"
rotten_tomatoes_score = "rotten tomatoes score"
start_year = "start year"
end_year = "end year"
tv_show = MockTvShow(name=name, rotten_tomatoes_score=rotten_tomatoes_score,
start_year=start_year, end_year=end_year)
rating_formatter = "rating formatter"
format_years = "format years"
self.row_builder.name = Mock("name")
self.row_builder.name.return_value = name
self.row_builder.rating_formatter.format = Mock("format")
self.row_builder.rating_formatter.format.return_value = rating_formatter
self.row_builder.format_years = Mock("format years")
self.row_builder.format_years.return_value = format_years

expected = [name, rating_formatter, format_years]

self.assertEqual(expected, self.row_builder.build(tv_show=tv_show))
self.row_builder.name.assert_called_once_with(name=name)
self.row_builder.rating_formatter.format.assert_called_once_with(rating=rotten_tomatoes_score)
self.row_builder.format_years.assert_called_once_with(start_year=start_year, end_year=end_year)

0 comments on commit 8ea9fbe

Please sign in to comment.