from datetime import timedelta
import json

from flask import Flask, request, make_response
from mock import patch, MagicMock

from cpucoolerchart import crawler
from cpucoolerchart._compat import to_native
from cpucoolerchart.crawler import update_data
from cpucoolerchart.extensions import db, cache
from cpucoolerchart.views import crossdomain
import cpucoolerchart.views

from .conftest import app, read_file, fill_data


class TestViews(object):

    def setup(self):
        self.app = app()
        self.app.testing = True
        self.client = self.app.test_client()
        self.ctx = self.app.app_context()
        self.ctx.push()
        db.drop_all()
        db.create_all()

    def teardown(self):
        db.session.close()
        db.drop_all()
        db.get_engine(self.app).dispose()
        self.ctx.pop()

    def test_crossdomain(self):
        app = Flask('__test__')
        app.config.update({
            'ACCESS_CONTROL_ALLOW_ORIGIN': 'http://foo.bar'
        })
        client = app.test_client()

        @app.route('/', methods=('GET', 'PUT', 'OPTIONS'))
        @crossdomain()
        def index():