Tests for the global skills xls import
======================================

Log in as manager:

    >>> manager = browsers.manager
    >>> manager.ui.login('manager', 'schooltool')

We'll define a helper function for printing the form fields of any object.

    >>> def print_attrs():
    ...     sel = 'table.form-fields tbody tr'
    ...     for row in manager.query_all.css(sel):
    ...         label = row.query.xpath('td[1]').text
    ...         value = row.query.xpath('td[2]').text
    ...         print '%s: %s' % (label, value)

    >>> from schooltool.testing.util import format_table
    >>> def print_table(manager, xpath):
    ...     table = manager.query.xpath(xpath)
    ...     nheader = len(table.query_all.xpath('./thead/tr'))
    ...     nrows = len(table.query_all.xpath('./tbody/tr|./tr'))
    ...     rows = []
    ...     row = []
    ...     cells = table.query_all.xpath('.//th|.//td')
    ...     ncols = len(cells) / (nheader + nrows)
    ...     for n, cell in enumerate(cells):
    ...         row.append(cell.text)
    ...         if (n+1) % ncols == 0:
    ...             rows.append(row)
    ...             row = []
    ...     print format_table(rows, header_rows=nheader)

Let's import a file containing all the skill object types:

    >>> import os
    >>> dirname = os.path.abspath(os.path.dirname(__file__))
    >>> filename = os.path.join(dirname, 'sample_data.xls')
    >>> manager.ui.skill.import_xls(filename)

There should be no errors so we should be back at the Documents view:

    >>> manager.url
    u'http://localhost/documents'

Let's check the data.  There is one skillset called Carpentry.

    >>> manager.open('http://localhost/skills')

    >>> print_table(manager, '//table[@class="data"]')
    +------------+-----------+--------+
    | Label      | Title     | Skills |
    +------------+-----------+--------+
    | Carp label | Carpentry | 3      |
    +------------+-----------+--------+

It has three skills.

    >>> manager.query.link('Carpentry').click()

    >>> print_table(manager, '//table[@class="form-fields"]')
    +-------------+------------+
    | Description | ext_carp   |
    | Label       | Carp label |
    | Deprecated  | No         |
    +-------------+------------+

    >>> print_table(manager, '//table[@class="data"]')
    +-------+-----------+
    | Label | Title     |
    +-------+-----------+
    |       | Pounding  |
    |       | Whacking  |
    | Label | Hammering |
    +-------+-----------+

Let's visit each skill and print their attrs.

    >>> manager.query.link('Pounding').click()
    >>> print_attrs()
    Scoresystem: Letter Grade
    Description:
    Label:
    Required?: Required
    Deprecated: No
    External ID: ext_123
    >>> manager.query.link('Done').click()

    >>> manager.query.link('Whacking').click()
    >>> print_attrs()
    Scoresystem: Pass/Fail
    Description: Whacking desc.
    Label:
    Required?: Optional
    Deprecated: No
    External ID:
    >>> manager.query.link('Done').click()

    >>> manager.query.link('Hammering').click()
    >>> print_attrs()
    Scoresystem: Competency
    Description: This is hammering.
    Label: Label
    Required?: Optional
    Deprecated: Yes
    External ID:

There are five layers.

    >>> manager.query.link('School').click()
    >>> manager.query.link('Skills').click()
    >>> manager.query.link('Layers').click()

    >>> print_table(manager, '//table[@class="data"]')
    +--------------+---------------+---------------+
    | Title        | Parents       | Children      |
    +--------------+---------------+---------------+
    | Branch       | Craft         | Topic         |
    | Competencies |               |               |
    | Competency   |               |               |
    | Craft        |               | Branch, Topic |
    | Topic        | Branch, Craft |               |
    +--------------+---------------+---------------+

There is a single sample document.

    >>> manager.query.link('School').click()
    >>> manager.query.link('Skills').click()
    >>> manager.query.link('Documents').click()

    >>> print_table(manager, '//table[@class="data"]')
    +------------+
    | Title      |
    +------------+
    | Sample Doc |
    +------------+

It has five layers in its heirarchy: top three are for use with nodes,
bottom two are only used to name skills and skillsets.

    >>> manager.query.link('Sample Doc').click()
    >>> manager.query.link('Layer Hierarchy').click()

    >>> print_table(manager, '//table[@class="data relationships-table"]')
    +--------------+---------------+---------------+--------+
    | Title        | Parents       | Children      | Remove |
    +--------------+---------------+---------------+--------+
    | Craft        |               | Branch, Topic |        |
    | Branch       | Craft         | Topic         |        |
    | Topic        | Branch, Craft |               |        |
    | Competencies |               |               |        |
    | Competency   |               |               |        |
    +--------------+---------------+---------------+--------+

The search view returns nodes mixed with skillsets and skills.

    >>> manager.open('http://localhost/nodes')

    >>> def searchAndPrintTable(manager):
    ...     table = manager.query.xpath('//table[@class="data"]')
    ...     manager.query.button('Search').click()
    ...     manager.wait(lambda: table.expired)
    ...     print_table(manager, '//table[@class="data"]')

    >>> searchAndPrintTable(manager)
    +------------+--------------+---------------+
    | Label      | Title        | Layers        |
    +------------+--------------+---------------+
    | carp       | Carpentry    | Craft         |
    | Carp label | Carpentry    | Competencies  |
    |            | Conventional | Branch        |
    | creat      | Creative     | Branch        |
    |            | Hammering    | Topic, Branch |
    | Label      | Hammering    | Competency    |
    |            | Pounding     | Topic         |
    |            | Pounding     | Competency    |
    |            | Sample Doc   |               |
    |            | Whacking     | Topic         |
    |            | Whacking     | Competency    |
    +------------+--------------+---------------+

We can filter out some of the layers.

    >>> print '\n'.join([e.text for e in manager.query_all.xpath('//fieldset//div[@class="row"]//label')])
    Search
    Field
    Craft
    Branch
    Topic
    Skill Set (Competencies)
    Skill (Competency)
    No layer assigned

    >>> manager.query.xpath('//label[contains(., "Competency")]').click()
    >>> manager.query.xpath('//label[contains(., "Branch")]').click()
    >>> manager.query.xpath('//label[contains(., "Craft")]').click()

    >>> searchAndPrintTable(manager)
    +------------+------------+---------------+
    | Label      | Title      | Layers        |
    +------------+------------+---------------+
    | Carp label | Carpentry  | Competencies  |
    |            | Hammering  | Topic, Branch |
    |            | Pounding   | Topic         |
    |            | Sample Doc |               |
    |            | Whacking   | Topic         |
    +------------+------------+---------------+
