Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

relation: modified lookup_data for nested fields & 📦 release: v2.4.0 #325

Merged
merged 4 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 10 additions & 60 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
#
# This file is part of Invenio.
# Copyright (C) 2020 CERN.
# Copyright (C) 2020-2024 CERN.
# Copyright (C) 2022 Graz University of Technology.
#
# Invenio is free software; you can redistribute it and/or modify it
Expand All @@ -11,9 +11,11 @@ name: CI

on:
push:
branches: master
branches:
- master
pull_request:
branches: master
branches:
- master
schedule:
# * is a special character in YAML so you have to quote this string
- cron: "0 3 * * 6"
Expand All @@ -25,60 +27,8 @@ on:
default: "Manual trigger"

jobs:
Tests:
runs-on: ubuntu-20.04
strategy:
matrix:
python-version: [3.8, 3.9]
requirements-level: [pypi]
db-service: [postgresql14, mysql8]
include:
- db-service: postgresql14
DB: postgresql
POSTGRESQL_VERSION: POSTGRESQL_14_LATEST
SQLALCHEMY_DATABASE_URI: "postgresql+psycopg2://invenio:invenio@localhost:5432/invenio"
EXTRAS: "tests,postgresql"

- db-service: mysql8
DB: mysql
MYSQL_VERSION: MYSQL_8_LATEST
SQLALCHEMY_DATABASE_URI: "mysql+pymysql://invenio:invenio@localhost:3306/invenio"
EXTRAS: "tests,mysql"

env:
SQLALCHEMY_DATABASE_URI: ${{matrix.SQLALCHEMY_DATABASE_URI}}
POSTGRESQL_VERSION: ${{matrix.POSTGRESQL_VERSION}}
MYSQL_VERSION: ${{matrix.MYSQL_VERSION}}
DB: ${{ matrix.DB }}

steps:
- name: Checkout
uses: actions/checkout@v2

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}

- name: Generate dependencies
run: |
pip install wheel requirements-builder
requirements-builder -e ${{ matrix.EXTRAS }} --level=${{ matrix.requirements-level }} setup.py > .${{ matrix.requirements-level }}-${{ matrix.python-version }}-requirements.txt

- name: Cache pip
uses: actions/cache@v2
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('.${{ matrix.requirements-level }}-${{ matrix.python-version }}-requirements.txt') }}

- name: Install dependencies
run: |
pip install -r .${{matrix.requirements-level}}-${{ matrix.python-version }}-requirements.txt
pip install .[${{ matrix.EXTRAS }}]
pip freeze
docker --version
docker-compose --version

- name: Run tests
run: |
./run-tests.sh
Python:
uses: inveniosoftware/workflows/.github/workflows/tests-python.yml@master
with:
extras: "tests,postgresql,mysql"
search-service: '[""]'
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
Changes
=======

Version v2.4.0 (released 2024-10-08)

- relation: modified lookup_data for nested fields
- Adds translations

Version 2.3.0 (released 2024-02-19)

- tests: add tests for filter_dict_keys
Expand Down
2 changes: 1 addition & 1 deletion invenio_records/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@
from .api import Record
from .ext import InvenioRecords

__version__ = "2.3.0"
__version__ = "2.4.0"

__all__ = (
"InvenioRecords",
Expand Down
17 changes: 12 additions & 5 deletions invenio_records/systemfields/relations/results.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# -*- coding: utf-8 -*-
#
# This file is part of Invenio.
# Copyright (C) 2020-2021 CERN.
# Copyright (C) 2020-2024 CERN.
#
# Invenio is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.

"""Relations system field."""

from itertools import chain

from ...dictutils import dict_lookup, dict_set
from .errors import InvalidCheckValue, InvalidRelationValue

Expand Down Expand Up @@ -154,10 +156,15 @@ def _lookup_id(self, data):
def _lookup_data(self):
data = dict_lookup(self.record, self.key)
if self.relation_field:
filtered_data = [
e.get(self.relation_field) for e in data if self.relation_field in e
]
return filtered_data
fields = self.relation_field.split(".")
0einstein0 marked this conversation as resolved.
Show resolved Hide resolved
for field in fields:
if isinstance(data, list):
data = [d.get(field) for d in data if d.get(field)]
else:
data = data.get(field)
if not data:
return []

return data

def validate(self):
Expand Down
4 changes: 2 additions & 2 deletions invenio_records/validators.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
#
# This file is part of Invenio.
# Copyright (C) 2015-2018 CERN.
# Copyright (C) 2015-2024 CERN.
# Copyright (C) 2021 TU Wien.
#
# Invenio is free software; you can redistribute it and/or modify it
Expand All @@ -11,7 +11,7 @@

from jsonschema.validators import Draft4Validator, extend, validator_for

PartialDraft4Validator = extend(Draft4Validator, {"required": None})
PartialDraft4Validator = extend(Draft4Validator, {"required": lambda *args: None})
"""Partial JSON Schema (draft 4) validator.

Special validator that contains the same validation rules of Draft4Validator,
Expand Down
53 changes: 52 additions & 1 deletion tests/test_relations_systemfield.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
#
# This file is part of Invenio.
# Copyright (C) 2020 CERN.
# Copyright (C) 2020-2024 CERN.
#
# Invenio is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.
Expand Down Expand Up @@ -1356,3 +1356,54 @@ class Record4(Record, SystemFieldsMixin):
assert res == oe_lang
with pytest.raises(StopIteration): # finished first iterator
next(res_inner_iter)


def test_nested_field_dereferencing(testapp, db, languages):
"""Test dereferencing with relation_field containing nested relation."""
Language, languages = languages
en_lang = languages["en"]
fr_lang = languages["fr"]

class Record1(Record, SystemFieldsMixin):
# Example for testing a nested field dereferencing
relations = RelationsField(
nested_array_of_objects=PKNestedListRelation(
key="metadata",
keys=["iso", "information", "native_speakers"],
record_cls=Language,
relation_field="desc.languages",
),
)

# Define the record with nested fields
record_data = {
"metadata": [
{
"desc": {
"id": "1",
"text": "Lorem ipsum dolor sit amet, consectetur adipiscing elit",
"languages": [{"id": str(en_lang.id)}, {"id": str(fr_lang.id)}],
}
},
]
}

# Create the record instance
record = Record1(record_data)

# Test that dereferencing works for the languages
record.relations.nested_array_of_objects.dereference()

# Check that the languages have been dereferenced correctly
assert record["metadata"][0]["desc"]["languages"][0] == {
"id": str(en_lang.id),
"iso": en_lang["iso"],
"information": en_lang["information"],
"@v": str(en_lang.id) + "::" + str(en_lang.revision_id),
zzacharo marked this conversation as resolved.
Show resolved Hide resolved
}
assert record["metadata"][0]["desc"]["languages"][1] == {
zzacharo marked this conversation as resolved.
Show resolved Hide resolved
"id": str(fr_lang.id),
"iso": fr_lang["iso"],
"information": fr_lang["information"],
"@v": str(fr_lang.id) + "::" + str(fr_lang.revision_id),
}