PageRenderTime 59ms CodeModel.GetById 26ms RepoModel.GetById 2ms app.codeStats 0ms

/tests/modeltests/m2m_recursive/tests.py

https://code.google.com/p/mango-py/
Python | 253 lines | 195 code | 11 blank | 47 comment | 1 complexity | 078fbf7477c50cdddf0c5dc2230d7599 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. from operator import attrgetter
  2. from django.test import TestCase
  3. from models import Person
  4. class RecursiveM2MTests(TestCase):
  5. def test_recursive_m2m(self):
  6. a, b, c, d = [
  7. Person.objects.create(name=name)
  8. for name in ["Anne", "Bill", "Chuck", "David"]
  9. ]
  10. # Add some friends in the direction of field definition
  11. # Anne is friends with Bill and Chuck
  12. a.friends.add(b, c)
  13. # David is friends with Anne and Chuck - add in reverse direction
  14. d.friends.add(a,c)
  15. # Who is friends with Anne?
  16. self.assertQuerysetEqual(
  17. a.friends.all(), [
  18. "Bill",
  19. "Chuck",
  20. "David"
  21. ],
  22. attrgetter("name")
  23. )
  24. # Who is friends with Bill?
  25. self.assertQuerysetEqual(
  26. b.friends.all(), [
  27. "Anne",
  28. ],
  29. attrgetter("name")
  30. )
  31. # Who is friends with Chuck?
  32. self.assertQuerysetEqual(
  33. c.friends.all(), [
  34. "Anne",
  35. "David"
  36. ],
  37. attrgetter("name")
  38. )
  39. # Who is friends with David?
  40. self.assertQuerysetEqual(
  41. d.friends.all(), [
  42. "Anne",
  43. "Chuck",
  44. ],
  45. attrgetter("name")
  46. )
  47. # Bill is already friends with Anne - add Anne again, but in the
  48. # reverse direction
  49. b.friends.add(a)
  50. # Who is friends with Anne?
  51. self.assertQuerysetEqual(
  52. a.friends.all(), [
  53. "Bill",
  54. "Chuck",
  55. "David",
  56. ],
  57. attrgetter("name")
  58. )
  59. # Who is friends with Bill?
  60. self.assertQuerysetEqual(
  61. b.friends.all(), [
  62. "Anne",
  63. ],
  64. attrgetter("name")
  65. )
  66. # Remove Anne from Bill's friends
  67. b.friends.remove(a)
  68. # Who is friends with Anne?
  69. self.assertQuerysetEqual(
  70. a.friends.all(), [
  71. "Chuck",
  72. "David",
  73. ],
  74. attrgetter("name")
  75. )
  76. # Who is friends with Bill?
  77. self.assertQuerysetEqual(
  78. b.friends.all(), []
  79. )
  80. # Clear Anne's group of friends
  81. a.friends.clear()
  82. # Who is friends with Anne?
  83. self.assertQuerysetEqual(
  84. a.friends.all(), []
  85. )
  86. # Reverse relationships should also be gone
  87. # Who is friends with Chuck?
  88. self.assertQuerysetEqual(
  89. c.friends.all(), [
  90. "David",
  91. ],
  92. attrgetter("name")
  93. )
  94. # Who is friends with David?
  95. self.assertQuerysetEqual(
  96. d.friends.all(), [
  97. "Chuck",
  98. ],
  99. attrgetter("name")
  100. )
  101. # Add some idols in the direction of field definition
  102. # Anne idolizes Bill and Chuck
  103. a.idols.add(b, c)
  104. # Bill idolizes Anne right back
  105. b.idols.add(a)
  106. # David is idolized by Anne and Chuck - add in reverse direction
  107. d.stalkers.add(a, c)
  108. # Who are Anne's idols?
  109. self.assertQuerysetEqual(
  110. a.idols.all(), [
  111. "Bill",
  112. "Chuck",
  113. "David",
  114. ],
  115. attrgetter("name")
  116. )
  117. # Who is stalking Anne?
  118. self.assertQuerysetEqual(
  119. a.stalkers.all(), [
  120. "Bill",
  121. ],
  122. attrgetter("name")
  123. )
  124. # Who are Bill's idols?
  125. self.assertQuerysetEqual(
  126. b.idols.all(), [
  127. "Anne",
  128. ],
  129. attrgetter("name")
  130. )
  131. # Who is stalking Bill?
  132. self.assertQuerysetEqual(
  133. b.stalkers.all(), [
  134. "Anne",
  135. ],
  136. attrgetter("name")
  137. )
  138. # Who are Chuck's idols?
  139. self.assertQuerysetEqual(
  140. c.idols.all(), [
  141. "David",
  142. ],
  143. attrgetter("name"),
  144. )
  145. # Who is stalking Chuck?
  146. self.assertQuerysetEqual(
  147. c.stalkers.all(), [
  148. "Anne",
  149. ],
  150. attrgetter("name")
  151. )
  152. # Who are David's idols?
  153. self.assertQuerysetEqual(
  154. d.idols.all(), []
  155. )
  156. # Who is stalking David
  157. self.assertQuerysetEqual(
  158. d.stalkers.all(), [
  159. "Anne",
  160. "Chuck",
  161. ],
  162. attrgetter("name")
  163. )
  164. # Bill is already being stalked by Anne - add Anne again, but in the
  165. # reverse direction
  166. b.stalkers.add(a)
  167. # Who are Anne's idols?
  168. self.assertQuerysetEqual(
  169. a.idols.all(), [
  170. "Bill",
  171. "Chuck",
  172. "David",
  173. ],
  174. attrgetter("name")
  175. )
  176. # Who is stalking Anne?
  177. self.assertQuerysetEqual(
  178. a.stalkers.all(), [
  179. "Bill",
  180. ],
  181. attrgetter("name")
  182. )
  183. # Who are Bill's idols
  184. self.assertQuerysetEqual(
  185. b.idols.all(), [
  186. "Anne",
  187. ],
  188. attrgetter("name")
  189. )
  190. # Who is stalking Bill?
  191. self.assertQuerysetEqual(
  192. b.stalkers.all(), [
  193. "Anne",
  194. ],
  195. attrgetter("name"),
  196. )
  197. # Remove Anne from Bill's list of stalkers
  198. b.stalkers.remove(a)
  199. # Who are Anne's idols?
  200. self.assertQuerysetEqual(
  201. a.idols.all(), [
  202. "Chuck",
  203. "David",
  204. ],
  205. attrgetter("name")
  206. )
  207. # Who is stalking Anne?
  208. self.assertQuerysetEqual(
  209. a.stalkers.all(), [
  210. "Bill",
  211. ],
  212. attrgetter("name")
  213. )
  214. # Who are Bill's idols?
  215. self.assertQuerysetEqual(
  216. b.idols.all(), [
  217. "Anne",
  218. ],
  219. attrgetter("name")
  220. )
  221. # Who is stalking Bill?
  222. self.assertQuerysetEqual(
  223. b.stalkers.all(), []
  224. )
  225. # Clear Anne's group of idols
  226. a.idols.clear()
  227. # Who are Anne's idols
  228. self.assertQuerysetEqual(
  229. a.idols.all(), []
  230. )
  231. # Reverse relationships should also be gone
  232. # Who is stalking Chuck?
  233. self.assertQuerysetEqual(
  234. c.stalkers.all(), []
  235. )
  236. # Who is friends with David?
  237. self.assertQuerysetEqual(
  238. d.stalkers.all(), [
  239. "Chuck",
  240. ],
  241. attrgetter("name")
  242. )