from datetime import date, datetime, timedelta
import pytest
from web_test_base import *

class TestIATIDatastore(WebTestBase):
    requests_to_load = {
        'Datastore Homepage': {
            'url': 'http://datastore.iatistandard.org/'
        },
        'API - Activities Updated since 2 days ago': {
            'url': 'http://datastore.iatistandard.org/api/1/access/activity.xml?limit=0&last-updated-datetime__gt=' + str(date.today() - timedelta(days=2)),
            'min_response_size': 295
        },
        'API - Activities Updated since 3 days ago': {
            'url': 'http://datastore.iatistandard.org/api/1/access/activity.xml?limit=0&last-updated-datetime__gt=' + str(date.today() - timedelta(days=3)),
            'min_response_size': 295
        },
        'Datastore download: csv': {
            'url': 'http://datastore.iatistandard.org/api/1/access/activity.csv'
        },
        'Datastore download: xml': {
            'url': 'http://datastore.iatistandard.org/api/1/access/activity.xml'
        },
        'Datastore download: json': {
            'url': 'http://datastore.iatistandard.org/api/1/access/activity.json'
        }
    }

    @pytest.mark.parametrize("target_request", ["Datastore Homepage"])
    def test_contains_links(self, target_request):
        """
        Test that each page contains links to the defined URLs.
        """
        loaded_request = self.loaded_request_from_test_name(target_request)

        result = utility.get_links_from_page(loaded_request)

        assert "http://iatiregistry.org/" in result

    @pytest.mark.parametrize("target_request", ["API - Activities Updated since 2 days ago", "API - Activities Updated since 3 days ago"])
    def test_recent_activities(self, target_request):
        """
        Test that the datastore API knows of activities updated in the past
        few days.
        """
        req = self.loaded_request_from_test_name(target_request)
        xpath = '//result/iati-activities/query/total-count'

        result = utility.get_single_int_from_xpath(req, xpath)

        assert result > 0

    @pytest.mark.parametrize("content_type", ["application/xml", "application/json", "text/csv"])
    def test_api_output_filetype(self, content_type):
        """
        Test that API calls return data in the expected filetypes.

        The test is conducted based upon the data returned in the request
        headers["content-type"]. For example, 'application/xml; charset=utf-8'
        """
        file_extenstion = content_type.split("/")[1]
        loaded_request = self.loaded_request_from_test_name("Datastore download: {}".format(file_extenstion))

        result = loaded_request.headers["content-type"]

        assert result.startswith(content_type)

@@ -54,16 +54,32 @@
  pytest%0A
+import requests%0A
 from web
@@ -99,16 +99,17 @@
 port *%0A%0A
+%0A
 class Te
@@ -1160,16 +1160,142 @@
 y.json'%0A
+        %7D,%0A        'Datastore list of datasets': %7B%0A            'url': 'http://datastore.iatistandard.org/api/1/about/dataset'%0A
         
@@ -1432,33 +1432,24 @@
 %0A        %22%22%22
-%0A        
 Test that ea
@@ -1491,25 +1491,16 @@
 ed URLs.
-%0A        
 %22%22%22%0A    
@@ -1887,33 +1887,24 @@
 %0A        %22%22%22
-%0A        
 Test that th
@@ -1958,24 +1958,16 @@
 the past
-%0A       
  few day
@@ -1968,25 +1968,16 @@
 ew days.
-%0A        
 %22%22%22%0A    
@@ -2808,16 +2808,16 @@
 type%22%5D%0A%0A
-
         
@@ -2855,8 +2855,1067 @@
 t_type)%0A
+%0A    @pytest.mark.parametrize(%22target_request%22, %5B%22Datastore list of datasets%22%5D)%0A    def test_last_successful_fetch(self, target_request):%0A        %22%22%22Test that the datastore has successfully fetched data within the expected time frame.%22%22%22%0A        loaded_request = self.loaded_request_from_test_name(target_request)%0A        successful_fetch_dates = list()%0A        json_dataset_list = loaded_request.json()%0A        for dataset in json_dataset_list%5B'datasets'%5D:%0A            if dataset != None:%0A                dataset_url = '/'.join(%5Bloaded_request.url, dataset%5D)%0A                data = requests.get(dataset_url).json()%0A                try:%0A                    if (data%5B'resources'%5D%5B0%5D%5B'last_successful_fetch'%5D is not None) or (data%5B'resources'%5D != list()):%0A                        successful_fetch_dates.append(datetime.strptime(data%5B'resources'%5D%5B0%5D%5B'last_successful_fetch'%5D%5B:10%5D, '%25Y-%25m-%25d'))%0A                except (TypeError, IndexError, ValueError):%0A                    pass%0A        assert max(successful_fetch_dates) %3E= (datetime.now() - timedelta(days=2))%0A

Add last fetch test