Skip to content

Commit

Permalink
feat: rename new attribute in CeramicCache, fixing failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nutrina committed Jan 14, 2025
1 parent c365786 commit 22bad4a
Show file tree
Hide file tree
Showing 13 changed files with 58 additions and 88 deletions.
2 changes: 1 addition & 1 deletion api/aws_lambdas/scorer_api_passport/v1/stamps/bulk_POST.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def _handler(event, context):
payload = [CacheStampPayload(**p) for p in body]

return format_response(
handle_add_stamps(address, payload, CeramicCache.StampCreator.PASSPORT)
handle_add_stamps(address, payload, CeramicCache.SourceApp.PASSPORT)
)


Expand Down
6 changes: 3 additions & 3 deletions api/ceramic_cache/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ class CeramicCacheAdmin(ScorerModelAdmin):
"compose_db_save_status",
"compose_db_stream_id",
"proof_value",
"stamp_created_by",
"scorer_id",
"source_app",
"source_scorer_id",
"stamp",
)
list_filter = ("deleted_at", "compose_db_save_status", "stamp_created_by")
list_filter = ("deleted_at", "compose_db_save_status", "source_app")
search_fields = ("address__exact", "compose_db_stream_id__exact", "proof_value")
search_help_text = (
"This will perform a search by 'address' and 'compose_db_stream_id'"
Expand Down
10 changes: 5 additions & 5 deletions api/ceramic_cache/api/v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def cache_stamps(request, payload: List[CacheStampPayload]):
return handle_add_stamps(
address,
payload,
CeramicCache.StampCreator.PASSPORT,
CeramicCache.SourceApp.PASSPORT,
settings.CERAMIC_CACHE_SCORER_ID,
)

Expand All @@ -156,7 +156,7 @@ def cache_stamps(request, payload: List[CacheStampPayload]):
def handle_add_stamps_only(
address,
payload: List[CacheStampPayload],
stamp_creator: CeramicCache.StampCreator,
source_app: CeramicCache.SourceApp,
alternate_scorer_id: Optional[int] = None,
) -> GetStampResponse:
if len(payload) > settings.MAX_BULK_CACHE_SIZE:
Expand Down Expand Up @@ -184,8 +184,8 @@ def handle_add_stamps_only(
compose_db_save_status=CeramicCache.ComposeDBSaveStatus.PENDING,
issuance_date=p.stamp.get("issuanceDate", None),
expiration_date=p.stamp.get("expirationDate", None),
stamp_created_by=stamp_creator,
scorer_id=alternate_scorer_id,
source_app=source_app,
source_scorer_id=alternate_scorer_id,
)
for p in payload
]
Expand Down Expand Up @@ -216,7 +216,7 @@ def handle_add_stamps_only(
def handle_add_stamps(
address,
payload: List[CacheStampPayload],
stamp_creator: CeramicCache.StampCreator,
stamp_creator: CeramicCache.SourceApp,
alternate_scorer_id: Optional[int] = None,
) -> GetStampsWithScoreResponse:
stamps_response = handle_add_stamps_only(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 4.2.6 on 2025-01-10 15:00
# Generated by Django 4.2.6 on 2025-01-14 14:35

from django.db import migrations, models

Expand All @@ -15,25 +15,25 @@ class Migration(migrations.Migration):
operations = [
migrations.AddField(
model_name="ceramiccache",
name="scorer_id",
field=models.BigIntegerField(
name="source_app",
field=models.IntegerField(
blank=True,
choices=[(1, "Passport"), (2, "Embed")],
db_index=True,
help_text="This is field is only used to indicate for analytic purposes which scorer was targeted \n when claiming the users credential (when used from embed, it will indicate what scorer id was set in \n the embed component)",
help_text="Which entity created the stamp. At the moment there are 2 options: the 'Passport App' and 'Embed Widget'",
null=True,
verbose_name="Scorer ID",
verbose_name="Creating Enity",
),
),
migrations.AddField(
model_name="ceramiccache",
name="stamp_created_by",
field=models.IntegerField(
name="source_scorer_id",
field=models.BigIntegerField(
blank=True,
choices=[(1, "Passport"), (2, "Embed")],
db_index=True,
help_text="Which entity created the stamp. At the moment there are 2 options: the 'Passport App' and 'Embed Widget'",
help_text="This is field is only used to indicate for analytic purposes which scorer was targeted \n when claiming the users credential (when used from embed, it will indicate what scorer id was set in \n the embed component)",
null=True,
verbose_name="Creating Enity",
verbose_name="Scorer ID",
),
),
]
14 changes: 10 additions & 4 deletions api/ceramic_cache/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class StampType(IntEnum):
V1 = 1
V2 = 2

class StampCreator(models.IntegerChoices):
class SourceApp(models.IntegerChoices):
PASSPORT = 1
EMBED = 2

Expand Down Expand Up @@ -85,7 +85,10 @@ class ComposeDBSaveStatus(models.TextChoices):
null=True, db_index=True
) # stamp['expirationDate']

scorer_id = models.BigIntegerField(
################################################################################################
# Begin metadata fields, to determine the scorer that initiated the creation of a stamp
#################################################################################################
source_scorer_id = models.BigIntegerField(
verbose_name="Scorer ID",
help_text="""This is field is only used to indicate for analytic purposes which scorer was targeted
when claiming the users credential (when used from embed, it will indicate what scorer id was set in
Expand All @@ -94,14 +97,17 @@ class ComposeDBSaveStatus(models.TextChoices):
blank=True,
db_index=True,
)
stamp_created_by = models.IntegerField(
source_app = models.IntegerField(
verbose_name="Creating Enity",
help_text="""Which entity created the stamp. At the moment there are 2 options: the 'Passport App' and 'Embed Widget'""",
choices=StampCreator.choices,
choices=SourceApp.choices,
null=True,
blank=True,
db_index=True,
)
################################################################################################
# End metadata fields
#################################################################################################

class Meta:
unique_together = ["type", "address", "provider", "deleted_at"]
Expand Down
22 changes: 11 additions & 11 deletions api/ceramic_cache/test/test_bulk_updates_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def test_bulk_create(
"provider": provider,
"scorer_id": ui_scorer,
"stamp": stamp,
"stamp_created_by": CeramicCache.StampCreator.PASSPORT.value,
"source_app": CeramicCache.SourceApp.PASSPORT.value,
"type": 1,
}

Expand Down Expand Up @@ -219,9 +219,9 @@ def test_bulk_update(
"issuance_date": datetime.fromisoformat(stamp["issuanceDate"]),
"proof_value": stamp["proof"]["proofValue"],
"provider": provider,
"scorer_id": ui_scorer,
"source_scorer_id": ui_scorer,
"stamp": stamp,
"stamp_created_by": CeramicCache.StampCreator.PASSPORT.value,
"source_app": CeramicCache.SourceApp.PASSPORT.value,
"type": 1,
}

Expand Down Expand Up @@ -345,9 +345,9 @@ def test_bulk_patch_partial_update(
"issuance_date": datetime.fromisoformat(stamp["issuanceDate"]),
"proof_value": stamp["proof"]["proofValue"],
"provider": provider,
"scorer_id": ui_scorer,
"source_scorer_id": ui_scorer,
"stamp": stamp,
"stamp_created_by": CeramicCache.StampCreator.PASSPORT.value,
"source_app": CeramicCache.SourceApp.PASSPORT.value,
"type": 1,
}

Expand Down Expand Up @@ -378,9 +378,9 @@ def test_bulk_patch_partial_update(
"issuance_date": datetime.fromisoformat(stamp["issuanceDate"]),
"proof_value": stamp["proof"]["proofValue"],
"provider": provider,
"scorer_id": ui_scorer,
"source_scorer_id": ui_scorer,
"stamp": stamp,
"stamp_created_by": CeramicCache.StampCreator.PASSPORT.value,
"source_app": CeramicCache.SourceApp.PASSPORT.value,
"type": 1,
}

Expand Down Expand Up @@ -425,8 +425,8 @@ def test_successful_bulk_delete(
address=sample_address,
provider=stamp["credentialSubject"]["provider"],
stamp=stamp,
stamp_created_by=CeramicCache.StampCreator.PASSPORT.value,
scorer_id=ui_scorer,
source_app=CeramicCache.SourceApp.PASSPORT.value,
source_scorer_id=ui_scorer,
compose_db_save_status="pending",
expiration_date=datetime.fromisoformat(stamp["expirationDate"]),
issuance_date=datetime.fromisoformat(stamp["issuanceDate"]),
Expand Down Expand Up @@ -493,9 +493,9 @@ def test_successful_bulk_delete(
"issuance_date": datetime.fromisoformat(stamp["issuanceDate"]),
"proof_value": stamp["proof"]["proofValue"],
"provider": provider,
"scorer_id": ui_scorer,
"source_scorer_id": ui_scorer,
"stamp": stamp,
"stamp_created_by": CeramicCache.StampCreator.PASSPORT.value,
"source_app": CeramicCache.SourceApp.PASSPORT.value,
"type": 1,
}

Expand Down
2 changes: 1 addition & 1 deletion api/embed/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def handle_embed_add_stamps(

try:
added_stamps = handle_add_stamps_only(
address, add_stamps_payload, CeramicCache.StampCreator.EMBED, payload.scorer_id
address, add_stamps_payload, CeramicCache.SourceApp.EMBED, payload.scorer_id
)
user_account = Community.objects.get(id=payload.scorer_id).account
score = async_to_sync(handle_scoring)(address, payload.scorer_id, user_account)
Expand Down
4 changes: 2 additions & 2 deletions api/embed/test/test_api_stamps.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,8 @@ def test_submitted_stamps_are_saved_properly(self, _test_submit_valid_stamps):
"expiration_date": datetime.fromisoformat(m["expirationDate"]),
"issuance_date": datetime.fromisoformat(m["issuanceDate"]),
"proof_value": m["proof"]["proofValue"],
"stamp_created_by": CeramicCache.StampCreator.EMBED.value,
"scorer_id": self.community.id,
"source_app": CeramicCache.SourceApp.EMBED.value,
"source_scorer_id": self.community.id,
"stamp": m,
"type": 1,
}
Expand Down
4 changes: 2 additions & 2 deletions api/embed/test/test_lambda_stamps.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ def test_storing_stamps_and_score(
"expiration_date": datetime.fromisoformat(m["expirationDate"]),
"issuance_date": datetime.fromisoformat(m["issuanceDate"]),
"proof_value": m["proof"]["proofValue"],
"stamp_created_by": CeramicCache.StampCreator.EMBED.value,
"scorer_id": self.community.id,
"source_app": CeramicCache.SourceApp.EMBED.value,
"source_scorer_id": self.community.id,
"stamp": m,
"type": 1,
}
Expand Down
28 changes: 0 additions & 28 deletions api/registry/api/v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,34 +183,6 @@ async def a_submit_passport(
) from e


def sync_test_function():
account_api_keys = []
for api_key in AccountAPIKey.objects.all():
account_api_keys.append(api_key)

print("account_api_keys", account_api_keys)

users = []
for user in User.objects.all():
users.append(user)

print("user", users)


async def async_test_function():
account_api_keys = []
async for api_key in AccountAPIKey.objects.all():
account_api_keys.append(api_key)

print("account_api_keys", account_api_keys)

users = []
async for user in User.objects.all():
users.append(user)

print("user", users)


async def ahandle_submit_passport(
payload: SubmitPassportPayload, account: Account
) -> DetailedScoreResponse:
Expand Down
8 changes: 0 additions & 8 deletions api/registry/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,6 @@ def save_api_key_analytics(
log.error("Failed to save analytics. Error: '%s'", e, exc_info=True)


def score_passport_passport(community_id: int, address: str):
score_passport(community_id, address)


def score_registry_passport(community_id: int, address: str):
score_passport(community_id, address)


def score_passport(community_id: int, address: str):
passport = load_passport_record(community_id, address)

Expand Down
22 changes: 11 additions & 11 deletions api/registry/test/test_score_passport.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from registry.api.schema import StatusEnum
from registry.api.v1 import SubmitPassportPayload, a_submit_passport, get_score
from registry.models import Event, HashScorerLink, Passport, Score, Stamp
from registry.tasks import score_passport_passport, score_registry_passport
from registry.tasks import score_passport
from scorer_weighted.models import Scorer, WeightedScorer

User = get_user_model()
Expand Down Expand Up @@ -135,7 +135,7 @@ def setUp(self):

def test_no_passport(self):
with patch("registry.atasks.aget_passport", return_value=None):
score_passport_passport(self.community.pk, self.account.address)
score_passport(self.community.pk, self.account.address)

passport = Passport.objects.get(
address=self.account.address, community_id=self.community.pk
Expand Down Expand Up @@ -174,7 +174,7 @@ def __init__(self):
mock_request.GET = {}
mock_request.headers = {}

with patch("registry.api.v1.score_passport_passport", return_value=None):
with patch("registry.api.v1.ascore_passport", return_value=None):
async_to_sync(a_submit_passport)(
mock_request,
SubmitPassportPayload(
Expand All @@ -187,7 +187,7 @@ def __init__(self):
with patch(
"registry.atasks.validate_credential", side_effect=mock_validate
):
score_passport_passport(self.community.pk, address)
score_passport(self.community.pk, address)

Passport.objects.get(address=address, community_id=self.community.pk)

Expand Down Expand Up @@ -228,7 +228,7 @@ def test_cleaning_stale_stamps(self):
with patch(
"registry.atasks.validate_credential", side_effect=mock_validate
):
score_passport_passport(self.community.pk, self.account.address)
score_passport(self.community.pk, self.account.address)

my_stamps = Stamp.objects.filter(passport=passport)
assert len(my_stamps) == 3
Expand Down Expand Up @@ -263,8 +263,8 @@ def test_deduplication_of_scoring_tasks(self):
):
with patch("registry.tasks.log.info") as mock_log:
# Call score_passport_passport twice, but only one of them should actually execute the scoring calculation
score_passport_passport(self.community.pk, self.account.address)
score_passport_passport(self.community.pk, self.account.address)
score_passport(self.community.pk, self.account.address)
score_passport(self.community.pk, self.account.address)

expected_call = call(
"Passport no passport found for address='%s', community_id='%s' that has requires_calculation=True or None",
Expand Down Expand Up @@ -316,7 +316,7 @@ def test_lifo_duplicate_stamp_scoring(self):
"registry.atasks.aget_passport",
return_value=mock_passport_data,
):
score_registry_passport(self.community.pk, passport.address)
score_passport(self.community.pk, passport.address)

original_stamps = Stamp.objects.filter(passport=passport)
assert len(original_stamps) == len(mocked_non_duplicate_stamps["stamps"])
Expand Down Expand Up @@ -382,7 +382,7 @@ def test_score_expiration_time(self):
with patch(
"registry.atasks.validate_credential", side_effect=mock_validate
):
score_passport_passport(self.community.pk, self.account.address)
score_passport(self.community.pk, self.account.address)

score = Score.objects.get(passport=passport)

Expand Down Expand Up @@ -421,7 +421,7 @@ def test_score_expiration_time_when_all_stamps_expired(self):
with patch(
"registry.atasks.validate_credential", side_effect=mock_validate
):
score_passport_passport(self.community.pk, self.account.address)
score_passport(self.community.pk, self.account.address)

score = Score.objects.get(passport=passport)

Expand All @@ -445,7 +445,7 @@ def test_score_expiration_time_when_all_stamps_expired(self):
with patch(
"registry.atasks.validate_credential", side_effect=mock_validate
):
score_passport_passport(self.community.pk, self.account.address)
score_passport(self.community.pk, self.account.address)

score = Score.objects.get(passport=passport)

Expand Down
Loading

0 comments on commit 22bad4a

Please sign in to comment.