11import pytest
2- from functools import partial
3- from rest_framework import status , test
2+ from rest_framework import status
43
5- from django .db import models
6- from collab .models import Project , File , FileVersion , Task , Instance , Vector
7- from collab .matchers import matchers_list
8-
9- import random
10- import json
11- import inspect
12- from dateutil .parser import parse as parse_date
13-
14-
15- try :
16- strtypes = (str , unicode )
17- inttypes = (int , long )
18- except NameError :
19- strtypes = str
20- inttypes = int
21-
22-
23- @pytest .fixture
24- def api_client ():
25- return test .APIClient ()
26-
27-
28- @pytest .fixture
29- def admin_api_client (admin_user ):
30- client = test .APIClient ()
31- client .force_authenticate (user = admin_user )
32- return client
33-
34-
35- def rand_hash (n ):
36- return '' .join (random .choice ("01234567890ABCDEF" ) for _ in range (n ))
37-
38-
39- requested_matchers = json .dumps ([m .match_type for m in matchers_list ])
40-
41- collab_models = {'projects' : {'name' : 'test_project_1' , 'private' : False ,
42- 'description' : 'description_1' , 'files' : []},
43- 'files' : {'md5hash' : 'H' * 32 , 'name' : 'file1' ,
44- 'description' : 'desc1' },
45- 'file_versions' : {'md5hash' : 'J' * 32 },
46- 'tasks' : {},
47- 'instances' : {'offset' : 0 , 'type' : 'function' , 'vectors' : [],
48- 'annotations' : []},
49- 'vectors' : {'type' : 'assembly_hash' , 'type_version' : 0 ,
50- 'data' : 'data' }}
51-
52- collab_model_objects = {'projects' : partial (Project , private = False ),
53- 'files' : partial (File , name = 'name' , description = 'desc' ,
54- md5hash = 'H' * 32 ),
55- 'file_versions' : partial (FileVersion ),
56- 'tasks' : partial (Task , matchers = requested_matchers ),
57- 'instances' : partial (Instance , offset = 0 ),
58- 'vectors' : partial (Vector , type = 'assembly_hash' ,
59- data = 'data' , type_version = 0 ),
60- 'rand_hash' : partial (rand_hash , 32 )}
61-
62- collab_model_reqs = {'projects' : {},
63- 'files' : {},
64- 'file_versions' : {'file' : 'files' ,
65- 'md5hash' : 'rand_hash' },
66- 'tasks' : {'target_project' : 'projects' ,
67- 'source_file_version' : 'file_versions' },
68- 'instances' : {'file_version' : 'file_versions' },
69- 'vectors' : {'instance' : 'instances' ,
70- 'file_version' : 'file_versions' }}
71-
72-
73- def resolve_reqs (model_name , user ):
74- model_reqs = collab_model_reqs [model_name ]
75-
76- for req_field , req_model in model_reqs .items ():
77- obj = collab_model_objects [req_model ]()
78-
79- create_model (req_model , user , base_obj = obj )
80-
81- if isinstance (obj , models .Model ):
82- obj .owner = user
83- obj .save ()
84- print ("Created model: {} ({}) at {}" .format (obj , obj .id , req_field ))
85- yield req_field , obj
86-
87-
88- def create_model (model_name , user , base_obj = None ):
89- if base_obj is None :
90- base_obj = collab_model_objects [model_name ]()
91-
92- if isinstance (base_obj , models .Model ):
93- base_obj .owner = user
94-
95- for req_field , obj in resolve_reqs (model_name , user ):
96- base_obj .__setattr__ (req_field , obj )
97-
98- print ("base_obj" , base_obj )
99- return base_obj
100-
101-
102- def setup_model (model_name , user ):
103- model_dict = collab_models [model_name ]
104-
105- for req_field , obj in resolve_reqs (model_name , user ):
106- if isinstance (obj , models .Model ):
107- model_dict [req_field ] = obj .id
108- else :
109- model_dict [req_field ] = obj
110-
111- print ("model_dict" , model_dict )
112- return model_dict
113-
114-
115- def simplify_object (obj ):
116- try :
117- obj = parse_date (obj )
118- except (AttributeError , ValueError , TypeError ):
119- pass
120- try :
121- obj = obj .replace (microsecond = 0 , tzinfo = None ).isoformat ()
122- except (AttributeError , TypeError ):
123- pass
124- return obj
125-
126-
127- def assert_eq (a , b ):
128- a , b = simplify_object (a ), simplify_object (b )
129- print (a , b )
130- if isinstance (a , list ) and isinstance (b , list ):
131- assert len (a ) == len (b )
132- for a_item , b_item in zip (a , b ):
133- assert_eq (a_item , b_item )
134- elif isinstance (a , dict ) and isinstance (b , dict ):
135- for k in b :
136- assert_eq (a [k ], b [k ])
137- elif isinstance (b , dict ) and (isinstance (a , models .Model ) or
138- inspect .isclass (a )):
139- assert_eq (b , a )
140- elif isinstance (a , dict ) and (isinstance (b , models .Model ) or
141- inspect .isclass (b )):
142- for k in a :
143- # TODO: serializer-added values cannot be validated, so we'll have to
144- # ignore any attribute that does not exist in Model object
145- if not hasattr (b , k ):
146- print ("Ignoring missing model parameter: {} in {}" .format (k , b ))
147- continue
148- a_value = a .__getitem__ (k )
149- b_value = getattr (b , k )
150- assert_eq (a_value , b_value )
151- elif isinstance (a , inttypes ) and isinstance (b , models .Model ):
152- assert_eq (a , b .id )
153- elif isinstance (a , strtypes ) and isinstance (b , models .Model ):
154- assert_eq (a , b .username )
155- elif b .__class__ .__name__ == 'RelatedManager' :
156- assert_eq (a , list (b .all ()))
157- else :
158- assert a == b
159-
160-
161- def assert_response (response , status , data = None ):
162- print (response .content )
163- assert response .status_code == status
164- if data is None :
165- pass
166- elif isinstance (data , (list , dict )):
167- if 'results' in response .data :
168- assert_eq (response .data ['results' ], data )
169- else :
170- assert_eq (response .data , data )
171- elif data :
172- assert_eq (response .content , data )
4+ from utils import (rand_hash , create_model , setup_model , assert_eq ,
5+ assert_response , collab_models_keys )
1736
1747
1758@pytest .mark .django_db
176- @pytest .mark .parametrize ('model_name' , collab_models . keys () )
9+ @pytest .mark .parametrize ('model_name' , collab_models_keys )
17710def test_model_guest_list_empty (api_client , model_name ):
17811 response = api_client .get ('/collab/{}/' .format (model_name ),
17912 HTTP_ACCEPT = 'application/json' )
18013 assert_response (response , status .HTTP_401_UNAUTHORIZED )
18114
18215
18316@pytest .mark .django_db
184- @pytest .mark .parametrize ('model_name' , collab_models . keys () )
17+ @pytest .mark .parametrize ('model_name' , collab_models_keys )
18518def test_model_list_empty (admin_api_client , model_name ):
18619 response = admin_api_client .get ('/collab/{}/' .format (model_name ),
18720 HTTP_ACCEPT = 'application/json' )
18821 assert_response (response , status .HTTP_200_OK , [])
18922
19023
19124@pytest .mark .django_db
192- @pytest .mark .parametrize ('model_name' , collab_models . keys () )
25+ @pytest .mark .parametrize ('model_name' , collab_models_keys )
19326def test_model_guest_list (api_client , admin_user , model_name ):
19427 # setup objects
19528 obj = create_model (model_name , admin_user )
@@ -201,7 +34,7 @@ def test_model_guest_list(api_client, admin_user, model_name):
20134
20235
20336@pytest .mark .django_db
204- @pytest .mark .parametrize ('model_name' , collab_models . keys () )
37+ @pytest .mark .parametrize ('model_name' , collab_models_keys )
20538def test_model_list (admin_api_client , admin_user , model_name ):
20639 # setup objects
20740 obj = create_model (model_name , admin_user )
@@ -213,7 +46,7 @@ def test_model_list(admin_api_client, admin_user, model_name):
21346
21447
21548@pytest .mark .django_db
216- @pytest .mark .parametrize ('model_name' , collab_models . keys () )
49+ @pytest .mark .parametrize ('model_name' , collab_models_keys )
21750def test_model_guest_creation (api_client , admin_user , model_name ):
21851 model_data = setup_model (model_name , admin_user )
21952
@@ -224,7 +57,7 @@ def test_model_guest_creation(api_client, admin_user, model_name):
22457
22558
22659@pytest .mark .django_db
227- @pytest .mark .parametrize ('model_name' , collab_models . keys () )
60+ @pytest .mark .parametrize ('model_name' , collab_models_keys )
22861def test_model_creation (admin_api_client , admin_user , model_name ):
22962 model_data = setup_model (model_name , admin_user )
23063
@@ -280,19 +113,3 @@ def test_task(admin_user):
280113
281114 from collab .tasks import match
282115 match (task .id )
283-
284-
285- def test_matchers (admin_client ):
286- response = admin_client .get ('/collab/matches/matchers/' ,
287- content_type = "application/json" )
288- assert_response (response , status .HTTP_200_OK , matchers_list )
289-
290-
291- def test_matchers_abstract (admin_client ):
292- from collab import matchers
293- matchers .matchers_list .append (matchers .Matcher )
294-
295- with pytest .raises (Exception ) as ex :
296- admin_client .get ('/collab/matches/matchers/' ,
297- content_type = "application/json" )
298- assert ex .value .args [0 ] == "Abstract matcher in list"
0 commit comments