|
| 1 | +"""Unit tests for the Diff filter and exclude methods. |
| 2 | +
|
| 3 | +Copyright (c) 2020-2021 Network To Code, LLC <info@networktocode.com> |
| 4 | +
|
| 5 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +you may not use this file except in compliance with the License. |
| 7 | +You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +""" |
| 17 | + |
| 18 | +import pytest |
| 19 | + |
| 20 | +from diffsync.diff import Diff, DiffElement |
| 21 | + |
| 22 | + |
| 23 | +def test_diff_filter_by_action_create(diff_with_children): |
| 24 | + """Filtering a Diff by action='create' should only retain elements with create diffs.""" |
| 25 | + filtered = diff_with_children.filter(actions={"create"}) |
| 26 | + actions = [] |
| 27 | + for child in filtered.get_children(): |
| 28 | + if child.action: |
| 29 | + actions.append(child.action) |
| 30 | + assert "create" in actions |
| 31 | + assert "delete" not in actions |
| 32 | + |
| 33 | + |
| 34 | +def test_diff_filter_by_action_delete(diff_with_children): |
| 35 | + """Filtering a Diff by action='delete' should only retain elements with delete diffs.""" |
| 36 | + filtered = diff_with_children.filter(actions={"delete"}) |
| 37 | + actions = [] |
| 38 | + for child in filtered.get_children(): |
| 39 | + if child.action: |
| 40 | + actions.append(child.action) |
| 41 | + assert "delete" in actions |
| 42 | + assert "create" not in actions |
| 43 | + |
| 44 | + |
| 45 | +def test_diff_filter_by_model_types(diff_with_children): |
| 46 | + """Filtering a Diff by model_types should only retain elements of those types.""" |
| 47 | + filtered = diff_with_children.filter(model_types={"person"}) |
| 48 | + types = [child.type for child in filtered.get_children()] |
| 49 | + assert "person" in types |
| 50 | + assert "device" not in types |
| 51 | + assert "address" not in types |
| 52 | + |
| 53 | + |
| 54 | +def test_diff_filter_by_action_and_model_type(diff_with_children): |
| 55 | + """Filtering by both action and model type should apply both criteria.""" |
| 56 | + filtered = diff_with_children.filter(actions={"create"}, model_types={"person"}) |
| 57 | + elements = list(filtered.get_children()) |
| 58 | + assert len(elements) == 1 |
| 59 | + assert elements[0].type == "person" |
| 60 | + assert elements[0].action == "create" |
| 61 | + |
| 62 | + |
| 63 | +def test_diff_filter_does_not_mutate_original(diff_with_children): |
| 64 | + """Calling filter() should return a new Diff without modifying the original.""" |
| 65 | + original_len = len(diff_with_children) |
| 66 | + _ = diff_with_children.filter(actions={"create"}) |
| 67 | + assert len(diff_with_children) == original_len |
| 68 | + |
| 69 | + |
| 70 | +def test_diff_exclude_by_action(diff_with_children): |
| 71 | + """Excluding by action should remove elements with that action.""" |
| 72 | + excluded = diff_with_children.exclude(actions={"delete"}) |
| 73 | + for child in excluded.get_children(): |
| 74 | + assert child.action != "delete" |
| 75 | + |
| 76 | + |
| 77 | +def test_diff_exclude_by_model_types(diff_with_children): |
| 78 | + """Excluding by model_types should remove elements of those types.""" |
| 79 | + excluded = diff_with_children.exclude(model_types={"person"}) |
| 80 | + types = [child.type for child in excluded.get_children()] |
| 81 | + assert "person" not in types |
| 82 | + |
| 83 | + |
| 84 | +def test_diff_filter_no_criteria_returns_full_copy(diff_with_children): |
| 85 | + """Filtering with no criteria should return a copy of the entire Diff.""" |
| 86 | + filtered = diff_with_children.filter() |
| 87 | + assert len(filtered) == len(diff_with_children) |
| 88 | + |
| 89 | + |
| 90 | +def test_diff_exclude_no_criteria_returns_full_copy(diff_with_children): |
| 91 | + """Excluding with no criteria should return a copy of the entire Diff.""" |
| 92 | + excluded = diff_with_children.exclude() |
| 93 | + assert len(excluded) == len(diff_with_children) |
| 94 | + |
| 95 | + |
| 96 | +def test_diff_filter_preserves_models_processed(diff_with_children): |
| 97 | + """The models_processed count should be preserved on the filtered Diff.""" |
| 98 | + filtered = diff_with_children.filter(actions={"create"}) |
| 99 | + assert filtered.models_processed == diff_with_children.models_processed |
0 commit comments