| 76 | |
| 77 | def test_assigning_any_iterable_with_valid_models_to_m2m_works(self): |
| 78 | t1 = Tag.objects.create(name='t1') |
| 79 | t2 = Tag.objects.create(name='t2') |
| 80 | |
| 81 | c1 = TagCollection.objects.create(name='c1') |
| 82 | c1.tags = [t1,t2] |
| 83 | |
| 84 | c1 = TagCollection.objects.get(pk=c1.pk) |
| 85 | self.assertQuerysetEqual(c1.tags.all(), ["<Tag: t1>", "<Tag: t2>"]) |
| 86 | |
| 87 | c1.tags = set([t1, t2]) |
| 88 | |
| 89 | c1 = TagCollection.objects.get(pk=c1.pk) |
| 90 | self.assertQuerysetEqual(c1.tags.order_by('name'), ["<Tag: t1>", "<Tag: t2>"]) |
| 91 | |
| 92 | c1.tags = Tag.objects.filter(name__in=('t1', 't2')) |
| 93 | |
| 94 | c1 = TagCollection.objects.get(pk=c1.pk) |
| 95 | self.assertQuerysetEqual(c1.tags.all(), ["<Tag: t1>", "<Tag: t2>"]) |
| 96 | |
| 97 | c1.tags = (t1, t2) |
| 98 | |
| 99 | c1 = TagCollection.objects.get(pk=c1.pk) |
| 100 | self.assertQuerysetEqual(c1.tags.all(), ["<Tag: t1>", "<Tag: t2>"]) |
| 101 | |
| 102 | def test_assigning_any_iterable_with_valid_primary_keys_to_m2m_works(self): |
| 103 | t1 = Tag.objects.create(name='t1') |
| 104 | t2 = Tag.objects.create(name='t2') |
| 105 | |
| 106 | c1 = TagCollection.objects.create(name='c1') |
| 107 | c1.tags = [t1.pk, t2.pk] |
| 108 | |
| 109 | c1 = TagCollection.objects.get(pk=c1.pk) |
| 110 | self.assertQuerysetEqual(c1.tags.all(), ["<Tag: t1>", "<Tag: t2>"]) |
| 111 | |
| 112 | def test_assigning_non_iterable_to_m2m_doesnt_clear_existing_relations(self): |
| 113 | t1 = Tag.objects.create(name='t1') |
| 114 | t2 = Tag.objects.create(name='t2') |
| 115 | |
| 116 | c1 = TagCollection.objects.create(name='c1') |
| 117 | c1.tags = [t1,t2] |
| 118 | |
| 119 | def invalid_assigment(): |
| 120 | c1.tags = 7 |
| 121 | |
| 122 | self.assertRaises(TypeError, invalid_assigment) |
| 123 | |
| 124 | c1 = TagCollection.objects.get(pk=c1.pk) |
| 125 | |
| 126 | self.assertQuerysetEqual(c1.tags.all(), ["<Tag: t1>", "<Tag: t2>"]) |
| 127 | |
| 128 | def test_assigning_invalid_iterable_to_m2m_doesnt_clear_existing_relations(self): |
| 129 | t1 = Tag.objects.create(name='t1') |
| 130 | t2 = Tag.objects.create(name='t2') |
| 131 | |
| 132 | c1 = TagCollection.objects.create(name='c1') |
| 133 | c1.tags = [t1,t2] |
| 134 | |
| 135 | def invalid_assigment(): |
| 136 | c1.tags = ["foo"] |
| 137 | |
| 138 | self.assertRaises(TypeError, invalid_assigment) |
| 139 | |
| 140 | c1 = TagCollection.objects.get(pk=c1.pk) |
| 141 | self.assertQuerysetEqual(c1.tags.all(), ["<Tag: t1>", "<Tag: t2>"]) |
| 142 | |
| 143 | def test_assigning_iterable_with_invalid_pks_to_m2m_doesnt_clear_existing_relations(self): |
| 144 | t1 = Tag.objects.create(name='t1') |
| 145 | t2 = Tag.objects.create(name='t2') |
| 146 | |
| 147 | c1 = TagCollection.objects.create(name='c1') |
| 148 | c1.tags = [t1,t2] |
| 149 | |
| 150 | def invalid_assigment(): |
| 151 | c1.tags = [1234] |
| 152 | |
| 153 | self.assertRaises(TypeError, invalid_assigment) |
| 154 | |
| 155 | c1 = TagCollection.objects.get(pk=c1.pk) |
| 156 | self.assertQuerysetEqual(c1.tags.all(), ["<Tag: t1>", "<Tag: t2>"]) |
| 157 | |
| 158 | def test_assigning_iterable_with_unsaved_models_to_m2m_doesnt_clear_existing_relations(self): |
| 159 | t1 = Tag.objects.create(name='t1') |
| 160 | t2 = Tag.objects.create(name='t2') |
| 161 | t3 = Tag(name='t3') |
| 162 | |
| 163 | c1 = TagCollection.objects.create(name='c1') |
| 164 | c1.tags = [t1,t2] |
| 165 | |
| 166 | def invalid_assigment(): |
| 167 | c1.tags = [t3] |
| 168 | |
| 169 | self.assertRaises(TypeError, invalid_assigment) |
| 170 | |
| 171 | c1 = TagCollection.objects.get(pk=c1.pk) |
| 172 | self.assertQuerysetEqual(c1.tags.all(), ["<Tag: t1>", "<Tag: t2>"]) |
| 173 | |
| 174 | def test_assigning_iterable_with_wrong_models_to_m2m_doesnt_clear_existing_relations(self): |
| 175 | t1 = Tag.objects.create(name='t1') |
| 176 | t2 = Tag.objects.create(name='t2') |
| 177 | line = Line.objects.create(name="not a tag") |
| 178 | |
| 179 | c1 = TagCollection.objects.create(name='c1') |
| 180 | c1.tags = [t1,t2] |
| 181 | |
| 182 | def invalid_assigment(): |
| 183 | c1.tags = [line] |
| 184 | |
| 185 | self.assertRaises(TypeError, invalid_assigment) |
| 186 | |
| 187 | c1 = TagCollection.objects.get(pk=c1.pk) |
| 188 | self.assertQuerysetEqual(c1.tags.all(), ["<Tag: t1>", "<Tag: t2>"]) |