Skip to content

Commit

Permalink
split tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shish committed Apr 1, 2024
1 parent 3621c54 commit ad6a119
Show file tree
Hide file tree
Showing 6 changed files with 404 additions and 383 deletions.
4 changes: 2 additions & 2 deletions backend/__tests__/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
def app():
yield create_app(
test_config={
"DATABASE_URL": "sqlite:///:memory:",
"DATABASE_ECHO": True,
"SQLALCHEMY_DATABASE_URI": "sqlite:///:memory:",
"SQLALCHEMY_DATABASE_ECHO": True,
}
)

Expand Down
110 changes: 110 additions & 0 deletions backend/__tests__/test_schema_mutations_friendship.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# mypy: disable-error-code="index"

from sqlalchemy import select

Check failure on line 3 in backend/__tests__/test_schema_mutations_friendship.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F401)

backend/__tests__/test_schema_mutations_friendship.py:3:24: F401 `sqlalchemy.select` imported but unused
from sqlalchemy.orm import Session

Check failure on line 4 in backend/__tests__/test_schema_mutations_friendship.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F401)

backend/__tests__/test_schema_mutations_friendship.py:4:28: F401 `sqlalchemy.orm.Session` imported but unused
import pytest
from .. import models as m

Check failure on line 6 in backend/__tests__/test_schema_mutations_friendship.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F401)

backend/__tests__/test_schema_mutations_friendship.py:6:26: F401 `..models` imported but unused
from .conftest import Query, Login, Logout

Check failure on line 7 in backend/__tests__/test_schema_mutations_friendship.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F401)

backend/__tests__/test_schema_mutations_friendship.py:7:37: F401 `.conftest.Logout` imported but unused


@pytest.mark.asyncio
async def test_addFriend_anon(query: Query):
# anon can't add a friend
result = await query(
'mutation m { addFriend(username: "Frank") }',
error="Anonymous users can't add friends",
)
assert result.data["addFriend"] is None


@pytest.mark.asyncio
async def test_addFriend_self(query: Query, login: Login):
# log in as alice and add herself as a friend
await login("Alice")
result = await query(
'mutation m { addFriend(username: "Alice") }',
error="You can't add yourself",
)
assert result.data["addFriend"] is None


@pytest.mark.asyncio
async def test_addFriend_dupe(query: Query, login: Login):
# log in as alice and add frank as a friend
await login("Alice")
result = await query('mutation m { addFriend(username: "Frank") }')
assert result.data["addFriend"] is None

# try to add frank again
result = await query(
'mutation m { addFriend(username: "Frank") }',
error="Friend request already sent",
)
assert result.data["addFriend"] is None


@pytest.mark.asyncio
async def test_addFriend_notfound(query: Query, login: Login):
# log in as alice and add a non-existent user
await login("Alice")
result = await query(
'mutation m { addFriend(username: "NotAUser") }', error="User not found"
)
assert result.data["addFriend"] is None


@pytest.mark.asyncio
async def test_removeFriend_notfound(query: Query, login: Login):
# log in as alice and remove a non-existent user
await login("Alice")
result = await query(
'mutation m { removeFriend(username: "NotAUser") }', error="User not found"
)
assert result.data["removeFriend"] is None


@pytest.mark.asyncio
async def test_addFriend_e2e(query: Query, login: Login):
# log in as alice and add frank as a friend
await login("Alice")
result = await query('mutation m { addFriend(username: "Frank") }')
assert result.data["addFriend"] is None

# check the request was created
result = await query("query q { user { friendsOutgoing { username } } }")
assert "Frank" in [
user["username"] for user in result.data["user"]["friendsOutgoing"]
]

# log in as frank and check from his end
await login("Frank")
result = await query("query q { user { friendsIncoming { username } } }")
assert "Alice" in [
user["username"] for user in result.data["user"]["friendsIncoming"]
]

# accept the request
result = await query('mutation m { addFriend(username: "Alice") }')
assert result.data["addFriend"] is None

# check frank's friends list
result = await query("query q { user { friends { username } } }")
assert "Alice" in [user["username"] for user in result.data["user"]["friends"]]

# check alice's friends list
await login("Alice")
result = await query("query q { user { friends { username } } }")
assert "Frank" in [user["username"] for user in result.data["user"]["friends"]]

# remove the friend
result = await query('mutation m { removeFriend(username: "Frank") }')
assert result.data["removeFriend"] is None

# check alice's friends list
result = await query("query q { user { friends { username } } }")
assert "Frank" not in [user["username"] for user in result.data["user"]["friends"]]

# check frank's friends list
await login("Frank")
result = await query("query q { user { friends { username } } }")
assert "Alice" not in [user["username"] for user in result.data["user"]["friends"]]
Loading

0 comments on commit ad6a119

Please sign in to comment.