Skip to content

Commit e9754e9

Browse files
committed
feat: add workflows for pytest
1 parent f4c4cbb commit e9754e9

File tree

7 files changed

+114
-0
lines changed

7 files changed

+114
-0
lines changed

.github/workflows/pytest.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: pytest
2+
3+
on:
4+
push:
5+
paths:
6+
- 'src/python/test/*.py'
7+
pull_request:
8+
paths:
9+
- 'src/python/test/*.py'
10+
11+
jobs:
12+
build:
13+
14+
runs-on: ubuntu-latest
15+
strategy:
16+
max-parallel: 4
17+
matrix:
18+
python-version: [3.6, 3.7]
19+
steps:
20+
- uses: actions/checkout@v1
21+
- name: Set up Python ${{ matrix.python-version }}
22+
uses: actions/setup-python@v1
23+
with:
24+
python-version: ${{ matrix.python-version }}
25+
- name: Install dependencies
26+
run: |
27+
sudo apt-get install redis
28+
python -m pip install --upgrade pip
29+
pip install redis
30+
- name: Test with pytest
31+
run: |
32+
pip install pytest
33+
pytest
File renamed without changes.

src/python/main/__init__.py

Whitespace-only changes.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from time import time
2+
3+
from redis import Redis
4+
5+
6+
def following_key(user):
7+
return 'following:' + user
8+
9+
10+
def followers_key(user):
11+
return 'followers:' + user
12+
13+
14+
def common_following_key(user1, user2):
15+
return 'common:following:{}:{}'.format(user1, user2)
16+
17+
18+
class SocialRelationship:
19+
def __init__(self, client: Redis, user):
20+
self.client = client
21+
self.user = user
22+
23+
def follow(self, target):
24+
"""关注目标用户"""
25+
self.client.zadd(following_key(self.user), {target: time()})
26+
self.client.zadd(followers_key(target), {self.user: time()})
27+
28+
def unfollow(self, target):
29+
"""取关目标用户"""
30+
self.client.zrem(following_key(self.user), target)
31+
self.client.zrem(followers_key(target), self.user)
32+
33+
def is_following(self, target):
34+
"""判断是否关注着目标用户"""
35+
return self.client.zrank(following_key(self.user), target) is not None
36+
37+
def get_all_following(self):
38+
"""获取当前用户关注的所有人,并按最近关注时间倒序排列"""
39+
return self.client.zrevrange(following_key(self.user), 0, -1)
40+
41+
def get_all_followers(self):
42+
"""获取当前用户的所有粉丝,并按最近关注时间倒序排列"""
43+
return self.client.zrevrange(followers_key(self.user), 0, -1)
44+
45+
def count_following(self):
46+
"""获取当前用户关注的人数"""
47+
return self.client.zcard(following_key(self.user))
48+
49+
def count_followers(self):
50+
"""获取当前用户的粉丝数"""
51+
return self.client.zcard(followers_key(self.user))
52+
53+
def get_common_following(self, one):
54+
"""获取与某用户的共同关注"""
55+
common_key = common_following_key(self.user, one)
56+
self.client.zinterstore(
57+
common_key, (following_key(self.user), following_key(one)))
58+
return self.client.zrevrange(common_key, 0, -1)

src/python/test/__init__.py

Whitespace-only changes.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from unittest import TestCase
2+
3+
from redis import Redis
4+
5+
from main.social_relationship import SocialRelationship
6+
7+
test_case = TestCase()
8+
9+
10+
def test_sns():
11+
redis = Redis(decode_responses=True)
12+
bingo = SocialRelationship(redis, 'Bingo')
13+
iris = SocialRelationship(redis, 'Iris')
14+
bingo.follow('Iris')
15+
bingo.follow('GitHub')
16+
bingo.follow('Apple')
17+
iris.follow('Bingo')
18+
iris.follow('GitHub')
19+
20+
assert bingo.is_following('Iris') is True
21+
assert iris.is_following('Bingo') is True
22+
assert bingo.count_following() == 3
23+
test_case.assertListEqual(['GitHub'], bingo.get_common_following('Iris'))
File renamed without changes.

0 commit comments

Comments
 (0)