view data/plugin/macro/_tests/test_DictColumns.py @ 510:6318c91263c6

test_DictColumns: column_names_by_name checks now order too
author Reimar Bauer <rb.proj AT googlemail DOT com>
date Fri, 19 Mar 2010 21:22:57 +0100
parents b7a9843c5730
children ef1f74bf4776
line wrap: on
line source
# -*- coding: iso-8859-1 -*-
"""
    MoinMoin - MoinMoin.macro DictColumns tests

    @copyright: 2010 MoinMoin:ReimarBauer
    @license: GNU GPL, see COPYING for details.
"""
import py

from MoinMoin._tests import become_trusted, create_page, make_macro, nuke_page
from MoinMoin.macro.DictColumns import _name2index, _csv2list, DictColumns

class TestDictColumns(object):
    """ setup of common vars for testing of DictColumns """
    pagename = "DictColumns"
    template_page = "BooksTemplate"
    alias_page = "BookAlias"

    page_dict = [{"book": "Gray Hat Python: Python Programming for Hackers and Reverse Engineers",
                  "author": "Justin Seitz",
                  "language": "english",
                  "prize": "17.00",
                  "in stock": "31",
                 },
                 {"book": "Python Cookbook",
                  "author": "Alex Martelli",
                  "language": "english",
                  "prize": "21.00",
                  "in stock": "37",
                 },
                 {"book": "High Performance Python",
                  "author": "Anthony Lewis, Andrew Lewis, and Lewis Andrew",
                  "language": "english",
                  "prize": "24.90",
                  "in stock": "70",
                 },
                 {"book": "Einstieg in Python 3",
                  "author": "Thomas Theis",
                  "language": "german",
                  "prize": "24.90",
                  "in stock": "20",
                 },
                 {"book": "Hacking mit Python: Fehlersuche, Programmanalyse, Reverse Engineering",
                  "author": "Justin Seitz",
                  "language": "german",
                  "prize": "33.00",
                  "in stock": "70",
                 }]

    text = """\
 book:: %(book)s
 author:: %(author)s
 language:: %(language)s
 prize:: %(prize)s
 in stock:: %(in stock)s

----
CategoryTest%(language)s
"""

    template_dict = {"book": "", "author": "",
                     "prize": "", "in stock": "", "publisher": ""}

    template_text = """\
 book:: %(book)s
 author:: %(author)s
 prize:: %(prize)s
 in stock:: %(in stock)s
 publisher:: %(publisher)s

----
CategoryTest
"""
    alias_text = " prize:: %(prize)s"
    alias_dict = {"prize": "prize (euro)"}

    def setup_class(self):
        """ initializes content """
        become_trusted(self.request)
        template_text = self.template_text % self.template_dict
        template_page = create_page(self.request, self.template_page, template_text)
        alias_text = self.alias_text % self.alias_dict
        alias_page = create_page(self.request, self.alias_page, alias_text)
        for content in self.page_dict:
            pagename = self.pagename + '/' + content["book"]
            examples = create_page(self.request, pagename, self.text % content)
        self.page = create_page(self.request, self.pagename, u'<<DictColumns>>')
        self.macro = make_macro(self.request, self.page)

    def teardown_class(self):
        """ cleans everything """
        for content in self.page_dict:
            pagename = self.pagename + '/' + content["book"]
            nuke_page(self.request, pagename)
        nuke_page(self.request, self.pagename)
        nuke_page(self.request, self.template_page)
        nuke_page(self.request, self.alias_page)

    def test_get_page_list(self):
        """ tests selection of pages by the default regex search_term """
        result = DictColumns(self.macro, pagename=self.pagename).get_page_list()
        expected_result = [self.pagename + '/' + content["book"] for content in self.page_dict]
        # needs to have same length
        assert len(result) == len(expected_result)
        # needs to have same elements (order is independent)
        assert set(result) == set(expected_result)

    def test_get_page_list_default_pagename(self):
        """ tests selection of pages by the default regex search_term and default pagename """
        result = DictColumns(self.macro).get_page_list()
        expected_result = [self.pagename + '/' + content["book"] for content in self.page_dict]
        # needs to have same length
        assert len(result) == len(expected_result)
        # needs to have same elements (order is independent)
        assert set(result) == set(expected_result)

    def test_get_page_list_search_term(self):
        """ tests selection of pages by a search term of a category """
        # we search for search_term="category::CategoryTestgerman"
        result = DictColumns(self.macro, search_term="category:CategoryTestgerman").get_page_list()
        assert result is not None
        expected_result = [self.pagename + '/' + content["book"] for content in self.page_dict
                           if content["language"] == "german"]
        # needs to have same length
        assert len(result) == len(expected_result)
        # needs to have same elements (order is independent)
        assert set(result) == set(expected_result)
        # we search for search_term="category:CategoryTestenglish"
        result = DictColumns(self.macro, search_term="category:CategoryTestenglish").get_page_list()
        assert result is not None
        expected_result = [self.pagename + '/' + content["book"] for content in self.page_dict
                           if content["language"] == "english"]
        # needs to have same length
        assert len(result) == len(expected_result)
        # needs to have same elements (order is independent)
        assert set(result) == set(expected_result)

    def test_get_column_names_by_name(self):
        """ tests used column names based on given name list """
        selected_pages = DictColumns(self.macro, pagename=self.pagename).get_page_list()
        names = ['book', 'prize']
        result = DictColumns(self.macro, pagename=self.pagename, names=names).get_names(selected_pages)
        # needs to have same length
        assert len(result) == len(names)
        # needs to have same elements and order
        assert result == names

    def test_get_column_names_from_all_pages(self):
        """ tests used column names based on all pages (page_dict) """
        selected_pages = DictColumns(self.macro, pagename=self.pagename).get_page_list()
        # template_page has an additional key and one other is missing
        selected_pages.append(self.template_page)
        result = DictColumns(self.macro, pagename=self.pagename).get_names(selected_pages)
        # we have different and duplicated names on both list, we need unique names for the test
        expected_result = set(list(self.page_dict[0]) + list(self.template_dict))
        # needs to have same length
        assert len(result) == len(expected_result)
        # needs to have same elements (order is independent)
        assert set(result) == set(expected_result)

    def test_get_column_names_from_template_page(self):
        """ tests used column names based on a template page (template_dict)"""
        # you can use very different template pages for presenting the result
        # also for creating the dict pages they are usefull too
        selected_pages = DictColumns(self.macro, pagename=self.pagename).get_page_list()
        result = DictColumns(self.macro, pagename=self.pagename,
                             template_page=self.template_page).get_names(selected_pages)
        expected_result = list(self.template_dict)
        # needs to have same length
        assert len(result) == len(expected_result)
        # needs to have same elements (order is independent)
        assert set(result) == set(expected_result)

    def test_dataset_all_names(self):
        """ tests dataset for all keys (names) given from pages """
        selected_pages = DictColumns(self.macro, pagename=self.pagename).get_page_list()
        names = DictColumns(self.macro, pagename=self.pagename).get_names(selected_pages)
        result = DictColumns(self.macro, pagename=self.pagename).dataset(names, selected_pages)
        # if something is broken we have no data but we can have result.columns
        data = result.data
        assert data != []
        # we need as much lines in result as we have in self.page_dict
        assert len(result) == len(self.page_dict)
        # columns are independent from table data.
        assert result.columns[0].name == 'DictColumns'
        assert result.columns[0].label == 'DictColumns'
        # on default nothing is hidden
        for idx in range(len(result.columns)):
            assert result.columns[idx].hidden == 0
        # check for author entry
        idx_author = names.index('author') + 1  # offset because of pagename in first column
        # check that this is the right entry in result.columns too
        assert result.columns[idx_author].name == 'author'
        # all authors from the page dict as reference
        author_list = [self.page_dict[ix]['author'] for ix in range(len(self.page_dict))]
        # we check every row for some entries
        for row in data:
            #  line of data should have the length of names + 1 (for 'DictColumns' name)
            assert len(row) == len(names) + 1
            # check if the author is in the list
            assert row[idx_author][1] in author_list
            # check for pagename in alias of href
            test_string = selected_pages[data.index(row)].split('/', 1)[1]
            assert test_string in row[0]

    def test_dataset_some_names(self):
        """ tests setting of some names """
        selected_pages = DictColumns(self.macro, pagename=self.pagename).get_page_list()
        names = ['author', 'language', 'prize']
        result = DictColumns(self.macro, pagename=self.pagename).dataset(names, selected_pages)
        data = result.data
        # if something is broken we have no data but we can have result.columns
        assert data != []
        assert len(result) == len(self.page_dict)
        # needs to have same length
        assert len(result.columns) == len(names) + 1
        for name in names:
            idx = names.index(name) + 1
            assert result.columns[idx].name == name

    def test_dataset_filter_name(self):
        """ tests filtering of output by a given key value pair """
        selected_pages = DictColumns(self.macro, pagename=self.pagename).get_page_list()
        names = ["book", "author", "language", "prize"]
        # filters for this condition
        filter_name = ["prize", "language"]
        hidden_columns = ["book", "language"]
        result = DictColumns(self.macro, pagename=self.pagename, hide=hidden_columns, filter_name=filter_name).dataset(names, selected_pages)
        # if something is broken we have no data but we can have result.columns
        data = result.data
        assert data != []
        # we insert the self.pagename at position 0 because this makes comparison easier
        names.insert(0, self.pagename)
        for idx in range(len(result.columns)):
            if names[idx] in filter_name:
                assert result.columns[idx].name in filter_name
                assert result.columns[idx].autofilter == 1
            else:
                assert result.columns[idx].name in set(names) - set(filter_name)
                assert result.columns[idx].autofilter == 0
            if names[idx] in hidden_columns:
                assert result.columns[idx].name in hidden_columns
                assert result.columns[idx].hidden == 1
            else:
                assert result.columns[idx].name in set(names) - set(hidden_columns)
                assert result.columns[idx].hidden == 0

    def test_dataset_filter_value(self):
        """ tests filtering of output by a given key value pair """
        selected_pages = DictColumns(self.macro, pagename=self.pagename).get_page_list()
        names = ['author', 'language', 'prize']
        # filters for this condition
        filter_value = "prize:: 24.90"
        result = DictColumns(self.macro, pagename=self.pagename, filter_value=filter_value).dataset(names, selected_pages)
        assert len(result.columns) == len(names) + 1
        idx = names.index("prize") + 1
        # if something is broken we have no data but we can have result.columns
        data = result.data
        assert data != []
        for row in data:
            # there should be no other result
            assert row[idx][1] == u"24.90"

    def test_dataset_hide_names(self):
        """ tests hiding columns from output """
        selected_pages = DictColumns(self.macro, pagename=self.pagename).get_page_list()
        names = list(self.page_dict[0])
        hidden_columns = ["author", "language", "example"]
        title = "example"
        result = DictColumns(self.macro, pagename=self.pagename,
                             hide=hidden_columns, title=title).dataset(names, selected_pages)
        # if something is broken we have no data but we can have result.columns
        data = result.data
        assert data != []
        # we insert the title at position 0 because this makes comparison easier
        names.insert(0, title)
        for idx in range(len(result.columns)):
            if names[idx] in hidden_columns:
                assert result.columns[idx].name in hidden_columns
                assert result.columns[idx].hidden == 1
            else:
                assert result.columns[idx].name in set(names) - set(hidden_columns)
                assert result.columns[idx].hidden == 0

    def test_dataset_alias_page(self):
        """ tests column header settings from an alias page """
        selected_pages = DictColumns(self.macro, pagename=self.pagename).get_page_list()
        names = list(self.page_dict[0])
        result = DictColumns(self.macro, pagename=self.pagename,
                             alias_page=self.alias_page).dataset(names, selected_pages)
        data = result.data
        # if something is broken we have no data but we can have result.columns
        assert data != []
        # get names from all columns
        names = [result.columns[idx].name for idx in range(len(result.columns))]
        # there should be nothing left over
        assert set(self.alias_dict.values()) - set(names) == set([])

    def test_render(self):
        """ tests if the macro renders by default settings """
        result = DictColumns(self.macro, pagename=self.pagename).render()
        # if there is a table generated we have much output
        assert len(result) > 2000

    def test_render_no_result(self):
        """ tests for no result """
        result = DictColumns(self.macro,
                             pagename="I'am pretty sure this does never exist").render()
        # if there is not a table generated we get a hint what is wrong
        assert len(result) < 200

class TestDictColumns_name2index(object):
    def test_name2index(self):
        """ tests index of selected names in names """
        names = ['A', 'B', 'C', 'D', 'E', ]
        selected_names = ['E', 'A', 'B']
        result = _name2index(names, selected_names)
        assert result == [4, 0, 1]
        # tests wrong selection (name not in names)
        selected_names = ['E', 'A', 'B', 'G']
        result = _name2index(names, selected_names)
        assert result == []

class TestDictColumns_csv2list(object):
    def test_csv2list(self):
        """ tests conversion from csv input to list value """
        text = "A, B,C ,D,  E,"
        result = _csv2list(text)
        expected_result = ['A', 'B', 'C', 'D', 'E']
        assert result == expected_result

coverage_modules = ['MoinMoin.macro.DictColumns']