def test_update_location(user: User):
    parent_location = locations.create_location("Parent Location",
                                                "This is an example location",
                                                None, user.id)
    location = locations.create_location("Location",
                                         "This is an example location", None,
                                         user.id)
    assert len(locations.get_locations()) == 2
    locations.update_location(location.id, "Updated Location",
                              "This is a location description", None, user.id)
    assert len(locations.get_locations()) == 2
    location = locations.get_location(location.id)
    assert location.name == "Updated Location"
    assert location.description == "This is a location description"
    assert location.parent_location_id is None
    locations.update_location(location.id, "Updated Location",
                              "This is a location description",
                              parent_location.id, user.id)
    location = locations.get_location(location.id)
    assert location.parent_location_id == parent_location.id
    user_log_entries = user_log.get_user_log_entries(user.id)
    assert [
        e for e in user_log_entries
        if e.data.get('location_id', -1) == location.id
        and e.type == UserLogEntryType.UPDATE_LOCATION
    ]
def test_get_location_tree(user: User):
    child_location2 = locations.create_location("Location",
                                                "This is an example location",
                                                None, user.id)
    parent_location = locations.create_location("Location",
                                                "This is an example location",
                                                None, user.id)
    location = locations.create_location("Location",
                                         "This is an example location",
                                         parent_location.id, user.id)
    child_location1 = locations.create_location("Location",
                                                "This is an example location",
                                                location.id, user.id)
    locations.update_location(child_location2.id, "Location",
                              "This is an example location", location.id,
                              user.id)
    locations_map, locations_tree = locations.get_locations_tree()
    child_location2 = locations.get_location(child_location2.id)
    assert locations_map == {
        parent_location.id: parent_location,
        location.id: location,
        child_location1.id: child_location1,
        child_location2.id: child_location2
    }
    assert locations_tree == {
        parent_location.id: {
            location.id: {
                child_location1.id: {},
                child_location2.id: {}
            }
        }
    }
def test_create_location_with_parent_location(user: User):
    parent_location = locations.create_location("Parent Location",
                                                "This is an example location",
                                                None, user.id)
    assert len(locations.get_locations()) == 1
    user_log_entries = user_log.get_user_log_entries(user.id)
    assert len(user_log_entries) == 1
    location = locations.create_location("Example Location",
                                         "This is an example location",
                                         parent_location.id, user.id)
    assert len(locations.get_locations()) == 2
    location = locations.get_location(location.id)
    assert location.name == "Example Location"
    assert location.description == "This is an example location"
    assert location.parent_location_id == parent_location.id
    user_log_entries = user_log.get_user_log_entries(user.id)
    assert len(user_log_entries) == 2
    user_log_entry = [
        e for e in user_log_entries
        if e.data.get('location_id', -1) == location.id
    ][0]
    assert user_log_entry.type == UserLogEntryType.CREATE_LOCATION
    assert user_log_entry.data['location_id'] == location.id