test_hf_api.py 3.43 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# coding=utf-8
# Copyright 2019-present, the HuggingFace Inc. team.
#
# 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 __future__ import absolute_import, division, print_function

import os
import time
import unittest

21
22
import requests
import six
23
from requests.exceptions import HTTPError
24

25
from transformers.hf_api import HfApi, HfFolder, PresignedUrl, S3Obj
26

Aymeric Augustin's avatar
Aymeric Augustin committed
27

28
29
USER = "__DUMMY_TRANSFORMERS_USER__"
PASS = "__DUMMY_TRANSFORMERS_PASS__"
30
31
32
FILES = [
    (
        "Test-{}.txt".format(int(time.time())),
33
        os.path.join(os.path.dirname(os.path.abspath(__file__)), "fixtures/input.txt"),
34
35
    ),
    (
36
37
        "yoyo {}.txt".format(int(time.time())),  # space is intentional
        os.path.join(os.path.dirname(os.path.abspath(__file__)), "fixtures/empty.txt"),
38
39
    ),
]
40
41
42
43
44
45
46
47
48
49
50
51
52


class HfApiCommonTest(unittest.TestCase):
    _api = HfApi(endpoint="https://moon-staging.huggingface.co")


class HfApiLoginTest(HfApiCommonTest):
    def test_login_invalid(self):
        with self.assertRaises(HTTPError):
            self._api.login(username=USER, password="fake")

    def test_login_valid(self):
        token = self._api.login(username=USER, password=PASS)
53
        self.assertIsInstance(token, six.string_types)
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68


class HfApiEndpointsTest(HfApiCommonTest):
    @classmethod
    def setUpClass(cls):
        """
        Share this valid token in all tests below.
        """
        cls._token = cls._api.login(username=USER, password=PASS)

    def test_whoami(self):
        user = self._api.whoami(token=self._token)
        self.assertEqual(user, USER)

    def test_presign(self):
69
70
71
72
        for FILE_KEY, FILE_PATH in FILES:
            urls = self._api.presign(token=self._token, filename=FILE_KEY)
            self.assertIsInstance(urls, PresignedUrl)
            self.assertEqual(urls.type, "text/plain")
73
74

    def test_presign_and_upload(self):
75
        for FILE_KEY, FILE_PATH in FILES:
76
            access_url = self._api.presign_and_upload(token=self._token, filename=FILE_KEY, filepath=FILE_PATH)
77
            self.assertIsInstance(access_url, six.string_types)
78
            with open(FILE_PATH, "r") as f:
79
80
81
                body = f.read()
            r = requests.get(access_url)
            self.assertEqual(r.text, body)
82
83
84

    def test_list_objs(self):
        objs = self._api.list_objs(token=self._token)
85
86
87
88
        self.assertIsInstance(objs, list)
        if len(objs) > 0:
            o = objs[-1]
            self.assertIsInstance(o, S3Obj)
89
90
91
92
93
94
95
96
97
98


class HfFolderTest(unittest.TestCase):
    def test_token_workflow(self):
        """
        Test the whole token save/get/delete workflow,
        with the desired behavior with respect to non-existent tokens.
        """
        token = "token-{}".format(int(time.time()))
        HfFolder.save_token(token)
99
        self.assertEqual(HfFolder.get_token(), token)
100
101
102
103
        HfFolder.delete_token()
        HfFolder.delete_token()
        # ^^ not an error, we test that the
        # second call does not fail.
104
        self.assertEqual(HfFolder.get_token(), None)