alozowski commited on
Commit
e641f84
1 Parent(s): 35a2b07

fix tests and add new function to utils.py

Browse files
src/display/utils.py CHANGED
@@ -9,6 +9,11 @@ import pandas as pd
9
  # Configure logging
10
  logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
11
 
 
 
 
 
 
12
 
13
  def parse_datetime(datetime_str):
14
  formats = [
 
9
  # Configure logging
10
  logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
11
 
12
+ # Convert ISO 8601 dates to datetime objects for comparison
13
+ def parse_iso8601_datetime(date_str):
14
+ if date_str.endswith('Z'):
15
+ date_str = date_str[:-1] + '+00:00'
16
+ return datetime.fromisoformat(date_str)
17
 
18
  def parse_datetime(datetime_str):
19
  formats = [
src/submission/check_validity.py CHANGED
@@ -9,6 +9,7 @@ from huggingface_hub import ModelCard
9
  from huggingface_hub.hf_api import ModelInfo, get_safetensors_metadata
10
  from transformers import AutoConfig, AutoTokenizer
11
 
 
12
  from src.envs import HAS_HIGHER_RATE_LIMIT
13
 
14
 
@@ -113,15 +114,9 @@ def user_submission_permission(org_or_user, users_to_submission_dates, rate_limi
113
  submission_dates = sorted(users_to_submission_dates[org_or_user])
114
  time_limit = datetime.now(timezone.utc) - timedelta(days=rate_limit_period)
115
 
116
- # Convert ISO 8601 dates to datetime objects for comparison
117
- def parse_datetime(date_str):
118
- if date_str.endswith('Z'):
119
- date_str = date_str[:-1] + '+00:00'
120
- return datetime.fromisoformat(date_str)
121
-
122
  submissions_after_timelimit = [
123
- parse_datetime(d) for d in submission_dates
124
- if parse_datetime(d) > time_limit
125
  ]
126
 
127
  num_models_submitted_in_period = len(submissions_after_timelimit)
 
9
  from huggingface_hub.hf_api import ModelInfo, get_safetensors_metadata
10
  from transformers import AutoConfig, AutoTokenizer
11
 
12
+ from src.display.utils import parse_iso8601_datetime
13
  from src.envs import HAS_HIGHER_RATE_LIMIT
14
 
15
 
 
114
  submission_dates = sorted(users_to_submission_dates[org_or_user])
115
  time_limit = datetime.now(timezone.utc) - timedelta(days=rate_limit_period)
116
 
 
 
 
 
 
 
117
  submissions_after_timelimit = [
118
+ parse_iso8601_datetime(d) for d in submission_dates
119
+ if parse_iso8601_datetime(d) > time_limit
120
  ]
121
 
122
  num_models_submitted_in_period = len(submissions_after_timelimit)
tests/submission/test_user_submission_permission.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import unittest
2
+ from unittest.mock import patch
3
+ from datetime import datetime, timedelta, timezone
4
+
5
+ from src.submission.check_validity import user_submission_permission
6
+ from src.envs import RATE_LIMIT_PERIOD, RATE_LIMIT_QUOTA
7
+
8
+ class TestUserSubmissionPermission(unittest.TestCase):
9
+
10
+ def setUp(self):
11
+ self.user_name = "test_user"
12
+ self.rate_limit_period = RATE_LIMIT_PERIOD
13
+ self.rate_limit_quota = RATE_LIMIT_QUOTA
14
+ self.fixed_now = datetime(2023, 6, 1, 12, 0, 0, tzinfo=timezone.utc)
15
+ # Submission dates that simulate various test cases
16
+ self.users_to_submission_dates = {
17
+ "test_user": [
18
+ (self.fixed_now - timedelta(days=1)).isoformat(),
19
+ (self.fixed_now - timedelta(days=2)).isoformat(),
20
+ (self.fixed_now - timedelta(days=3)).isoformat(),
21
+ (self.fixed_now - timedelta(days=4)).isoformat(),
22
+ ]
23
+ }
24
+
25
+ @staticmethod
26
+ def fixed_datetime_now(tz=None):
27
+ return datetime(2023, 6, 1, 12, 0, 0, tzinfo=timezone.utc)
28
+
29
+ @patch('src.submission.check_validity.datetime')
30
+ def test_user_below_quota(self, mock_datetime):
31
+ mock_datetime.now.side_effect = self.fixed_datetime_now
32
+ mock_datetime.fromisoformat = datetime.fromisoformat
33
+ allowed, message = user_submission_permission(
34
+ self.user_name, self.users_to_submission_dates, self.rate_limit_period, self.rate_limit_quota
35
+ )
36
+ self.assertTrue(allowed)
37
+
38
+ @patch('src.submission.check_validity.datetime')
39
+ def test_user_at_quota(self, mock_datetime):
40
+ mock_datetime.now.side_effect = self.fixed_datetime_now
41
+ mock_datetime.fromisoformat = datetime.fromisoformat
42
+
43
+ # Add one more submission to reach the quota
44
+ self.users_to_submission_dates["test_user"].append(self.fixed_now.isoformat())
45
+
46
+ allowed, message = user_submission_permission(
47
+ self.user_name, self.users_to_submission_dates, self.rate_limit_period, self.rate_limit_quota
48
+ )
49
+ self.assertFalse(allowed)
50
+ expected_message = (
51
+ f"Organisation or user `{self.user_name}` already has {self.rate_limit_quota} model requests submitted "
52
+ f"in the last {self.rate_limit_period} days.\n"
53
+ "Please wait a couple of days before resubmitting, so that everybody can enjoy using the leaderboard 🤗"
54
+ )
55
+ self.assertEqual(message, expected_message)
56
+
57
+ @patch('src.submission.check_validity.datetime')
58
+ def test_user_above_quota(self, mock_datetime):
59
+ mock_datetime.now.side_effect = self.fixed_datetime_now
60
+ mock_datetime.fromisoformat = datetime.fromisoformat
61
+ # Add more than quota submissions
62
+ for _ in range(self.rate_limit_quota + 1):
63
+ self.users_to_submission_dates["test_user"].append(self.fixed_now.isoformat())
64
+ allowed, message = user_submission_permission(
65
+ self.user_name, self.users_to_submission_dates, self.rate_limit_period, self.rate_limit_quota
66
+ )
67
+ self.assertFalse(allowed)
68
+
69
+ def test_user_no_previous_submissions(self):
70
+ allowed, message = user_submission_permission(
71
+ "new_user", self.users_to_submission_dates, self.rate_limit_period, self.rate_limit_quota
72
+ )
73
+ self.assertTrue(allowed)
74
+
75
+ @patch('src.submission.check_validity.HAS_HIGHER_RATE_LIMIT', ["specific_user"])
76
+ @patch('src.submission.check_validity.datetime')
77
+ def test_user_higher_rate_limit(self, mock_datetime):
78
+ mock_datetime.now.side_effect = self.fixed_datetime_now
79
+ mock_datetime.fromisoformat = datetime.fromisoformat
80
+ self.users_to_submission_dates["specific_user"] = [self.fixed_now.isoformat()] * (self.rate_limit_quota + 1)
81
+ allowed, message = user_submission_permission(
82
+ "specific_user", self.users_to_submission_dates, self.rate_limit_period, self.rate_limit_quota
83
+ )
84
+ self.assertTrue(allowed)
85
+
86
+ @patch('src.submission.check_validity.datetime')
87
+ def test_submission_just_outside_window(self, mock_datetime):
88
+ mock_datetime.now.side_effect = self.fixed_datetime_now
89
+ mock_datetime.fromisoformat = datetime.fromisoformat
90
+ old_submission = (self.fixed_now - timedelta(days=self.rate_limit_period, seconds=1)).isoformat()
91
+ self.users_to_submission_dates["test_user"] = [old_submission]
92
+ allowed, message = user_submission_permission(
93
+ self.user_name, self.users_to_submission_dates, self.rate_limit_period, self.rate_limit_quota
94
+ )
95
+ self.assertTrue(allowed)
96
+
97
+ if __name__ == '__main__':
98
+ unittest.main()