from unittest import TestCase
from nose.tools import assert_raises
from nose.tools import assert_not_equal, assert_equal
from ..helpers.integration_test_helper import IntegrationTestHelper


class TestDataObservatoryFunctions(TestCase):

    def setUp(self):
        self.env_variables = IntegrationTestHelper.get_environment_variables()
        self.sql_api_url = "https://{0}.{1}/api/v1/sql".format(
            self.env_variables['username'],
            self.env_variables['host'],
            self.env_variables['api_key']
        )

    def test_if_get_demographic_snapshot_is_ok(self):
        query = "SELECT obs_get_demographic_snapshot(CDB_LatLng(40.704512, -73.936669));&api_key={0}".format(self.env_variables['api_key'])
        routing = IntegrationTestHelper.execute_query(self.sql_api_url, query)
        assert_not_equal(routing['the_geom'], None)

    def test_if_get_demographic_snapshot_without_api_key_raise_error(self):
        query = "SELECT obs_get_demographic_snapshot(CDB_LatLng(40.704512, -73.936669));"
        try:
            IntegrationTestHelper.execute_query(self.sql_api_url, query)
        except Exception as e:
            assert_equal(e.message[0], "The api_key must be provided")

    def test_if_get_segment_snapshot_is_ok(self):
        query = "SELECT obs_get_segment_snapshot(CDB_LatLng(40.704512, -73.936669));&api_key={0}".format(self.env_variables['api_key'])
        routing = IntegrationTestHelper.execute_query(self.sql_api_url, query)
        assert_not_equal(routing['the_geom'], None)

    def test_if_get_segment_snapshot_without_api_key_raise_error(self):
        query = "SELECT obs_get_segment_snapshot(CDB_LatLng(40.704512, -73.936669));"
        try:
            IntegrationTestHelper.execute_query(self.sql_api_url, query)
        except Exception as e:
            assert_equal(e.message[0], "The api_key must be provided")

@@ -665,32 +665,44 @@
 12, -73.936669))
+ as snapshot
 ;&api_key=%7B0%7D%22.f
@@ -738,39 +738,40 @@
 _key'%5D)%0A        
-routing
+snapshot
  = IntegrationTe
@@ -843,33 +843,34 @@
 t_equal(
-routing%5B'the_geom
+snapshot%5B'snapshot
 '%5D, None
@@ -1360,16 +1360,28 @@
 936669))
+ as snapshot
 ;&api_ke
@@ -1433,23 +1433,24 @@
         
-routing
+snapshot
  = Integ
@@ -1534,25 +1534,26 @@
 ual(
-routing%5B'the_geom
+snapshot%5B'snapshot
 '%5D, 

Check for snapshot instead of the_geom in the data observatory integration tests