1
- # Create your tests here.
2
- from django .contrib .auth .models import User
3
- from rest_framework import status
4
- from rest_framework .reverse import reverse
5
- from rest_framework .test import APITestCase , APIClient
1
+ from django .test import TestCase
6
2
7
- from musics .models import Music
8
-
9
-
10
- class MusicViewTestCase (APITestCase ):
11
- url_reverse = reverse ('api:music-list' )
12
- url = '/api/music/'
13
- url_detail = '/api/music/{}/'
14
- url_detail_route_reverse = reverse ('api:music-detail' , kwargs = {"pk" : 1 })
15
- url_detail_route = '/api/music/{}/detail/'
16
- url_list_route = '/api/music/all_singer/'
17
-
18
- def setUp (self ):
19
- print ('setUp' )
20
-
21
- self .client = APIClient ()
22
- # create user
23
- User .objects .create_user (username = 'test_user' , password = 'password123' )
24
-
25
- self .client .login (username = 'test_user' , password = 'password123' )
26
-
27
- self .request_data = {
28
- 'song' : 'song_test' ,
29
- 'singer' : 'singer_test'
30
- }
31
-
32
- self .music = Music .objects .create (song = 'song_test' , singer = 'singer_test' )
33
-
34
- def test_api_music_create (self ):
35
- print ('test_api_music_create' )
36
- self .response = self .client .post (
37
- self .url ,
38
- self .request_data ,
39
- format = "json"
40
- )
41
- self .assertEqual (self .response .status_code , status .HTTP_201_CREATED )
42
- self .assertEqual (Music .objects .count (), 2 )
43
- self .assertEqual (Music .objects .get (pk = self .music .id ).song , 'song_test' )
44
- self .assertEqual (Music .objects .get (pk = self .music .id ).singer , 'singer_test' )
45
-
46
- def test_api_music_retrieve (self ):
47
- print ('test_api_music_retrieve' )
48
- music = Music .objects .get (pk = self .music .id )
49
- response = self .client .get (self .url_detail .format (self .music .id ))
50
- self .assertEqual (response .status_code , status .HTTP_200_OK )
51
- self .assertEqual (response .data .get ('song' , None ), music .song )
52
- self .assertEqual (response .data .get ('singer' , None ), music .singer )
53
-
54
- def test_api_music_partial_update (self ):
55
- print ('test_api_music_partial_update' )
56
- update_song = {'song' : 'song_update' }
57
- response = self .client .patch (self .url_detail .format (self .music .id ), update_song , format = 'json' )
58
- self .assertEqual (response .status_code , status .HTTP_200_OK )
59
- self .assertEqual (response .data .get ('song' , None ), update_song .get ('song' , None ))
60
-
61
- def test_api_music_update (self ):
62
- print ('test_api_music_update' )
63
- update_song = {'song' : 'song_update' , 'singer' : 'singer_update' }
64
- response = self .client .put (self .url_detail .format (self .music .id ), update_song , format = 'json' )
65
- self .assertEqual (response .status_code , status .HTTP_200_OK )
66
- self .assertEqual (response .data .get ('song' , None ), update_song .get ('song' ))
67
- self .assertEqual (response .data .get ('singer' , None ), update_song .get ('singer' ))
68
-
69
- def test_api_music_delete (self ):
70
- print ('test_api_music_delete' )
71
- response = self .client .delete (self .url_detail .format (self .music .id ))
72
- self .assertEqual (response .status_code , status .HTTP_204_NO_CONTENT )
73
-
74
- def test_api_music_detail_route (self ):
75
- print ('test_api_music_detail_route' )
76
- music = Music .objects .get (pk = self .music .id )
77
- response = self .client .get (self .url_detail_route .format (self .music .id ))
78
- self .assertEqual (response .status_code , status .HTTP_200_OK )
79
- self .assertEqual (response .data .get ('song' , None ), music .song )
80
- self .assertEqual (response .data .get ('singer' , None ), music .singer )
81
-
82
- def test_api_music_list_route (self ):
83
- print ('test_api_music_list_route' )
84
- music = Music .objects .values_list ('singer' , flat = True ).distinct ()
85
- response = self .client .get (self .url_list_route )
86
- self .assertEqual (response .status_code , status .HTTP_200_OK )
87
- self .assertEqual (next (iter (response .data )), next (iter (music )))
3
+ # Create your tests here.
0 commit comments