Skip to content

Commit

Permalink
Fix deleting some others projects checks when deleting project
Browse files Browse the repository at this point in the history
  • Loading branch information
m.kindritskiy committed Dec 6, 2024
1 parent ed82c59 commit 760cbaa
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
4 changes: 2 additions & 2 deletions featureflags/graph/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,12 +557,12 @@ async def delete_project(

# Delete project values
await conn.execute(
Flag.__table__.delete().where(Flag.project == project_id)
Value.__table__.delete().where(Value.project == project_id)
)

# Delete project variables
variables = await conn.execute(
select([Variable.id]).where(Project.id == project_id)
select([Variable.id]).where(Variable.project == project_id)
)
variable_ids = [v.id for v in await variables.fetchall()]

Expand Down
70 changes: 70 additions & 0 deletions featureflags/tests/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
update_changelog,
update_value_changelog,
update_value_value_override,
delete_project,
)
from featureflags.graph.types import ValueAction, ValuesChanges
from featureflags.models import (
Expand All @@ -44,6 +45,7 @@
Value,
ValueChangelog,
ValueCondition,
Variable,
)
from featureflags.services import auth
from featureflags.services.auth import (
Expand Down Expand Up @@ -95,6 +97,19 @@ async def check_value_override(value, *, conn):
return await result.scalar()


async def fetch_val(stmt, *, conn):
result = await conn.execute(stmt)
return await result.scalar()


async def check_exists(stmt, resource_id, *, conn):
return await fetch_val(stmt, conn=conn) == resource_id


async def check_not_exists(stmt, *, conn):
return await fetch_val(stmt, conn=conn) is None


@pytest.mark.asyncio
async def test_sign_in_new(conn):
username = "[email protected]"
Expand Down Expand Up @@ -861,3 +876,58 @@ async def test_update_value_value_override(
)

assert await check_value_override(value.id, conn=conn) == value_override


@pytest.mark.asyncio
async def test_delete_project(conn, db_engine):
# setup project 1
project1 = await mk_project(db_engine)
flag1 = await mk_flag(db_engine, enabled=True, project=project1)
variable1 = await mk_variable(db_engine, project=project1)
check1 = await mk_check(db_engine, variable=variable1)
cond1 = await mk_condition(
db_engine, flag=flag1, checks=[check1.id], project=project1
)

# setup project 2
project2 = await mk_project(db_engine)
flag2 = await mk_flag(db_engine, enabled=True, project=project2)
variable2 = await mk_variable(db_engine, project=project2)
check2 = await mk_check(db_engine, variable=variable2)
cond2 = await mk_condition(
db_engine, flag=flag2, checks=[check2.id], project=project2
)

await delete_project(project1.id.hex, conn=conn)

# check deleted project's resources gone
assert await check_not_exists(
select([Flag.id]).where(Flag.id == flag1.id), conn=conn
)
assert await check_not_exists(
select([Condition.id]).where(Condition.id == cond1.id), conn=conn
)
assert await check_not_exists(
select([Check.id]).where(Check.id == check1.id), conn=conn
)
assert await check_not_exists(
select([Variable.id]).where(Variable.id == variable1.id), conn=conn
)

# check other project's resources not touched
assert await check_exists(
select([Flag.id]).where(Flag.id == flag2.id), flag2.id, conn=conn
)
assert await check_exists(
select([Condition.id]).where(Condition.id == cond2.id),
cond2.id,
conn=conn,
)
assert await check_exists(
select([Check.id]).where(Check.id == check2.id), check2.id, conn=conn
)
assert await check_exists(
select([Variable.id]).where(Variable.id == variable2.id),
variable2.id,
conn=conn,
)

0 comments on commit 760cbaa

Please sign in to comment.