예제 #1
0
 def import_units(self, pk=None):
     if pk is not None:
         import_units(fetch_only_id=pk)
         return
     import_units()
     update_service_node_counts()
     update_service_counts()
예제 #2
0
 def import_units(self, pk=None):
     if pk is not None:
         import_units(fetch_only_id=pk)
         return
     import_units()
     update_service_node_counts()
     remove_empty_service_nodes(self.logger)
     update_service_counts()
def test_import_units(api_client, muni_admin_div_type, resources):

    def fetch_resource(name):
        return resources.get(name, set())

    def fetch_units():
        return fetch_resource('unit')

    dept_syncher = import_departments(fetch_resource=fetch_resource)

    import_services(
        ontologytrees=fetch_resource('ontologytree'),
        ontologywords=fetch_resource('ontologyword'))

    response = get(api_client, reverse('servicenode-list'))
    assert_resource_synced(response, 'ontologytree', resources)

    response = get(api_client, reverse('service-list'))
    assert_resource_synced(response, 'ontologyword', resources)

    import_units(
        fetch_units=fetch_units, fetch_resource=fetch_resource,
        dept_syncher=dept_syncher)

    update_service_node_counts()

    response = get(api_client, '{}?include=department,services'.format(reverse('unit-list')))
    assert_resource_synced(response, 'unit', resources)

    source_units_by_id = dict((u['id'], u) for u in resources['unit'])
    ontologyword_ids_by_unit_id = dict()
    ontologyword_by_id = {}
    ontologytree_by_id = {}

    for owd in resources['ontologyword_details']:
        s = ontologyword_ids_by_unit_id.setdefault(owd['unit_id'], set())
        s.add(owd['ontologyword_id'])

    for ot in resources['ontologytree']:
        ontologytree_by_id[ot['id']] = ot

    for ow in resources['ontologyword']:
        ontologyword_by_id[ow['id']] = ow

    service_node_counts = {}
    service_counts = {}

    imported_service_details = {}

    for unit in response.data['results']:
        assert_unit_correctly_imported(unit, source_units_by_id.get(unit['id']),
                                       ontologyword_ids_by_unit_id[unit['id']])
        imported_service_details[unit['id']] = unit['services']
        for service_node_id in unit['service_nodes']:
            service_node_counts.setdefault(service_node_id, 0)
            service_node_counts[service_node_id] += 1
        for service in unit['services']:
            service_counts[service['id']] = service_counts.get(service['id'], 0) + 1

    assert_service_details_correctly_imported(
        resources['ontologyword_details'], imported_service_details)

    # Check unit counts in related objects

    response = get(api_client, reverse('servicenode-list'))
    service_nodes = response.data['results']
    for service_node in service_nodes:
        # Note: only the totals are currently tested
        # TODO: hierarchy + municipality specific tests
        assert service_node_counts.get(service_node['id'], 0) == service_node['unit_count']['total']
        assert_keywords_correct(ontologytree_by_id.get(service_node['id']), service_node)

    response = get(api_client, reverse('service-list'))
    services = response.data['results']
    for service in services:
        assert service_counts.get(service['id'], 0) == service['unit_count']['total']
        to = service
        frm = ontologyword_by_id[service['id']]
        assert to['period_enabled'] == frm['can_add_schoolyear']
        assert to['clarification_enabled'] == frm['can_add_clarification']
        assert_keywords_correct(ontologyword_by_id.get(service['id']), service)
 def import_units(self, pk=None):
     if pk is not None:
         import_units(fetch_only_id=pk)
         return
     import_units()
     self.update_service_node_unit_counts()
예제 #5
0
def test_import_units(api_client, muni_admin_div_type, resources):
    def fetch_resource(name):
        return resources.get(name, set())

    def fetch_units():
        return fetch_resource('unit')

    dept_syncher = import_departments(fetch_resource=fetch_resource)

    import_services(ontologytrees=fetch_resource('ontologytree'),
                    ontologywords=fetch_resource('ontologyword'))

    response = get(api_client, reverse('servicenode-list'))
    assert_resource_synced(response, 'ontologytree', resources)

    response = get(api_client, reverse('service-list'))
    assert_resource_synced(response, 'ontologyword', resources)

    import_units(fetch_units=fetch_units,
                 fetch_resource=fetch_resource,
                 dept_syncher=dept_syncher)

    update_service_node_counts()

    response = get(
        api_client,
        '{}?include=department,services'.format(reverse('unit-list')))
    assert_resource_synced(response, 'unit', resources)

    source_units_by_id = dict((u['id'], u) for u in resources['unit'])
    ontologyword_ids_by_unit_id = dict()
    ontologyword_by_id = {}
    ontologytree_by_id = {}

    for owd in resources['ontologyword_details']:
        s = ontologyword_ids_by_unit_id.setdefault(owd['unit_id'], set())
        s.add(owd['ontologyword_id'])

    for ot in resources['ontologytree']:
        ontologytree_by_id[ot['id']] = ot

    for ow in resources['ontologyword']:
        ontologyword_by_id[ow['id']] = ow

    service_node_counts = {}
    service_counts = {}

    imported_service_details = {}

    for unit in response.data['results']:
        assert_unit_correctly_imported(unit,
                                       source_units_by_id.get(unit['id']),
                                       ontologyword_ids_by_unit_id[unit['id']])
        imported_service_details[unit['id']] = unit['services']
        for service_node_id in unit['service_nodes']:
            service_node_counts.setdefault(service_node_id, 0)
            service_node_counts[service_node_id] += 1
        for service in unit['services']:
            service_counts[service['id']] = service_counts.get(
                service['id'], 0) + 1

    assert_service_details_correctly_imported(
        resources['ontologyword_details'], imported_service_details)

    # Check unit counts in related objects

    response = get(api_client, reverse('servicenode-list'))
    service_nodes = response.data['results']
    for service_node in service_nodes:
        # Note: only the totals are currently tested
        # TODO: hierarchy + municipality specific tests
        assert service_node_counts.get(
            service_node['id'], 0) == service_node['unit_count']['total']
        assert_keywords_correct(ontologytree_by_id.get(service_node['id']),
                                service_node)

    response = get(api_client, reverse('service-list'))
    services = response.data['results']
    for service in services:
        assert service_counts.get(service['id'],
                                  0) == service['unit_count']['total']
        to = service
        frm = ontologyword_by_id[service['id']]
        assert to['period_enabled'] == frm['can_add_schoolyear']
        assert to['clarification_enabled'] == frm['can_add_clarification']
        assert_keywords_correct(ontologyword_by_id.get(service['id']), service)
def test_import_units(api_client, resources):
    # Manual setup: needed because of poor hypothesis-pytest integration
    a, created = AdministrativeDivisionType.objects.get_or_create(
        type='muni', defaults=dict(name='Municipality'))

    # End of manual setup

    def fetch_resource(name):
        return resources.get(name, set())

    def fetch_units():
        return fetch_resource('unit')

    dept_syncher = import_departments(fetch_resource=fetch_resource)

    import_services(ontologytrees=fetch_resource('ontologytree'),
                    ontologywords=fetch_resource('ontologyword'))

    response = get(api_client, reverse('servicenode-list'))
    assert_resource_synced(response, 'ontologytree', resources)

    response = get(api_client, reverse('service-list'))
    assert_resource_synced(response, 'ontologyword', resources)

    import_units(fetch_units=fetch_units,
                 fetch_resource=fetch_resource,
                 dept_syncher=dept_syncher)

    update_service_node_counts()
    update_service_counts()

    response = get(
        api_client,
        '{}?include=department,services'.format(reverse('unit-list')))
    assert_resource_synced(response, 'unit', resources)

    source_units_by_id = dict((u['id'], u) for u in resources['unit'])
    ontologyword_ids_by_unit_id = dict()
    ontologyword_by_id = {}
    ontologytree_by_id = {}

    for owd in resources['ontologyword_details']:
        s = ontologyword_ids_by_unit_id.setdefault(owd['unit_id'], set())
        s.add(owd['ontologyword_id'])

    for ot in resources['ontologytree']:
        ontologytree_by_id[ot['id']] = ot

    for ow in resources['ontologyword']:
        ontologyword_by_id[ow['id']] = ow

    service_node_counts = {}
    service_counts = {}

    imported_service_details = {}

    for unit in response.data['results']:
        assert_unit_correctly_imported(unit,
                                       source_units_by_id.get(unit['id']),
                                       ontologyword_ids_by_unit_id[unit['id']])
        imported_service_details[unit['id']] = unit['services']
        for service_node_id in unit['service_nodes']:
            service_node_counts.setdefault(service_node_id, 0)
            service_node_counts[service_node_id] += 1
        for service in unit['services']:
            service_counts[service['id']] = service_counts.get(
                service['id'], 0) + 1

    assert_service_details_correctly_imported(
        resources['ontologyword_details'], imported_service_details)

    # Check unit counts in related objects

    response = get(api_client, reverse('servicenode-list'))
    service_nodes = response.data['results']
    for service_node in service_nodes:
        # Note: only the totals are currently tested
        # TODO: hierarchy + municipality specific tests
        assert service_node_counts.get(
            service_node['id'], 0) == service_node['unit_count']['total']
        assert_keywords_correct(ontologytree_by_id.get(service_node['id']),
                                service_node)

    response = get(api_client, reverse('service-list'))
    services = response.data['results']
    for service in services:
        assert service_counts.get(service['id'],
                                  0) == service['unit_count']['total']
        to = service
        frm = ontologyword_by_id[service['id']]
        assert to['period_enabled'] == frm['can_add_schoolyear']
        assert to['clarification_enabled'] == frm['can_add_clarification']
        assert_keywords_correct(ontologyword_by_id.get(service['id']), service)

    # Manual teardown: needed because of poor hypothesis-pytest integration
    ServiceUnitCount.objects.all().delete()
    ServiceNodeUnitCount.objects.all().delete()
    a.delete()
예제 #7
0
def test_import_units(api_client, resources):
    # Manual setup: needed because of poor hypothesis-pytest integration
    a, created = AdministrativeDivisionType.objects.get_or_create(
        type="muni", defaults=dict(name="Municipality"))

    # End of manual setup

    def fetch_resource(name):
        return resources.get(name, set())

    def fetch_units():
        return fetch_resource("unit")

    dept_syncher = import_departments(fetch_resource=fetch_resource)

    import_services(
        ontologytrees=fetch_resource("ontologytree"),
        ontologywords=fetch_resource("ontologyword"),
    )

    response = get(api_client, reverse("servicenode-list"))
    assert_resource_synced(response, "ontologytree", resources)

    response = get(api_client, reverse("service-list"))
    assert_resource_synced(response, "ontologyword", resources)

    import_units(
        fetch_units=fetch_units,
        fetch_resource=fetch_resource,
        dept_syncher=dept_syncher,
    )

    update_service_node_counts()
    update_service_counts()

    response = get(
        api_client,
        "{}?include=department,services".format(reverse("unit-list")))
    assert_resource_synced(response, "unit", resources)

    source_units_by_id = dict((u["id"], u) for u in resources["unit"])
    ontologyword_ids_by_unit_id = dict()
    ontologyword_by_id = {}
    ontologytree_by_id = {}

    for owd in resources["ontologyword_details"]:
        s = ontologyword_ids_by_unit_id.setdefault(owd["unit_id"], set())
        s.add(owd["ontologyword_id"])

    for ot in resources["ontologytree"]:
        ontologytree_by_id[ot["id"]] = ot

    for ow in resources["ontologyword"]:
        ontologyword_by_id[ow["id"]] = ow

    service_node_counts = {}
    service_counts = {}

    imported_service_details = {}

    for unit in response.data["results"]:
        assert_unit_correctly_imported(
            unit,
            source_units_by_id.get(unit["id"]),
            ontologyword_ids_by_unit_id[unit["id"]],
        )
        imported_service_details[unit["id"]] = unit["services"]
        for service_node_id in unit["service_nodes"]:
            service_node_counts.setdefault(service_node_id, 0)
            service_node_counts[service_node_id] += 1
        for service in unit["services"]:
            service_counts[service["id"]] = service_counts.get(
                service["id"], 0) + 1

    assert_service_details_correctly_imported(
        resources["ontologyword_details"], imported_service_details)

    # Check unit counts in related objects

    response = get(api_client, reverse("servicenode-list"))
    service_nodes = response.data["results"]
    for service_node in service_nodes:
        # Note: only the totals are currently tested
        # TODO: hierarchy + municipality specific tests
        assert (service_node_counts.get(
            service_node["id"], 0) == service_node["unit_count"]["total"])
        assert_keywords_correct(ontologytree_by_id.get(service_node["id"]),
                                service_node)

    response = get(api_client, reverse("service-list"))
    services = response.data["results"]
    for service in services:
        assert service_counts.get(service["id"],
                                  0) == service["unit_count"]["total"]
        to = service
        frm = ontologyword_by_id[service["id"]]
        assert to["period_enabled"] == frm["can_add_schoolyear"]
        assert to["clarification_enabled"] == frm["can_add_clarification"]
        assert_keywords_correct(ontologyword_by_id.get(service["id"]), service)

    # Manual teardown: needed because of poor hypothesis-pytest integration
    ServiceUnitCount.objects.all().delete()
    ServiceNodeUnitCount.objects.all().delete()
    a.delete()