def setUp(self): super().setUp() self.cohort = CohortFactory() self.cohort.instructors.add(self.u) TopicFactory(cohort=self.cohort) TopicFactory(cohort=self.cohort) self.topic = self.cohort.get_general_topic()
class CohortTest(TestCase): def setUp(self): self.instructor = InstructorFactory() self.x = CohortFactory(instructors=(self.instructor, )) def test_is_valid_from_factory(self): self.x.full_clean() def test_clone(self): GraphFactory(topic=self.x.get_general_topic()) GraphFactory(topic=self.x.get_general_topic()) self.assertEqual(Graph.objects.count(), 2) self.assertEqual(Topic.objects.count(), 2) self.assertEqual(Cohort.objects.count(), 2) self.assertEqual(self.x.graph_count(), 2) cloned = self.x.clone() self.assertNotEqual(cloned.pk, self.x.pk) self.assertEqual(cloned.graph_count(), 2) self.assertEqual(Graph.objects.count(), 4) self.assertEqual(Topic.objects.count(), 3) self.assertEqual(Cohort.objects.count(), 3) # Test that cloning the sample course works too. sample = CohortFactory(is_sample=True) cloned = sample.clone() self.assertNotEqual(cloned.pk, sample.pk) self.assertFalse(cloned.is_sample) self.assertEqual(cloned.graph_count(), 0) self.assertEqual(Graph.objects.count(), 4) self.assertEqual(Topic.objects.count(), 5) self.assertEqual(Cohort.objects.count(), 5)
def test_clone(self): g = GraphFactory(topic=self.topic) self.assertEqual(Graph.objects.count(), 1) r = self.client.get( reverse('cohort_graph_clone', kwargs={ 'cohort_pk': self.cohort.pk, 'pk': g.pk })) self.assertEqual(r.status_code, 200) self.assertContains(r, 'Cloning <strong>{}</strong>'.format(g.title)) # Clone it to this cohort. r = self.client.post(reverse('cohort_graph_clone', kwargs={ 'cohort_pk': self.cohort.pk, 'pk': g.pk, }), {'course': self.cohort.pk}, follow=True) self.assertEqual(r.status_code, 200) self.assertContains(r, g.title) self.assertContains(r, 'copied to') self.assertContains(r, self.cohort.title) new_graph = Graph.objects.filter(topic=self.topic, title='{} (clone)'.format( g.title)).first() self.assertEqual(new_graph.title, g.title + ' (clone)') self.assertEqual(new_graph.topic, self.topic) self.assertEqual(new_graph.author, self.u) self.assertEqual(Graph.objects.count(), 2) # Clone it to a new cohort. new_cohort = CohortFactory() new_cohort.instructors.add(self.u) new_topic = new_cohort.get_general_topic() r = self.client.post(reverse('cohort_graph_clone', kwargs={ 'cohort_pk': new_cohort.pk, 'pk': g.pk, }), {'course': new_cohort.pk}, follow=True) self.assertEqual(r.status_code, 200) self.assertContains(r, g.title) self.assertContains(r, 'copied to') self.assertContains(r, new_cohort.title) self.assertEqual(Graph.objects.count(), 3) new_graph = Graph.objects.filter(topic=new_topic, title=g.title).first() self.assertEqual(new_graph.title, g.title) self.assertEqual(new_graph.topic, new_topic) self.assertEqual(new_graph.author, self.u)
def test_get_move_unassociated(self): cohort = CohortFactory() topic = TopicFactory(cohort=cohort) graph = GraphFactory(topic=topic, featured=True) self.assertEqual(graph.order, 0) r = self.client.get( reverse('cohort_graph_edit', kwargs={ 'cohort_pk': cohort.pk, 'pk': graph.pk, }) + '?move=down') self.assertEqual(r.status_code, 403) self.assertEqual(graph.order, 0) r = self.client.get(reverse('cohort_graph_edit', kwargs={ 'cohort_pk': cohort.pk, 'pk': graph.pk, }) + '?move=up', follow=True) self.assertEqual(r.status_code, 403) graph.refresh_from_db() self.assertEqual(graph.order, 0)
def test_get_move_unassociated(self): cohort = CohortFactory() topic = TopicFactory(cohort=cohort) self.assertEqual(topic.order, 1) r = self.client.get( reverse('topic_edit', kwargs={ 'cohort_pk': cohort.pk, 'pk': topic.pk, }) + '?move=down') self.assertEqual(r.status_code, 403) self.assertEqual(topic.order, 1) r = self.client.get(reverse('topic_edit', kwargs={ 'cohort_pk': cohort.pk, 'pk': topic.pk, }) + '?move=up', follow=True) self.assertEqual(r.status_code, 403) topic.refresh_from_db() self.assertEqual(topic.order, 1)
def test_get(self): cohort = CohortFactory() my_cohort = CohortFactory() my_cohort.instructors.add(self.u) r = self.client.get(reverse('cohort_list')) self.assertEqual(r.status_code, 200) self.assertContains(r, 'My Courses') # Hide cohorts this instructor isn't a part of. self.assertNotContains(r, cohort.title) self.assertContains(r, 'Create a new course.') self.assertContains(r, my_cohort.title) self.assertContains(r, my_cohort.description)
def setUp(self): super(TopicUpdateViewTest, self).setUp() self.cohort = CohortFactory() self.cohort.instructors.add(self.u) TopicFactory(cohort=self.cohort) self.topic = TopicFactory(cohort=self.cohort) TopicFactory(cohort=self.cohort) TopicFactory(cohort=self.cohort)
def test_get(self): CohortFactory() r = self.client.get(reverse('cohort_list'), follow=True) self.assertEqual(r.status_code, 200) self.assertEqual( r.request.get('PATH_INFO'), '/accounts/login/', 'Accessing course list page as a student redirects to login page.') self.assertContains(r, 'Log In')
def test_get_sample_course_creation(self): cohort = CohortFactory() sample_cohort = CohortFactory(title='Sample Course', is_sample=True) self.assertEqual(self.u.cohort_set.count(), 0) r = self.client.get(reverse('cohort_list')) self.assertEqual(r.status_code, 200) self.assertContains(r, 'My Courses') self.assertEqual(self.u.cohort_set.count(), 1) # Hide cohorts this instructor isn't a part of. self.assertNotContains(r, cohort.title) self.assertContains(r, 'Create a new course.') self.assertContains(r, sample_cohort.title) self.assertContains(r, sample_cohort.description)
def setUp(self): super(FeaturedGraphUpdateViewTest, self).setUp() self.cohort = CohortFactory() self.cohort.instructors.add(self.u) TopicFactory(cohort=self.cohort) self.topic = TopicFactory(cohort=self.cohort) TopicFactory(cohort=self.cohort) TopicFactory(cohort=self.cohort) self.graph = GraphFactory(topic=self.topic, featured=True) GraphFactory(topic=self.topic, featured=True) GraphFactory(topic=self.topic, featured=True) GraphFactory(topic=self.topic, featured=False) GraphFactory(topic=self.topic, featured=False)
def test_edit_cohort(self): CohortFactory() url = reverse('cohort_edit', kwargs={'pk': self.cohort.pk}) response = self.client.get(url) self.assertEqual(response.status_code, 403, 'Students can\'t edit cohorts.') response = self.client.post(url, {'title': 'Lorem Ipsum'}) self.assertEqual(self.u.cohort_set.count(), 0) self.assertFalse('messages' in response.cookies)
def test_get_unassociated(self): cohort = CohortFactory() topic = TopicFactory(cohort=cohort) r = self.client.get( reverse('topic_delete', kwargs={ 'cohort_pk': cohort.pk, 'pk': topic.pk, })) self.assertEqual( r.status_code, 403, "Can't delete a topic of a cohort I'm not an instructor of.")
def test_clone(self): GraphFactory(topic=self.x.get_general_topic()) GraphFactory(topic=self.x.get_general_topic()) self.assertEqual(Graph.objects.count(), 2) self.assertEqual(Topic.objects.count(), 2) self.assertEqual(Cohort.objects.count(), 2) self.assertEqual(self.x.graph_count(), 2) cloned = self.x.clone() self.assertNotEqual(cloned.pk, self.x.pk) self.assertEqual(cloned.graph_count(), 2) self.assertEqual(Graph.objects.count(), 4) self.assertEqual(Topic.objects.count(), 3) self.assertEqual(Cohort.objects.count(), 3) # Test that cloning the sample course works too. sample = CohortFactory(is_sample=True) cloned = sample.clone() self.assertNotEqual(cloned.pk, sample.pk) self.assertFalse(cloned.is_sample) self.assertEqual(cloned.graph_count(), 0) self.assertEqual(Graph.objects.count(), 4) self.assertEqual(Topic.objects.count(), 5) self.assertEqual(Cohort.objects.count(), 5)
def test_post_unassociated(self): cohort = CohortFactory() topic = TopicFactory(cohort=cohort) original_topic_name = topic.name r = self.client.post(reverse('topic_edit', kwargs={ 'cohort_pk': cohort.pk, 'pk': topic.pk, }), {'name': 'New Topic Name'}, follow=True) self.assertEqual( r.status_code, 403, "Can't update a topic of a cohort I'm not an instructor of.") topic.refresh_from_db() self.assertEqual(topic.name, original_topic_name)
def test_edit_wrong_cohort(self): cohort = CohortFactory() title = cohort.title url = reverse('cohort_edit', kwargs={'pk': cohort.pk}) instructor = self.u response = self.client.get(url) self.assertEqual(response.status_code, 403) response = self.client.post(url, {'title': 'Lorem Ipsum'}) self.assertEqual(instructor.cohort_set.count(), 1) self.assertEqual(cohort.title, title) self.assertEqual(Topic.objects.filter(cohort=cohort).count(), 1) self.assertEqual( cohort.instructors.filter(username=self.u.username).count(), 0) self.assertEqual( Topic.objects.filter(cohort=cohort).first().name, 'General')
def setUp(self): super(CohortDetailStudentViewTest, self).setUp() self.cohort = CohortFactory() self.t1 = TopicFactory(name='Topic A', cohort=self.cohort) self.t2 = TopicFactory(name='Topic B', cohort=self.cohort) self.t3 = TopicFactory(name='Empty Topic', cohort=self.cohort) self.t4 = TopicFactory(name='Topic with unpublished graph', cohort=self.cohort) GraphFactory(title='Graph 1', is_published=True, featured=True, topic=self.t1) GraphFactory(title='Demand-Supply', is_published=True, topic=self.t1, featured=True) GraphFactory(title='abc', is_published=True, topic=self.t2) GraphFactory(title='Submittable graph', needs_submit=True, is_published=True, topic=self.t1) GraphFactory(title='Draft graph', is_published=False, topic=self.t2) GraphFactory(title='Another draft', is_published=False, topic=self.t4)
def test_can_delete_topics(self): # Verify that Topic.delete() still works cohort = CohortFactory() t = Topic.objects.create(name='Topic', order=2, cohort=cohort) t.delete()
def setUp(self): self.instructor = InstructorFactory() self.x = CohortFactory(instructors=(self.instructor, ))
def setUp(self): super(CohortPasswordGraphDetailViewTest, self).setUp() cohort = CohortFactory(password='******') topic = TopicFactory(cohort=cohort) self.g = GraphFactory(topic=topic)
def setUp(self): super().setUp() self.cohort = CohortFactory() self.cohort.instructors.add(self.u)
def setUp(self): super(TopicCreateViewTest, self).setUp() self.cohort = CohortFactory() self.cohort.instructors.add(self.u)
def setUp(self): super(TopicListStudentViewTest, self).setUp() self.cohort = CohortFactory()
def test_can_delete_topics_qs(self): # Verify that the QuerySet delete method still works cohort = CohortFactory() Topic.objects.create(name='topic 1', order=2, cohort=cohort) Topic.objects.create(name='Topic 2', order=3, cohort=cohort) Topic.objects.all().delete()
def setUp(self): super(CohortUpdateStudentViewTest, self).setUp() self.cohort = CohortFactory()