canonical / canonical/checkbox

`and`ed expressions evaluate different objects unless parenthesized

Open
#665 7 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Python
Stars
52
Forks
81
Avg merge
2d 18h
Merged PRs (30d)
70

Description

### Bug Description

According to [the documentation](https://checkbox.readthedocs.io/en/latest/reference/units/resource.html#exactly-one-resource-bound-to-a-variable-at-once):
> It’s not possible to refer to two different resources, from the same resource group, in one resource expression.

Consider this test:

```python
def test_evaluate_same_object(self):
resource_map = {
'a': [Resource({'foo': 1, 'baz': 'b'}), Resource({'foo': 2, 'baz': 'a'})],
}
expr = ResourceExpression("a.foo == 1 and a.baz != 'b'")
self.assertFalse(expr.evaluate(
resource_map['a'],
resource_map=resource_map
))
```

And it failing:
```
FAIL: test_evaluate_same_object (plainbox.impl.test_resource.ResourceExpressionTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/michal/dev/canonical/checkbox/checkbox-ng/plainbox/impl/test_resource.py", line 426, in test_evaluate_same_object
self.assertFalse(expr.evaluate(
AssertionError: True is not false
```

There is not a single resource whose `foo` is 1, and `baz` is not `b`. But the expression succeeds. Wrapping the first expression in parentheses makes it work as intended.

### To Reproduce

Run the above test.

### Environment

N/A

### Relevant log output

_No response_

### Additional context

_No response_

Contributor guide

Open the contributing guide

Research direction

Start with plainbox/impl/test_resource.py and ResourceExpressionTests.test_evaluate_same_object, using the reproducer from the issue. Then trace ResourceExpression evaluation to understand why the two sides of the `and` use different resources. Done means the unparenthesized expression evaluates false and the regression test passes.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.