#!/usr/bin/env python
# -*- coding: utf-8 -*-

###############################################################################
#  Copyright Kitware Inc.
#
#  Licensed under the Apache License, Version 2.0 ( the "License" );
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
###############################################################################

from tests import base


def setUpModule():
    """
    Enable the minerva plugin and start the server.
    """
    base.enabledPlugins.append('minerva')
    base.startServer()


def tearDownModule():
    """
    Stop the server.
    """
    base.stopServer()


class DatasetTestCase(base.TestCase):
    """
    Tests of the minerva dataset API endpoints.
    """

    def setUp(self):
        """
        Set up the test case with  a user
        """
        super(DatasetTestCase, self).setUp()

        self._user = self.model('user').createUser(
            'minervauser', 'password', 'minerva', 'user',
            'minervauser@example.com')


    def testDataset(self):
        """
        Test the minerva dataset API enpdpoints.
        """
        self.assertEqual(0, 0)

        # at first the dataset folder is None

        path = '/minerva_dataset/folder'
        params = {
            'userId': self._user['_id'],
        }
        response = self.request(path=path, method='GET', params=params)
        self.assertStatusOk(response)
        folder = response.json['folder']
        self.assertEquals(folder, None)

        # create a dataset folder

        response = self.request(path=path, method='POST', params=params)
        self.assertStatus(response, 401)  # unauthorized

        response = self.request(path=path, method='POST', params=params, user=self._user)

        self.assertStatusOk(response)
        folder = response.json['folder']
        self.assertNotEquals(folder, None)
        self.assertEquals(folder['baseParentType'], 'user')
        self.assertEquals(folder['baseParentId'], str(self._user['_id']))

        # get the folder now that is has been created

        response = self.request(path=path, method='GET', params=params)
        self.assertStatusOk(response)
        # response should be Null b/c we don't have permissions to see anything
        # TODO is it better to always make it private and just throw a 401 in this case ?
        folder = response.json['folder']
        self.assertEquals(folder, None)

        # get the folder passing in the user

        response = self.request(path=path, method='GET', params=params, user=self._user)

        self.assertStatusOk(response)
        folder = response.json['folder']
        self.assertNotEquals(folder, None)
        self.assertEquals(folder['baseParentType'], 'user')
        self.assertEquals(folder['baseParentId'], str(self._user['_id']))

        # TODO test listing datasets

@@ -3251,31 +3251,1677 @@
   # 
-TODO test listing datasets
+create some items in the dataset folder, even though these aren't real datasets%0A        # this exercises the endpoint to return datasets%0A%0A        params = %7B%0A            'name': 'item1',%0A            'folderId': folder%5B'_id'%5D%0A        %7D%0A        response = self.request(path='/item', method='POST', params=params,%0A                                            user=self._user)%0A        item1Id = response.json%5B'_id'%5D%0A        params = %7B%0A            'name': 'item2',%0A            'folderId': folder%5B'_id'%5D%0A        %7D%0A        response = self.request(path='/item', method='POST', params=params,%0A                                            user=self._user)%0A        item2Id = response.json%5B'_id'%5D%0A%0A        path = '/minerva_dataset'%0A        params = %7B%0A            'userId': self._user%5B'_id'%5D,%0A        %7D%0A%0A        # need to check with user and without%0A%0A        response = self.request(path=path, method='GET', params=params)%0A        # should have no responses because we didn't pass in a user%0A        self.assertStatusOk(response)%0A        self.assertEquals(len(response.json), 0)%0A%0A        response = self.request(path=path, method='GET', params=params, user=self._user)%0A        self.assertStatusOk(response)%0A        self.assertEquals(len(response.json), 2)%0A        datasetIds = %5Bd%5B'_id'%5D for d in response.json%5D%0A        self.assertTrue(item1Id in datasetIds, %22expected item1Id in datasets%22)%0A        self.assertTrue(item2Id in datasetIds, %22expected item2Id in datasets%22)%0A%0A        # TODO%0A        # will want some test data , a shapefile and a geojson%0A        # upload the geojson, then process it, then list it as%0A        # a dataset%0A%0A        # probably change this to an overall minerva test
 %0A

Test list dataset endpoint