import datasets_lib as datasets
import projects_lib as projects
import accounts_lib as accounts
import common
from pprint import pprint 

accountId1 = None
accountId2 = None
session1 = None
session2 = None
projectId11 = None
projectId21 = None
projectId21 = None
projectId22 = None

def setup_module(module):
    global accountId1
    global accountId2
    global session1
    global session2
    global projectId11, projectId12, projectId21, projectId22
    global datasetId11, datasetId12, datasetId21, datasetId22,datasetId31,datasetId32, dataset41, datasetId42

    accountId1, accountId2, session1, session2, projectId11, projectId12, projectId21, projectId22, datasetId11, datasetId12,datasetId21, datasetId22 ,datasetId31, datasetId32,datasetId41, datasetId42  = common.two_accounts_with_projects_and_datasets()

def teardown_module(module):
    if accountId1 != None:
        accounts.delete_account(accountId1, session1)

    if accountId2 != None:
        accounts.delete_account(accountId2, session2)

def test_add_column(): 
    # 1. all ok
    column = {
        'name':'mycoll',
        'dataType':'string'
    }
    ret = datasets.add_column(datasetId11, column, session1)
    
    # 2. missing name
    column = {
        'dataType':'string'
    }
    ret = datasets.add_column(datasetId11, column, session1, status = 400, error = "ErrBadRequest")

    # 3. missing dataType
    column = {
        'name':'mycoll',
    }
    ret = datasets.add_column(datasetId11, column, session1, status = 400, error = "ErrBadRequest")

def test_delete_column(): 
    columns = [
        {
            'name':'mycoll1',
            'dataType':'string'
        },
        {
            'name':'mycoll2',
            'dataType':'number'
        }
    ]
    for col in columns:       
        datasets.add_column(datasetId11, col, session1)

    for col in columns:       
        datasets.add_column(datasetId31, col, session2)

    # 1. missing session
    ret = datasets.delete_column(datasetId11, "mycoll1", session = None, status = 401, error = "ErrNotAuthorized")

    # 2. wrong session
    ret = datasets.delete_column(datasetId11, "mycoll1", session = session2, status = 401, error = "ErrNotAuthorized")
    ret = datasets.delete_column(datasetId31, "mycoll1", session = session1, status = 401, error = "ErrNotAuthorized")

    # 3. all ok
    ret = datasets.delete_column(datasetId11, "mycoll1", session1)
    ret = datasets.delete_column(datasetId31, "mycoll1", session2)
    print(ret)
    
    ret = datasets.get_dataset(datasetId11, session1)
    assert len(ret["columns"]), 1

    ret = datasets.get_dataset(datasetId31, session2)
    assert len(ret["columns"]), 1

    # 4. not found
    ret = datasets.delete_column(datasetId11, "mycoll1", session1, 404, "ErrColumnNotExists")
    ret = datasets.delete_column(datasetId31, "mycoll1", session2, 404, "ErrColumnNotExists")

    # 3. delete second, ok
    ret = datasets.delete_column(datasetId11, "mycoll2", session1)
    ret = datasets.delete_column(datasetId31, "mycoll2", session2)
    print(ret)
    
    ret = datasets.get_dataset(datasetId11, session1)
    assert len(ret["columns"]), 0
    
    ret = datasets.get_dataset(datasetId31, session2)
    assert len(ret["columns"]), 0

def test_contributor(): 
    ret =  projects.add_contributor(projectId11, 'd2@d.com', session = session1)

    #1. Create dataset column, ok
    column = {
        'name':'colladdedbyd2dcom',
        'dataType':'date'
    }
    datasets.add_column(datasetId11, column, session2)
    dataset = datasets.get_dataset(datasetId11, session2)
    assert len(dataset["columns"]) == 4

    pprint(dataset)

    #2. Delete dataset column, ok
    datasets.delete_column(datasetId11, 'col2', session2)
    dataset = datasets.get_dataset(datasetId11, session2)
    assert len(dataset["columns"]) == 3

    pprint(dataset)

