# Path: performanceplatform/collector/ga/plugins/department.py
# class ComputeDepartmentKey(object):
# 
#     """
#     Adds a 'department' key to a dictionary by looking up the department from
#     the specified, key_name. It takes the first department code of form
#     <[code]> from document[key_name].
#     """
# 
#     def __init__(self, key_name):
#         self.key_name = key_name
# 
#     def __call__(self, documents):
#         def compute_department(department):
#             assert self.key_name in document, (
#                 'key "{}" not found "{}"'.format(self.key_name, document))
#             department_codes = document[self.key_name]
#             department_code = take_first_department_code(department_codes)
#             document["department"] = DEPARTMENT_MAPPING.get(
#                 department_code, department_code)
#             return document
# 
#         return [compute_department(document) for document in documents]
# 
# def try_get_department(department_or_code):
#     """
#     Try to take the first department code, or fall back to string as passed
#     """
#     try:
#         value = take_first_department_code(department_or_code)
#     except AssertionError:
#         value = department_or_code
# 
#     if value in DEPARTMENT_MAPPING:
#         value = DEPARTMENT_MAPPING[value]
# 
#     return value


from performanceplatform.collector.ga.plugins.department \
    import ComputeDepartmentKey, try_get_department
    from nose.tools import assert_equal
    from nose.tools import assert_equal, assert_in
    from nose.tools import assert_raises
    from nose.tools import assert_equal
    from nose.tools import assert_equal


def test_try_get_department():
    assert_equal(try_get_department("<D1>"), "attorney-generals-office")
    assert_equal(try_get_department("<D1><foo>"), "attorney-generals-office")
    assert_equal(try_get_department("<foo>"), "<foo>")


def test_mapping():

    plugin = ComputeDepartmentKey("key_name")
    documents = [{"key_name": "<D10>"}]
    (transformed_document, ) = plugin(documents)

    assert_in("department", transformed_document)
    assert_equal(transformed_document["department"],
                 "department-for-work-pensions")


def test_fail_if_no_key_name_in_document():

    plugin = ComputeDepartmentKey("key_name")
    documents = [{"foo": "<D10>"}]

    with assert_raises(AssertionError):
        plugin(documents)


def test_unknown_department_code():


# Path: performanceplatform/collector/ga/plugins/department.py
# class ComputeDepartmentKey(object):
# 
#     """
#     Adds a 'department' key to a dictionary by looking up the department from
#     the specified, key_name. It takes the first department code of form
#     <[code]> from document[key_name].
#     """
# 
#     def __init__(self, key_name):
#         self.key_name = key_name
# 
#     def __call__(self, documents):
#         def compute_department(department):
#             assert self.key_name in document, (
#                 'key "{}" not found "{}"'.format(self.key_name, document))
#             department_codes = document[self.key_name]
#             department_code = take_first_department_code(department_codes)
#             document["department"] = DEPARTMENT_MAPPING.get(
#                 department_code, department_code)
#             return document
# 
#         return [compute_department(document) for document in documents]
# 
# def try_get_department(department_or_code):
#     """
#     Try to take the first department code, or fall back to string as passed
#     """
#     try:
#         value = take_first_department_code(department_or_code)
#     except AssertionError:
#         value = department_or_code
# 
#     if value in DEPARTMENT_MAPPING:
#         value = DEPARTMENT_MAPPING[value]
# 
#     return value


# Path: tests/performanceplatform/collector/ga/plugins/test_department.py
from performanceplatform.collector.ga.plugins.department \
    import ComputeDepartmentKey, try_get_department
    from nose.tools import assert_equal
    from nose.tools import assert_equal, assert_in
    from nose.tools import assert_raises
    from nose.tools import assert_equal
    from nose.tools import assert_equal



def test_try_get_department():
    assert_equal(try_get_department("<D1>"), "attorney-generals-office")
    assert_equal(try_get_department("<D1><foo>"), "attorney-generals-office")
    assert_equal(try_get_department("<foo>"), "<foo>")


def test_mapping():

    plugin = ComputeDepartmentKey("key_name")
    documents = [{"key_name": "<D10>"}]
    (transformed_document, ) = plugin(documents)

    assert_in("department", transformed_document)
    assert_equal(transformed_document["department"],
                 "department-for-work-pensions")


def test_fail_if_no_key_name_in_document():

    plugin = ComputeDepartmentKey("key_name")
    documents = [{"foo": "<D10>"}]

    with assert_raises(AssertionError):
        plugin(documents)


def test_unknown_department_code():


    plugin = ComputeDepartmentKey("key_name")