def test_dict(self) -> None: r1 = LayoutDOM() r2 = LayoutDOM() obj = dict(r1=r1, r2=r2) vals = set() assert bmu.visit_value_and_its_immediate_references( obj, lambda x: vals.add(x)) is None assert vals == {r1, r2}
def test_seq(self, typ) -> None: r1 = LayoutDOM() r2 = LayoutDOM() obj = typ([r1, r2]) vals = set() assert bmu.visit_value_and_its_immediate_references( obj, lambda x: vals.add(x)) is None assert vals == {r1, r2}
def test_visit_immediate_value_references() -> None: r1 = LayoutDOM() r2 = LayoutDOM() row = Row(children=[LayoutDOM()]) obj = Row(children=[r1, r2, row]) vals = set() assert bmu.visit_immediate_value_references(obj, lambda x: vals.add(x)) is None assert vals == {r1, r2, row}
def test_collect_models() -> None: r1 = LayoutDOM() r2 = LayoutDOM() r3 = LayoutDOM() row1 = Row(children=[r1, r2]) row2 = Row(children=[r2, r3]) models = bmu.collect_models(row1, row2) assert len(models) == 5 assert set(models) == {r1, r2, r3, row1, row2}
def test_Model(self) -> None: r1 = LayoutDOM() r2 = LayoutDOM() r3 = LayoutDOM() row = Row(children=[r3]) obj = Row(children=[r1, r2, row]) vals = set() assert bmu.visit_value_and_its_immediate_references( obj, lambda x: vals.add(x)) is None assert vals == {obj}
def test_make_charts(self, mock_render_to_string, mock_row, mock_get_per_month_chart, mock_get_proportions_chart, mock_get_frequency_charts): # Bokeh row() expects a LayoutDOM object, so just create empty ones for the mocks to use mock_get_per_month_chart.return_value = LayoutDOM() mock_get_proportions_chart.return_value = LayoutDOM() # There are two frequency charts mock_get_frequency_charts.return_value = [LayoutDOM(), LayoutDOM()] mock_row.return_value = LayoutDOM() mock_render_to_string.return_value = 'stuff that got returned' words = [ 'word', ] months = ['Jan'] proportions = [1] word_freqs = [1] totals = [1] averages = [1] doc_counts = [1] make_charts(words=words, months=months, proportions=proportions, word_freqs=word_freqs, totals=totals, averages=averages, doc_counts=doc_counts) mock_get_per_month_chart.reset_mock() args, kwargs = mock_get_frequency_charts.call_args self.assertEqual( args[0], words, 'make_charts() should call get_frequency_charts() with words as 1st arg' ) # If number of words searched for is not 2, get_proportions_chart() shouldn't be called self.assertEqual( mock_get_proportions_chart.call_count, 0, "make_charts() shouldn't call get_proportions_chart() if # of words searched for != 2" ) # If number of words searched for is 2, get_proportions_chart() should be called words = ['and', '&'] months = [1, 2] proportions = [1, 2] word_freqs = [1, 2, 3, 4] totals = [1, 2] averages = [1, 2] doc_counts = [1, 2] result = make_charts(words=words, months=months, proportions=proportions, word_freqs=word_freqs, totals=totals, averages=averages, doc_counts=doc_counts) args, kwargs = mock_get_frequency_charts.call_args self.assertEqual( args[0], words, 'make_charts() should call get_proportions_chart() with words as 1st arg if # of words searched for == 2' ) # get_per_month_chart() should be called 3 times self.assertEqual( mock_get_per_month_chart.call_count, 3, 'make_charts() should call get_per_month_chart() 3 times') # render_to_string() should be called and its return value returned args, kwars = mock_render_to_string.call_args self.assertEqual( args[0], 'snippets/chart.html', 'make_charts() should call render_to_string() with charts snippet as first arg' ) for key in ['script', 'divs']: self.assertTrue( key in args[1], "make_charts() should call render_to_string() with '{}' in second arg" ) # make_charts() should return the return value of render_to_string self.assertEqual( result, mock_render_to_string.return_value, 'make_charts() should return the return value of render_to_string')