Skip to content

Commit

Permalink
improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Schwannden Kuo committed Jul 20, 2022
1 parent 63c8138 commit 8442e74
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 21 deletions.
2 changes: 1 addition & 1 deletion awesome_sso/service/user/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class AwesomeUser(Document):
line_linked: bool = False
language: Language = Language.ZH_TW

class Collection:
class Settings:
name = "user"

@classmethod
Expand Down
3 changes: 2 additions & 1 deletion awesome_sso/service/user/sync_user.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import requests
from awesome_exception.exceptions import InternalServerError

from awesome_sso.service.settings import Settings
from awesome_sso.service.user.schema import AwesomeUserType
Expand All @@ -12,7 +13,7 @@ async def sync_user(user: AwesomeUserType):
)
resp.close()
if resp.status_code / 2 != 100:
raise RuntimeError(
raise InternalServerError(
"update vendor info failed: " + str(resp.content.decode("utf-8"))
)
sso_user = resp.json()
Expand Down
20 changes: 1 addition & 19 deletions run_test.sh
Original file line number Diff line number Diff line change
@@ -1,33 +1,15 @@
#!/usr/bin/env bash

function run_minio_server() {
if [ ! -f tests/minio ]; then
wget --quiet --output-document tests/minio https://dl.min.io/server/minio/release/linux-amd64/minio
chmod +x tests/minio
fi

export MINIO_ACCESS_KEY=minio
export MINIO_SECRET_KEY=minio123
export MINIO_ADDRESS=0.0.0.0:9002
tests/minio server /tmp/fs --address $MINIO_ADDRESS >tests/minio.log 2>&1 &
}

function run_mongo() {
docker run --name awesome-mongo --rm -p 27000:27017 -d mongo:4.4.10
}

if [ -z ${SERVER_ENDPOINT+x} ]; then
run_minio_server
MINIO_PID=$!
trap 'kill -9 ${MINIO_PID} 2>/dev/null' INT
trap 'docker rm -f awesome-mongo' INT
run_mongo
fi

export MONGODB_DNS=mongodb://localhost:27000/beanie_db
pytest --cov=awesome_sso tests --cov-report term-missing
if [ -n "$MINIO_PID" ]; then
kill -9 "$MINIO_PID" 2>/dev/null
docker rm -f awesome-mongo
fi
docker rm -f awesome-mongo

8 changes: 8 additions & 0 deletions tests/service/test_user.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import pytest
from awesome_exception.exceptions import InternalServerError

from awesome_sso.service.user.schema import AwesomeUser
from awesome_sso.service.user.sync_user import sync_user
from tests.conftest import init_mongo
from tests.service.model import FantasticUser, UserWithExtraParameter

Expand Down Expand Up @@ -32,3 +34,9 @@ async def test_child_user_with_extra_param(register_model):
assert user.email == register_model.email
assert user.sso_user_id == register_model.sso_user_id
assert user.extra.nickname == register_model.name


async def test_sync_user(register_model):
user: FantasticUser = await FantasticUser.register(register_model)
with pytest.raises(InternalServerError):
await sync_user(user)

0 comments on commit 8442e74

Please sign in to comment.