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

Calling first() on NodeSet causes future calls on .all() to be filtered #581

Open
gs00cut opened this issue Oct 15, 2021 · 2 comments
Open

Comments

@gs00cut
Copy link

gs00cut commented Oct 15, 2021

Hi,

I am running neomodel 4.0.1.

I have implemented a method that performs simple ordering on node set based on 2 integer properties MajorVersion/MinorVersion. I simply do a test on first node to see if nodes well have these properties and then do the ordering (see below).

I made some tests and noticed strange behaviours between theNodeSetobject and the expanded list for the same NodeSet when calling the .all() method.

Behaviour 1
Calling .first() method on NodeSet causes later calls to .all() to be filtered on the first node of the nodeset:

image

As you can see, calling .all() after the check on the first item in nodeset returns a list of 1 item while the nodeset contains 6 as expected.

Behaviour 2
As a workaround, I thought avoiding calling .first() method would do the trick but I noticed that once .all() is called, the order_by method no longer works on the nodeset:

image

As you can see in debug console, the languages are not sorted ascendingly by MajorVersion (version can be seen in field slug_languageid) as it should be.

I finally ended up with this workaround working well:

    @staticmethod
    def order_by_version(nodes: List[NodeEntity]) -> List[NodeEntity]:
        """Order nodeset set by descending version.

        Nodes in the node set must have MajorVersion and MinorVersion attributes.
        """
        if (
            not hasattr(nodes[0], "MajorVersion")
            or not hasattr(nodes[0], "MinorVersion")
        ):
            raise AttributeError("Attributes 'MajorVersion'/'MinorVersion' not defined.")

        nodes = sorted(nodes, key=lambda x: (-x.MajorVersion, -x.MinorVersion))
        return nodes

However I have the feeling that something is not working as expected regarding usage of nodeset / nodeset.all().

Thanks,

Gilles

@AntonLydike
Copy link
Collaborator

Hi Gilles, can you provide a small, self-contained test case so that we can better understand the issue and fix it?

@gs00cut
Copy link
Author

gs00cut commented Nov 29, 2022

Hi,

Sorry for the late answer. Thanks for looking at the issue.

Basically the scenario to reproduce the problem is quite basic:

For the first observation:

  1. Populate a NodeSet nodeset object of length > 1 (no matter the type of nodes in it)
  2. Call the method nodeset.first()
  3. Call the method nodeset.all() => you don't get a list of all nodes, but only a list containing the first node

For the second observation:

  1. Populate a NodeSet nodeset object of length > 1 (no matter the type of nodes in it)
  2. Call the method nodeset.all()
  3. Then call the method nodeset.order_by("YOUR_PROPERTY").all() (you can order by any property you have defined on your nodes) => the list of nodes is not ordered as expected

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants