Skip to content

Commit

Permalink
re-enable disabled database mirrors if in current mirrors
Browse files Browse the repository at this point in the history
  • Loading branch information
elfkuzco committed Jun 10, 2024
1 parent a5b9259 commit 8040088
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 25 deletions.
13 changes: 11 additions & 2 deletions backend/src/mirrors_qa_backend/db/mirrors.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,12 @@ def update_mirrors(
# Create any mirror that doesn't exist on the database
for mirror_id, mirror in current_mirrors.items():
if mirror_id not in db_mirrors:
# Create the mirror as it doesn't exists on the database.
# Create the mirror as it doesn't exist on the database.
result.nb_mirrors_added += create_mirrors(session, [mirror])

# Disable any mirror in the database that doesn't exist on the current
# list of mirrors
# list of mirrors. However, if a mirror is diabled in the database and
# exists in the list, re-enable it
for db_mirror_id, db_mirror in db_mirrors.items():
if db_mirror_id not in current_mirrors:
logger.debug(
Expand All @@ -107,4 +108,12 @@ def update_mirrors(
db_mirror.enabled = False
session.add(db_mirror)
result.nb_mirrors_disabled += 1
elif not db_mirror.enabled: # re-enable mirror if it was disabled
logger.debug(
f"Re-enabling mirror: {db_mirror.id!r} for "
f"country: {db_mirror.country.name!r}"
)
db_mirror.enabled = True
session.add(db_mirror)
result.nb_mirrors_added += 1
return result
2 changes: 1 addition & 1 deletion backend/src/mirrors_qa_backend/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


class BaseModel(pydantic.BaseModel):
model_config = ConfigDict(use_enum_values=True)
model_config = ConfigDict(use_enum_values=True, from_attributes=True)


class Country(BaseModel):
Expand Down
56 changes: 34 additions & 22 deletions backend/tests/db/test_mirrors.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,6 @@


@pytest.fixture(scope="session")
def schema_mirror() -> schemas.Mirror:
return schemas.Mirror(
id="mirror-sites-in.mblibrary.info",
base_url="https://mirror-sites-in.mblibrary.info/mirror-sites/download.kiwix.org/",
enabled=True,
region=None,
asn=None,
score=None,
latitude=None,
longitude=None,
country_only=None,
region_only=None,
as_only=None,
other_countries=None,
country=schemas.Country(
code="in",
name="India",
),
)


@pytest.fixture
def db_mirror() -> models.Mirror:
mirror = models.Mirror(
id="mirror-sites-in.mblibrary.info",
Expand All @@ -50,6 +28,11 @@ def db_mirror() -> models.Mirror:
return mirror


@pytest.fixture(scope="session")
def schema_mirror(db_mirror: models.Mirror) -> schemas.Mirror:
return schemas.Mirror.model_validate(db_mirror)


@pytest.fixture(scope="session")
def new_schema_mirror() -> schemas.Mirror:
return schemas.Mirror(
Expand Down Expand Up @@ -124,3 +107,32 @@ def test_no_mirrors_added(
dbsession.add(db_mirror)
result = mirrors.update_mirrors(dbsession, [schema_mirror])
assert result.nb_mirrors_added == 0


def test_re_enable_existing_mirror(
dbsession: OrmSession,
):
# Create a mirror in the database with enabled set to False
db_mirror = models.Mirror(
id="mirrors.dotsrc.org",
base_url="https://mirrors.dotsrc.org/kiwix/",
enabled=False,
region=None,
asn=None,
score=None,
latitude=None,
longitude=None,
country_only=None,
region_only=None,
as_only=None,
other_countries=None,
)
db_mirror.country = models.Country(code="dk", name="Denmark")
dbsession.add(db_mirror)

# Update the status of the mirror
schema_mirror = schemas.Mirror.model_validate(db_mirror)
schema_mirror.enabled = True

result = mirrors.update_mirrors(dbsession, [schema_mirror])
assert result.nb_mirrors_added == 1

0 comments on commit 8040088

Please sign in to comment.