Skip to content

Commit

Permalink
test: Add PlaylistAdminForm tests
Browse files Browse the repository at this point in the history
  • Loading branch information
drikusroor committed Jan 8, 2024
1 parent c4cb2fe commit e903dc4
Showing 1 changed file with 48 additions and 1 deletion.
49 changes: 48 additions & 1 deletion backend/section/tests.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from typing import Any
from django.test import Client, TestCase
from django.contrib.admin.sites import AdminSite
from django.core.files.uploadedfile import SimpleUploadedFile
from django.urls import reverse
from section.admin import PlaylistAdmin
from section.models import Playlist, Section, Song

from section.forms import PlaylistAdminForm

class PlaylistModelTest(TestCase):

Expand Down Expand Up @@ -144,3 +146,48 @@ def test_export_json(self):
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.assertEqual(response['Content-Type'], 'application/json')

class PlaylistAdminFormTest(TestCase):

def setUp(self):
self.csv_content = b'The Beatles, A day in the life,1.0,1.0,https://example.com/the-beatles/a-day-in-the-life.mp3,0,band,9\nGustav Mahler, Symphony No. 5,2.0,2.0,https://example.com/gustav-mahler/symphony-no-5.mp3,0,composer,5\nDjango Reinhardt, Minor Swing,3.0,3.0,https://example.com/django-reinhardt/minor-swing.mp3,0,artist,3\n'

def test_csv_file_upload(self):
uploaded_file = SimpleUploadedFile('test.csv', self.csv_content, content_type='text/csv')

form_data = {'name': 'Test Playlist', 'process_csv': True}
file_data = {'csv_file': uploaded_file}

form = PlaylistAdminForm(data=form_data, files=file_data)

self.assertTrue(form.is_valid())

playlist = form.save()

self.assertEqual(playlist.csv, self.csv_content.decode('utf-8'))

def test_csv_text_input(self):
form_data = {'name': 'Test Playlist', 'process_csv': True, 'csv': self.csv_content.decode('utf-8')}

form = PlaylistAdminForm(data=form_data)

self.assertTrue(form.is_valid())

playlist = form.save()

self.assertEqual(playlist.csv.strip(), self.csv_content.decode('utf-8').strip())


def test_should_not_process_csv(self):
uploaded_file = SimpleUploadedFile('test.csv', self.csv_content, content_type='text/csv')

form_data = {'name': 'Test Playlist', 'process_csv': False}
file_data = {'csv_file': uploaded_file}

form = PlaylistAdminForm(data=form_data, files=file_data)

self.assertTrue(form.is_valid())

playlist = form.save()

self.assertEqual(playlist.csv, '')

0 comments on commit e903dc4

Please sign in to comment.