Skip to content

Commit 8d5b1cc

Browse files
committed
Unit test sponsors page
1 parent 582581f commit 8d5b1cc

File tree

4 files changed

+101
-0
lines changed

4 files changed

+101
-0
lines changed

tests/common/db/base.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,26 @@ def fuzz(self):
4646
chars = string.ascii_letters + string.digits
4747
username = "".join(random.choice(chars) for i in range(12))
4848
return "@".join([username, self.domain])
49+
50+
51+
class FuzzyList(fuzzy.BaseFuzzyAttribute):
52+
def __init__(self, item_factory, item_kwargs=None, size=1, *args, **kwargs):
53+
super().__init__(*args, **kwargs)
54+
self.item_factory = item_factory
55+
self.item_kwargs = item_kwargs or {}
56+
self.size = size
57+
58+
def fuzz(self):
59+
return [self.item_factory(**self.item_kwargs).fuzz() for i in range(self.size)]
60+
61+
62+
class FuzzyUrl(fuzzy.BaseFuzzyAttribute):
63+
def __init__(self, domain="example.com", is_secure=False, *args, **kwargs):
64+
super().__init__(*args, **kwargs)
65+
self.domain = domain
66+
self.protocol = "https" if is_secure else "http"
67+
68+
def fuzz(self):
69+
chars = string.ascii_letters
70+
path = "".join(random.choice(chars) for i in range(12))
71+
return f"{self.protocol}://{self.domain}/{path}"

tests/common/db/sponsors.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License");
2+
# you may not use this file except in compliance with the License.
3+
# You may obtain a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS,
9+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
# See the License for the specific language governing permissions and
11+
# limitations under the License.
12+
13+
import datetime
14+
15+
from factory import fuzzy
16+
17+
from warehouse.sponsors.models import Sponsor
18+
19+
from .base import FuzzyList, FuzzyUrl, WarehouseFactory
20+
21+
22+
class SponsorFactory(WarehouseFactory):
23+
class Meta:
24+
model = Sponsor
25+
26+
name = fuzzy.FuzzyText(length=12)
27+
service = fuzzy.FuzzyText(length=12)
28+
activity = FuzzyList(fuzzy.FuzzyText, {"length": 30}, size=2)
29+
30+
url = FuzzyUrl()
31+
color_logo_url = FuzzyUrl()
32+
white_logo_url = FuzzyUrl()
33+
34+
footer = True
35+
psf_sponsor = True
36+
infra_sponsor = False
37+
one_time = False
38+
sidebar = True

tests/unit/sponsors/__init__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License");
2+
# you may not use this file except in compliance with the License.
3+
# You may obtain a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS,
9+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
# See the License for the specific language governing permissions and
11+
# limitations under the License.

tests/unit/sponsors/test_views.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License");
2+
# you may not use this file except in compliance with the License.
3+
# You may obtain a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS,
9+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
# See the License for the specific language governing permissions and
11+
# limitations under the License.
12+
13+
import pretend
14+
import pytest
15+
16+
from warehouse.sponsors import views
17+
from warehouse.sponsors.models import Sponsor
18+
19+
from ...common.db.sponsors import SponsorFactory
20+
21+
22+
class TestSponsorsPage:
23+
def test_list_sponsors(self, db_request):
24+
sponsors = [SponsorFactory.create() for i in range(3)]
25+
26+
result = views.display_sponsors_page(db_request)
27+
28+
expected = db_request.db.query(Sponsor).all()
29+
assert result == {"sponsors": expected}

0 commit comments

Comments
 (0)