using System;
using System.Threading;
using edu.jhu.Cas.Jobs;
using edu.jhu.Cas.Users;
using edu.jhu.Cas.Util;
using edu.jhu.Cas.Servers;
using NUnit.Framework;
using NUnit.Core;
using System.Configuration;

/**
 * output tests
 * these test for succesful extraction but do not test validity or if you can actually dl the file
*/
namespace edu.jhu.Cas.Test{
	[TestFixture]	
	public class OutputTests{
		
		private CasJobs cJobs;
		private TableJobs tJobs;
		private const string tableName = "outputtable";
		private const long wsid = 193424723;

		[TestFixtureSetUp]
		public void Init(){
			cJobs = new CasJobs();
			tJobs = new TableJobs();
			//a long, float and int
			string qry = "select top 1000 objid,skyversion,ra into mydb."+tableName+" from photoobj";
			cJobs.SubmitShortJob(wsid,qry,"DR1","output test init query",0);
		}
		[TestFixtureTearDown]
		public void ItMustDie(){
			string qry = "drop table "+tableName;
			cJobs.SubmitShortJob(wsid,qry,"MYDB","output test destruction",0);
		}
        
		[Test]
		public void ExtractCSV(){
			Extract("CSV");
		}
		[Test]
		public void ExtractFITS(){
			Extract("FITS");
		}
		[Test]
		public void ExtractVOTABLE(){
			Extract("VOTABLE");
		}

		private void Extract(string output){
			long jobid = tJobs.RequestFileOutput(wsid,tableName,output,"myDB","") ;
			int status;
			while((status = cJobs.StatusJob(wsid,jobid)) < 4){
				Console.Write(".");
				Thread.Sleep(3*1000);
			}
			if(status == 4){
				Job j = cJobs.FindJob(wsid,"jobid = "+jobid)[0];
				throw new Exception(j.Message);
			}
		}
	}
}