from SAGA.database import *
import tempfile
import os
import numpy as np

try:
    FileNotFoundError
except NameError:
    FileNotFoundError = IOError

def test_create_database():
    d = Database()
    d = Database('.')

def test_download_google():
    d = Database()
    d1 = d['hosts_named'].read()
    t = tempfile.mkstemp()[1]
    try:
        d['hosts_named'].download(t, overwrite=True)
        d['hosts_named'].clear_cache()
        d['hosts_named'].remote = None
        d2 = d['hosts_named'].read()
        for k in d1.columns:
            assert (d1[k]==d2[k]).all()
    finally:
        os.unlink(t)

def test_set_local():
    d = Database()
    d1 = d['hosts_named'].read()
    t = tempfile.mkstemp()[1]
    try:
        d['hosts_named'].download(t, overwrite=True, set_as_local=False)
        d['hosts_named'].clear_cache()
        d['hosts_named'].remote = None
        d['hosts_named'].local = t
        d2 = d['hosts_named'].read()
        for k in d1.columns:
            assert (d1[k]==d2[k]).all()
    finally:
        os.unlink(t)

def test_set_base():
    d = Database()
    d['base', 32].local = 'path'
    d1 = d.get(('base', 32))
    try:
        d1.read()
    except FileNotFoundError:
        pass
    assert isinstance(d1.local, FitsTable)
    assert d1.local.path == 'path'

def test_download_default():
    d = Database()
    d1 = d['hosts_named'].read()
    t = tempfile.mkstemp()[1]
    try:
        dobj = DataObject(d['hosts_named'].remote, CsvTable(t))
        dobj.download(overwrite=True)
        dobj.clear_cache()
        dobj.remote = None
        d2 = dobj.read()
        for k in d1.columns:
            assert (d1[k]==d2[k]).all()
    finally:
        os.unlink(t)

@@ -271,38 +271,32 @@
    d1 = d%5B'hosts
-_named
 '%5D.read()%0A    t 
@@ -281,32 +281,32 @@
 'hosts'%5D.read()%0A
+
     t = tempfile
@@ -336,38 +336,32 @@
         d%5B'hosts
-_named
 '%5D.download(t, o
@@ -383,38 +383,32 @@
         d%5B'hosts
-_named
 '%5D.clear_cache()
@@ -416,38 +416,32 @@
         d%5B'hosts
-_named
 '%5D.remote = None
@@ -454,38 +454,32 @@
    d2 = d%5B'hosts
-_named
 '%5D.read()%0A      
@@ -626,38 +626,32 @@
    d1 = d%5B'hosts
-_named
 '%5D.read()%0A    t 
@@ -699,22 +699,16 @@
 d%5B'hosts
-_named
 '%5D.downl
@@ -766,22 +766,16 @@
 d%5B'hosts
-_named
 '%5D.clear
@@ -791,38 +791,32 @@
         d%5B'hosts
-_named
 '%5D.remote = None
@@ -832,22 +832,16 @@
 d%5B'hosts
-_named
 '%5D.local
@@ -858,38 +858,32 @@
    d2 = d%5B'hosts
-_named
 '%5D.read()%0A      
@@ -1292,30 +1292,24 @@
 1 = d%5B'hosts
-_named
 '%5D.read()%0A  
@@ -1337,32 +1337,32 @@
 p()%5B1%5D%0A    try:%0A
+
         dobj = D
@@ -1383,14 +1383,8 @@
 osts
-_named
 '%5D.r

update test