88from  pydatalab .models  import  Sample , StartingMaterial 
99
1010
11- def  test_single_starting_material (admin_client ):
11+ def  test_single_starting_material (admin_client ,  client ):
1212    item_id  =  "material" 
1313
1414    material  =  StartingMaterial (item_id = item_id )
1515
16-     creation  =  admin_client .post (
16+     creation  =  client .post (
1717        "/new-sample/" ,
1818        json = {"new_sample_data" : json .loads (material .json ())},
1919    )
2020
2121    assert  creation .status_code  ==  201 
2222
2323    # A single material without connections should be ignored 
24-     graph  =  admin_client .get ("/item-graph" ).json 
24+     graph  =  client .get ("/item-graph" ).json 
2525    assert  len (graph ["nodes" ]) ==  0 
2626
2727    # Unless it is asked for directly 
28-     graph  =  admin_client .get (f"/item-graph/{ item_id }  ).json 
28+     graph  =  client .get (f"/item-graph/{ item_id }  ).json 
2929    assert  len (graph ["nodes" ]) ==  1 
3030
3131    # Now make a sample and connect it to the starting material; check that the 
@@ -37,13 +37,90 @@ def test_single_starting_material(admin_client):
3737        ],
3838    )
3939
40-     creation  =  admin_client .post (
40+     creation  =  client .post (
4141        "/new-sample/" ,
4242        json = {"new_sample_data" : json .loads (parent .json ())},
4343    )
4444
4545    assert  creation .status_code  ==  201 
4646
47-     graph  =  admin_client .get ("/item-graph" ).json 
47+     graph  =  client .get ("/item-graph" ).json 
48+     assert  len (graph ["nodes" ]) ==  2 
49+     assert  len (graph ["edges" ]) ==  1 
50+ 
51+     # From both the starting material and the sample 
52+     graph  =  client .get (f"/item-graph/{ item_id }  ).json 
53+     assert  len (graph ["nodes" ]) ==  2 
54+     assert  len (graph ["edges" ]) ==  1 
55+ 
56+     # From both the starting material and the sample 
57+     graph  =  client .get ("/item-graph/parent" ).json 
58+     assert  len (graph ["nodes" ]) ==  2 
59+     assert  len (graph ["edges" ]) ==  1 
60+ 
61+     # Now add a few more samples in a chain and check that only the relevant ones are shown 
62+     child  =  Sample (
63+         item_id = "child" ,
64+         synthesis_constituents = [
65+             {"item" : {"item_id" : "parent" , "type" : "samples" }, "quantity" : None }
66+         ],
67+     )
68+ 
69+     creation  =  client .post (
70+         "/new-sample/" ,
71+         json = {"new_sample_data" : json .loads (child .json ())},
72+     )
73+ 
74+     grandchild  =  Sample (
75+         item_id = "grandchild" ,
76+         synthesis_constituents = [
77+             {"item" : {"item_id" : "child" , "type" : "samples" }, "quantity" : None }
78+         ],
79+     )
80+ 
81+     creation  =  client .post (
82+         "/new-sample/" ,
83+         json = {"new_sample_data" : json .loads (grandchild .json ())},
84+     )
85+ 
86+     great_grandchild  =  Sample (
87+         item_id = "great-grandchild" ,
88+         synthesis_constituents = [
89+             {"item" : {"item_id" : "grandchild" , "type" : "samples" }, "quantity" : None }
90+         ],
91+     )
92+ 
93+     creation  =  client .post (
94+         "/new-sample/" ,
95+         json = {"new_sample_data" : json .loads (great_grandchild .json ())},
96+     )
97+ 
98+     graph  =  client .get ("/item-graph" ).json 
99+     assert  len (graph ["nodes" ]) ==  5 
100+     assert  len (graph ["edges" ]) ==  4 
101+ 
102+     # Check for bug where this behaviour was inconsistent between admin and non-admin users 
103+     graph  =  admin_client .get ("/item-graph/great-grandchild" ).json 
104+     assert  len (graph ["nodes" ]) ==  2 
105+     assert  len (graph ["edges" ]) ==  1 
106+ 
107+     # Add an admin only item and check that the non-admin user still sees the same graph 
108+     admin_great_great_grandchild  =  Sample (
109+         item_id = "admin-great-great-grandchild" ,
110+         synthesis_constituents = [
111+             {"item" : {"item_id" : "great-grandchild" , "type" : "samples" }, "quantity" : None }
112+         ],
113+     )
114+ 
115+     creation  =  admin_client .post (
116+         "/new-sample/" , json = {"new_sample_data" : json .loads (admin_great_great_grandchild .json ())}
117+     )
118+ 
119+     graph  =  admin_client .get ("/item-graph/great-grandchild" ).json 
120+     assert  len (graph ["nodes" ]) ==  3 
121+     assert  len (graph ["edges" ]) ==  2 
122+ 
123+     # Current broken behaviour: non-admin users see the full graph, but not the things they don't have permission for 
124+     graph  =  client .get ("/item-graph/great-grandchild" ).json 
48125    assert  len (graph ["nodes" ]) ==  2 
49126    assert  len (graph ["edges" ]) ==  1 
0 commit comments