|
7 | 7 | from unittest.mock import patch |
8 | 8 |
|
9 | 9 | from _libyang import lib |
10 | | -from libyang import Context, DContainer, DRpc, LibyangError |
| 10 | +from libyang import Context, DContainer, DDiff, DRpc, LibyangError |
11 | 11 |
|
12 | 12 |
|
13 | 13 | YANG_DIR = os.path.join(os.path.dirname(__file__), "yang") |
@@ -489,3 +489,79 @@ def test_data_to_dict_action(self): |
489 | 489 | }, |
490 | 490 | }, |
491 | 491 | ) |
| 492 | + |
| 493 | + XML_DIFF_STATE1 = """<state xmlns="urn:yang:yolo:system"> |
| 494 | + <hostname>foo</hostname> |
| 495 | + <speed>1234</speed> |
| 496 | + <number>1000</number> |
| 497 | + <number>2000</number> |
| 498 | + <number>3000</number> |
| 499 | + <url> |
| 500 | + <proto>https</proto> |
| 501 | + <host>github.com</host> |
| 502 | + <path>/CESNET/libyang-python</path> |
| 503 | + <enabled>false</enabled> |
| 504 | + </url> |
| 505 | + <url> |
| 506 | + <proto>http</proto> |
| 507 | + <host>foobar.com</host> |
| 508 | + <port>8080</port> |
| 509 | + <path>/index.html</path> |
| 510 | + <enabled>true</enabled> |
| 511 | + </url> |
| 512 | +</state> |
| 513 | +""" |
| 514 | + XML_DIFF_STATE2 = """<state xmlns="urn:yang:yolo:system"> |
| 515 | + <hostname>foo</hostname> |
| 516 | + <speed>5432</speed> |
| 517 | + <number>1000</number> |
| 518 | + <number>3000</number> |
| 519 | + <url> |
| 520 | + <proto>https</proto> |
| 521 | + <host>github.com</host> |
| 522 | + <path>/CESNET/libyang-python</path> |
| 523 | + <enabled>true</enabled> |
| 524 | + </url> |
| 525 | + <url> |
| 526 | + <proto>http</proto> |
| 527 | + <host>foobar.com</host> |
| 528 | + <port>8080</port> |
| 529 | + <path>/index.html</path> |
| 530 | + <enabled>false</enabled> |
| 531 | + </url> |
| 532 | + <url> |
| 533 | + <proto>ftp</proto> |
| 534 | + <host>github.com</host> |
| 535 | + <path>/CESNET/libyang-python</path> |
| 536 | + <enabled>false</enabled> |
| 537 | + </url> |
| 538 | +</state> |
| 539 | +""" |
| 540 | + |
| 541 | + def test_data_diff(self): |
| 542 | + dnode1 = self.ctx.parse_data_mem( |
| 543 | + self.XML_DIFF_STATE1, "xml", data=True, no_yanglib=True |
| 544 | + ) |
| 545 | + self.assertIsInstance(dnode1, DContainer) |
| 546 | + dnode2 = self.ctx.parse_data_mem( |
| 547 | + self.XML_DIFF_STATE2, "xml", data=True, no_yanglib=True |
| 548 | + ) |
| 549 | + self.assertIsInstance(dnode2, DContainer) |
| 550 | + |
| 551 | + diffs = dnode1.diff(dnode2) |
| 552 | + diffs_result = [ |
| 553 | + (diff.dtype, diff.first.name(), diff.second.name() if diff.second else None) |
| 554 | + for diff in diffs |
| 555 | + ] |
| 556 | + expected = [ |
| 557 | + (DDiff.CHANGED, "speed", "speed"), |
| 558 | + (DDiff.CHANGED, "enabled", "enabled"), |
| 559 | + (DDiff.CHANGED, "enabled", "enabled"), |
| 560 | + (DDiff.DELETED, "number", None), |
| 561 | + (DDiff.CREATED, "state", "url"), |
| 562 | + ] |
| 563 | + |
| 564 | + self.assertListEqual(diffs_result, expected) |
| 565 | + |
| 566 | + dnode1.free() |
| 567 | + dnode2.free() |
0 commit comments