33from rest_framework .test import APIClient , APITestCase
44from rest_framework import status
55
6- from data .models import DataSet , MetaData , OperationTree , Quantity
6+ from data .models import DataSet , MetaData , OperationTree , Quantity , ReferenceQuantity
77
88
99class TestCreateOperationTree (APITestCase ):
@@ -47,7 +47,16 @@ def test_operation_tree_created_variable(self):
4747 "operation" : "variable" ,
4848 "parameters" : {"hash_value" : 0 , "name" : "test" },
4949 },
50- "references" : [],
50+ "references" : [
51+ {
52+ "label" : "test" ,
53+ "value" : {"array_contents" : [0 , 0 , 0 , 0 ], "shape" : (2 , 2 )},
54+ "variance" : {"array_contents" : [0 , 0 , 0 , 0 ], "shape" : (2 , 2 )},
55+ "units" : "none" ,
56+ "hash" : 0 ,
57+ "history" : {},
58+ }
59+ ],
5160 }
5261 request = self .client .post ("/v1/data/set/" , data = self .dataset , format = "json" )
5362 max_id = DataSet .objects .aggregate (Max ("id" ))["id__max" ]
@@ -59,6 +68,7 @@ def test_operation_tree_created_variable(self):
5968 self .get_operation_tree ,
6069 quantity = new_quantity ,
6170 )
71+ self .assertEqual (len (new_quantity .references .all ()), 0 )
6272
6373 # Test creating quantity with unary operation
6474 def test_operation_tree_created_unary (self ):
@@ -72,7 +82,9 @@ def test_operation_tree_created_unary(self):
7282 }
7383 },
7484 },
75- "references" : [],
85+ "references" : [
86+ {"value" : 5 , "variance" : 0 , "units" : "none" , "hash" : 111 , "history" : {}}
87+ ],
7688 }
7789 request = self .client .post ("/v1/data/set/" , data = self .dataset , format = "json" )
7890 max_id = DataSet .objects .aggregate (Max ("id" ))["id__max" ]
@@ -88,6 +100,9 @@ def test_operation_tree_created_unary(self):
88100 self .assertEqual (variable .operation , "variable" )
89101 self .assertEqual (len (reciprocal .parent_operations .all ()), 1 )
90102 self .assertEqual (reciprocal .parameters , {})
103+ self .assertEqual (len (ReferenceQuantity .objects .all ()), 1 )
104+ self .assertEqual (len (new_quantity .references .all ()), 1 )
105+ self .assertEqual (new_quantity .references .get (hash = 111 ).value , 5 )
91106
92107 # Test creating quantity with binary operation
93108 def test_operation_tree_created_binary (self ):
@@ -102,7 +117,9 @@ def test_operation_tree_created_binary(self):
102117 "b" : {"operation" : "constant" , "parameters" : {"value" : 5 }},
103118 },
104119 },
105- "references" : [],
120+ "references" : [
121+ {"value" : 5 , "variance" : 0 , "units" : "none" , "hash" : 111 , "history" : {}}
122+ ],
106123 }
107124 request = self .client .post ("/v1/data/set/" , data = self .dataset , format = "json" )
108125 max_id = DataSet .objects .aggregate (Max ("id" ))["id__max" ]
@@ -119,6 +136,8 @@ def test_operation_tree_created_binary(self):
119136 self .assertEqual (constant .operation , "constant" )
120137 self .assertEqual (constant .parameters , {"value" : 5 })
121138 self .assertEqual (len (add .parent_operations .all ()), 2 )
139+ self .assertEqual (len (new_quantity .references .all ()), 1 )
140+ self .assertEqual (new_quantity .references .get (hash = 111 ).value , 5 )
122141
123142 # Test creating quantity with exponent
124143 def test_operation_tree_created_pow (self ):
@@ -133,7 +152,9 @@ def test_operation_tree_created_pow(self):
133152 "power" : 2 ,
134153 },
135154 },
136- "references" : [],
155+ "references" : [
156+ {"value" : 5 , "variance" : 0 , "units" : "none" , "hash" : 111 , "history" : {}}
157+ ],
137158 }
138159 request = self .client .post ("/v1/data/set/" , data = self .dataset , format = "json" )
139160 max_id = DataSet .objects .aggregate (Max ("id" ))["id__max" ]
@@ -143,6 +164,8 @@ def test_operation_tree_created_pow(self):
143164 self .assertEqual (request .status_code , status .HTTP_201_CREATED )
144165 self .assertEqual (pow .operation , "pow" )
145166 self .assertEqual (pow .parameters , {"power" : 2 })
167+ self .assertEqual (len (new_quantity .references .all ()), 1 )
168+ self .assertEqual (new_quantity .references .get (hash = 111 ).value , 5 )
146169
147170 # Test creating a transposed quantity
148171 def test_operation_tree_created_transpose (self ):
@@ -157,7 +180,9 @@ def test_operation_tree_created_transpose(self):
157180 "axes" : [1 , 0 ],
158181 },
159182 },
160- "references" : [],
183+ "references" : [
184+ {"value" : 5 , "variance" : 0 , "units" : "none" , "hash" : 111 , "history" : {}}
185+ ],
161186 }
162187 request = self .client .post ("/v1/data/set/" , data = self .dataset , format = "json" )
163188 max_id = DataSet .objects .aggregate (Max ("id" ))["id__max" ]
@@ -170,6 +195,8 @@ def test_operation_tree_created_transpose(self):
170195 self .assertEqual (transpose .parameters , {"axes" : [1 , 0 ]})
171196 self .assertEqual (variable .operation , "variable" )
172197 self .assertEqual (variable .parameters , {"hash_value" : 111 , "name" : "x" })
198+ self .assertEqual (len (new_quantity .references .all ()), 1 )
199+ self .assertEqual (new_quantity .references .get (hash = 111 ).value , 5 )
173200
174201 # Test creating a quantity with multiple operations
175202 def test_operation_tree_created_nested (self ):
@@ -192,7 +219,9 @@ def test_operation_tree_created_nested(self):
192219 },
193220 },
194221 },
195- "references" : [],
222+ "references" : [
223+ {"value" : 5 , "variance" : 0 , "units" : "none" , "hash" : 111 , "history" : {}}
224+ ],
196225 }
197226 request = self .client .post ("/v1/data/set/" , data = self .dataset , format = "json" )
198227 max_id = DataSet .objects .aggregate (Max ("id" ))["id__max" ]
@@ -211,6 +240,8 @@ def test_operation_tree_created_nested(self):
211240 self .assertEqual (constant .parameters , {"value" : {"type" : "int" , "value" : 7 }})
212241 self .assertEqual (variable .operation , "variable" )
213242 self .assertEqual (variable .parameters , {"hash_value" : 111 , "name" : "x" })
243+ self .assertEqual (len (new_quantity .references .all ()), 1 )
244+ self .assertEqual (new_quantity .references .get (hash = 111 ).value , 5 )
214245
215246 # Test creating a quantity with tensordot
216247 def test_operation_tree_created_tensor (self ):
@@ -227,7 +258,9 @@ def test_operation_tree_created_tensor(self):
227258 "b_index" : 1 ,
228259 },
229260 },
230- "references" : [],
261+ "references" : [
262+ {"value" : 5 , "variance" : 0 , "units" : "none" , "hash" : 111 , "history" : {}}
263+ ],
231264 }
232265 request = self .client .post ("/v1/data/set/" , data = self .dataset , format = "json" )
233266 max_id = DataSet .objects .aggregate (Max ("id" ))["id__max" ]
@@ -237,6 +270,8 @@ def test_operation_tree_created_tensor(self):
237270 self .assertEqual (request .status_code , status .HTTP_201_CREATED )
238271 self .assertEqual (tensor .operation , "tensor_product" )
239272 self .assertEqual (tensor .parameters , {"a_index" : 1 , "b_index" : 1 })
273+ self .assertEqual (len (new_quantity .references .all ()), 1 )
274+ self .assertEqual (new_quantity .references .get (hash = 111 ).value , 5 )
240275
241276 # Test creating a quantity with no history
242277 def test_operation_tree_created_no_history (self ):
@@ -250,6 +285,7 @@ def test_operation_tree_created_no_history(self):
250285 new_quantity = new_dataset .data_contents .get (hash = 0 )
251286 self .assertEqual (request .status_code , status .HTTP_201_CREATED )
252287 self .assertIsNone (new_quantity .operation_tree )
288+ self .assertEqual (len (new_quantity .references .all ()), 0 )
253289
254290 def tearDown (self ):
255291 DataSet .objects .all ().delete ()
@@ -449,6 +485,8 @@ def test_create_missing_parameter_tensor(self):
449485 self .assertEqual (len (Quantity .objects .all ()), 0 )
450486 self .assertEqual (len (OperationTree .objects .all ()), 0 )
451487
488+ # TODO: Test variables have corresponding reference quantities
489+
452490 @classmethod
453491 def tearDownClass (cls ):
454492 cls .user .delete ()
@@ -481,13 +519,29 @@ def setUpTestData(cls):
481519 cls .constant = OperationTree .objects .create (
482520 id = 2 , operation = "constant" , parameters = {"value" : 1 }
483521 )
484- # cls.dataset.data_contents.add(cls.quantity)
522+ cls .ref_quantity = ReferenceQuantity .objects .create (
523+ id = 1 ,
524+ value = 5 ,
525+ variance = 0 ,
526+ units = "none" ,
527+ hash = 111 ,
528+ derived_quantity = cls .quantity ,
529+ )
485530 cls .client = APIClient ()
486531 cls .client .force_authenticate (cls .user )
487532
488533 # Test accessing a quantity with no operations performed
489534 def test_get_operation_tree_none (self ):
535+ self .ref_quantity .delete ()
490536 request = self .client .get ("/v1/data/set/1/" )
537+ self .ref_quantity = ReferenceQuantity .objects .create (
538+ id = 1 ,
539+ value = 5 ,
540+ variance = 0 ,
541+ units = "none" ,
542+ hash = 111 ,
543+ derived_quantity = self .quantity ,
544+ )
491545 self .assertEqual (request .status_code , status .HTTP_200_OK )
492546 self .assertEqual (
493547 request .data ["data_contents" ][0 ],
@@ -537,7 +591,14 @@ def test_get_operation_tree_unary(self):
537591 }
538592 },
539593 },
540- "references" : [],
594+ "references" : [
595+ {
596+ "value" : 5 ,
597+ "variance" : 0 ,
598+ "units" : "none" ,
599+ "hash" : 111 ,
600+ }
601+ ],
541602 },
542603 },
543604 )
0 commit comments