| 1 |
# SPDX-FileCopyrightText: 2015 CERN. |
| 2 |
# SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
import unittest |
| 5 |
|
| 6 |
from dictdiffer import patch |
| 7 |
from dictdiffer.merge import Merger, UnresolvedConflictsException |
| 8 |
|
| 9 |
|
| 10 |
class MergerTest(unittest.TestCase): |
| 11 |
def test_run(self): |
| 12 |
lca = {'changeme': 'Jo'} |
| 13 |
first = {'changeme': 'Joe'} |
| 14 |
second = {'changeme': 'John'} |
| 15 |
|
| 16 |
m = Merger(lca, first, second, {}) |
| 17 |
|
| 18 |
self.assertRaises(UnresolvedConflictsException, m.run) |
| 19 |
|
| 20 |
def test_continue_run(self): |
| 21 |
def take_first(conflict, _, __, ___): |
| 22 |
conflict.take = [('f', x) for x |
| 23 |
in range(len(conflict.first_patch.patches))] |
| 24 |
return True |
| 25 |
|
| 26 |
lca = {'changeme': 'Jo'} |
| 27 |
first = {'changeme': 'Joe'} |
| 28 |
second = {'changeme': 'John'} |
| 29 |
|
| 30 |
m = Merger(lca, first, second, {}) |
| 31 |
|
| 32 |
try: |
| 33 |
m.run() |
| 34 |
except UnresolvedConflictsException: |
| 35 |
pass |
| 36 |
|
| 37 |
m.continue_run(['f']) |
| 38 |
|
| 39 |
self.assertEqual(m.unified_patches, |
| 40 |
[('change', 'changeme', ('Jo', 'Joe'))]) |
| 41 |
|
| 42 |
def test_continue_run_multiple_conflicts_per_patch(self): |
| 43 |
lca = {'foo': [{'x': 1}, {'y': 2}]} |
| 44 |
first = {'foo': [{'x': 1}, {'y': 2}, {'z': 4}]} |
| 45 |
second = {'bar': 'baz'} |
| 46 |
|
| 47 |
expected = { |
| 48 |
'f': {'foo': [{'x': 1}, {'y': 2}, {'z': 4}], |
| 49 |
'bar': 'baz'}, |
| 50 |
's': {'bar': 'baz'}} |
| 51 |
|
| 52 |
for resolution, expected_value in expected.items(): |
| 53 |
m = Merger(lca, first, second, {}) |
| 54 |
try: |
| 55 |
m.run() |
| 56 |
except UnresolvedConflictsException as e: |
| 57 |
m.continue_run([resolution for _ in e.content]) |
| 58 |
|
| 59 |
self.assertEqual(patch(m.unified_patches, lca), |
| 60 |
expected_value) |
| 61 |
|
| 62 |
def test_run_with_ignore(self): |
| 63 |
lca = {'changeme': 'Jo', 'ignore': 'Something'} |
| 64 |
first = {'changeme': 'Joe', 'ignore': 'Nothing'} |
| 65 |
second = {'changeme': 'Jo', 'ignore': ''} |
| 66 |
|
| 67 |
m = Merger(lca, first, second, {}, ignore={'ignore'}) |
| 68 |
|
| 69 |
try: |
| 70 |
m.run() |
| 71 |
except UnresolvedConflictsException: |
| 72 |
self.fail('UnresolvedConflictsException should not be raised') |
| 73 |
|
| 74 |
|
| 75 |
if __name__ == '__main__': |
| 76 |
unittest.main() |