def test_passes_correct_list_to_template(self):
     user_obj = get_user_obj_by_name('Zlatan Ibrahimovic')
     file_obj = File.objects.create(
         name='test_bank_revenue1.csv',
         desc='Data of test banks revenue',
         file=get_test_file(),
         user=user_obj,
     )
     file_obj = File.objects.create(
         name='test_bank_revenue2.csv',
         desc='Data of test banks revenue',
         file=get_test_file(),
         user=user_obj,
     )
     file_obj = File.objects.create(
         name='test_bank_revenue3.csv',
         desc='Data of test banks revenue',
         file=get_test_file(),
         user=user_obj,
     )
     file_qs = File.objects.filter(user__username=user_obj.username)
     c = Client()
     c.force_login(user_obj)
     response = c.get(reverse('files:list'))
     self.assertEqual(list(response.context['file_qs']), list(file_qs))
    def test_form_can_be_validated(self):
        form = FileForm(data={
            'name': 'Demo Name',
            'desc': 'This is a demo description',
        },
                        files={'file': get_test_file()})

        self.assertTrue(form.is_valid())
 def test_uses_list_template(self):
     user_obj = get_user_obj_by_name('Zlatan Ibrahimovic')
     file_obj = File.objects.create(
         name='test_bank_revenue.csv',
         desc='Data of test banks revenue',
         file=get_test_file(),
         user=user_obj,
     )
     c = Client()
     c.force_login(user_obj)
     response = c.get(reverse('files:list'))
     self.assertTemplateUsed(response, 'files/list.html')
 def test_uses_correct_instance(self):
     user_obj = get_user_obj_by_name('Zlatan Ibrahimovic')
     file_obj = File.objects.create(
         name='test_bank_revenue.csv',
         desc='Data of test banks revenue',
         file=get_test_file(),
         user=user_obj,
     )
     c = Client()
     c.force_login(user_obj)
     response = c.get(reverse('files:view', kwargs={'name': file_obj.name}))
     self.assertEqual(response.context['instance'], file_obj)
    def test_form_can_update_file_info(self):
        user_obj = get_user_obj_by_name('Zlatan Ibrahimovic')
        file_obj = File.objects.create(
            name='test_bank_revenue.csv',
            desc='Data of test banks revenue',
            file=get_test_file(),
            user=user_obj,
        )
        form = FileForm(instance=file_obj,
                        data={
                            'name': 'Demo Name',
                            'desc': 'This is a demo description',
                        },
                        files={'file': get_test_file()})
        if form.is_valid():
            instance = form.save(user_obj)

        self.assertEqual(File.objects.count(), 1)
        file_obj = File.objects.first()
        self.assertEqual(file_obj.name, instance.name)
        self.assertEqual(file_obj.desc, instance.desc)
        self.assertEqual(file_obj.file, instance.file)
    def test_form_can_save_data_in_db(self):
        user_obj = get_user_obj_by_name('Zlatan Ibrahimovic')
        form = FileForm(data={
            'name': 'Demo Name',
            'desc': 'This is a demo description',
        },
                        files={'file': get_test_file()})
        if form.is_valid():
            instance = form.save(user_obj)

        file_obj = File.objects.first()
        self.assertEqual(file_obj.name, instance.name)
        self.assertEqual(file_obj.desc, instance.desc)
        self.assertEqual(file_obj.user.username, instance.user.username)
        self.assertEqual(file_obj.file, instance.file)
 def test_redirects_after_POST(self):
     user_obj = get_user_obj_by_name()
     c = Client()
     c.force_login(user_obj, backend=None)
     response = c.post(
         reverse('files:create'),
         data={
             'name': 'DemoFile',
             'desc': 'This is a demo description',
             'file': get_test_file()
         },
     )
     self.assertEqual(File.objects.all().count(), 1)
     new_file = File.objects.first()
     self.assertRedirects(
         response, reverse('files:view', kwargs={'name': new_file.name}))
 def test_saving_a_POST_request(self):
     user_obj = get_user_obj_by_name()
     c = Client()
     c.force_login(user_obj, backend=None)
     response = c.post(
         reverse('files:create'),
         data={
             'name': 'DemoFile',
             'desc': 'This is a demo description',
             'file': get_test_file()
         },
     )
     self.assertEqual(File.objects.all().count(), 1)
     new_file = File.objects.first()
     self.assertEqual(new_file.name, 'DemoFile')
     self.assertEqual(new_file.user.id, user_obj.id)