Skip to content

Commit 9d04fc9

Browse files
Fix code formatting for pre-commit compliance
1 parent 6421a8b commit 9d04fc9

2 files changed

Lines changed: 43 additions & 27 deletions

File tree

src/dstack/_internal/server/services/projects.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -262,11 +262,22 @@ async def list_user_project_models(
262262
session: AsyncSession,
263263
user: UserModel,
264264
include_members: bool = False,
265-
) -> List[ProjectModel]:
265+
) -> List[ProjectModel]:
266+
"""
267+
Get projects for a user where they are a member.
268+
269+
Args:
270+
session: Database session
271+
user: User model
272+
include_members: Whether to join and load project members
273+
274+
Returns:
275+
List of ProjectModel instances where user is a member
276+
"""
266277
options = []
267278
if include_members:
268279
options.append(joinedload(ProjectModel.members))
269-
280+
270281
res = await session.execute(
271282
select(ProjectModel)
272283
.where(
@@ -276,7 +287,7 @@ async def list_user_project_models(
276287
)
277288
.options(*options)
278289
)
279-
290+
280291
return list(res.scalars().unique().all())
281292

282293

@@ -288,13 +299,12 @@ async def _list_public_non_member_project_models(
288299
Get public projects where user is NOT a member.
289300
"""
290301
res = await session.execute(
291-
select(ProjectModel)
292-
.where(
302+
select(ProjectModel).where(
293303
ProjectModel.deleted == False,
294304
ProjectModel.is_public == True,
295305
ProjectModel.id.notin_(
296306
select(MemberModel.project_id).where(MemberModel.user_id == user.id)
297-
)
307+
),
298308
)
299309
)
300310
return list(res.scalars().all())

src/tests/_internal/server/routers/test_projects.py

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ async def test_returns_projects(self, test_db, session: AsyncSession, client: As
8181

8282
@pytest.mark.asyncio
8383
@pytest.mark.parametrize("test_db", ["sqlite", "postgres"], indirect=True)
84-
async def test_returns_public_projects_to_non_members(self, test_db, session: AsyncSession, client: AsyncClient):
84+
async def test_returns_public_projects_to_non_members(
85+
self, test_db, session: AsyncSession, client: AsyncClient
86+
):
8587
# Create project owner
8688
owner = await create_user(
8789
session=session,
@@ -136,7 +138,9 @@ async def test_returns_public_projects_to_non_members(self, test_db, session: As
136138

137139
@pytest.mark.asyncio
138140
@pytest.mark.parametrize("test_db", ["sqlite", "postgres"], indirect=True)
139-
async def test_member_sees_both_public_and_private_projects(self, test_db, session: AsyncSession, client: AsyncClient):
141+
async def test_member_sees_both_public_and_private_projects(
142+
self, test_db, session: AsyncSession, client: AsyncClient
143+
):
140144
# Create project owner
141145
owner = await create_user(
142146
session=session,
@@ -347,7 +351,9 @@ async def test_forbids_if_no_permission_to_create_projects(
347351
@pytest.mark.asyncio
348352
@pytest.mark.parametrize("test_db", ["sqlite", "postgres"], indirect=True)
349353
@freeze_time(datetime(2023, 1, 2, 3, 4, tzinfo=timezone.utc))
350-
async def test_creates_public_project(self, test_db, session: AsyncSession, client: AsyncClient):
354+
async def test_creates_public_project(
355+
self, test_db, session: AsyncSession, client: AsyncClient
356+
):
351357
user = await create_user(session=session)
352358
project_id = UUID("1b0e1b45-2f8c-4ab6-8010-a0d1a3e44e0e")
353359
project_name = "test_public_project"
@@ -376,58 +382,58 @@ async def test_creates_public_project(self, test_db, session: AsyncSession, clie
376382
@pytest.mark.asyncio
377383
@pytest.mark.parametrize("test_db", ["sqlite", "postgres"], indirect=True)
378384
@freeze_time(datetime(2023, 1, 2, 3, 4, tzinfo=timezone.utc))
379-
async def test_creates_private_project_by_default(self, test_db, session: AsyncSession, client: AsyncClient):
385+
async def test_creates_private_project_by_default(
386+
self, test_db, session: AsyncSession, client: AsyncClient
387+
):
380388
user = await create_user(session=session)
381389
project_id = UUID("1b0e1b45-2f8c-4ab6-8010-a0d1a3e44e0e")
382390
project_name = "test_private_project"
383-
body = {"project_name": project_name} # No is_public specified
384-
with patch("uuid.uuid4") as m:
385-
m.return_value = project_id
391+
body = {"project_name": project_name}
392+
393+
with patch("uuid.uuid4", return_value=project_id):
386394
response = await client.post(
387395
"/api/projects/create",
388396
headers=get_auth_headers(user.token),
389397
json=body,
390398
)
391399
assert response.status_code == 200, response.json()
392-
400+
393401
# Check that the response includes is_public=False (default)
394402
response_data = response.json()
395403
assert "is_public" in response_data
396404
assert response_data["is_public"] is False
397-
405+
398406
# Verify the project was created as private in the database
399-
res = await session.execute(
400-
select(ProjectModel).where(ProjectModel.name == project_name)
401-
)
407+
res = await session.execute(select(ProjectModel).where(ProjectModel.name == project_name))
402408
project = res.scalar_one()
403409
assert project.is_public is False
404410

405411
@pytest.mark.asyncio
406412
@pytest.mark.parametrize("test_db", ["sqlite", "postgres"], indirect=True)
407413
@freeze_time(datetime(2023, 1, 2, 3, 4, tzinfo=timezone.utc))
408-
async def test_creates_private_project_explicitly(self, test_db, session: AsyncSession, client: AsyncClient):
414+
async def test_creates_private_project_explicitly(
415+
self, test_db, session: AsyncSession, client: AsyncClient
416+
):
409417
user = await create_user(session=session)
410418
project_id = UUID("1b0e1b45-2f8c-4ab6-8010-a0d1a3e44e0e")
411419
project_name = "test_explicit_private_project"
412-
body = {"project_name": project_name, "is_public": False} # Explicitly set to false
413-
with patch("uuid.uuid4") as m:
414-
m.return_value = project_id
420+
body = {"project_name": project_name, "is_public": False}
421+
422+
with patch("uuid.uuid4", return_value=project_id):
415423
response = await client.post(
416424
"/api/projects/create",
417425
headers=get_auth_headers(user.token),
418426
json=body,
419427
)
420428
assert response.status_code == 200, response.json()
421-
429+
422430
# Check that the response includes is_public=False (explicit)
423431
response_data = response.json()
424432
assert "is_public" in response_data
425433
assert response_data["is_public"] is False
426-
434+
427435
# Verify the project was created as private in the database
428-
res = await session.execute(
429-
select(ProjectModel).where(ProjectModel.name == project_name)
430-
)
436+
res = await session.execute(select(ProjectModel).where(ProjectModel.name == project_name))
431437
project = res.scalar_one()
432438
assert project.is_public is False
433439

0 commit comments

Comments
 (0)