|
| 1 | +import pytest |
| 2 | + |
| 3 | +import plotly.graph_objects as go |
| 4 | +from plotly.basedatatypes import BaseFigure |
| 5 | + |
| 6 | + |
| 7 | +def test_selector_none(): |
| 8 | + # should return True |
| 9 | + assert BaseFigure._selector_matches({}, None) == True # arbitrary, |
| 10 | + |
| 11 | + |
| 12 | +def test_selector_empty_dict(): |
| 13 | + # should return True |
| 14 | + assert ( |
| 15 | + BaseFigure._selector_matches(dict(hello="everybody"), {}) == True # arbitrary, |
| 16 | + ) |
| 17 | + |
| 18 | + |
| 19 | +def test_selector_matches_subset_of_obj(): |
| 20 | + # should return True |
| 21 | + assert ( |
| 22 | + BaseFigure._selector_matches( |
| 23 | + dict(hello="everybody", today="cloudy", myiq=55), |
| 24 | + dict(myiq=55, today="cloudy"), |
| 25 | + ) |
| 26 | + == True |
| 27 | + ) |
| 28 | + |
| 29 | + |
| 30 | +def test_selector_has_nonmatching_key(): |
| 31 | + # should return False |
| 32 | + assert ( |
| 33 | + BaseFigure._selector_matches( |
| 34 | + dict(hello="everybody", today="cloudy", myiq=55), |
| 35 | + dict(myiq=55, cronenberg="scanners"), |
| 36 | + ) |
| 37 | + == False |
| 38 | + ) |
| 39 | + |
| 40 | + |
| 41 | +def test_selector_has_nonmatching_value(): |
| 42 | + # should return False |
| 43 | + assert ( |
| 44 | + BaseFigure._selector_matches( |
| 45 | + dict(hello="everybody", today="cloudy", myiq=55), |
| 46 | + dict(myiq=55, today="sunny"), |
| 47 | + ) |
| 48 | + == False |
| 49 | + ) |
| 50 | + |
| 51 | + |
| 52 | +def test_baseplotlytypes_could_match(): |
| 53 | + # should return True |
| 54 | + obj = go.layout.Annotation(x=1, y=2, text="pat metheny") |
| 55 | + sel = go.layout.Annotation(x=1, y=2, text="pat metheny") |
| 56 | + assert BaseFigure._selector_matches(obj, sel) == True |
| 57 | + |
| 58 | + |
| 59 | +def test_baseplotlytypes_could_not_match(): |
| 60 | + # should return False |
| 61 | + obj = go.layout.Annotation(x=1, y=3, text="pat metheny") |
| 62 | + sel = go.layout.Annotation(x=1, y=2, text="pat metheny") |
| 63 | + assert BaseFigure._selector_matches(obj, sel) == False |
| 64 | + |
| 65 | + |
| 66 | +def test_baseplotlytypes_cannot_match_subset(): |
| 67 | + # should return False because "undefined" keys in sel return None, and are |
| 68 | + # compared (because "key in sel" returned True, it's value was None) |
| 69 | + obj = go.layout.Annotation(x=1, y=2, text="pat metheny") |
| 70 | + sel = go.layout.Annotation(x=1, y=2,) |
| 71 | + assert BaseFigure._selector_matches(obj, sel) == False |
| 72 | + |
| 73 | + |
| 74 | +def test_function_selector_could_match(): |
| 75 | + # should return True |
| 76 | + obj = go.layout.Annotation(x=1, y=2, text="pat metheny") |
| 77 | + |
| 78 | + def _sel(d): |
| 79 | + return d["x"] == 1 and d["y"] == 2 and d["text"] == "pat metheny" |
| 80 | + |
| 81 | + assert BaseFigure._selector_matches(obj, _sel) == True |
| 82 | + |
| 83 | + |
| 84 | +def test_function_selector_could_not_match(): |
| 85 | + # should return False |
| 86 | + obj = go.layout.Annotation(x=1, y=2, text="pat metheny") |
| 87 | + |
| 88 | + def _sel(d): |
| 89 | + return d["x"] == 1 and d["y"] == 3 and d["text"] == "pat metheny" |
| 90 | + |
| 91 | + assert BaseFigure._selector_matches(obj, _sel) == False |
0 commit comments