1- import os
21import re
32from unittest import mock
43
@@ -39,7 +38,7 @@ def __call__(self, query, *args, **kwargs):
3938 "no_pa_check" : lambda st : "published_at" not in st
4039 },
4140 pd .DataFrame (
42- {"something" : [12 , 14 ]},
41+ {"something" : [12 , 14 ]},
4342 index = pd .Index (name = "id" , data = [5 , 6 ])
4443 )
4544 ],
@@ -48,10 +47,11 @@ def __call__(self, query, *args, **kwargs):
4847 {"session_ids" : [1 , 2 , 3 ]},
4948 pd .DataFrame ({"id" : [5 , 6 ], "something" : [12 , 14 ]}),
5049 {
51- "filters_sessions" : lambda st : re .compile (r".+and es.id in \(1,2,3\).*" , re .DOTALL ).match (st ) is not None
50+ "filters_sessions" : lambda st : re .compile (
51+ r".+and es.id in \(1,2,3\).*" , re .DOTALL ).match (st ) is not None
5252 },
5353 pd .DataFrame (
54- {"something" : [12 , 14 ]},
54+ {"something" : [12 , 14 ]},
5555 index = pd .Index (name = "id" , data = [5 , 6 ])
5656 )
5757 ],
@@ -60,10 +60,11 @@ def __call__(self, query, *args, **kwargs):
6060 {"unit_ids" : [1 , 2 , 3 ]},
6161 pd .DataFrame ({"id" : [5 , 6 ], "something" : [12 , 14 ]}),
6262 {
63- "filters_units" : lambda st : re .compile (r".+and eu.id in \(1,2,3\).*" , re .DOTALL ).match (st ) is not None
63+ "filters_units" : lambda st : re .compile (
64+ r".+and eu.id in \(1,2,3\).*" , re .DOTALL ).match (st ) is not None
6465 },
6566 pd .DataFrame (
66- {"something" : [12 , 14 ]},
67+ {"something" : [12 , 14 ]},
6768 index = pd .Index (name = "id" , data = [5 , 6 ])
6869 )
6970 ],
@@ -72,11 +73,14 @@ def __call__(self, query, *args, **kwargs):
7273 {"channel_ids" : [1 , 2 , 3 ], "probe_ids" : [4 , 5 , 6 ]},
7374 pd .DataFrame ({"id" : [5 , 6 ], "something" : [12 , 14 ]}),
7475 {
75- "filters_channels" : lambda st : re .compile (r".+and ec.id in \(1,2,3\).*" , re .DOTALL ).match (st ) is not None ,
76- "filters_probes" : lambda st : re .compile (r".+and ep.id in \(4,5,6\).*" , re .DOTALL ).match (st ) is not None
76+ "filters_channels" : lambda st : re .compile (
77+ r".+and ec.id in \(1,2,3\).*" , re .DOTALL ).match (
78+ st ) is not None ,
79+ "filters_probes" : lambda st : re .compile (
80+ r".+and ep.id in \(4,5,6\).*" , re .DOTALL ).match (st ) is not None
7781 },
7882 pd .DataFrame (
79- {"something" : [12 , 14 ]},
83+ {"something" : [12 , 14 ]},
8084 index = pd .Index (name = "id" , data = [5 , 6 ])
8185 )
8286 ],
@@ -85,11 +89,15 @@ def __call__(self, query, *args, **kwargs):
8589 {"published_at" : "2019-10-22" },
8690 pd .DataFrame ({"id" : [5 , 6 ], "something" : [12 , 14 ]}),
8791 {
88- "checks_pa_not_null" : lambda st : re .compile (r".+and es.published_at is not null.*" , re .DOTALL ).match (st ) is not None ,
89- "checks_pa" : lambda st : re .compile (r".+and es.published_at <= '2019-10-22'.*" , re .DOTALL ).match (st ) is not None
92+ "checks_pa_not_null" : lambda st : re .compile (
93+ r".+and es.published_at is not null.*" , re .DOTALL ).match (
94+ st ) is not None ,
95+ "checks_pa" : lambda st : re .compile (
96+ r".+and es.published_at <= '2019-10-22'.*" , re .DOTALL ).match (
97+ st ) is not None
9098 },
9199 pd .DataFrame (
92- {"something" : [12 , 14 ]},
100+ {"something" : [12 , 14 ]},
93101 index = pd .Index (name = "id" , data = [5 , 6 ])
94102 )
95103 ],
@@ -98,12 +106,17 @@ def __call__(self, query, *args, **kwargs):
98106 {"published_at" : "2019-10-22" , "session_ids" : [1 , 2 , 3 ]},
99107 pd .DataFrame ({"id" : [5 , 6 ], "something" : [12 , 14 ]}),
100108 {
101- "checks_pa_not_null" : lambda st : re .compile (r".+and es.published_at is not null.*" , re .DOTALL ).match (st ) is not None ,
102- "checks_pa" : lambda st : re .compile (r".+and es.published_at <= '2019-10-22'.*" , re .DOTALL ).match (st ) is not None ,
103- "filters_sessions" : lambda st : re .compile (r".+and es.id in \(1,2,3\).*" , re .DOTALL ).match (st ) is not None
109+ "checks_pa_not_null" : lambda st : re .compile (
110+ r".+and es.published_at is not null.*" , re .DOTALL ).match (
111+ st ) is not None ,
112+ "checks_pa" : lambda st : re .compile (
113+ r".+and es.published_at <= '2019-10-22'.*" , re .DOTALL ).match (
114+ st ) is not None ,
115+ "filters_sessions" : lambda st : re .compile (
116+ r".+and es.id in \(1,2,3\).*" , re .DOTALL ).match (st ) is not None
104117 },
105118 pd .DataFrame (
106- {"something" : [12 , 14 ]},
119+ {"something" : [12 , 14 ]},
107120 index = pd .Index (name = "id" , data = [5 , 6 ])
108121 )
109122 ],
@@ -112,63 +125,81 @@ def __call__(self, query, *args, **kwargs):
112125 {"published_at" : "2019-10-22" , "session_ids" : [1 , 2 , 3 ]},
113126 pd .DataFrame ({"id" : [5 , 6 ], "something" : [12 , 14 ]}),
114127 {
115- "checks_pa_not_null" : lambda st : re .compile (r".+and es.published_at is not null.*" , re .DOTALL ).match (st ) is not None ,
116- "checks_pa" : lambda st : re .compile (r".+and es.published_at <= '2019-10-22'.*" , re .DOTALL ).match (st ) is not None ,
117- "filters_sessions" : lambda st : re .compile (r".+and es.id in \(1,2,3\).*" , re .DOTALL ).match (st ) is not None
128+ "checks_pa_not_null" : lambda st : re .compile (
129+ r".+and es.published_at is not null.*" , re .DOTALL ).match (
130+ st ) is not None ,
131+ "checks_pa" : lambda st : re .compile (
132+ r".+and es.published_at <= '2019-10-22'.*" , re .DOTALL ).match (
133+ st ) is not None ,
134+ "filters_sessions" : lambda st : re .compile (
135+ r".+and es.id in \(1,2,3\).*" , re .DOTALL ).match (st ) is not None
118136 },
119137 pd .DataFrame (
120- {"something" : [12 , 14 ]},
138+ {"something" : [12 , 14 ]},
121139 index = pd .Index (name = "id" , data = [5 , 6 ])
122140 )
123141 ],
124142 [
125143 "get_sessions" ,
126144 {"published_at" : "2019-10-22" , "session_ids" : [1 , 2 , 3 ]},
127- pd .DataFrame ({"id" : [5 , 6 ], "something" : [12 , 14 ], "genotype" : ["foo" , np .nan ]}),
145+ pd .DataFrame ({"id" : [5 , 6 ], "something" : [12 , 14 ],
146+ "genotype" : ["foo" , np .nan ]}),
128147 {
129- "checks_pa_not_null" : lambda st : re .compile (r".+and es.published_at is not null.*" , re .DOTALL ).match (st ) is not None ,
130- "checks_pa" : lambda st : re .compile (r".+and es.published_at <= '2019-10-22'.*" , re .DOTALL ).match (st ) is not None ,
131- "filters_sessions" : lambda st : re .compile (r".+and es.id in \(1,2,3\).*" , re .DOTALL ).match (st ) is not None
148+ "checks_pa_not_null" : lambda st : re .compile (
149+ r".+and es.published_at is not null.*" , re .DOTALL ).match (
150+ st ) is not None ,
151+ "checks_pa" : lambda st : re .compile (
152+ r".+and es.published_at <= '2019-10-22'.*" , re .DOTALL ).match (
153+ st ) is not None ,
154+ "filters_sessions" : lambda st : re .compile (
155+ r".+and es.id in \(1,2,3\).*" , re .DOTALL ).match (st ) is not None
132156 },
133157 pd .DataFrame (
134- {"something" : [12 , 14 ], "genotype" : ["foo" , "wt" ]},
158+ {"something" : [12 , 14 ], "genotype" : ["foo" , "wt" ]},
135159 index = pd .Index (name = "id" , data = [5 , 6 ])
136160 )
137161 ],
138162 [
139163 "get_unit_analysis_metrics" ,
140164 {"ecephys_session_ids" : [1 , 2 , 3 ]},
141- pd .DataFrame ({"id" : [5 , 6 ], "data" : [{"a" : 1 , "b" : 2 }, {"a" : 3 , "b" : 4 }], "ecephys_unit_id" : [10 , 11 ]}),
165+ pd .DataFrame (
166+ {"id" : [5 , 6 ], "data" : [{"a" : 1 , "b" : 2 }, {"a" : 3 , "b" : 4 }],
167+ "ecephys_unit_id" : [10 , 11 ]}),
142168 {
143- "filters_sessions" : lambda st : re .compile (r".+and es.id in \(1,2,3\).*" , re .DOTALL ).match (st ) is not None
169+ "filters_sessions" : lambda st : re .compile (
170+ r".+and es.id in \(1,2,3\).*" , re .DOTALL ).match (st ) is not None
144171 },
145172 pd .DataFrame (
146- {"id" : [5 , 6 ], "a" : [1 , 3 ], "b" : [2 , 4 ]},
147- index = pd .Index (name = "iecephys_unit_id " , data = [10 , 11 ])
173+ {"id" : [5 , 6 ], "a" : [1 , 3 ], "b" : [2 , 4 ]},
174+ index = pd .Index (name = "ecephys_unit_id " , data = [10 , 11 ])
148175 )
149176 ]
150177])
151178def test_pg_query (method_name , kwargs , response , checks , expected ):
152-
153179 selector = MockSelector (checks , response )
154180
155- with mock .patch ("allensdk.internal.api.psycopg2_select" , new = selector ) as ptc :
156- api = epla .EcephysProjectLimsApi .default (lims_credentials = mock_lims_credentials )
181+ with mock .patch ("allensdk.internal.api.psycopg2_select" ,
182+ new = selector ) as ptc :
183+ api = epla .EcephysProjectLimsApi .default (
184+ lims_credentials = mock_lims_credentials )
157185 obtained = getattr (api , method_name )(** kwargs )
158- pd .testing .assert_frame_equal (expected , obtained , check_like = True , check_dtype = False )
186+ pd .testing .assert_frame_equal (expected , obtained , check_like = True ,
187+ check_dtype = False )
159188
160189 any_checks_failed = False
161190 for name , result in ptc .passed .items ():
162191 if not result :
163192 print (f"check { name } failed" )
164193 any_checks_failed = True
165-
194+
166195 if any_checks_failed :
167196 print (ptc .query )
168197 assert not any_checks_failed
169198
170199
171200WKF_ID = 12345
201+
202+
172203class MockPgEngine :
173204
174205 def __init__ (self , query_pattern ):
@@ -220,8 +251,8 @@ def stream(self, url):
220251 ]
221252])
222253def test_file_getter (method , kwargs , query_pattern , pg_engine_cls ):
223-
224254 api = epla .EcephysProjectLimsApi (
225- postgres_engine = pg_engine_cls (query_pattern ), app_engine = MockHttpEngine ()
255+ postgres_engine = pg_engine_cls (query_pattern ),
256+ app_engine = MockHttpEngine ()
226257 )
227- getattr (api , method )(** kwargs )
258+ getattr (api , method )(** kwargs )
0 commit comments