def test_service_node_counts_add_service_node_to_units(units, api_client):
    # Add service node 3 to all units
    sn3_obj = ServiceNode.objects.get(pk=3)
    for o in Unit.objects.all():
        o.service_nodes.add(sn3_obj)

    update_service_node_counts()

    service_nodes = sorted(get_nodes(api_client), key=lambda x: x['id'])
    service_node_1 = service_nodes[0]
    service_node_2 = service_nodes[1]
    service_node_3 = service_nodes[2]

    assert service_node_1['id'] == 1
    assert service_node_2['id'] == 2
    assert service_node_1['unit_count']['total'] == 2
    assert service_node_1['unit_count']['municipality']['a'] == 1
    assert service_node_1['unit_count']['municipality']['b'] == 1
    assert len(service_node_1['unit_count']['municipality']) == 2
    assert service_node_2['unit_count']['total'] == 2
    assert service_node_2['unit_count']['municipality']['a'] == 1
    assert service_node_2['unit_count']['municipality']['_unknown'] == 1
    assert len(service_node_2['unit_count']['municipality']) == 2
    assert service_node_3['unit_count']['total'] == 5
    assert service_node_3['unit_count']['municipality']['a'] == 3
    assert service_node_3['unit_count']['municipality']['b'] == 1
    assert service_node_3['unit_count']['municipality']['_unknown'] == 1
    assert len(service_node_3['unit_count']['municipality']) == 3
Ejemplo n.º 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()
     update_service_counts()
Ejemplo n.º 3
0
def test_service_node_counts_add_service_node_to_units(units, api_client):
    # Add service node 3 to all units
    sn3_obj = ServiceNode.objects.get(pk=3)
    for o in Unit.objects.all():
        o.service_nodes.add(sn3_obj)

    update_service_node_counts()

    service_nodes = sorted(get_nodes(api_client), key=lambda x: x["id"])
    service_node_1 = service_nodes[0]
    service_node_2 = service_nodes[1]
    service_node_3 = service_nodes[2]

    assert service_node_1["id"] == 1
    assert service_node_2["id"] == 2
    assert service_node_1["unit_count"]["total"] == 2
    assert service_node_1["unit_count"]["municipality"]["a"] == 1
    assert service_node_1["unit_count"]["municipality"]["b"] == 1
    assert len(service_node_1["unit_count"]["municipality"]) == 2
    assert service_node_2["unit_count"]["total"] == 2
    assert service_node_2["unit_count"]["municipality"]["a"] == 1
    assert service_node_2["unit_count"]["municipality"]["_unknown"] == 1
    assert len(service_node_2["unit_count"]["municipality"]) == 2
    assert service_node_3["unit_count"]["total"] == 5
    assert service_node_3["unit_count"]["municipality"]["a"] == 3
    assert service_node_3["unit_count"]["municipality"]["b"] == 1
    assert service_node_3["unit_count"]["municipality"]["_unknown"] == 1
    assert len(service_node_3["unit_count"]["municipality"]) == 3
def test_service_node_counts_add_service_node_to_units(units, api_client):
    # Add service node 3 to all units
    sn3_obj = ServiceNode.objects.get(pk=3)
    for o in Unit.objects.all():
        o.service_nodes.add(sn3_obj)

    update_service_node_counts()

    service_nodes = sorted(get_nodes(api_client), key=lambda x: x['id'])
    service_node_1 = service_nodes[0]
    service_node_2 = service_nodes[1]
    service_node_3 = service_nodes[2]

    assert service_node_1['id'] == 1
    assert service_node_2['id'] == 2
    assert service_node_1['unit_count']['total'] == 2
    assert service_node_1['unit_count']['municipality']['a'] == 1
    assert service_node_1['unit_count']['municipality']['b'] == 1
    assert len(service_node_1['unit_count']['municipality']) == 2
    assert service_node_2['unit_count']['total'] == 2
    assert service_node_2['unit_count']['municipality']['a'] == 1
    assert service_node_2['unit_count']['municipality']['_unknown'] == 1
    assert len(service_node_2['unit_count']['municipality']) == 2
    assert service_node_3['unit_count']['total'] == 5
    assert service_node_3['unit_count']['municipality']['a'] == 3
    assert service_node_3['unit_count']['municipality']['b'] == 1
    assert service_node_3['unit_count']['municipality']['_unknown'] == 1
    assert len(service_node_3['unit_count']['municipality']) == 3
Ejemplo n.º 5
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()
Ejemplo n.º 6
0
    def import_units(self):
        units = get_turku_resource("palvelupisteet")

        for unit in units:
            self._handle_unit(unit)

        self.unitsyncher.finish()

        update_service_node_counts()
        remove_empty_service_nodes(self.logger)
def test_service_node_counts_remove_service_node_from_units(units, api_client):
    # Remove service node 1 from all units
    sn1_obj = ServiceNode.objects.get(pk=1)
    for unit in sn1_obj.units.all():
        unit.service_nodes.remove(sn1_obj)

    update_service_node_counts()

    service_nodes = sorted(get_nodes(api_client), key=lambda x: x['id'])
    service_node_1 = service_nodes[0]
    service_node_2 = service_nodes[1]
    service_node_3 = service_nodes[2]

    assert service_node_1['id'] == 1
    assert service_node_2['id'] == 2
    assert service_node_1['unit_count']['total'] == 0
    assert len(service_node_1['unit_count']['municipality']) == 0
    assert service_node_2['unit_count']['total'] == 2
    assert service_node_2['unit_count']['municipality']['a'] == 1
    assert service_node_2['unit_count']['municipality']['_unknown'] == 1
    assert len(service_node_2['unit_count']['municipality']) == 2
    assert service_node_3['unit_count']['total'] == 1
    assert service_node_3['unit_count']['municipality']['a'] == 1
    assert len(service_node_3['unit_count']['municipality']) == 1
def test_service_node_counts_remove_service_node_from_units(units, api_client):
    # Remove service node 1 from all units
    sn1_obj = ServiceNode.objects.get(pk=1)
    for unit in sn1_obj.units.all():
        unit.service_nodes.remove(sn1_obj)

    update_service_node_counts()

    service_nodes = sorted(get_nodes(api_client), key=lambda x: x['id'])
    service_node_1 = service_nodes[0]
    service_node_2 = service_nodes[1]
    service_node_3 = service_nodes[2]

    assert service_node_1['id'] == 1
    assert service_node_2['id'] == 2
    assert service_node_1['unit_count']['total'] == 0
    assert len(service_node_1['unit_count']['municipality']) == 0
    assert service_node_2['unit_count']['total'] == 2
    assert service_node_2['unit_count']['municipality']['a'] == 1
    assert service_node_2['unit_count']['municipality']['_unknown'] == 1
    assert len(service_node_2['unit_count']['municipality']) == 2
    assert service_node_3['unit_count']['total'] == 1
    assert service_node_3['unit_count']['municipality']['a'] == 1
    assert len(service_node_3['unit_count']['municipality']) == 1
Ejemplo n.º 9
0
def test_service_node_counts_remove_service_node_from_units(units, api_client):
    # Remove service node 1 from all units
    sn1_obj = ServiceNode.objects.get(pk=1)
    for unit in sn1_obj.units.all():
        unit.service_nodes.remove(sn1_obj)

    update_service_node_counts()

    service_nodes = sorted(get_nodes(api_client), key=lambda x: x["id"])
    service_node_1 = service_nodes[0]
    service_node_2 = service_nodes[1]
    service_node_3 = service_nodes[2]

    assert service_node_1["id"] == 1
    assert service_node_2["id"] == 2
    assert service_node_1["unit_count"]["total"] == 0
    assert len(service_node_1["unit_count"]["municipality"]) == 0
    assert service_node_2["unit_count"]["total"] == 2
    assert service_node_2["unit_count"]["municipality"]["a"] == 1
    assert service_node_2["unit_count"]["municipality"]["_unknown"] == 1
    assert len(service_node_2["unit_count"]["municipality"]) == 2
    assert service_node_3["unit_count"]["total"] == 1
    assert service_node_3["unit_count"]["municipality"]["a"] == 1
    assert len(service_node_3["unit_count"]["municipality"]) == 1
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_service_node_counts_delete_units(units, api_client):
    for service_node in get_nodes(api_client):
        assert service_node['unit_count']['total'] == 0
        assert len(service_node['unit_count']['municipality']) == 0

    update_service_node_counts()

    def check_before_deletions():
        service_nodes = sorted(get_nodes(api_client), key=lambda x: x['id'])

        service_node_1 = service_nodes[0]
        service_node_2 = service_nodes[1]
        service_node_3 = service_nodes[2]

        assert service_node_1['id'] == 1
        assert service_node_2['id'] == 2
        assert service_node_1['unit_count']['total'] == 2
        assert service_node_1['unit_count']['municipality']['a'] == 1
        assert service_node_1['unit_count']['municipality']['b'] == 1
        assert len(service_node_1['unit_count']['municipality']) == 2
        assert service_node_2['unit_count']['total'] == 2
        assert service_node_2['unit_count']['municipality']['a'] == 1
        assert service_node_2['unit_count']['municipality']['_unknown'] == 1
        assert len(service_node_2['unit_count']['municipality']) == 2
        assert service_node_3['unit_count']['total'] == 1
        assert service_node_3['unit_count']['municipality']['a'] == 1
        assert len(service_node_3['unit_count']['municipality']) == 1

    check_before_deletions()

    u = Unit.objects.get(pk=4)
    assert u.service_nodes.count() == 0
    u.delete()

    # Deleting a Unit without services shouldn't affect the results
    check_before_deletions()

    # From service nodes 1 & 2 remove one unit with muni 'a' (delete unit)
    Unit.objects.get(pk=1).delete()
    update_service_node_counts()

    service_nodes = sorted(get_nodes(api_client), key=lambda x: x['id'])

    service_node_1 = service_nodes[0]
    service_node_2 = service_nodes[1]
    service_node_3 = service_nodes[2]

    assert service_node_1['id'] == 1
    assert service_node_2['id'] == 2
    assert service_node_1['unit_count']['total'] == 1
    assert service_node_1['unit_count']['municipality'].get('a') is None
    assert service_node_1['unit_count']['municipality']['b'] == 1
    assert len(service_node_1['unit_count']['municipality']) == 1
    assert service_node_2['unit_count']['total'] == 1
    assert service_node_2['unit_count']['municipality'].get('a') is None
    assert service_node_2['unit_count']['municipality']['_unknown'] == 1
    assert len(service_node_2['unit_count']['municipality']) == 1
    assert service_node_3['unit_count']['total'] == 1
    assert service_node_3['unit_count']['municipality']['a'] == 1
    assert len(service_node_3['unit_count']['municipality']) == 1

    # From service node 3 remove all units (delete unit)
    Unit.objects.get(pk=5).delete()
    update_service_node_counts()

    service_nodes = sorted(get_nodes(api_client), key=lambda x: x['id'])

    service_node_1 = service_nodes[0]
    service_node_2 = service_nodes[1]
    service_node_3 = service_nodes[2]

    assert service_node_1['id'] == 1
    assert service_node_2['id'] == 2
    assert service_node_1['unit_count']['total'] == 1
    assert service_node_1['unit_count']['municipality'].get('a') is None
    assert service_node_1['unit_count']['municipality']['b'] == 1
    assert len(service_node_1['unit_count']['municipality']) == 1
    assert service_node_2['unit_count']['total'] == 1
    assert service_node_2['unit_count']['municipality'].get('a') is None
    assert service_node_2['unit_count']['municipality']['_unknown'] == 1
    assert len(service_node_2['unit_count']['municipality']) == 1
    assert service_node_3['unit_count']['total'] == 0
    assert len(service_node_3['unit_count']['municipality']) == 0

    # From service node 2 remove unit with muncipality None
    Unit.objects.get(pk=3).delete()
    update_service_node_counts()

    service_nodes = sorted(get_nodes(api_client), key=lambda x: x['id'])

    service_node_1 = service_nodes[0]
    service_node_2 = service_nodes[1]
    service_node_3 = service_nodes[2]

    assert service_node_1['id'] == 1
    assert service_node_2['id'] == 2
    assert service_node_1['unit_count']['total'] == 1
    assert service_node_1['unit_count']['municipality'].get('a') is None
    assert service_node_1['unit_count']['municipality']['b'] == 1
    assert len(service_node_1['unit_count']['municipality']) == 1
    assert service_node_2['unit_count']['total'] == 0
    assert service_node_2['unit_count']['municipality'].get('a') is None
    assert service_node_2['unit_count']['municipality'].get('_unknown') is None
    assert len(service_node_2['unit_count']['municipality']) == 0
    assert service_node_3['unit_count']['total'] == 0
    assert len(service_node_3['unit_count']['municipality']) == 0
 def update_service_node_unit_counts(self):
     update_service_node_counts()
Ejemplo n.º 13
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()
Ejemplo n.º 14
0
def test_service_node_counts_delete_units(units, api_client):
    for service_node in get_nodes(api_client):
        assert service_node["unit_count"]["total"] == 0
        assert len(service_node["unit_count"]["municipality"]) == 0

    update_service_node_counts()

    def check_before_deletions():
        service_nodes = sorted(get_nodes(api_client), key=lambda x: x["id"])

        service_node_1 = service_nodes[0]
        service_node_2 = service_nodes[1]
        service_node_3 = service_nodes[2]

        assert service_node_1["id"] == 1
        assert service_node_2["id"] == 2
        assert service_node_1["unit_count"]["total"] == 2
        assert service_node_1["unit_count"]["municipality"]["a"] == 1
        assert service_node_1["unit_count"]["municipality"]["b"] == 1
        assert len(service_node_1["unit_count"]["municipality"]) == 2
        assert service_node_2["unit_count"]["total"] == 2
        assert service_node_2["unit_count"]["municipality"]["a"] == 1
        assert service_node_2["unit_count"]["municipality"]["_unknown"] == 1
        assert len(service_node_2["unit_count"]["municipality"]) == 2
        assert service_node_3["unit_count"]["total"] == 1
        assert service_node_3["unit_count"]["municipality"]["a"] == 1
        assert len(service_node_3["unit_count"]["municipality"]) == 1

    check_before_deletions()

    u = Unit.objects.get(pk=4)
    assert u.service_nodes.count() == 0
    u.delete()

    # Deleting a Unit without services shouldn't affect the results
    check_before_deletions()

    # From service nodes 1 & 2 remove one unit with muni 'a' (delete unit)
    Unit.objects.get(pk=1).delete()
    update_service_node_counts()

    service_nodes = sorted(get_nodes(api_client), key=lambda x: x["id"])

    service_node_1 = service_nodes[0]
    service_node_2 = service_nodes[1]
    service_node_3 = service_nodes[2]

    assert service_node_1["id"] == 1
    assert service_node_2["id"] == 2
    assert service_node_1["unit_count"]["total"] == 1
    assert service_node_1["unit_count"]["municipality"].get("a") is None
    assert service_node_1["unit_count"]["municipality"]["b"] == 1
    assert len(service_node_1["unit_count"]["municipality"]) == 1
    assert service_node_2["unit_count"]["total"] == 1
    assert service_node_2["unit_count"]["municipality"].get("a") is None
    assert service_node_2["unit_count"]["municipality"]["_unknown"] == 1
    assert len(service_node_2["unit_count"]["municipality"]) == 1
    assert service_node_3["unit_count"]["total"] == 1
    assert service_node_3["unit_count"]["municipality"]["a"] == 1
    assert len(service_node_3["unit_count"]["municipality"]) == 1

    # From service node 3 remove all units (delete unit)
    Unit.objects.get(pk=5).delete()
    update_service_node_counts()

    service_nodes = sorted(get_nodes(api_client), key=lambda x: x["id"])

    service_node_1 = service_nodes[0]
    service_node_2 = service_nodes[1]
    service_node_3 = service_nodes[2]

    assert service_node_1["id"] == 1
    assert service_node_2["id"] == 2
    assert service_node_1["unit_count"]["total"] == 1
    assert service_node_1["unit_count"]["municipality"].get("a") is None
    assert service_node_1["unit_count"]["municipality"]["b"] == 1
    assert len(service_node_1["unit_count"]["municipality"]) == 1
    assert service_node_2["unit_count"]["total"] == 1
    assert service_node_2["unit_count"]["municipality"].get("a") is None
    assert service_node_2["unit_count"]["municipality"]["_unknown"] == 1
    assert len(service_node_2["unit_count"]["municipality"]) == 1
    assert service_node_3["unit_count"]["total"] == 0
    assert len(service_node_3["unit_count"]["municipality"]) == 0

    # From service node 2 remove unit with muncipality None
    Unit.objects.get(pk=3).delete()
    update_service_node_counts()

    service_nodes = sorted(get_nodes(api_client), key=lambda x: x["id"])

    service_node_1 = service_nodes[0]
    service_node_2 = service_nodes[1]
    service_node_3 = service_nodes[2]

    assert service_node_1["id"] == 1
    assert service_node_2["id"] == 2
    assert service_node_1["unit_count"]["total"] == 1
    assert service_node_1["unit_count"]["municipality"].get("a") is None
    assert service_node_1["unit_count"]["municipality"]["b"] == 1
    assert len(service_node_1["unit_count"]["municipality"]) == 1
    assert service_node_2["unit_count"]["total"] == 0
    assert service_node_2["unit_count"]["municipality"].get("a") is None
    assert service_node_2["unit_count"]["municipality"].get("_unknown") is None
    assert len(service_node_2["unit_count"]["municipality"]) == 0
    assert service_node_3["unit_count"]["total"] == 0
    assert len(service_node_3["unit_count"]["municipality"]) == 0
Ejemplo n.º 15
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()
Ejemplo n.º 16
0
 def update_service_node_unit_counts(self):
     update_service_node_counts()
Ejemplo n.º 17
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)