def setUp(self):
     url = 'http://drive.google.com'
     self.importer = BulkTaskGDImport(googledocs_url=url)
 def setUp(self):
     url = "http://drive.google.com"
     self.importer = BulkTaskGDImport(googledocs_url=url)
class TestBulkTaskGDImport(object):
    def setUp(self):
        url = 'http://drive.google.com'
        self.importer = BulkTaskGDImport(googledocs_url=url)

    def test_count_tasks_returns_0_if_no_rows_other_than_header(self, request):
        empty_file = FakeResponse(text='CSV,with,no,content\n',
                                  status_code=200,
                                  headers={'content-type': 'text/plain'},
                                  encoding='utf-8')
        request.return_value = empty_file

        number_of_tasks = self.importer.count_tasks()

        assert number_of_tasks is 0, number_of_tasks

    def test_count_tasks_returns_1_for_CSV_with_one_valid_row(self, request):
        valid_file = FakeResponse(text='Foo,Bar,Baz\n1,2,3',
                                  status_code=200,
                                  headers={'content-type': 'text/plain'},
                                  encoding='utf-8')
        request.return_value = valid_file

        number_of_tasks = self.importer.count_tasks()

        assert number_of_tasks is 1, number_of_tasks

    def test_count_tasks_raises_exception_if_file_forbidden(self, request):
        forbidden_request = FakeResponse(
            text='Forbidden',
            status_code=403,
            headers={'content-type': 'text/plain'},
            encoding='utf-8')
        request.return_value = forbidden_request
        msg = "Oops! It looks like you don't have permission to access that file"

        assert_raises(BulkImportException, self.importer.count_tasks)
        try:
            self.importer.count_tasks()
        except BulkImportException as e:
            assert e[0] == msg, e

    def test_count_tasks_raises_exception_if_not_CSV_file(self, request):
        html_request = FakeResponse(text='Not a CSV',
                                    status_code=200,
                                    headers={'content-type': 'text/html'},
                                    encoding='utf-8')
        request.return_value = html_request
        msg = "Oops! That file doesn't look like the right file."

        assert_raises(BulkImportException, self.importer.count_tasks)
        try:
            self.importer.count_tasks()
        except BulkImportException as e:
            assert e[0] == msg, e

    def test_count_tasks_raises_exception_if_dup_header(self, request):
        empty_file = FakeResponse(text='Foo,Bar,Foo\n1,2,3',
                                  status_code=200,
                                  headers={'content-type': 'text/plain'},
                                  encoding='utf-8')
        request.return_value = empty_file
        msg = "The file you uploaded has two headers with the same name."

        assert_raises(BulkImportException, self.importer.count_tasks)
        try:
            self.importer.count_tasks()
        except BulkImportException as e:
            assert e[0] == msg, e

    def test_tasks_raises_exception_if_file_forbidden(self, request):
        forbidden_request = FakeResponse(
            text='Forbidden',
            status_code=403,
            headers={'content-type': 'text/plain'},
            encoding='utf-8')
        request.return_value = forbidden_request
        msg = "Oops! It looks like you don't have permission to access that file"

        assert_raises(BulkImportException, self.importer.tasks)
        try:
            self.importer.tasks()
        except BulkImportException as e:
            assert e[0] == msg, e

    def test_tasks_raises_exception_if_not_CSV_file(self, request):
        html_request = FakeResponse(text='Not a CSV',
                                    status_code=200,
                                    headers={'content-type': 'text/html'},
                                    encoding='utf-8')
        request.return_value = html_request
        msg = "Oops! That file doesn't look like the right file."

        assert_raises(BulkImportException, self.importer.tasks)
        try:
            self.importer.tasks()
        except BulkImportException as e:
            assert e[0] == msg, e

    def test_tasks_raises_exception_if_dup_header(self, request):
        csv_file = FakeResponse(text='Foo,Bar,Foo\n1,2,3',
                                status_code=200,
                                headers={'content-type': 'text/plain'},
                                encoding='utf-8')
        request.return_value = csv_file
        msg = "The file you uploaded has two headers with the same name."

        raised = False
        try:
            self.importer.tasks().next()
        except BulkImportException as e:
            assert e[0] == msg, e
            raised = True
        finally:
            assert raised, "Exception not raised"

    def test_tasks_return_tasks_with_only_info_fields(self, request):
        csv_file = FakeResponse(text='Foo,Bar,Baz\n1,2,3',
                                status_code=200,
                                headers={'content-type': 'text/plain'},
                                encoding='utf-8')
        request.return_value = csv_file

        tasks = self.importer.tasks()
        task = tasks.next()

        assert task == {
            "info": {
                u'Bar': u'2',
                u'Foo': u'1',
                u'Baz': u'3'
            }
        }, task

    def test_tasks_return_tasks_with_non_info_fields_too(self, request):
        csv_file = FakeResponse(text='Foo,Bar,priority_0\n1,2,3',
                                status_code=200,
                                headers={'content-type': 'text/plain'},
                                encoding='utf-8')
        request.return_value = csv_file

        tasks = self.importer.tasks()
        task = tasks.next()

        assert task == {
            'info': {
                u'Foo': u'1',
                u'Bar': u'2'
            },
            u'priority_0': u'3'
        }, task

    def test_tasks_works_with_encodings_other_than_utf8(self, request):
        csv_file = FakeResponse(text=u'Foo\nM\xc3\xbcnchen',
                                status_code=200,
                                headers={'content-type': 'text/plain'},
                                encoding='ISO-8859-1')
        request.return_value = csv_file

        tasks = self.importer.tasks()
        task = tasks.next()

        assert csv_file.encoding == 'utf-8'
class TestBulkTaskGDImport(object):
    def setUp(self):
        url = "http://drive.google.com"
        self.importer = BulkTaskGDImport(googledocs_url=url)

    def test_count_tasks_returns_0_if_no_rows_other_than_header(self, request):
        empty_file = FakeResponse(
            text="CSV,with,no,content\n", status_code=200, headers={"content-type": "text/plain"}, encoding="utf-8"
        )
        request.return_value = empty_file

        number_of_tasks = self.importer.count_tasks()

        assert number_of_tasks is 0, number_of_tasks

    def test_count_tasks_returns_1_for_CSV_with_one_valid_row(self, request):
        valid_file = FakeResponse(
            text="Foo,Bar,Baz\n1,2,3", status_code=200, headers={"content-type": "text/plain"}, encoding="utf-8"
        )
        request.return_value = valid_file

        number_of_tasks = self.importer.count_tasks()

        assert number_of_tasks is 1, number_of_tasks

    def test_count_tasks_raises_exception_if_file_forbidden(self, request):
        forbidden_request = FakeResponse(
            text="Forbidden", status_code=403, headers={"content-type": "text/plain"}, encoding="utf-8"
        )
        request.return_value = forbidden_request
        msg = "Oops! It looks like you don't have permission to access that file"

        assert_raises(BulkImportException, self.importer.count_tasks)
        try:
            self.importer.count_tasks()
        except BulkImportException as e:
            assert e[0] == msg, e

    def test_count_tasks_raises_exception_if_not_CSV_file(self, request):
        html_request = FakeResponse(
            text="Not a CSV", status_code=200, headers={"content-type": "text/html"}, encoding="utf-8"
        )
        request.return_value = html_request
        msg = "Oops! That file doesn't look like the right file."

        assert_raises(BulkImportException, self.importer.count_tasks)
        try:
            self.importer.count_tasks()
        except BulkImportException as e:
            assert e[0] == msg, e

    def test_count_tasks_raises_exception_if_dup_header(self, request):
        empty_file = FakeResponse(
            text="Foo,Bar,Foo\n1,2,3", status_code=200, headers={"content-type": "text/plain"}, encoding="utf-8"
        )
        request.return_value = empty_file
        msg = "The file you uploaded has two headers with the same name."

        assert_raises(BulkImportException, self.importer.count_tasks)
        try:
            self.importer.count_tasks()
        except BulkImportException as e:
            assert e[0] == msg, e

    def test_tasks_raises_exception_if_file_forbidden(self, request):
        forbidden_request = FakeResponse(
            text="Forbidden", status_code=403, headers={"content-type": "text/plain"}, encoding="utf-8"
        )
        request.return_value = forbidden_request
        msg = "Oops! It looks like you don't have permission to access that file"

        assert_raises(BulkImportException, self.importer.tasks)
        try:
            self.importer.tasks()
        except BulkImportException as e:
            assert e[0] == msg, e

    def test_tasks_raises_exception_if_not_CSV_file(self, request):
        html_request = FakeResponse(
            text="Not a CSV", status_code=200, headers={"content-type": "text/html"}, encoding="utf-8"
        )
        request.return_value = html_request
        msg = "Oops! That file doesn't look like the right file."

        assert_raises(BulkImportException, self.importer.tasks)
        try:
            self.importer.tasks()
        except BulkImportException as e:
            assert e[0] == msg, e

    def test_tasks_raises_exception_if_dup_header(self, request):
        csv_file = FakeResponse(
            text="Foo,Bar,Foo\n1,2,3", status_code=200, headers={"content-type": "text/plain"}, encoding="utf-8"
        )
        request.return_value = csv_file
        msg = "The file you uploaded has two headers with the same name."

        raised = False
        try:
            self.importer.tasks().next()
        except BulkImportException as e:
            assert e[0] == msg, e
            raised = True
        finally:
            assert raised, "Exception not raised"

    def test_tasks_return_tasks_with_only_info_fields(self, request):
        csv_file = FakeResponse(
            text="Foo,Bar,Baz\n1,2,3", status_code=200, headers={"content-type": "text/plain"}, encoding="utf-8"
        )
        request.return_value = csv_file

        tasks = self.importer.tasks()
        task = tasks.next()

        assert task == {"info": {u"Bar": u"2", u"Foo": u"1", u"Baz": u"3"}}, task

    def test_tasks_return_tasks_with_non_info_fields_too(self, request):
        csv_file = FakeResponse(
            text="Foo,Bar,priority_0\n1,2,3", status_code=200, headers={"content-type": "text/plain"}, encoding="utf-8"
        )
        request.return_value = csv_file

        tasks = self.importer.tasks()
        task = tasks.next()

        assert task == {"info": {u"Foo": u"1", u"Bar": u"2"}, u"priority_0": u"3"}, task

    def test_tasks_works_with_encodings_other_than_utf8(self, request):
        csv_file = FakeResponse(
            text=u"Foo\nM\xc3\xbcnchen", status_code=200, headers={"content-type": "text/plain"}, encoding="ISO-8859-1"
        )
        request.return_value = csv_file

        tasks = self.importer.tasks()
        task = tasks.next()

        assert csv_file.encoding == "utf-8"