PageRenderTime 25ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/test_create_popcon_clusters.py

https://gitlab.com/TCC-AppRecommender/scripts
Python | 241 lines | 53 code | 43 blank | 145 comment | 5 complexity | 6f62b6c597d6ae2ab8e18bf8de9cfb76 MD5 | raw file
  1. #!/usr/bin/env python
  2. import os
  3. import shutil
  4. import unittest
  5. import numpy as np
  6. import create_popcon_clusters as cpc
  7. class CreatePopconClustersTests(unittest.TestCase):
  8. def create_popcon_file(self, file_path, pkgs):
  9. popcon_header = 'POPULARITY-CONTEST-0 TIME:370542026 ID:1popcon' \
  10. 'ARCH:amd64 POPCONVER: VENDOR:Debian\n'
  11. with open(file_path, 'wb') as text:
  12. text.write(popcon_header)
  13. for pkg in pkgs:
  14. line = '15019500 154428337 {0} /usr/bin/{0}\n'.format(pkg)
  15. text.write(line)
  16. text.write('END-POPULARITY-CONTEST-0 TIME:1464009355\n')
  17. def test_get_submissions(self):
  18. popcon_entries_path = 'popcon_entries_for_tests/'
  19. popcon_folders = [popcon_entries_path + '1a/',
  20. popcon_entries_path + '2a/']
  21. popcon_files_path = [popcon_folders[0] + '1a1popcon',
  22. popcon_folders[0] + '1a2popcon',
  23. popcon_folders[0] + '1a3popcon',
  24. popcon_folders[1] + '2a1popcon']
  25. file_pkgs = {popcon_files_path[0]: ['vim', 'git'],
  26. popcon_files_path[1]: ['vagrant', 'ruby'],
  27. popcon_files_path[2]: ['python', 'ruby'],
  28. popcon_files_path[3]: ['git', 'ruby']}
  29. assert_all_pkgs = ['git', 'python', 'ruby', 'vagrant', 'vim']
  30. assert_submissions = np.matrix("0 0 1 1 0; 1 0 0 0 1; 0 1 1 0 0;"\
  31. "1 0 1 0 0", dtype=np.uint8)
  32. if os.path.exists(popcon_entries_path):
  33. shutil.rmtree(popcon_entries_path)
  34. os.mkdir(popcon_entries_path)
  35. for popcon_folder in popcon_folders:
  36. os.mkdir(popcon_folder)
  37. for file_path in popcon_files_path:
  38. self.create_popcon_file(file_path, file_pkgs[file_path])
  39. all_pkgs = cpc.get_all_pkgs()
  40. submissions = cpc.get_popcon_submissions(all_pkgs,
  41. popcon_entries_path, 2)
  42. all_pkgs, submissions = cpc.discard_nonpupular_pkgs(all_pkgs,
  43. submissions)
  44. all_pkgs, submissions = cpc.filter_little_used_packages(all_pkgs,
  45. submissions)
  46. data = cpc.generate_kmeans_data(3, 170, 2, submissions)
  47. clusters, submissions_clusters = data
  48. pkgs_clusters = cpc.create_pkgs_clusters(all_pkgs, submissions,
  49. submissions_clusters,
  50. len(clusters))
  51. shutil.rmtree(popcon_entries_path)
  52. self.assertEqual(assert_all_pkgs, all_pkgs)
  53. self.assertEqual(sorted(assert_submissions.tolist()),
  54. sorted(submissions.todense().tolist()))
  55. # def test_get_all_pkgs_rate(self):
  56. # users_binary = [[1, 0, 1, 0, 1, 0],
  57. # [0, 1, 0, 1, 1, 0],
  58. # [1, 0, 1, 1, 0, 1],
  59. # [0, 1, 1, 1, 1, 1]]
  60. # assert_all_pkgs_rate = [0.5, 0.5, 0.75, 0.75, 0.75, 0.5]
  61. # all_pkgs_rate = cpc.get_all_pkgs_rate(users_binary)
  62. # self.assertEqual(assert_all_pkgs_rate, all_pkgs_rate)
  63. # def test_get_filtered_users_pkgs(self):
  64. # all_pkgs = ['vim', 'vagrant', 'chef', 'python', 'ruby', 'git']
  65. # users_pkgs = [['vim', 'chef', 'ruby'],
  66. # ['vagrant', 'python', 'ruby'],
  67. # ['vagrant', 'python', 'ruby'],
  68. # ['vagrant', 'python', 'ruby'],
  69. # ['vagrant', 'python', 'ruby'],
  70. # ['vagrant', 'python', 'ruby'],
  71. # ['vagrant', 'python', 'ruby'],
  72. # ['vagrant', 'python', 'ruby'],
  73. # ['vagrant', 'python', 'ruby'],
  74. # ['vagrant', 'python', 'ruby'],
  75. # ['vagrant', 'python', 'ruby'],
  76. # ['vagrant', 'python', 'ruby'],
  77. # ['vagrant', 'python', 'ruby'],
  78. # ['vagrant', 'python', 'ruby'],
  79. # ['vagrant', 'python', 'ruby'],
  80. # ['vagrant', 'python', 'ruby'],
  81. # ['vagrant', 'python', 'ruby'],
  82. # ['vagrant', 'python', 'ruby'],
  83. # ['vagrant', 'python', 'ruby'],
  84. # ['vagrant', 'python', 'ruby'],
  85. # ['vagrant', 'python', 'ruby'],
  86. # ['vim', 'chef', 'python', 'ruby'],
  87. # ['vagrant', 'chef', 'python', 'ruby', 'git']]
  88. # users_binary = [[1, 0, 1, 0, 1, 0],
  89. # [0, 1, 0, 1, 1, 0],
  90. # [0, 1, 0, 1, 1, 0],
  91. # [0, 1, 0, 1, 1, 0],
  92. # [0, 1, 0, 1, 1, 0],
  93. # [0, 1, 0, 1, 1, 0],
  94. # [0, 1, 0, 1, 1, 0],
  95. # [0, 1, 0, 1, 1, 0],
  96. # [0, 1, 0, 1, 1, 0],
  97. # [0, 1, 0, 1, 1, 0],
  98. # [0, 1, 0, 1, 1, 0],
  99. # [0, 1, 0, 1, 1, 0],
  100. # [0, 1, 0, 1, 1, 0],
  101. # [0, 1, 0, 1, 1, 0],
  102. # [0, 1, 0, 1, 1, 0],
  103. # [0, 1, 0, 1, 1, 0],
  104. # [0, 1, 0, 1, 1, 0],
  105. # [0, 1, 0, 1, 1, 0],
  106. # [0, 1, 0, 1, 1, 0],
  107. # [0, 1, 0, 1, 1, 0],
  108. # [0, 1, 0, 1, 1, 0],
  109. # [1, 0, 1, 1, 1, 0],
  110. # [0, 1, 1, 1, 1, 1]]
  111. # assert_filtered_users_pkgs = [['vim', 'chef', 'ruby'],
  112. # ['vagrant', 'python', 'ruby'],
  113. # ['vagrant', 'python', 'ruby'],
  114. # ['vagrant', 'python', 'ruby'],
  115. # ['vagrant', 'python', 'ruby'],
  116. # ['vagrant', 'python', 'ruby'],
  117. # ['vagrant', 'python', 'ruby'],
  118. # ['vagrant', 'python', 'ruby'],
  119. # ['vagrant', 'python', 'ruby'],
  120. # ['vagrant', 'python', 'ruby'],
  121. # ['vagrant', 'python', 'ruby'],
  122. # ['vagrant', 'python', 'ruby'],
  123. # ['vagrant', 'python', 'ruby'],
  124. # ['vagrant', 'python', 'ruby'],
  125. # ['vagrant', 'python', 'ruby'],
  126. # ['vagrant', 'python', 'ruby'],
  127. # ['vagrant', 'python', 'ruby'],
  128. # ['vagrant', 'python', 'ruby'],
  129. # ['vagrant', 'python', 'ruby'],
  130. # ['vagrant', 'python', 'ruby'],
  131. # ['vagrant', 'python', 'ruby'],
  132. # ['vim', 'chef', 'python', 'ruby'],
  133. # ['vagrant', 'chef', 'python', 'ruby']]
  134. # filtered_users_pkgs = cpc.get_filtered_users_pkgs(all_pkgs,
  135. # users_pkgs,
  136. # users_binary)
  137. # self.assertEqual(assert_filtered_users_pkgs, filtered_users_pkgs)
  138. # def test_get_all_pkgs(self):
  139. # popcon_entries = [['vim', 'chef', 'ruby'],
  140. # ['vagrant', 'python', 'ruby'],
  141. # ['vim', 'chef', 'python', 'git'],
  142. # ['vagrant', 'chef', 'python', 'ruby', 'git']]
  143. # assert_all_pkgs = ['vim', 'chef', 'ruby', 'vagrant', 'python', 'git']
  144. # all_pkgs = cpc.get_all_pkgs(popcon_entries)
  145. # self.assertEqual(sorted(assert_all_pkgs), sorted(all_pkgs))
  146. # def test_read_popcon_file(self):
  147. # file_path = '1apopcon'
  148. # pkgs = ['vim', 'git', 'ruby', 'python', 'libruby', 'pythondoc']
  149. # self.create_popcon_file(file_path, pkgs)
  150. # assert_popcon_entry = ['git', 'python', 'ruby', 'vim']
  151. # popcon_entry = cpc.read_popcon_file(file_path)
  152. # os.remove(file_path)
  153. # self.assertEqual(assert_popcon_entry, popcon_entry)
  154. # def test_get_popcon_files(self):
  155. # popcon_entries_path = 'popcon_entries_for_tests/'
  156. # popcon_folders = [popcon_entries_path + '1a/',
  157. # popcon_entries_path + '2a/']
  158. # popcon_files_path = [popcon_folders[0] + '1a1popcon',
  159. # popcon_folders[0] + '1a2popcon',
  160. # popcon_folders[0] + '1a3popcon',
  161. # popcon_folders[1] + '2a1popcon']
  162. # if os.path.exists(popcon_entries_path):
  163. # shutil.rmtree(popcon_entries_path)
  164. # os.mkdir(popcon_entries_path)
  165. # for popcon_folder in popcon_folders:
  166. # os.mkdir(popcon_folder)
  167. # for popcon_file_path in popcon_files_path:
  168. # popcon_file = open(popcon_file_path, 'a')
  169. # popcon_file.close()
  170. # popcon_files = cpc.get_popcon_files(popcon_entries_path)
  171. # shutil.rmtree(popcon_entries_path)
  172. # self.assertEqual(sorted(popcon_files_path), sorted(popcon_files))
  173. # def test_get_popcon_entries(self):
  174. # popcon_entries_path = 'popcon_entries_for_tests/'
  175. # popcon_folders = [popcon_entries_path + '1a/',
  176. # popcon_entries_path + '2a/']
  177. # popcon_files_path = [popcon_folders[0] + '1a1popcon',
  178. # popcon_folders[0] + '1a2popcon',
  179. # popcon_folders[0] + '1a3popcon',
  180. # popcon_folders[1] + '2a1popcon']
  181. # file_pkgs = {popcon_files_path[0]: ['vim', 'git'],
  182. # popcon_files_path[1]: ['vagrant', 'ruby'],
  183. # popcon_files_path[2]: ['python', 'ruby'],
  184. # popcon_files_path[3]: ['git', 'ruby']}
  185. # assert_popcon_entries = [['ruby', 'vagrant'],
  186. # ['git', 'vim'],
  187. # ['python', 'ruby'],
  188. # ['git', 'ruby']]
  189. # if os.path.exists(popcon_entries_path):
  190. # shutil.rmtree(popcon_entries_path)
  191. # os.mkdir(popcon_entries_path)
  192. # for popcon_folder in popcon_folders:
  193. # os.mkdir(popcon_folder)
  194. # for file_path in popcon_files_path:
  195. # self.create_popcon_file(file_path, file_pkgs[file_path])
  196. # popcon_entries = cpc.get_popcon_entries(popcon_entries_path)
  197. # shutil.rmtree(popcon_entries_path)
  198. # self.assertEqual(assert_popcon_entries, popcon_entries)