<commit_before>from utils.testcase import EndpointTestCase
from rest_framework import status

from music.models import Music

import sure


class TestMusic(EndpointTestCase):
    def test_delete(self):
        m = Music(
            music_id="a",
            name="a",
            thumbnail="https://a.com",
            duration=114,
            url="https://www.a.com",
            source="youtube",
            room=self.r,
        )
        m.save()

        response = self.client.delete('/music/' + str(m.pk))

        response.status_code.should.eql(status.HTTP_204_NO_CONTENT)

        Music.objects.filter(music_id="a", room=self.r).exists().should.be.false

    def test_delete_current_music(self):
        m = Music(
            music_id="a",
            name="a",
            thumbnail="https://a.com",
            duration=114,
            url="https://www.a.com",
            source="youtube",
            room=self.r,
        )
        m.save()
        self.r.current_music = m
        self.r.save()

        response = self.client.delete('/music/' + str(m.pk))

        response.status_code.should.eql(status.HTTP_204_NO_CONTENT)

        Music.objects.filter(music_id="a", room=self.r).exists().should.be.false

    def test_delete_not_exists(self):
        response = self.client.delete('/music/42')

        response.status_code.should.eql(status.HTTP_404_NOT_FOUND)

    def test_patch(self):
        m = Music(
            music_id="a",
            name="a",
            thumbnail="https://a.com",
            duration=114,
            url="https://www.a.com",
            source="youtube",
            room=self.r,
        )
        m.save()

        response = self.client.patch('/music/' + str(m.pk), {'timer_start': 8, "timer_end": 100})

        response.status_code.should.eql(status.HTTP_200_OK)

        m = self.reload(m)

        m.timer_start.should.eql(8)
        m.timer_end.should.eql(100)
        m.duration.should.eql(92)

    def test_post(self):
        music_to_post = {
            "music_id": "a",
            "name": "a",
            "thumbnail": "https://a.com",
            "duration": 114,
            "url": "https://www.a.com",
            "source": "youtube",
        }

        response = self.client.post('/music', music_to_post)

        response.status_code.should.eql(status.HTTP_201_CREATED)

        Music.objects.filter(music_id='a', room=self.r).exists().should.be.true<commit_msg>improve music_endpoint tests<commit_after>from utils.testcase import EndpointTestCase
from rest_framework import status

from music.models import Music

import sure


class TestMusic(EndpointTestCase):
    def test_delete(self):
        m = Music(
            music_id="a",
            name="a",
            thumbnail="https://a.com",
            duration=114,
            url="https://www.a.com",
            source="youtube",
            room=self.r,
        )
        m.save()

        response = self.client.delete('/music/' + str(m.pk))

        response.status_code.should.eql(status.HTTP_204_NO_CONTENT)

        Music.objects.filter(music_id="a", room=self.r).exists().should.be.false

    def test_delete_current_music(self):
        m = Music(
            music_id="a",
            name="a",
            thumbnail="https://a.com",
            duration=114,
            url="https://www.a.com",
            source="youtube",
            room=self.r,
        )
        m.save()
        self.r.current_music = m
        self.r.save()

        response = self.client.delete('/music/' + str(m.pk))

        response.status_code.should.eql(status.HTTP_204_NO_CONTENT)

        Music.objects.filter(music_id="a", room=self.r).exists().should.be.false

    def test_delete_not_exists(self):
        response = self.client.delete('/music/42')

        response.status_code.should.eql(status.HTTP_404_NOT_FOUND)

    def test_patch(self):
        m = Music(
            music_id="a",
            name="a",
            thumbnail="https://a.com",
            duration=114,
            url="https://www.a.com",
            source="youtube",
            room=self.r,
        )
        m.save()

        response = self.client.patch('/music/' + str(m.pk), {'timer_start': 8, "timer_end": 100})

        response.status_code.should.eql(status.HTTP_200_OK)

        m = self.reload(m)

        m.timer_start.should.eql(8)
        m.timer_end.should.eql(100)
        m.duration.should.eql(92)

    def test_patch_unexisting_music(self):
        response = self.client.patch('/music/165423123', {'timer_start': 8, "timer_end": 100})

        response.status_code.should.eql(status.HTTP_404_NOT_FOUND)

    def test_patch_bad_arguments(self):
        m = Music(
            music_id="a",
            name="a",
            thumbnail="https://a.com",
            duration=114,
            url="https://www.a.com",
            source="youtube",
            room=self.r,
        )
        m.save()

        response = self.client.patch('/music/' + str(m.pk), {'duration': 'wtf'})

        response.status_code.should.eql(status.HTTP_400_BAD_REQUEST)

    def test_post(self):
        music_to_post = {
            "music_id": "a",
            "name": "a",
            "thumbnail": "https://a.com",
            "duration": 114,
            "url": "https://www.a.com",
            "source": "youtube",
        }

        response = self.client.post('/music', music_to_post)

        response.status_code.should.eql(status.HTTP_201_CREATED)

        Music.objects.filter(music_id='a', room=self.r).exists().should.be.true

    def test_post_invalid_data(self):
        music_to_post = {
            "music_id": "a",
            "lolilol": "coucou"
        }

        response = self.client.post('/music', music_to_post)

        response.status_code.should.eql(status.HTTP_400_BAD_REQUEST)
{'fraction_non_alphanumeric': 0.14629948364888123, 'fraction_numerical': 0.01772805507745267, 'mean_word_length': 1.927455919395466, 'pattern_counts': {'":': 17, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 18, 'lorem ipsum': 0, 'www.': 9, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7194551', 'n_tokens_mistral': 1957, 'n_tokens_neox': 1812, 'n_words': 251}