test_hf_api.py 4.77 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 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.
Aymeric Augustin's avatar
Aymeric Augustin committed
15

16
17
18
19
20

import os
import time
import unittest

21
import requests
22
from requests.exceptions import HTTPError
Julien Chaumond's avatar
Julien Chaumond committed
23
from transformers.hf_api import HfApi, HfFolder, ModelInfo, PresignedUrl, RepoObj, S3Obj
24

Aymeric Augustin's avatar
Aymeric Augustin committed
25

26
27
USER = "__DUMMY_TRANSFORMERS_USER__"
PASS = "__DUMMY_TRANSFORMERS_PASS__"
28
29
FILES = [
    (
30
        "nested/Test-{}.txt".format(int(time.time())),
31
        os.path.join(os.path.dirname(os.path.abspath(__file__)), "fixtures/input.txt"),
32
33
    ),
    (
34
        "nested/yoyo {}.txt".format(int(time.time())),  # space is intentional
35
        os.path.join(os.path.dirname(os.path.abspath(__file__)), "fixtures/empty.txt"),
36
37
    ),
]
Julien Chaumond's avatar
Julien Chaumond committed
38
REPO_NAME = "my-model-{}".format(int(time.time()))
39
ENDPOINT_STAGING = "https://moon-staging.huggingface.co"
40
41
42


class HfApiCommonTest(unittest.TestCase):
43
    _api = HfApi(endpoint=ENDPOINT_STAGING)
44
45
46
47
48
49
50
51
52


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)
Aymeric Augustin's avatar
Aymeric Augustin committed
53
        self.assertIsInstance(token, str)
54
55
56
57
58
59
60
61
62
63


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

64
65
66
67
68
    @classmethod
    def tearDownClass(cls):
        for FILE_KEY, FILE_PATH in FILES:
            cls._api.delete_obj(token=cls._token, filename=FILE_KEY)

69
    def test_whoami(self):
70
        user, orgs = self._api.whoami(token=self._token)
71
        self.assertEqual(user, USER)
72
73
74
75
        self.assertIsInstance(orgs, list)

    def test_presign_invalid_org(self):
        with self.assertRaises(HTTPError):
76
            _ = self._api.presign(token=self._token, filename="nested/fake_org.txt", organization="fake")
77
78

    def test_presign_valid_org(self):
79
        urls = self._api.presign(token=self._token, filename="nested/valid_org.txt", organization="valid_org")
80
        self.assertIsInstance(urls, PresignedUrl)
81
82

    def test_presign(self):
83
84
85
86
        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")
87
88

    def test_presign_and_upload(self):
89
        for FILE_KEY, FILE_PATH in FILES:
90
            access_url = self._api.presign_and_upload(token=self._token, filename=FILE_KEY, filepath=FILE_PATH)
Aymeric Augustin's avatar
Aymeric Augustin committed
91
            self.assertIsInstance(access_url, str)
92
            with open(FILE_PATH, "r") as f:
93
94
95
                body = f.read()
            r = requests.get(access_url)
            self.assertEqual(r.text, body)
96
97
98

    def test_list_objs(self):
        objs = self._api.list_objs(token=self._token)
99
100
101
102
        self.assertIsInstance(objs, list)
        if len(objs) > 0:
            o = objs[-1]
            self.assertIsInstance(o, S3Obj)
103

Julien Chaumond's avatar
Julien Chaumond committed
104
105
106
107
108
109
110
111
112
113
114
    def test_list_repos_objs(self):
        objs = self._api.list_repos_objs(token=self._token)
        self.assertIsInstance(objs, list)
        if len(objs) > 0:
            o = objs[-1]
            self.assertIsInstance(o, RepoObj)

    def test_create_and_delete_repo(self):
        self._api.create_repo(token=self._token, name=REPO_NAME)
        self._api.delete_repo(token=self._token, name=REPO_NAME)

115

116
117
118
119
120
121
122
123
124
125
126
127
class HfApiPublicTest(unittest.TestCase):
    def test_staging_model_list(self):
        _api = HfApi(endpoint=ENDPOINT_STAGING)
        _ = _api.model_list()

    def test_model_list(self):
        _api = HfApi()
        models = _api.model_list()
        self.assertGreater(len(models), 100)
        self.assertIsInstance(models[0], ModelInfo)


128
129
130
131
132
133
134
135
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)
136
        self.assertEqual(HfFolder.get_token(), token)
137
138
139
140
        HfFolder.delete_token()
        HfFolder.delete_token()
        # ^^ not an error, we test that the
        # second call does not fail.
141
        self.assertEqual(HfFolder.get_token(), None)