File: taptest.py

package info (click to toggle)
gavodachs 2.11%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 8,972 kB
  • sloc: python: 100,078; xml: 3,014; javascript: 2,360; ansic: 918; sh: 216; makefile: 31
file content (1333 lines) | stat: -rw-r--r-- 43,357 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
"""
Simple tests for TAP.

All these tests really stink because TAP isn't really a good match for the
basically stateless unit tests that are executed in an arbitrary order.

There's more TAP/UWS related tests in test_tap.py; these require a
running reactor and are based on trial.
"""

#c Copyright 2008-2024, the GAVO project <gavo@ari.uni-heidelberg.de>
#c
#c This program is free software, covered by the GNU GPL.  See the
#c COPYING file in the source distribution.


import datetime
import io
import functools
import os
import time

from gavo.helpers import testhelpers
from gavo.helpers import trialhelpers

from gavo import base
from gavo import rsc
from gavo import rscdesc  # uws needs getRD
from gavo import svcs
from gavo import utils
from gavo import votable
from gavo.helpers import testtricks
from gavo.protocols import adqlglue
from gavo.protocols import tap
from gavo.protocols import taprunner
from gavo.protocols import uws
from gavo.protocols import uwsactions
from gavo.registry import capabilities
from gavo.web import tap_uploads
from gavo.web import vosi

import adqltest
import tresc

@functools.lru_cache(None)
def _getTAPService():
	return base.caches.getRD("//tap").getById("run")

@functools.lru_cache(None)
def _getTAPParGrammar():
	return _getTAPService().getContextGrammarFor("sync", None)


class TAPFakeRequest(trialhelpers.FakeRequest):
  def __init__(self, *posargs, **kwargs):
  	super().__init__(*posargs, **kwargs)
  	uws.prepareRequest(self, _getTAPService())
  	self.asyncGrammar = _getTAPParGrammar()

def _makeATAPJob(pars):
	"""returns a job id for a TAP job described by pars.

	pars needs to be like request.strargs.
	"""
	return tap.WORKER_SYSTEM.getNewJobId(
		parameters=_getTAPParGrammar().parseStrargs(pars, ensureRequired=False))


class TestVersionParsing(testhelpers.VerboseTest):
	def testSupportedLang(self):
		self.assertEqual(
			taprunner._assertSupportedLanguage(None, "ADQL"), None)

	def testSupportedLangVersion(self):
		self.assertEqual(
			taprunner._assertSupportedLanguage(None, "ADQL-2.0"), None)

	def testAnotherSupportedLangVersion(self):
		self.assertEqual(
			taprunner._assertSupportedLanguage(None, "ADQL-2.1"), None)

	def testUnsupportedLang(self):
		self.assertRaisesWithMsg(
			uws.UWSError,
			"This service does not support the query language ShittyQL",
			taprunner._assertSupportedLanguage,
			(None, "ShittyQL"))

	def testUnsupportedVersion(self):
		self.assertRaisesWithMsg(
			uws.UWSError,
			"This service does not support version 30.12-pre_foo of the query"
			" language ADQL (but some other version; see capabilities).",
			taprunner._assertSupportedLanguage,
			(None, "ADQL-30.12-pre_foo"))


class TestResponseformat(testhelpers.VerboseTest):
	def testRejection(self):
		self.assertRaisesWithMsg(base.ValidationError,
			"Field responseformat: 'xls' is not a valid value for responseformat",
			_getTAPService().run, ("sync", {
					"query": ["ignore"], "lang": ["ADQL"],
					"responseformat": ["xls"]}
				, None))
	
	def testAlias(self):
		args = {"lang": ["ADQL"], "responseformat": ["votable/td"],
				"query": ["SELECT TOP 1 * FROM tap_schema.tables"]}
		f, mediaType = base.resolveCrossId("//tap#run").run("sync",
			_getTAPParGrammar().parseStrargs(args),
			svcs.QueryMeta(args))
		self.assertEqual(mediaType,
			"application/x-votable+xml;serialization=TABLEDATA")
		self.assertEqual(f.read()[-50:],
			b"R></TABLEDATA></DATA></TABLE></RESOURCE></VOTABLE>")
		f.close()

	def testPreservedMediaType(self):
		args = {"lang": ["ADQL"], "responseformat": ["text/xml"],
				"query": ["SELECT TOP 1 * FROM tap_schema.tables"]}
		f, mediaType = base.resolveCrossId("//tap#run").run("sync",
			_getTAPParGrammar().parseStrargs(args),
			svcs.QueryMeta(args))
		self.assertEqual(mediaType, "text/xml")
		# I take it that text/xml ought to be formattable with XSLT, and
		# hence we want tabledata.
		self.assertEqual(f.read()[-50:],
			b"R></TABLEDATA></DATA></TABLE></RESOURCE></VOTABLE>")
		f.close()


class PlainJobCreationTest(testhelpers.VerboseTest):
	"""tests for working job creation and destruction.
	"""
	resources = [("conn", tresc.dbConnection)]

	def _createJob(self):
		# pass in an invalid key so we can see that it's swallowed.
		return _makeATAPJob({"foo": "bar"})

	def _deleteJob(self, jobId):
		return tap.WORKER_SYSTEM.destroy(jobId)

	def _assertJobCreated(self, jobId):
		res = list(self.conn.query(
			"SELECT jobId FROM tap_schema.tapjobs WHERE"
				" jobId=%(jobId)s", locals()))
		self.assertEqual(len(res), 1)
		job = tap.WORKER_SYSTEM.getJob(jobId)
		self.assertFalse("foo" in job.parameters)
		self.assertTrue(os.path.exists(job.getWD()))

	def _assertJobDeleted(self, jobId):
		res = list(self.conn.query(
			"SELECT destructiontime FROM tap_schema.tapjobs"
			" WHERE jobId=%(jobId)s", locals()))
		self.assertEqual(len(res), 0)
		self.assertRaises(base.NotFoundError, tap.WORKER_SYSTEM.getJob, jobId)
		self.assertFalse(
			os.path.exists(
				os.path.join(base.getConfig("uwsWD"), jobId)))

	def _assertUnqueueable(self, jobId):
		# we're missing all the relevant parameters, and hence the
		# this needs to blow up when we try to queue the job
		tap.WORKER_SYSTEM.changeToPhase(jobId, uws.QUEUED)
		job = tap.WORKER_SYSTEM.getJob(jobId)
		self.assertEqual(job.phase, uws.ERROR)
		self.assertEqual(job.error["msg"],
			"Field lang: Required parameter 'lang' missing.")

	def testBigAndUgly(self):
		# this may leave its job behind if the assertions fail.  They'll
		# eventually be garbage-collected by DaCHS, so I don't worry too much.
		jobId = self._createJob()
		self._assertJobCreated(jobId)
		self._assertUnqueueable(jobId)
		self._deleteJob(jobId)
		self._assertJobDeleted(jobId)


class _UWSJobResource(testhelpers.TestResource):
# just a UWS job.  Don't manipulate it.  Too badly.
	def make(self, ignored):
		return tap.WORKER_SYSTEM.getNewJobId()
	
	def clean(self, jobId):
		tap.WORKER_SYSTEM.destroy(jobId.original)

_uwsJobResource = _UWSJobResource()


class TAPParametersTest(testhelpers.VerboseTest):
	resources = [("jobIdResource", _uwsJobResource)]

	def setUp(self):
		testhelpers.VerboseTest.setUp(self)
		self.jobId = self.jobIdResource.original

	def testRepeatedSetting(self):
		with tap.WORKER_SYSTEM.changeableJob(self.jobId) as job:
			job.setParamsFromDict({"a": ["ob"], "b": ["knapp"]})
			self.assertEqual(job.parameters["a"], ["ob"])
			job.setParamsFromDict({"a": ["ja"], "b": []})
			self.assertEqual(job.parameters["a"], ["ob", "ja"])
			self.assertEqual(job.parameters["b"], ["knapp"])


class UWSResponsesValidTest(testhelpers.VerboseTest, testtricks.XSDTestMixin):
	resources = [("jobIdResource", _uwsJobResource)]

	def setUp(self):
		testhelpers.VerboseTest.setUp(self)
		self.jobId = self.jobIdResource.original

	def testJobRes(self):
		job = tap.WORKER_SYSTEM.getJob(self.jobId)
		self.assertValidates(uwsactions.RootAction().doGET(
			job, TAPFakeRequest()),
			leaveOffending=True)

	def testJobList(self):
		self.assertValidates(uwsactions.getJobList(tap.WORKER_SYSTEM),
			leaveOffending=True)


class BlockingTest(testhelpers.VerboseTest):
	"""tests for working impicit uws locking.
	"""
	resources = [("jobIdResource", _uwsJobResource)]

	def setUp(self):
		testhelpers.VerboseTest.setUp(self)
		self.jobId = self.jobIdResource.original

	def testIndexDoesNotBlock(self):
		with tap.WORKER_SYSTEM.changeableJob(self.jobId):
			self.assertTrue(b"uws:jobs" in uwsactions.getJobList(tap.WORKER_SYSTEM))

	def testGetPhaseDoesNotBlock(self):
		req = TAPFakeRequest()
		with tap.WORKER_SYSTEM.changeableJob(self.jobId):
			self.assertEqual(
				uwsactions.doJobAction(tap.WORKER_SYSTEM, req, (self.jobId, "phase")),
				"PENDING")

	def testPostPhaseDoesBlock(self):
		req = TAPFakeRequest(args={"PHASE": ["RUN"]})
		req.method = b"POST"
		uwsactions.PhaseAction.timeout = 0.05
		with tap.WORKER_SYSTEM.changeableJob(self.jobId):
			self.assertRaisesWithMsg(base.ReportableError,
				"Could not access the jobs table. This probably means"
				" there is a stale lock on it.  Please notify the service operators.",
				uwsactions.doJobAction,
				(tap.WORKER_SYSTEM, req, (self.jobId, "phase")))


class QueueTest(testhelpers.VerboseTest):
	def testQuote(self):
		now = datetime.datetime.utcnow()
		tick = datetime.timedelta(minutes=15)
		jobs = []
		try:
			jobId = tap.WORKER_SYSTEM.getNewJobId()
			with tap.WORKER_SYSTEM.changeableJob(jobId) as wjob:
				wjob.change(phase=uws.QUEUED, destructionTime=now+10*tick)
			jobs.append(jobId)

			testJob = tap.WORKER_SYSTEM.getJob(jobId)
			# don't fail just because jobs are left in the queue
			baseDelay = testJob.quote-now

			jobId = tap.WORKER_SYSTEM.getNewJobId()
			with tap.WORKER_SYSTEM.changeableJob(jobId) as wjob:
				wjob.change(phase=uws.QUEUED, destructionTime=now+9*tick)
			jobs.append(jobId)
			
			# our quote must now be roughly 10 minutes (as configured in
			# tap.EST_TIME_PER_JOB) later
			self.assertEqual((testJob.quote-now-baseDelay).seconds/10,
				tap.EST_TIME_PER_JOB.seconds/10)

			jobId = tap.WORKER_SYSTEM.getNewJobId()
			with tap.WORKER_SYSTEM.changeableJob(jobId) as wjob:
				wjob.change(phase=uws.QUEUED, destructionTime=now+11*tick)
			jobs.append(jobId)

			# the new job will run later then our test job, so no change
			# expected
			self.assertEqual((testJob.quote-now-baseDelay).seconds/10,
				tap.EST_TIME_PER_JOB.seconds/10)

		finally:
			for jobId in jobs:
				tap.WORKER_SYSTEM.destroy(jobId)


class TAPTransitionsTest(testhelpers.VerboseTest):
	def testAbortPending(self):
		jobId = None
		try:
			jobId = tap.WORKER_SYSTEM.getNewJobId(
				parameters={"query": "bogus", "request": "doQuery",
				"LANG": "ADQL-2.0"})
			tap.WORKER_SYSTEM.changeToPhase(jobId, uws.ABORTED)
			self.assertEqual(tap.WORKER_SYSTEM.getJob(jobId).phase,
				uws.ABORTED)
		finally:
			tap.WORKER_SYSTEM.destroy(jobId)


class UploadSyntaxOKTest(testhelpers.VerboseTest, metaclass=testhelpers.SamplesBasedAutoTest):
	def _runTest(self, sample):
		s, e = sample
		self.assertEqual(tap.parseUploadString(s), e)
	
	samples = [
		('a,b', [('a', 'b'),]),
		('a5_ug,http://knatter?RA=99&DEC=1.54',
			[('a5_ug', 'http://knatter?RA=99&DEC=1.54'),]),
		('a5_ug,http://knatter?RA=99&DEC=1.54;a,b',
			[('a5_ug', 'http://knatter?RA=99&DEC=1.54'), ('a', 'b')]),]


class UploadSyntaxNotOKTest(testhelpers.VerboseTest, metaclass=testhelpers.SamplesBasedAutoTest):
	def _runTest(self, sample):
		self.assertRaises(base.ValidationError, tap.parseUploadString,
			sample)
	
	samples = [
		'a,',
		',http://wahn',
		'a,b;',
		'a,b;whacky/name,b',]


class _TAPCapDocument(testhelpers.TestResource):
	def make(self, ignored):
		pub = base.caches.getRD("//tap").getById("run").publications[0]
		doc = vosi.CAP.capabilities[capabilities.getCapabilityElement(pub)
			].render()
		return doc, testhelpers.getXMLTree(doc, debug=False)


class CapabilitiesTest(testhelpers.VerboseTest, testtricks.XSDTestMixin):
	resources = [("dt", _TAPCapDocument())]

	def testValid(self):
		self.assertValidates(self.dt[0], leaveOffending=True)
	
	def testFunctionsSorted(self):
		funcs = [n.text for n in
			self.dt[1].xpath("//languageFeatures[@type='ivo://ivoa.net/std/"
				"TAPRegExt#features-udf']/feature/form")]
		self.assertEqual(list(sorted(funcs)), funcs)

	def testUploadMethods(self):
		ums = set(n.get("ivo-id") for n in self.dt[1].xpath("//uploadMethod"))
		self.assertEqual(set(ums),
			set("ivo://ivoa.net/std/TAPRegExt#upload-"+s
				for s in ("inline", "http", "https", "ftp")))

	def testType(self):
		self.assertEqual(self.dt[1].xpath("capability[1]")[0].get(
				"{http://www.w3.org/2001/XMLSchema-instance}type"),
			"tr:TableAccess")

	def testInterface(self):
		intfs = self.dt[1].xpath("capability[1]/interface")
		self.assertEqual(len(intfs), 2)
		intf = intfs[0]
		self.assertEqual(
			intf.get("{http://www.w3.org/2001/XMLSchema-instance}type"),
			"vs:ParamHTTP")

	def testAccessURL(self):
		self.assertEqual([n.text for n in
			self.dt[1].xpath("capability/interface/accessURL")],
			["http://localhost:8080/tap", "http://localhost:8080/tap"])


class TAPSchemaQueryTest(testhelpers.VerboseTest):
	def setUp(self):
		self.jobId = _makeATAPJob({
				"query": ["SELECT TOP 1 * FROM TAP_SCHEMA.tables"],
				"request": ["doQuery"],
				"lang": ["ADQL-2.1"]})
	
	def tearDown(self):
		tap.WORKER_SYSTEM.destroy(self.jobId)

	def testTAP_tables(self):
		taprunner.runSyncTAPJob(self.jobId)
		job = tap.WORKER_SYSTEM.getJob(self.jobId)
		self.assertEqual(job.phase, uws.COMPLETED)
		with open(job.getResult("result")[0]) as f:
			res = f.read()
		self.assertTrue('<RESOURCE type="results">' in res)


class _TAPSchemaContent(testhelpers.TestResource):
	def make(self, deps):
		rd = base.parseFromString(rscdesc.RD, """
			<resource schema="schm" resdir="data">
			<meta name="schema-rank">4000</meta>
			<meta name="utype">ivo://ivoa.net/std/cool-model</meta>
			<meta name="description">We actually don't test for empty
				schema descriptions yet</meta>

			<table id="nontap" onDisk="True">
				<column name="col1" description="A column that should not appear
					in tap_schema because its table isn't ADQL-visible"/>
			</table>

			<table id="boring" adql="True">
				<column name="col1" type="spoly"/>
				<column name="quoted/select" type="integer[20]"
					utype="omg:array"/>
				<viewStatement>
					(it's fake)
				</viewStatement>
			</table>

			<table id="tbl" adql="True" nrows="27">
				<meta name="table-rank">1</meta>
				<meta name="utype">adhoc:an.example.table</meta>
				<meta name="description">An example table</meta>
				<foreignKey source="col1,ivoa" inTable="boring" dest="col1,select"/>
				<column name="col1" ucd="src.sample" utype="adhoc:tbl.col1"
					description="A column with a UCD of src.sample"
					unit="km/s"/>
				<column name="ivoa">
					<property name="std">1</property>
				</column>
			</table>
			</resource>""")
	
		rows = {}
		tapRD = base.caches.getRD("//tap")

		# tables is a dispatching grammar
		dd = tapRD.getById("importTablesFromRD")
		for dest, row in dd.grammar.parse(rd):
			del row["parser_"]
			rows.setdefault(dest, []).append(row)

		for ddId in ["importDMsFromRD", "importColumnsFromRD",
				"importFkeysFromRD", "importGroupsFromRD"]:
			dd = tapRD.getById(ddId)
			rows[dd.makes[0].table.id
				] = list(dd.grammar.parse(rd))
		
		return rows


class TAPSchemaCreationTest(testhelpers.VerboseTest):
	resources = [("rows", _TAPSchemaContent())]

	def testSchemaRow(self):
		row = testhelpers.pickSingle(self.rows["schemas"])
		self.assertEqual(row, {
			'schema_description': "We actually don't test for"
				" empty schema descriptions yet",
			'schema_index': '4000',
			'schema_name': 'schm',
			'schema_utype': 'ivo://ivoa.net/std/cool-model'})

	def testRankedTable(self):
		rows = [r for r in self.rows["tables"] if r["table_name"]=="schm.tbl"]
		self.assertEqual(testhelpers.pickSingle(rows), {'nrows': 27,
			'schema_name': 'schm',
			'sourceRD': 'temporary',
			'table_description': 'An example table',
			'table_index': '1',
			'table_name': 'schm.tbl',
			'table_type': 'table',
			'table_utype': 'adhoc:an.example.table'})

	def testNonADQLTable(self):
		for r in self.rows["tables"]:
			if "nontap" in r["table_name"]:
				raise AssertionError("non-ADQL table in tap_schema?")
	
	def testPlainTable(self):
		row = testhelpers.pickSingle(
			[r for r in self.rows["tables"] if r["table_name"]=="schm.boring"])
		self.assertEqual(row, {'nrows': None,
			'schema_name': 'schm',
			'sourceRD': 'temporary',
			'table_description': "We actually don't test for empty schema"
				" descriptions yet",
			'table_index': None,
			'table_name': 'schm.boring',
			'table_type': 'view',
			'table_utype': None})

	def testColumnUtype(self):
		row = testhelpers.pickSingle([r for r in self.rows["columns"]
			if r["utype"]=="adhoc:tbl.col1"])
		self.assertEqual(row["datatype"], "float")
		self.assertEqual(row["column_name"], "col1")
		self.assertEqual(row["ucd"], "src.sample")
		self.assertEqual(row["utype"], "adhoc:tbl.col1")
		self.assertEqual(row["unit"], "km/s")
		self.assertEqual(row["description"],
			"A column with a UCD of src.sample")

	def testXtype(self):
		row = testhelpers.pickSingle([r for r in self.rows["columns"]
			if r["xtype"]=="polygon"])
		self.assertEqual(row["column_name"], "col1")
		self.assertEqual(row["xtype"], "polygon")
		self.assertEqual(row["datatype"], "double")
		self.assertEqual(row["arraysize"], "*")
	
	def testQuotedColname(self):
		row = testhelpers.pickSingle([r for r in self.rows["columns"]
			if r["utype"]=="omg:array"])
		self.assertEqual(str(row["column_name"]), '"select"')
	
	def testSizedArray(self):
		row = testhelpers.pickSingle([r for r in self.rows["columns"]
			if r["utype"]=="omg:array"])
		self.assertEqual(row["arraysize"], '20')
		self.assertEqual(row["datatype"], "int")
		self.assertEqual(row["size"], 20)

	def testStd(self):
		row = testhelpers.pickSingle([r for r in self.rows["columns"]
			if r["column_name"]=="ivoa"])
		self.assertEqual(row["std"], 1)

	def testKeys(self):
		# Update the grammar to be dispatching and then this test, too
		rows = self.rows["keys"]
		self.assertEqual(len(rows), 3)
		self.assertEqual(rows[0]["from_table"], "schm.tbl")
		self.assertEqual(rows[0]["target_table"], "schm.boring")

		self.assertEqual(rows[1]["from_column"], "col1")
		self.assertEqual(rows[1]["target_column"], "col1")

		self.assertEqual(rows[2]["from_column"], "ivoa")
		# TODO: select should probably be quoted here.
		self.assertEqual(rows[2]["target_column"], "select")

		self.assertEqual(len(set(r["key_id"] for r in rows)), 1)

# todo?: groups (but then it's been ages since we've last heard of it)

class EvilUploadTest(testhelpers.VerboseTest):
	def _runWithUpload(self, query, uploadContent):
		jobId = _makeATAPJob({
			"query": [query],
			"request": ["doQuery"],
			"lang": ["ADQL"],
			"responseformat": ["votable/td"],
			"upload": [("t1",  "file://evil.vot")]})

		job = tap.WORKER_SYSTEM.getJob(jobId)
		with testhelpers.testFile("evil.vot", uploadContent,
				inDir=job.getWD()):
			try:
				taprunner.runSyncTAPJob(jobId)
				job = tap.WORKER_SYSTEM.getJob(jobId)
				self.assertEqual(job.phase, uws.COMPLETED)
				with open(job.getResult("result")[0]) as f:
					return f.read()
			finally:
				tap.WORKER_SYSTEM.destroy(jobId)

	def testEvilColnameAndXtype(self):
		# because running these tests is expensive, I'm abusing this
		# to also test for fantasy xtype roundtripping.
		result = self._runWithUpload(
			"SELECT * FROM TAP_SCHEMA.tables join TAP_UPLOAD.t1 ON oid=table_index",
				"""<VOTABLE version="1.2"
					xmlns="http://www.ivoa.net/xml/VOTable/v1.2">
				<RESOURCE>
				<TABLE name="holler" nrows="1">
				<FIELD datatype="long" name="oid" ucd="meta.record;meta.id"
					xtype="magic:oid"/>
				<DATA><TABLEDATA><TR><TD>1</TD></TR></TABLEDATA></DATA>
				</TABLE>
				</RESOURCE>
				</VOTABLE>""")
		self.assertTrue('name="oid"' in result)
		self.assertTrue('xtype="magic:oid"' in result)

	def testArrayWithNaN(self):
		result = self._runWithUpload(
			"SELECT column_index FROM TAP_SCHEMA.columns join TAP_UPLOAD.t1 ON"
				" column_index between intv[1] and intv[2]",
				"""<VOTABLE xmlns="http://www.ivoa.net/xml/VOTable/v1.3">
				<RESOURCE><TABLE>
				<FIELD datatype="float" arraysize="2" name="intv"/>
				<DATA><TABLEDATA>
					<TR><TD>1.5 2.5</TD></TR>
					<TR><TD>4.9 NaN</TD></TR>
					<TR><TD>NaN NaN</TD></TR>
				</TABLEDATA></DATA>
				</TABLE></RESOURCE></VOTABLE>""")
		self.assertTrue("<TD>2</TD>" in result)
		self.assertFalse("<TD>5</TD>" in result)


def _getUnparsedQueryResult(query):
	jobId = _makeATAPJob({
			"query": [query],
			"request": ["doQuery"],
			"lang": ["ADQL"]})
	try:
		taprunner.runSyncTAPJob(jobId)
		job = tap.WORKER_SYSTEM.getJob(jobId)
		if job.phase==uws.ERROR:
			raise Exception("Job died with msg %s"%job.error)
		name, mime = job.getResult("result")
		with open(name, "rb") as f:
			res = f.read()
	finally:
		tap.WORKER_SYSTEM.destroy(jobId)
	return res


def _getQueryResult(query):
	# returns a votable.simple result for query.
	vot = _getUnparsedQueryResult(query)
	res = votable.load(io.BytesIO(vot))
	return res


class SimpleRunnerTest(testhelpers.VerboseTest):
	"""tests various taprunner scenarios.
	"""
	resources = [("ds", adqltest.adqlTestTable)]

	def setUp(self):
		testhelpers.VerboseTest.setUp(self)
		self.tableName = self.ds.tables["adql"].tableDef.getQName()

	def testSimpleJob(self):
		jobId = _makeATAPJob({
			"query": ["SELECT * FROM %s"%self.tableName],
			"request": ["doQuery"],
			"lang": ["ADQL"]})
		try:
			job = tap.WORKER_SYSTEM.getJob(jobId)
			self.assertEqual(job.phase, uws.PENDING)
			tap.WORKER_SYSTEM.changeToPhase(jobId, uws.QUEUED, None)
		
			runningPhases = set([uws.QUEUED, uws.UNKNOWN, uws.EXECUTING])
			# let things run, but bail out if nothing happens
			for i in range(200):
				time.sleep(0.1)
				job.update()
				if job.phase not in runningPhases:
					break
			else:
				raise AssertionError("Job does not finish.  Your machine cannot be"
					" *that* slow?")

			self.assertEqual(job.phase, uws.COMPLETED)
			with open(os.path.join(job.getWD(),
						job.getResults()[0]["resultName"]), "rb") as f:
				result = f.read()
			self.assertTrue((datetime.datetime.utcnow()-job.endTime).seconds<1)

		finally:
			job = tap.WORKER_SYSTEM.destroy(jobId)
		self.assertTrue(b'xmlns="http://www.ivoa.net/xml/VOTable/' in result)

	def testColumnNames(self):
		table, meta = _getQueryResult(
			'SELECT cos(delta) as frob, alpha as "AlPhA", delta, "delta",'
			' 20+30, 20+30 AS constant, rv as "AS"'
			' from %s'%self.tableName)
		fields = meta.getFields()
		self.assertEqual(fields[0].name, "frob")
		self.assertEqual(fields[0].getDescription(), "A sample Dec --"
			" *TAINTED*: the value was operated on in a way that unit and"
			" ucd may be severely wrong")
		self.assertEqual(fields[1].name, "AlPhA")
		self.assertEqual(fields[2].name, "delta")
		# the next line tests for a documented violation of the spec.
		self.assertEqual(fields[3].name, "delta_")
		self.assertTrue(isinstance(fields[4].name, str))
		self.assertEqual(fields[5].name, "constant")
		self.assertEqual(fields[6].name, "AS")

	def testColumnTypes(self):
		table, meta = _getQueryResult(
			"SELECT rv, point('icrs', alpha, delta), PI() from %s"%self.tableName)
		fields = meta.getFields()
		self.assertEqual(fields[0].datatype, "double")
		self.assertEqual(fields[1].datatype, "double")
		self.assertEqual(fields[1].xtype, "point")
		self.assertEqual(fields[2].datatype, "double")


class _RoundtrippedData(testhelpers.TestResource):
	resources = [("conn", tresc.dbConnection)]
	
	def make(self, deps):
		conn = deps["conn"]
		data = rsc.makeData(
			base.resolveCrossId("data/test#import_for_roundtrip"),
			connection=conn)
		try:
			return _getQueryResult("SELECT * FROM test.typestable")[0]
		finally:
			data.drop(data.dd, connection=conn)


class TAPRoundTripTest(testhelpers.VerboseTest):
	resources = [("data", _RoundtrippedData())]

	def testIntegers(self):
		self.assertEqual(self.data[0][0], None)
		self.assertEqual(self.data[1][0], 121)

	def testReals(self):
		self.assertEqual(self.data[0][1], None)
		# todo: can we do better and keep this at an actual float32?
		self.assertAlmostEqual(self.data[1][1], 121.12100219726562)
		self.assertEqual(self.data[0][2], None)
		self.assertAlmostEqual(self.data[1][2], 121.121)

	def testText(self):
		# VOTable doesn't know None from ''
		self.assertEqual(self.data[0][3], "")
		self.assertEqual(self.data[1][3], "hundertEinundzwanzig")

	def testDate(self):
		self.assertEqual(self.data[0][4], None)
		self.assertEqual(self.data[1][4], datetime.datetime(1969, 12, 24, 0, 0))

	def testJSON(self):
		# for now, our VOTable parser doesn't know the json xtype; we
		# way want to change this, in which case this test would need to
		# change.
		# Oh: and VOTable doesn't know None from ''; if we do treat the xtype
		# one day, the "" in the next line ought to become a None.
		self.assertEqual(self.data[0][5], "")
		self.assertEqual(self.data[1][5], '{"testing": true}')
		

class _TAPResultTable(testhelpers.TestResource):
	resources = [("ds", adqltest.adqlTestTable)]

	def make(self, deps):
		return testhelpers.getXMLTree(
			_getUnparsedQueryResult(
				"SELECT rv, PI(), alpha, delta from %s"%
					deps["ds"].tables["adql"].tableDef.getQName()), debug=False)


class VOTableMetaTest(testhelpers.VerboseTest):
	resources = [("tree", _TAPResultTable())]

	def testTranslatedQueryPresent(self):
		self.assertEqualIgnoringAliases(
			self.tree.xpath("//INFO[@name='sql_query']")[0].get("value"),
			"SELECT rv, PI() ASWHATEVER, alpha, delta FROM test.adql LIMIT 20000")

	def testADQLPresent(self):
		self.assertEqualIgnoringAliases(
			self.tree.xpath("//INFO[@name='query']")[0].get("value"),
			"SELECT rv, PI(), alpha, delta from test.adql")

	def testParamCopied(self):
		pNode = self.tree.xpath("//PARAM[@name='fib']")[0]
		self.assertEqual(pNode.get("utype"), "math:series.fibonacci")
		self.assertEqual(pNode.get("value"), "1 1 2")

	def testSourcesUniqued(self):
		self.assertEqual(len(self.tree.xpath("//INFO[@ucd='meta.bib.bibcode']")), 1)

	def testCitationInfo(self):
		info = self.tree.xpath("//INFO[@name='citation']")[0]
		self.assertEqual(info.get("value"),
			"http://localhost:8080/tableinfo/test.adql#ti-citing")
		self.assertTrue("Advice " in info.text)

	def testFullCoosys(self):
		coosysEl = self.tree.xpath("//COOSYS")[0]
		self.assertEqual(coosysEl.get("epoch"), "J2015.0")
		self.assertEqual(coosysEl.get("system"), "ICRS")
		self.assertEqual(coosysEl.get("ID"), "system")
		self.assertEqual(
			self.tree.xpath("//FIELD[@name='alpha']")[0].get("ref"),
			"system")
		self.assertEqual(
			self.tree.xpath("//FIELD[@name='rV']")[0].get("ref"),
			"system")


class _TAPJoinResultTable(testhelpers.TestResource):
	resources = [("ds", adqltest.adqlTestTable),
		("ssa", tresc.ssaTestTable)]

	def make(self, deps):
		return testhelpers.getXMLTree(
			_getUnparsedQueryResult(
				"SELECT delta, ssa_pubdid"
				" from test.adql join test.hcdtest on (alpha=ssa_length)"),
			debug=False)

TAP_JOIN_RESULT_TABLE = _TAPJoinResultTable()


class JoinMetaTest(testhelpers.VerboseTest):
	resources = [("tree", TAP_JOIN_RESULT_TABLE)]

	def testDatalinkServiceMentioned(self):
		svcDefinition = self.tree.xpath("//RESOURCE[@type='meta']")
		idid = testhelpers.pickSingle(svcDefinition
			).xpath("GROUP/PARAM[@name='ID']")[0].get("ref")
		self.assertEqual(
			self.tree.xpath("//FIELD[@ID='%s']"%idid)[0].get("name"),
			"ssa_pubDID")
	
	def testDatalinkServiceSkippedWithoutColumn(self):
		tree = testhelpers.getXMLTree(
			_getUnparsedQueryResult(
				"SELECT delta"
				" from test.adql join test.hcdtest on (alpha=ssa_length)"),
			debug=False)
		self.assertEqual(len(tree.xpath("//RESOURCE[@type='meta']")), 0)

	def testParamCopied(self):
		pNode = self.tree.xpath("//PARAM[@name='fib']")[0]
		self.assertEqual(pNode.get("utype"), "math:series.fibonacci")
		self.assertEqual(pNode.get("value"), "1 1 2")

		pNode = self.tree.xpath("//PARAM[@name='fab']")[0]
		self.assertEqual(pNode.get("value"), "four")


class DataOriginTest(testhelpers.VerboseTest):
	resources = [("tree", TAP_JOIN_RESULT_TABLE)]

	def _getINFO(self, key):
		matches = self.tree.xpath(f"//INFO[@name='{key}']")
		assert len(matches)==1
		return matches[0].get("value")

	def _getINFOs(self, key):
		return set(m.get("value")
			for m in self.tree.xpath(f"//INFO[@name='{key}']"))

	# Keep the sequence of tests in sync with the sequence of items in
	# the data origin paper so it's easy to see whether we're covering
	# them all.

	def testAllIVOIDsPresent(self):
		self.assertEqual(self._getINFOs("ivoid"), set([
				'ivo://x-testing/tap',
				'ivo://x-testing/data/ssatest/c',
				'ivo://x-testing/data/ssatest/s',
				"ivo://x-testing/data/ssatest/hcdtest"]))

	def testPublisher(self):
		self.assertEqual(
			self._getINFO("publisher"),
			"Your organisation's name")

	def testServerSoftware(self):
		self.assertEqual(self._getINFO("server_software"),
			base.SERVER_SOFTWARE)

	# We don't do request and request_post in TAP for now; the core
	# doesn't have a request object, and the renderer sees pre-rendered
	# data and can't put it in any more.

	def testRequestDate(self):
		self.assertTrue(self._getINFO("request_date").startswith("20"))

	def testContact(self):
		self.assertEqual(
			self._getINFOs("contact"),
			set(['invalid@wherever.else', 'hendrix@nirvana.int']))

	def testReferenceURL(self):
		self.assertEqual(
			self._getINFOs("reference_url"),
			set([
				'http://localhost:8080/tableinfo/test.adql',
				'http://localhost:8080/tableinfo/test.hcdtest',
				'http://localhost:8080/__system__/tap/run/info']))

	def testAllSourcesPresent(self):
		self.assertEqual(
			self._getINFOs("publication_id"),
			set(["1635QB41.G135......", "2015ivoa.spec.0617D"]))

	def testResourceVersion(self):
		self.assertEqual(
			self._getINFOs("resource_version"),
			set(["3.2.1-testing"]))

	def testRights(self):
		self.assertEqual(
			self._getINFOs("rights"),
			set(['Everything in here is pure fantasy (distributed under the GNU GPL)']))

	def testRightsURI(self):
		self.assertEqual(
			self._getINFOs("rights_uri"),
			set(["http://too-lazy-to/loop/up/spdx"]))

	def testCreator(self):
		self.assertEqual(
			self._getINFOs("creator"),
			set([
				'John C. Testwriter',
				'The Master Tester',
				'Could be same as contact.name',
				'Hendrix, J.; Page, J; et al.']))

	# the rest is non-data origin so far

	def testSQLQuery(self):
		self.assertEqual(
			self._getINFO("sql_query"),
			"SELECT delta, ssa_pubdid FROM test.adql JOIN test.hcdtest ON ( alpha = ssa_length ) LIMIT 20000")
	
	def testADQLQuery(self):
		self.assertEqual(
			self._getINFO("query"),
			"SELECT delta, ssa_pubdid from test.adql join test.hcdtest on (alpha=ssa_length)")

	def testAllCitationsPresent(self):
		self.assertEqual(
			self._getINFOs("citation"),
			set(['http://localhost:8080/tableinfo/test.adql#ti-citing',
				'http://localhost:8080/tableinfo/test.hcdtest#ti-citing',
				'http://localhost:8080/__system__/tap/run/howtocite']))


class TAPTableMetaTest(testhelpers.VerboseTest):
	resources = [("conn", tresc.dbConnection), ("ot", tresc.obscoreTable),
		("ssa", tresc.ssaTestTable)]

	def testSingleSourceTableMetadataCopied(self):
		res = adqlglue.runTAPQuery(
			"SELECT TOP 1 * FROM ivoa.obscore", 1, self.conn, [], 1,
			autoClose=False)
		list(res) # exhaust the cursor
		self.assertEqual(
			base.getMetaText(res, "description"),
			"The IVOA-defined obscore table, containing generic metadata for\n"
			"datasets within this data centre.")
	
	def testSingleSourceTableParamsCopied(self):
		res = adqlglue.runTAPQuery(
			"SELECT TOP 1 * FROM test.hcdtest", 1, self.conn, [], 1,
			autoClose=False)
		list(res) # exhaust the cursor
		self.assertEqual(res.getParam("ssa_model"), "Spectrum-1.0")
		self.assertEqual(res.getParam("ssa_specres"), 1e-10)

	def testNoMetadataOnComplexQuery(self):
		res = adqlglue.runTAPQuery(
			"SELECT TOP 1 * FROM (SELECT * from ivoa.obscore) as q",
				1, self.conn, [], 1, autoClose=False)
		self.assertEqual(
			base.getMetaText(res, "description"), None)
		list(res)

	def testNoMetadataOnJoin(self):
		res = adqlglue.runTAPQuery(
			"SELECT TOP 1 * FROM ivoa.obscore as a join ivoa.obscore as b"
				" ON (a.obs_collection=b.dataproduct_subtype)",
				1, self.conn, [], 1, autoClose=False)
		self.assertEqual(
			base.getMetaText(res, "description"), None)
		list(res)

	def testCasePreserved(self):
		res = adqlglue.runTAPQuery(
			"SELECT TOP 1 ssa_pubDID FROM %s"%self.ssa.tableDef.getQName(),
			1, self.conn, [], 1, autoClose=False)
		list(res)
		self.assertEqual(res.tableDef.columns[0].name, "ssa_pubDID")


class JobMetaTest(testhelpers.VerboseTest):
	def setUp(self):
		self.jobId = tap.WORKER_SYSTEM.getNewJobId()
	
	def tearDown(self):
		tap.WORKER_SYSTEM.destroy(self.jobId)

	def _postRedirectCheck(self, req, segments, method=b"POST"):
		req.method = method
		try:
			return uwsactions.doJobAction(tap.WORKER_SYSTEM, req,
				segments=(self.jobId,)+segments)
		except svcs.SeeOther:
			pass # that's the 303 expected
		else:
			self.fail("POSTing didn't redirect")

	def testExecD(self):
		res = uwsactions.doJobAction(tap.WORKER_SYSTEM,
			TAPFakeRequest(),
			segments=(self.jobId, "executionduration"))
		self.assertEqual(res, "3600")
	
	def testSetExecD(self):
		self._postRedirectCheck(
			TAPFakeRequest(args={"EXECUTIONDURATION": ["300"]}),
			("executionduration",))
		res = uwsactions.doJobAction(tap.WORKER_SYSTEM,
			TAPFakeRequest(),
			segments=(self.jobId, "executionduration"))
		self.assertEqual(res, "300")

	def testDestruction(self):
		self._postRedirectCheck(
			TAPFakeRequest(
				args={"DESTRuction": ["2000-10-10T10:12:13"]}),
			("destruction",))
		res = uwsactions.doJobAction(tap.WORKER_SYSTEM,
			TAPFakeRequest(),
			segments=(self.jobId, "destruction"))
		self.assertEqual(res, "2000-10-10T10:12:13Z")
	
	def testQuote(self):
		res = uwsactions.doJobAction(tap.WORKER_SYSTEM,
			TAPFakeRequest(),
			segments=(self.jobId, "quote"))
		# just make sure it's an iso date
		utils.parseISODT(res)

	def testNullError(self):
		res = uwsactions.doJobAction(tap.WORKER_SYSTEM, TAPFakeRequest(),
			segments=(self.jobId, "error"))
		self.assertEqual(res, b"")

	def testParametersPostAll(self):
		self._postRedirectCheck(
			TAPFakeRequest(args={"QUERY": ["Nicenice"]}),
			("parameters",))

		res = uwsactions.doJobAction(tap.WORKER_SYSTEM, TAPFakeRequest(),
			segments=(self.jobId, "parameters"))
		self.assertTrue(b"Nicenice" in res.render())

	def testOwner(self):
		res = uwsactions.doJobAction(tap.WORKER_SYSTEM, TAPFakeRequest(),
			segments=(self.jobId, "owner"))
		self.assertEqual(res, b"")


class TAPPublicationTest(testhelpers.VerboseTest):
	resources = [("table", tresc.tapPublishedADQLTable),
		("conn", tresc.dbConnection)]

	def testSchemaPublished(self):
		res = list(self.conn.queryToDicts(
			"select * from tap_schema.schemas where schema_name='test'"))[0]
		self.assertEqual(res["description"],
			"Sundry test tables and other fancy stuff")
		self.assertEqual(res["utype"],
			"http://www.g-vo.org/DaCHS")
		self.assertEqual(res["schema_index"], 100)

	def testTablesPublished(self):
		res = list(self.conn.queryToDicts(
			"select * from tap_schema.tables where table_name='test.csdata'"))
		row = testhelpers.pickSingle(res)
		self.assertEqual(row["table_type"], "table")
		# I suppose we ought to create some way (properties?) to set table_index
		self.assertEqual(row["table_index"], None)
		self.assertEqual(row["nrows"], 10)

	def testColumnsPublished(self):
		res = list(self.conn.query(
			"select column_name, unit, datatype, arraysize, description"
			" from tap_schema.columns where table_name='test.csdata'"))
		self.assertTrue(("alpha", "deg", "float", None, "A sample RA") in res)
		self.assertEqual(len(res), 5)

	def testModelsSupportedPresent(self):
		res = list(self.conn.query(
			"select * from tap_schema.supportedmodels where sourcerd='data/test'"))
		self.assertEqual(len(res), 2)
		self.assertEqual(set(r[1] for r in res),
			set(['Fantasy-1.0', 'Fantasy-1.1']))

	def testSTCGroupPresent(self):
		res = set(list(self.conn.query(
			"select * from tap_schema.groups")))
		self.assertTrue(
			('test.adql', 'alpha', 'col:weird.reason',
				'weird_columns', 'col:weird.name', "data/test") in res)
		self.assertTrue(
			('test.adql', 'rv', None, 'nice_columns', 'col:nice.name', "data/test")
				in res)


class _RenderedTAPExample(testhelpers.TestResource):
	def make(self, ignored):
		from gavo.web import examplesrender

		rd = base.parseFromString(rscdesc.RD,
			"""<resource schema="test"><meta name="_example"
				title="Query against boolean columns">
				Some tables this
				service exposes to TAP -- e.g., :taptable:`amanda.nucand`.
				To query those, you cannot say ``WHERE boolCol`` but have to compare
				``'True'`` or ``'False'`` as in

				.. tapquery::

					SELECT *
					  FROM amanda.nucand
					WHERE atmonusubset='True' and foo&lt;20
					
				(nothing else will work).</meta></resource>""")
		return testhelpers.getXMLTree(
			examplesrender._Example(
				rd.getMeta("_example"))._getTranslatedHTML(), debug=False)


class TAPExampleTest(testhelpers.VerboseTest):
	resources = [("tree", _RenderedTAPExample())]

	def testContainerDeclared(self):
		root = self.tree.xpath('/div[@typeof="example"]')[0]
		self.assertEqual(root.get("id"), "Queryagainstbooleancolumns")
		self.assertEqual(root.get("resource"), "#Queryagainstbooleancolumns")

	def testTitleMarkedUp(self):
		title = self.tree.xpath("h2")[0]
		self.assertEqual(title.get("property"), "name")
		self.assertEqual(title.text, "Query against boolean columns")

	def testPlainText(self):
		para = self.tree.xpath("p")[0]
		self.assertTrue(para.text.startswith("Some tables"))
	
	def testTableLink(self):
		tableAnchor = self.tree.xpath("//*[@property='table']")[0]
		self.assertEqual(len(tableAnchor), 0) # no children
		self.assertEqual(tableAnchor.text, "amanda.nucand")
		tableLink = self.tree.xpath("//a")[0]
		self.assertEqual(tableLink.get("href"), "/tableinfo/amanda.nucand")

	def testQueryPresent(self):
		queryEl = self.tree.xpath("//*[@property='query']")[0]
		self.assertEqual(queryEl.text.strip(),
			"SELECT *\n  FROM amanda.nucand\nWHERE atmonusubset='True' and foo<20")
		self.assertEqual(queryEl.attrib["class"],
			'dachs-ex-tapquery literal-block')


class _WeirdTypesTAPResult(testhelpers.TestResource):
	resources = [("table", tresc.weirdTypesTable)]

	def make(self, ignored):
		data, metadata = _getQueryResult(
			"select textarr from test.weirdtypes")
		return list(metadata.iterDicts(data)), metadata


class WeirdTypesTAPTest(testhelpers.VerboseTest):
	resources = [("res", _WeirdTypesTAPResult())]

	def testStringArrayValue(self):
		self.assertEqual(self.res[0][0]['textarr'],
			['ein       ', 'walde     ', 'viel zu la'])

	def testStringArrayMeta(self):
		self.assertEqual(
			self.res[1].votTable.getChildDict()["FIELD"][0].arraysize,
			"10x*")


class UserUploadCleanupTest(testhelpers.VerboseTest):
	def testCleanup(self):
		with base.getWritableAdminConn() as conn:
			with open(base.getConfig("inputsDir") / "data" / "vizier_votable.vot",
					"rb") as f:
				tap_uploads.uploadUserTable(
					conn, "glue", f, "testuser")
				f.seek(0)
				tap_uploads.uploadUserTable(
					conn, "flu", f, "testuser")
			conn.execute("UPDATE tap_user.tables SET expiry=CURRENT_TIMESTAMP"
				" WHERE user_name='testuser' AND table_name='flu'")

		try:
			cleanup = base.resolveCrossId("//tap_user#expire")
			cleanup.runImmediate()
			with base.getTableConn() as conn:
				still_there = [r[0] for r in
					conn.query("select table_name from tap_user.tables"
						" where user_name='testuser'")]
				self.assertEqual(still_there, ["glue"])

				q = base.UnmanagedQuerier(conn)
				self.assertEqual(
					q.getTableType("tap_user._testuser_glue"), "BASE TABLE")
				self.assertEqual(q.getTableType("tap_user._testuser_flu"), None)
		finally:
			with base.getWritableAdminConn() as conn:
				conn.execute("DROP TABLE IF EXISTS tap_user._testuser_glue")
				conn.execute("DROP TABLE IF EXISTS tap_user._testuser_flu")
				conn.execute("DELETE FROM tap_user.tables WHERE user_name='testuser'")


class _UploadedTables(testhelpers.TestResource):
	resources = [("conn", tresc.dbConnection)]

	def make(self, deps):
		conn = deps["conn"]
		with open(base.getConfig("inputsDir") / "data" / "vizier_votable.vot",
				"rb") as f:
			tap_uploads.uploadUserTable(
				conn, "glue", f, "anonymous")
		return conn

	def clean(self, conn):
		createdTables = ['_anonymous_glue']
		conn.execute("DELETE FROM tap_user.tables WHERE"
			" physical_name in ({})".format(",".join(repr(t) for t in createdTables)))
		for t in createdTables:
			conn.execute(f"DROP TABLE tap_user.{t}")


class UserUploadResolutionTest(testhelpers.VerboseTest):
	resources = [("uploaded", _UploadedTables()),
		("conn", tresc.dbConnection)]

	def testInfoGetterSuccess(self):
		getter = adqlglue.DaCHSFieldInfoGetter(forUser="anonymous")
		infos = getter.getInfosFor("tap_user.glue")
		col = infos[1]
		self.assertEqual(col[0].name, "_RAJ2000")
		self.assertEqual(col[1].ucd, "pos.eq.ra;meta.main")

	def testInfoGetterWrongUser(self):
		getter = adqlglue.DaCHSFieldInfoGetter(forUser="testing")
		self.assertRaisesWithMsg(utils.NotFoundError,
			"table 'tap_user.glue' could not be located in your uploaded tables",
			getter.getInfosFor,
			("tap_user.glue",))

	def testADQLMorphing(self):
		res = list(
			adqlglue.runTAPQuery("WITH a AS"
				" (SELECT * from tap_user.glue where mag<1)"
				" SELECT b.tyc2 from tap_user.glue as b join a using (iras)",
			2, self.conn, [], 20, False))
		self.assertEqual(res, [{'TYC2': '1266-01416-1'}])


class _UploadedTablesUsers(testhelpers.TestResource):
	resources = [("conn", tresc.dbConnection)]

	def make(self, deps):
		conn = deps["conn"]
		with open(base.getConfig("inputsDir") / "data" / "vizier_votable.vot",
				"rb") as f:
			tap_uploads.uploadUserTable(
				conn, "glue", f, "anonymous")
	
		with open(base.getConfig("inputsDir") / "data" / "vizier_votable.vot",
				"rb") as f:
			tap_uploads.uploadUserTable(
				conn, "honk", f, "testing")

		with open(base.getConfig("inputsDir") / "data" / "vizier_votable.vot",
				"rb") as f:
			tap_uploads.uploadUserTable(
				conn, "glue", f, "george")

		with open(base.getConfig("inputsDir") / "data" / "vizier_votable.vot",
				"rb") as f:
			tap_uploads.uploadUserTable(
				conn, "flu", f, "testing")

		return conn

	def clean(self, conn):
		createdTables = [
			'_anonymous_glue', "_testing_honk", "_george_glue", "_testing_flu"]
		conn.execute("DELETE FROM tap_user.tables WHERE"
			" physical_name in ({})".format(",".join(repr(t) for t in createdTables)))
		for t in createdTables:
			conn.execute(f"DROP TABLE tap_user.{t}")


TAP_SERVICE = base.resolveCrossId("//tap#run")

class TableListingTest(testhelpers.VerboseTest):
	resources = [("tables", _UploadedTablesUsers())]
	
	def getForUser(self, username="testting", password="testing"):
		req = trialhelpers.FakeRequest("")
		req.user, req.password = username, password
		tap_uploads.PersistentUploadRenderer(req, TAP_SERVICE
			).render(req)
		return testhelpers.getXMLTree(req.accumulator, debug=False)

	def testTablesListingAnonymous(self):
		self.assertEqual(
			self.getForUser("").xpath("//INFO[@name='QUERY_STATUS']")[0].text,
			"No persistent tables listing for anonymous users.")

	def testTablesListingAuthUser(self):
		tree = self.getForUser("testing")
		self.assertEqual(tree.xpath("schema/name")[0].text,  "tap_user")
		tables = tree.xpath("schema/table")
		# this asserts there's no tables from george or from anonymous
		self.assertEqual(len(tables), 2)

		table_names = {t.xpath("name")[0].text for t in tables}
		self.assertEqual(table_names, {"tap_user.honk", "tap_user.flu"})

		ucds = set(n.text for n in tree.xpath("//ucd"))
		self.assertEqual(ucds, {'pos.eq.ra;meta.main', 'meta.id;meta.main',
			'pos.eq.dec;meta.main', 'pos.angDistance', 'phot.color',
			'pos.pm;pos.eq.ra', 'pos.eq.dec', 'meta.id;src', 'pos.eq.ra',
			'phot.mag;em.opt.V', 'pos.pm;pos.eq.dec'})


if __name__=="__main__":
	testhelpers.main(JobMetaTest)