Skip to content

Commit f1976d8

Browse files
committed
added interpreter test
1 parent 002e0ee commit f1976d8

File tree

1 file changed

+170
-0
lines changed

1 file changed

+170
-0
lines changed

tests/test_interpreter.py

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
import unittest
2+
import easyaccess as ea
3+
import numpy as np
4+
import pandas as pd
5+
import os
6+
import fitsio
7+
8+
def create_test_data():
9+
r = np.linspace(0,360,100)
10+
d = np.linspace(-90,90,100)
11+
ra,dec = np.meshgrid(r,d)
12+
dtype = [('RA',float),('DEC',float)]
13+
return np.rec.fromarrays([ra.flat,dec.flat],dtype=dtype)
14+
15+
class TestInterpreter(unittest.TestCase):
16+
17+
conf = ea.config_mod.get_config(ea.config_file)
18+
conf.set('display', 'loading_bar', 'no')
19+
db = conf.get('easyaccess', 'database')
20+
desconf = ea.config_mod.get_desconfig(ea.desfile, db)
21+
con=ea.easy_or(conf, desconf, db, interactive=False, quiet=True, refresh=False)
22+
con2=ea.connect(quiet=True)
23+
tablename = 'testtable'
24+
nrows = 10000
25+
prefetch = 4000
26+
chunk = 1000
27+
sqlfile = 'temp.sql'
28+
csvfile = 'temp.csv'
29+
fitsfile = 'temp.fits'
30+
h5file = 'temp.h5'
31+
32+
def test_describe(self):
33+
data = create_test_data()
34+
df = pd.DataFrame(data)
35+
self.assertEqual( len(df), self.nrows)
36+
df.to_csv(self.csvfile,index=False, float_format='%.8f', sep=',')
37+
self.con.drop_table(self.tablename)
38+
command = "load_table %s --tablename %s" % (self.csvfile, self.tablename)
39+
self.con.onecmd(command)
40+
cursor = self.con2.cursor()
41+
command = 'describe_table %s;' % self.tablename.upper()
42+
self.con.onecmd(command)
43+
self.con.drop_table(self.tablename)
44+
os.remove(self.csvfile)
45+
46+
def test_help(self):
47+
command = 'help'
48+
self.con.onecmd(command)
49+
command = '?'
50+
self.con.onecmd(command)
51+
52+
def test_add_comment(self):
53+
data = create_test_data()
54+
df = pd.DataFrame(data)
55+
self.assertEqual( len(df), self.nrows)
56+
self.con.drop_table(self.tablename)
57+
df.to_csv(self.csvfile,index=False, float_format='%.8f', sep=',')
58+
command = "load_table %s --tablename %s" % (self.csvfile, self.tablename)
59+
self.con.onecmd(command)
60+
command = "add_comment table %s 'Test table'" % self.tablename.upper()
61+
self.con.onecmd(command)
62+
command = "add_comment column %s.RA 'Coordinate'" % self.tablename.upper()
63+
self.con.onecmd(command)
64+
command = 'describe_table %s;' % self.tablename.upper()
65+
self.con.onecmd(command)
66+
self.con.drop_table(self.tablename)
67+
os.remove(self.csvfile)
68+
69+
def test_select(self):
70+
data = create_test_data()
71+
df = pd.DataFrame(data)
72+
self.assertEqual( len(df), self.nrows)
73+
self.con.drop_table(self.tablename)
74+
df.to_csv(self.csvfile,index=False, float_format='%.8f', sep=',')
75+
command = "load_table %s --tablename %s" % (self.csvfile, self.tablename)
76+
self.con.onecmd(command)
77+
command = "select RA,DEC from %s ;" % self.tablename.upper()
78+
self.con.onecmd(command)
79+
self.con.drop_table(self.tablename)
80+
os.remove(self.csvfile)
81+
82+
def test_select_csv(self):
83+
data = create_test_data()
84+
df = pd.DataFrame(data)
85+
self.assertEqual( len(df), self.nrows)
86+
self.con.drop_table(self.tablename)
87+
df.to_csv(self.csvfile,index=False, float_format='%.8f', sep=',')
88+
command = "load_table %s --tablename %s" % (self.csvfile, self.tablename)
89+
self.con.onecmd(command)
90+
command = "select RA,DEC from %s ; > %s" % (self.tablename.upper(), self.csvfile)
91+
self.con.onecmd(command)
92+
self.assertTrue(os.path.exists(self.csvfile))
93+
os.remove(self.csvfile)
94+
self.con.drop_table(self.tablename)
95+
96+
97+
def test_select_fits(self):
98+
data = create_test_data()
99+
df = pd.DataFrame(data)
100+
self.assertEqual( len(df), self.nrows)
101+
self.con.drop_table(self.tablename)
102+
df.to_csv(self.csvfile,index=False, float_format='%.8f', sep=',')
103+
command = "load_table %s --tablename %s" % (self.csvfile, self.tablename)
104+
self.con.onecmd(command)
105+
os.remove(self.csvfile)
106+
command = "select RA,DEC from %s ; > %s" % (self.tablename.upper(), self.fitsfile)
107+
self.con.onecmd(command)
108+
self.assertTrue(os.path.exists(self.fitsfile))
109+
os.remove(self.fitsfile)
110+
self.con.drop_table(self.tablename)
111+
112+
def test_select_hdf5(self):
113+
data = create_test_data()
114+
df = pd.DataFrame(data)
115+
self.assertEqual( len(df), self.nrows)
116+
self.con.drop_table(self.tablename)
117+
df.to_csv(self.csvfile,index=False, float_format='%.8f', sep=',')
118+
command = "load_table %s --tablename %s" % (self.csvfile, self.tablename)
119+
self.con.onecmd(command)
120+
os.remove(self.csvfile)
121+
command = "select RA,DEC from %s ; > %s" % (self.tablename.upper(), self.h5file)
122+
self.con.onecmd(command)
123+
self.assertTrue(os.path.exists(self.h5file))
124+
os.remove(self.h5file)
125+
self.con.drop_table(self.tablename)
126+
127+
def test_select_by_chunks(self):
128+
global load_bar
129+
load_bar = False
130+
data = create_test_data()
131+
df = pd.DataFrame(data)
132+
self.assertEqual( len(df), self.nrows)
133+
df.to_csv(self.csvfile,index=False, float_format='%.8f', sep=',')
134+
self.assertTrue(os.path.exists(self.csvfile))
135+
self.con.drop_table(self.tablename)
136+
command = "load_table %s --tablename %s" % (self.csvfile, self.tablename)
137+
self.con.onecmd(command)
138+
cursor = self.con2.cursor()
139+
for i in range(34):
140+
command = "append_table %s --tablename %s" % (self.csvfile, self.tablename)
141+
self.con.onecmd(command)
142+
temp = cursor.execute('select RA,DEC from %s' % self.tablename.upper())
143+
fetch = temp.fetchall()
144+
self.assertEqual(len(fetch), self.nrows*35)
145+
command = "prefetch set 30000"
146+
self.con.onecmd(command)
147+
self.con.outfile_max_mb = 1
148+
command = "select RA,DEC from %s ; > %s" % (self.tablename.upper(), self.csvfile)
149+
self.con.onecmd(command)
150+
for i in range(6):
151+
self.assertTrue(os.path.exists(os.path.splitext(self.csvfile)[0]+'_00000'+str(i+1)+'.csv'))
152+
os.remove(os.path.splitext(self.csvfile)[0]+'_00000'+str(i+1)+'.csv')
153+
self.con.outfile_max_mb = 1000
154+
self.con.drop_table(self.tablename)
155+
if os.path.exists(self.csvfile): os.remove(self.csvfile)
156+
157+
158+
## TODO:
159+
# multiple files (prefetch)
160+
# load_table
161+
# append_table
162+
# exceproc
163+
# execute
164+
# import and inline query
165+
# loadsql and @
166+
# find_
167+
168+
169+
if __name__ == '__main__':
170+
unittest.main()

0 commit comments

Comments
 (0)