serialization_unittest.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #!/usr/bin/env python3
  2. # Copyright 2020 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. """Unit tests for dependency_analysis.serialization."""
  6. import unittest.mock
  7. import class_dependency
  8. import class_json_consts
  9. import graph
  10. import json_consts
  11. import package_dependency
  12. import package_json_consts
  13. import serialization
  14. class TestSerialization(unittest.TestCase):
  15. """Unit tests for various de/serialization functions."""
  16. CLASS_1 = 'p1.c1'
  17. CLASS_2 = 'p1.c2'
  18. CLASS_3 = 'p2.c3'
  19. BUILD_TARGET_1 = '//build/target:one'
  20. BUILD_TARGET_2 = '//build/target:two'
  21. CLASS_1_NESTED_1 = 'abc'
  22. CLASS_1_NESTED_2 = 'def'
  23. CLASS_2_NESTED_1 = 'ghi'
  24. # The lists in the following JSON are sorted,
  25. # since we sort lists when serializing (for easier testing).
  26. JSON_CLASS_GRAPH = {
  27. json_consts.NODES: [
  28. {
  29. json_consts.NAME: CLASS_1,
  30. json_consts.META: {
  31. class_json_consts.PACKAGE:
  32. 'p1',
  33. class_json_consts.CLASS:
  34. 'c1',
  35. class_json_consts.BUILD_TARGETS: [BUILD_TARGET_1],
  36. class_json_consts.NESTED_CLASSES:
  37. [CLASS_1_NESTED_1, CLASS_1_NESTED_2],
  38. },
  39. },
  40. {
  41. json_consts.NAME: CLASS_2,
  42. json_consts.META: {
  43. class_json_consts.PACKAGE: 'p1',
  44. class_json_consts.CLASS: 'c2',
  45. class_json_consts.BUILD_TARGETS: [],
  46. class_json_consts.NESTED_CLASSES: [CLASS_2_NESTED_1],
  47. },
  48. },
  49. {
  50. json_consts.NAME: CLASS_3,
  51. json_consts.META: {
  52. class_json_consts.PACKAGE:
  53. 'p2',
  54. class_json_consts.CLASS:
  55. 'c3',
  56. class_json_consts.BUILD_TARGETS:
  57. [BUILD_TARGET_1, BUILD_TARGET_2],
  58. class_json_consts.NESTED_CLASSES: [],
  59. },
  60. },
  61. ],
  62. json_consts.EDGES: [
  63. {
  64. json_consts.BEGIN: CLASS_1,
  65. json_consts.END: CLASS_2,
  66. },
  67. {
  68. json_consts.BEGIN: CLASS_1,
  69. json_consts.END: CLASS_3,
  70. },
  71. {
  72. json_consts.BEGIN: CLASS_2,
  73. json_consts.END: CLASS_3,
  74. },
  75. ],
  76. }
  77. JSON_PACKAGE_GRAPH = {
  78. json_consts.NODES: [
  79. {
  80. json_consts.NAME: 'p1',
  81. json_consts.META: {
  82. package_json_consts.CLASSES: [CLASS_1, CLASS_2],
  83. },
  84. },
  85. {
  86. json_consts.NAME: 'p2',
  87. json_consts.META: {
  88. package_json_consts.CLASSES: [CLASS_3],
  89. },
  90. },
  91. ],
  92. json_consts.EDGES: [
  93. {
  94. json_consts.BEGIN: 'p1',
  95. json_consts.END: 'p1',
  96. json_consts.META: {
  97. package_json_consts.CLASS_EDGES: [
  98. [CLASS_1, CLASS_2],
  99. ],
  100. },
  101. },
  102. {
  103. json_consts.BEGIN: 'p1',
  104. json_consts.END: 'p2',
  105. json_consts.META: {
  106. package_json_consts.CLASS_EDGES: [
  107. [CLASS_1, CLASS_3],
  108. [CLASS_2, CLASS_3],
  109. ],
  110. },
  111. },
  112. ],
  113. }
  114. def test_class_serialization(self):
  115. """Tests JSON serialization of a class dependency graph."""
  116. test_graph = class_dependency.JavaClassDependencyGraph()
  117. test_graph.add_edge_if_new(self.CLASS_1, self.CLASS_2)
  118. test_graph.add_edge_if_new(self.CLASS_1, self.CLASS_3)
  119. test_graph.add_edge_if_new(self.CLASS_2, self.CLASS_3)
  120. test_graph.get_node_by_key(self.CLASS_1).add_nested_class(
  121. self.CLASS_1_NESTED_1)
  122. test_graph.get_node_by_key(self.CLASS_1).add_nested_class(
  123. self.CLASS_1_NESTED_2)
  124. test_graph.get_node_by_key(self.CLASS_2).add_nested_class(
  125. self.CLASS_2_NESTED_1)
  126. test_graph.get_node_by_key(self.CLASS_1).add_build_target(
  127. self.BUILD_TARGET_1)
  128. test_graph.get_node_by_key(self.CLASS_3).add_build_target(
  129. self.BUILD_TARGET_1)
  130. test_graph.get_node_by_key(self.CLASS_3).add_build_target(
  131. self.BUILD_TARGET_2)
  132. test_json_obj = serialization.create_json_obj_from_graph(test_graph)
  133. self.assertEqual(test_json_obj, self.JSON_CLASS_GRAPH)
  134. def test_package_serialization(self):
  135. """Tests JSON serialization of a package dependency graph."""
  136. class_graph = class_dependency.JavaClassDependencyGraph()
  137. class_graph.add_edge_if_new(self.CLASS_1, self.CLASS_2)
  138. class_graph.add_edge_if_new(self.CLASS_1, self.CLASS_3)
  139. class_graph.add_edge_if_new(self.CLASS_2, self.CLASS_3)
  140. class_graph.get_node_by_key(self.CLASS_1).add_nested_class(
  141. self.CLASS_1_NESTED_1)
  142. class_graph.get_node_by_key(self.CLASS_1).add_nested_class(
  143. self.CLASS_1_NESTED_2)
  144. class_graph.get_node_by_key(self.CLASS_2).add_nested_class(
  145. self.CLASS_2_NESTED_1)
  146. package_graph = package_dependency.JavaPackageDependencyGraph(
  147. class_graph)
  148. test_json_obj = serialization.create_json_obj_from_graph(package_graph)
  149. self.assertEqual(test_json_obj, self.JSON_PACKAGE_GRAPH)
  150. def test_class_deserialization(self):
  151. """Tests JSON deserialization of a class dependency graph.
  152. Since we only ever construct package graphs from class graphs
  153. (and that feature is tested elsewhere), we do not need to test
  154. deserialization of package dependency graphs as well.
  155. """
  156. test_graph = serialization.create_class_graph_from_json_obj(
  157. self.JSON_CLASS_GRAPH)
  158. node_1 = test_graph.get_node_by_key(self.CLASS_1)
  159. node_2 = test_graph.get_node_by_key(self.CLASS_2)
  160. node_3 = test_graph.get_node_by_key(self.CLASS_3)
  161. self.assertIsNotNone(node_1)
  162. self.assertIsNotNone(node_2)
  163. self.assertIsNotNone(node_3)
  164. self.assertEqual(node_1.nested_classes,
  165. {self.CLASS_1_NESTED_1, self.CLASS_1_NESTED_2})
  166. self.assertEqual(node_2.nested_classes, {self.CLASS_2_NESTED_1})
  167. self.assertEqual(node_3.nested_classes, set())
  168. self.assertEqual(node_1.build_targets, {self.BUILD_TARGET_1})
  169. self.assertEqual(node_2.build_targets, set())
  170. self.assertEqual(node_3.build_targets,
  171. {self.BUILD_TARGET_1, self.BUILD_TARGET_2})
  172. self.assertEqual(
  173. graph.sorted_edges_by_name(test_graph.edges),
  174. graph.sorted_edges_by_name([(node_1, node_2), (node_1, node_3),
  175. (node_2, node_3)]))
  176. if __name__ == '__main__':
  177. unittest.main()