# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------
# Copyright 2016 Continuum Analytics, Inc.
#
# May be copied and distributed freely only as part of an Anaconda or
# Miniconda installation.
# -----------------------------------------------------------------------------
"""Tests for Anaconda API functions."""

# Standard library imports
import datetime

# Third party imports
import pytest

# Local imports
from anaconda_navigator.api.anaconda_api import AnacondaAPI
from anaconda_navigator.utils.fixtures import tmpfolder


def create_date_string(days_delta=30):
    now = datetime.datetime.now()
    year = now.year
    month = now.month
    day = now.day
    new_date = datetime.date(year, month, day)
    new_date += datetime.timedelta(days=days_delta)
    return new_date.strftime('%Y-%m-%d')


# --- Fixtures
# -----------------------------------------------------------------------------
@pytest.fixture
def api(tmpfolder):
    api = AnacondaAPI()
    api.license_location = lambda: tmpfolder
    return api


# --- Tests
# -----------------------------------------------------------------------------
def test_license_days_left(api):
    days_left = api.get_days_left({})
    assert days_left == 0

    days_left = api.get_days_left({'dummy_key': 'test'})
    assert days_left == float('inf')


def test_license_days_left_remaining(api):
    for day in range(-30, 0):
        days_left = api.get_days_left({'end_date': create_date_string(day)})
        assert days_left == 0

    for day in range(30):
        days_left = api.get_days_left({'end_date': create_date_string(day)})
        assert days_left == day
