# Copyright 2015 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
# in compliance with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed under the License
# is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
# or implied. See the License for the specific language governing permissions and limitations under
# the License.

from __future__ import absolute_import
from __future__ import unicode_literals
from builtins import str
import mock
from oauth2client.client import AccessTokenCredentials
import unittest

import google.datalab
import google.datalab.bigquery
import google.datalab.utils


class TestCases(unittest.TestCase):

  def _check_name_parts(self, dataset):
    parsed_name = dataset._name_parts
    self.assertEqual('test', parsed_name[0])
    self.assertEqual('requestlogs', parsed_name[1])
    self.assertEqual('test.requestlogs', dataset._full_name)
    self.assertEqual('test.requestlogs', str(dataset))

  def test_parse_full_name(self):
    dataset = TestCases._create_dataset('test.requestlogs')
    self._check_name_parts(dataset)

  def test_parse_local_name(self):
    dataset = TestCases._create_dataset('requestlogs')
    self._check_name_parts(dataset)

  def test_parse_dict_full_name(self):
    dataset = TestCases._create_dataset({'project_id': 'test', 'dataset_id': 'requestlogs'})
    self._check_name_parts(dataset)

  def test_parse_dict_local_name(self):
    dataset = TestCases._create_dataset({'dataset_id': 'requestlogs'})
    self._check_name_parts(dataset)

  def test_parse_named_tuple_name(self):
    dataset = TestCases._create_dataset(google.datalab.bigquery._utils.DatasetName('test',
                                                                                   'requestlogs'))
    self._check_name_parts(dataset)

  def test_parse_tuple_full_name(self):
    dataset = TestCases._create_dataset(('test', 'requestlogs'))
    self._check_name_parts(dataset)

  def test_parse_tuple_local(self):
    dataset = TestCases._create_dataset(('requestlogs'))
    self._check_name_parts(dataset)

  def test_parse_array_full_name(self):
    dataset = TestCases._create_dataset(['test', 'requestlogs'])
    self._check_name_parts(dataset)

  def test_parse_array_local(self):
    dataset = TestCases._create_dataset(['requestlogs'])
    self._check_name_parts(dataset)

  def test_parse_invalid_name(self):
    with self.assertRaises(Exception):
      TestCases._create_dataset('today@')

  @mock.patch('google.datalab.bigquery._api.Api.datasets_get')
  def test_dataset_exists(self, mock_api_datasets_get):
    mock_api_datasets_get.return_value = ''
    dataset = TestCases._create_dataset('test.requestlogs')
    self.assertTrue(dataset.exists())
    mock_api_datasets_get.side_effect = google.datalab.utils.RequestException(404, None)
    dataset._info = None
    self.assertFalse(dataset.exists())

  @mock.patch('google.datalab.bigquery._api.Api.datasets_insert')
  @mock.patch('google.datalab.bigquery._api.Api.datasets_get')
  def test_datasets_create_fails(self, mock_api_datasets_get, mock_api_datasets_insert):
    mock_api_datasets_get.side_effect = google.datalab.utils.RequestException(None, 404)
    mock_api_datasets_insert.return_value = {}

    ds = TestCases._create_dataset('requestlogs')
    with self.assertRaises(Exception):
      ds.create()

  @mock.patch('google.datalab.bigquery._api.Api.datasets_insert')
  @mock.patch('google.datalab.bigquery._api.Api.datasets_get')