"""
This module checks whether the data import has worked as specified.
"""

import os
os.environ["GAVO_OOTTEST"] = "dontcare"

from gavo import api
from gavo import rsc
from gavo.helpers import testhelpers


RD = api.getRD("califa/q")

# the following tuples of
# (object name, x, y, lambda, ra, dec, flux, error)
# were sent by Bernd Husemann, 2012-10-15
# The lines commented out correspond to galaxies that were not in DR1
_SAMPLES_FROM_BERND = """
UGC10650.V500.rscube.fits.gz 44 31 6359.0 255.058815556 23.106344 0.0208796 0.00303006
NGC7194.V500.rscube.fits.gz 32 38 3991.0 330.880011111 12.6381708889 0.109115 0.00744472
NGC5966.V500.rscube.fits.gz 17 41 6091.0 233.971286667 39.7711222222 0.0910541 0.00320204
NGC7563.V500.rscube.fits.gz 58 3 4245.0 348.976641111 13.1880664444 0.00227619 0.00752616
UGC10695.V500.rscube.fits.gz 29 71 3923.0 256.275164444 43.0534337778 0.00867972 0.0109683
IC5376.V500.rscube.fits.gz 6 35 4145.0 0.340466355556 34.5262255556 -0.00452334 0.00556155
UGC11958.V500.rscube.fits.gz 39 16 4425.0 333.694506667 13.8361477778 0.0389213 0.00694436
#NGC0496.V500.rscube.fits.gz 42 66 4885.0 20.7966463333 33.5384406667 0.00550054 0.00644931
#UGC05498NED01.V500.rscube.fits.gz 50 27 3959.0 153.011351111 23.0840531111 0.071156 0.0240554
UGC08267.V500.rscube.fits.gz 39 2 5127.0 197.796396667 43.7179966667 0.00804999 0.00490783
UGC09892.V500.rscube.fits.gz 48 41 4575.0 233.213116667 41.1934114444 0.0400607 0.00310674
UGC05396.V500.rscube.fits.gz 8 40 7311.0 150.426467778 10.758928 0.0137244 0.00593381
UGC08733.V500.rscube.fits.gz 33 70 4961.0 207.162202222 43.4230085556 0.00699409 0.00329952
UGC10905.V500.rscube.fits.gz 55 29 6109.0 263.521542222 25.3425811111 0.0246018 0.00371186
NGC4676B.V500.rscube.fits.gz 56 15 5491.0 191.541254444 30.7174642222 0.0168765 0.00535682
#UGC11740.V500.rscube.fits.gz 66 27 6567.0 321.553133333 9.795405 0.00275746 0.00313298
#UGC06312.V500.rscube.fits.gz 45 49 5917.0 169.49831 7.8498887 0.0140421 0.00411353
UGC10331.V500.rscube.fits.gz 30 8 6085.0 244.339954444 59.3131855556 0.00531404 0.00184781
UGC10384.V500.rscube.fits.gz 24 16 4185.0 246.698131111 11.5758245556 0.00528856 0.00679363
UGC08234.V500.rscube.fits.gz 34 52 5785.0 197.194325556 62.2772495556 0.0283263 0.00378093
IC1199.V1200.rscube.fits.gz 44 27 4348.6 242.641165556 10.0389781111 0.0857455 0.0165488
IC0944.V1200.rscube.fits.gz 38 56 4495.6 207.878342222 14.0988776667 0.000253072 0.00699955
IC1683.V1200.rscube.fits.gz 26 69 4602.0 20.6649827778 34.4474037778 0.0150016 0.00875981
IC1683.V1200.rscube.fits.gz 48 42 3814.5 20.6588716667 34.4399037778 0.0309427 0.0256032
IC2487.V1200.rscube.fits.gz 35 21 4126.0 142.538477778 20.0869561111 0.120058 0.00871743
IC5376.V1200.rscube.fits.gz 75 34 4298.2 0.321577466667 34.5262255556 -0.0180794 0.0133826
IC2487.V1200.rscube.fits.gz 13 39 4261.8 142.544588889 20.0919561111 0.0081011 0.00629242
IC1683.V1200.rscube.fits.gz 21 35 4385.0 20.6663716667 34.4379593333 0.0205824 0.00713015
IC1256.V1200.rscube.fits.gz 44 22 3834.1 260.944797778 26.4831896667 0.0415058 0.0361388
IC0540.V1200.rscube.fits.gz 48 41 4356.3 142.539468889 7.90497322222 -0.0171846 0.0182693
IC1199.V1200.rscube.fits.gz 24 40 4134.4 242.646721111 10.0425892222 0.00564891 0.0172992
IC0776.V1200.rscube.fits.gz 38 71 3968.5 184.762444444 8.86698683333 0.0571006 0.0520809
IC1256.V1200.rscube.fits.gz 35 18 3997.2 260.947297778 26.4820785556 0.0126038 0.0141042
IC2487.V1200.rscube.fits.gz 63 30 3678.7 142.5307 20.0894561111 -0.0125651 0.0393249
IC2487.V1200.rscube.fits.gz 48 0 4690.9 142.534866667 20.0811227778 0.00990041 0.00869361
#IC5309.V1200.rscube.fits.gz 41 25 4147.7 349.797111111 8.10751355556 0.108925 0.0188926
IC2487.V1200.rscube.fits.gz 46 43 3989.5 142.535422222 20.0930672222 0.0148747 0.00879295
IC1683.V1200.rscube.fits.gz 51 13 3732.6 20.6580383333 34.4318482222 0.00729627 0.0554592
IC1256.V1200.rscube.fits.gz 52 5 3834.1 260.942575556 26.4784674444 0.00867574 0.0368744
IC1199.V1200.rscube.fits.gz 47 62 3843.9 242.640332222 10.0487003333 0.0164007 0.0318277
UGC00036.V1200.rscube.fits.gz 50 9 4678.3 1.30422848889 6.76536273333 0.0153647 0.00730912
UGC07012.V1200.rscube.fits.gz 57 42 4417.9 180.506433333 29.8507597778 0.000602604 0.00860586
UGC00312.V1200.rscube.fits.gz 52 49 4415.8 7.84495247778 8.47089766667 0.0109886 0.00467975
IC1683.V1200.rscube.fits.gz 60 12 3901.3 20.6555383333 34.4315704444 0.00504028 0.0126732
UGC00312.V1200.rscube.fits.gz 53 40 4354.9 7.8446747 8.46839766667 0.0112435 0.00529093
UGC00312.V1200.rscube.fits.gz 33 26 4311.5 7.85023025556 8.46450877778 0.103589 0.00581869
UGC01057.V1200.rscube.fits.gz 44 34 4749.7 22.2191122222 13.7940757778 0.0322076 0.0100852
UGC09892.V1200.rscube.fits.gz 58 18 3876.8 233.210338889 41.1870225556 -0.0122476 0.0128571
IC5376.V1200.rscube.fits.gz 22 26 4073.5 0.336299688889 34.5240033333 0.0216104 0.0107994
IC5376.V1200.rscube.fits.gz 49 10 4221.9 0.328799688889 34.5195588889 0.0108382 0.00941175
UGC01057.V1200.rscube.fits.gz 71 52 4774.2 22.2116122222 13.7990757778 0.0113076 0.0168343
UGC01057.V1200.rscube.fits.gz 16 26 3818.7 22.22689 13.7918535556 0.0150078 0.0331894
UGC05358.V1200.rscube.fits.gz 54 2 4287.0 149.691955556 11.3795333333 -0.0064954 0.00656007
UGC07012.V1200.rscube.fits.gz 40 47 3946.8 180.511155556 29.8521486667 0.0374206 0.0136576
UGC00036.V1200.rscube.fits.gz 59 61 4709.8 1.30172848889 6.77980717778 0.0116782 0.0101867
IC5376.V1200.rscube.fits.gz 12 56 3802.6 0.339077466667 34.5323366667 -0.00969133 0.0291657
UGC09892.V1200.rscube.fits.gz 49 52 3862.8 233.212838889 41.196467 -0.0341613 0.0212049
IC1683.V1200.rscube.fits.gz 47 17 4487.9 20.6591494444 34.4329593333 0.0283438 0.0080975
UGC07012.V1200.rscube.fits.gz 42 31 4400.4 180.5106 29.8477042222 0.0934745 0.0122589
UGC00036.V1200.rscube.fits.gz 48 9 4753.9 1.30478404444 6.76536273333 0.0284292 0.00811654
"""

def _makeId(fName):
	parts = fName.split(".")
	return "%s/%s"%(parts[1], parts[0])


def _loadSamples():
	parsers = (_makeId, int, int, float, float, float,
		float, float)
	samples = []
	for raw in _SAMPLES_FROM_BERND.strip().split("\n"):
		if not raw.startswith("#"):
			samples.append([p(r) for p,r in zip(parsers, raw.split())])
	return samples

SAMPLES = _loadSamples()


class DataConnection(testhelpers.TestResource):
	"""an autocommitted DB connection using the trustedquery profile.
	"""
	setUpCost = 0.1

	def make(self, ignored):
		return api.getDBConnection("trustedquery", autocommitted="True")
	
	def clean(self, conn):
		if not conn.closed:
			conn.close()


dataConnection = DataConnection()

class IndicesTest(testhelpers.VerboseTest):
# fluxv<whatever> table, i.e., without WCS
	__metaclass__ = testhelpers.SamplesBasedAutoTest

	resources = [("conn", dataConnection)]

	def _runTest(self, sample):
		if "V1200" in sample[0]:
			destTable = "fluxv1200"
		else:
			destTable = "fluxv500"
		res = list(self.conn.query("select flux, error from califadr1.%s"
			" where obsId=%%(obsId)s and"
			" lambda between %%(lambdalo)s and %%(lambdahi)s"%destTable, {
				"obsId": "%s-%02d-%02d"%(sample[0], sample[1], sample[2]),
				"lambdalo": sample[3]-0.1,
				"lambdahi": sample[3]+0.1,}
			))
		self.assertEqual(len(res), 1, "%s missing"%sample[0])
		self.assertAlmostEqual(res[0][0], sample[6])
		self.assertAlmostEqual(res[0][1], sample[7])
	
	samples = SAMPLES


"""
Bernd's positions are hand-computed and hence wrong.  Thus, it's
ok for these tests to fail.
class PositionsTest(testhelpers.VerboseTest):
# fluxposv<whatever> table, i.e., the join with spectra
	__metaclass__ = testhelpers.SamplesBasedAutoTest

	resources = [("conn", dataConnection)]

	def _runTest(self, sample):
		res = list(self.conn.query("select flux, error from califadr1.fluxposv500"
			" where raj2000 between %(ramax)s and %(ramin)s"
			" and dej2000 between %(demax)s and %(demin)s"
			" and lambda=%(lambda)s", {
				"ramin": sample[4]-1e-8,
				"ramax": sample[4]+1e-8,
				"demin": sample[5]-1e-8,
				"demax": sample[5]+1e-8,
				"lambda": sample[3]}
			))[0]
		self.assertAlmostEqual(res[0], sample[6])
		self.assertAlmostEqual(res[1], sample[7])
	
	samples = SAMPLES
"""


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