#!/usr/bin/env python

from __future__ import print_function
from __future__ import unicode_literals

try:
    from unittest import mock
except ImportError:
    import mock

from tdclient import models
from tdclient.test.test_helper import *

def setup_function(function):
    unset_environ()

def test_database():
    client = mock.MagicMock()
    database = models.Database(client, "sample_datasets", tables=["nasdaq", "www_access"], count=12345, created_at="created_at", updated_at="updated_at", org_name="org_name", permission="administrator")
    assert database.org_name == "org_name"
    assert database.permission == "administrator"
    assert database.count == 12345
    assert database.name == "sample_datasets"
    assert database.tables() == ["nasdaq", "www_access"]
    assert database.created_at == "created_at"
    assert database.updated_at == "updated_at"

def test_database_update_tables():
    client = mock.MagicMock()
    client.tables = mock.MagicMock(return_value=[
        models.Table(client, "sample_datasets", "foo", "type", "schema", "count"),
        models.Table(client, "sample_datasets", "bar", "type", "schema", "count"),
        models.Table(client, "sample_datasets", "baz", "type", "schema", "count"),
    ])
    database = models.Database(client, "sample_datasets", tables=None, count=12345, created_at="created_at", updated_at="updated_at", org_name="org_name", permission="administrator")
    tables = database.tables()
    assert [ table.name for table in tables ] == ["foo", "bar", "baz"]
    client.tables.assert_called_with("sample_datasets")

[((18, 13, 18, 29), 'mock.MagicMock', 'mock.MagicMock', ({}, {}), '()', False, 'import mock\n'), ((19, 15, 19, 202), 'tdclient.models.Database', 'models.Database', (), '', False, 'from tdclient import models\n'), ((29, 13, 29, 29), 'mock.MagicMock', 'mock.MagicMock', ({}, {}), '()', False, 'import mock\n'), ((35, 15, 35, 182), 'tdclient.models.Database', 'models.Database', (), '', False, 'from tdclient import models\n'), ((31, 8, 31, 81), 'tdclient.models.Table', 'models.Table', ({(31, 21, 31, 27): 'client', (31, 29, 31, 46): '"""sample_datasets"""', (31, 48, 31, 53): '"""foo"""', (31, 55, 31, 61): '"""type"""', (31, 63, 31, 71): '"""schema"""', (31, 73, 31, 80): '"""count"""'}, {}), "(client, 'sample_datasets', 'foo', 'type', 'schema', 'count')", False, 'from tdclient import models\n'), ((32, 8, 32, 81), 'tdclient.models.Table', 'models.Table', ({(32, 21, 32, 27): 'client', (32, 29, 32, 46): '"""sample_datasets"""', (32, 48, 32, 53): '"""bar"""', (32, 55, 32, 61): '"""type"""', (32, 63, 32, 71): '"""schema"""', (32, 73, 32, 80): '"""count"""'}, {}), "(client, 'sample_datasets', 'bar', 'type', 'schema', 'count')", False, 'from tdclient import models\n'), ((33, 8, 33, 81), 'tdclient.models.Table', 'models.Table', ({(33, 21, 33, 27): 'client', (33, 29, 33, 46): '"""sample_datasets"""', (33, 48, 33, 53): '"""baz"""', (33, 55, 33, 61): '"""type"""', (33, 63, 33, 71): '"""schema"""', (33, 73, 33, 80): '"""count"""'}, {}), "(client, 'sample_datasets', 'baz', 'type', 'schema', 'count')", False, 'from tdclient import models\n')]