Skip to content

Commit

Permalink
2050 weights table remove settings (#665)
Browse files Browse the repository at this point in the history
* feat: db management of weights

* feat: source new weights across app

* fix: loading of weights from model

* chore: update tests for db weights

* feat: finish adjusting tests for weights db changes

* chore: adjust deafult thershold logic

* chore: remove duplicate fixture

* chore: unused var
  • Loading branch information
tim-schultz authored Aug 22, 2024
1 parent ce9d6be commit a492689
Show file tree
Hide file tree
Showing 37 changed files with 674 additions and 169 deletions.
12 changes: 12 additions & 0 deletions api/account/test/conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
"""
This module contains pytest fixtures and configuration for the account API tests.
It sets up common test data, mocks, and other utilities used across multiple test files
in the account API test suite.
"""

# pylint: disable=unused-import
import pytest

from registry.weight_models import WeightConfiguration, WeightConfigurationItem
from scorer.settings.gitcoin_passport_weights import GITCOIN_PASSPORT_WEIGHTS
from scorer.test.conftest import (
access_token,
scorer_account,
scorer_community,
scorer_user,
weight_config,
)
19 changes: 18 additions & 1 deletion api/account/test/test_community.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
from typing import cast

import pytest
from account.models import Account, Community
from django.contrib.auth import get_user_model
from django.contrib.auth.models import UserManager
from django.db.utils import IntegrityError
from django.test import Client, TestCase
from ninja_jwt.schema import RefreshToken

from account.models import Account, Community
from registry.weight_models import WeightConfiguration, WeightConfigurationItem
from scorer.settings.gitcoin_passport_weights import GITCOIN_PASSPORT_WEIGHTS

# Avoids type issues in standard django models
user_manager = cast(UserManager, get_user_model().objects)

Expand Down Expand Up @@ -40,6 +43,20 @@ def setUp(self):
user=self.user2, defaults={"address": "0x0"}
)

config = WeightConfiguration.objects.create(
version="v1",
threshold=5.0,
active=True,
description="Test",
)

for provider, weight in GITCOIN_PASSPORT_WEIGHTS.items():
WeightConfigurationItem.objects.create(
weight_configuration=config,
provider=provider,
weight=float(weight),
)

def test_create_community(self):
"""Test that creation of a community works and that attributes are saved correctly"""
client = Client()
Expand Down
7 changes: 5 additions & 2 deletions api/account/test/test_scorer.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import json

import pytest
from account.models import Community
from django.conf import settings
from django.test import Client
from scorer_weighted.models import Scorer, get_default_threshold
from web3 import Web3

from account.models import Community
from registry.weight_models import WeightConfiguration, WeightConfigurationItem
from scorer.settings.gitcoin_passport_weights import GITCOIN_PASSPORT_WEIGHTS
from scorer_weighted.models import Scorer, get_default_threshold

web3 = Web3()
web3.eth.account.enable_unaudited_hdwallet_features()

Expand Down
1 change: 1 addition & 0 deletions api/aws_lambdas/passport/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
scorer_community,
scorer_community_with_binary_scorer,
scorer_user,
weight_config,
)
4 changes: 4 additions & 0 deletions api/aws_lambdas/scorer_api_passport/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import pytest

from registry.weight_models import WeightConfiguration, WeightConfigurationItem
from scorer.settings.gitcoin_passport_weights import GITCOIN_PASSPORT_WEIGHTS
from scorer.test.conftest import (
passport_holder_addresses,
scorer_account,
scorer_api_key,
scorer_community_with_binary_scorer,
scorer_user,
weight_config,
)

from .helpers import address
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import json

import pytest
from django.conf import settings

from aws_lambdas.scorer_api_passport.v1.stamps import bulk_DELETE, bulk_PATCH, bulk_POST
from ceramic_cache.models import CeramicCache
from django.conf import settings

from .helpers import MockContext, address, good_stamp, headers

Expand Down Expand Up @@ -39,7 +40,7 @@ def test_patch(

assert response["statusCode"] == 200
assert body["stamps"][0]["provider"] == "Google"
assert int(body["score"]["evidence"]["rawScore"]) > 0
assert body["score"]["evidence"]["rawScore"] > 0
assert body["score"]["status"] == "DONE"
assert body["success"] is True

Expand Down Expand Up @@ -72,7 +73,7 @@ def test_delete(

assert response["statusCode"] == 200
assert len(body["stamps"]) == 0
assert int(body["score"]["evidence"]["rawScore"]) == 0
assert body["score"]["evidence"]["rawScore"] == 0
assert body["score"]["status"] == "DONE"
assert body["success"] is True

Expand Down Expand Up @@ -105,6 +106,6 @@ def test_post(

assert response["statusCode"] == 200
assert body["stamps"][0]["provider"] == "Google"
assert int(body["score"]["evidence"]["rawScore"]) > 0
assert body["score"]["evidence"]["rawScore"] > 0
assert body["score"]["status"] == "DONE"
assert body["success"] is True
1 change: 1 addition & 0 deletions api/aws_lambdas/submit_passport/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
scorer_api_key,
scorer_community_with_binary_scorer,
scorer_user,
weight_config,
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
from copy import deepcopy

import pytest
from account.models import AccountAPIKeyAnalytics
from registry.test.test_passport_submission import mock_passport

from account.models import AccountAPIKeyAnalytics
from aws_lambdas.scorer_api_passport.tests.helpers import MockContext
from aws_lambdas.scorer_api_passport.utils import strip_event
from registry.test.test_passport_submission import mock_passport

from ..submit_passport import _handler

Expand Down Expand Up @@ -110,14 +110,15 @@ def test_successful_authentication(
assert body["address"] == address
assert body["score"] == "0"
assert body["status"] == "DONE"

assert body["evidence"] == {
"type": "ThresholdScoreCheck",
"success": False,
"rawScore": 2,
"threshold": 75.0,
"rawScore": 0.9329999999999999,
"threshold": 20.0,
}
assert body["error"] is None
assert body["stamp_scores"] == {"Ens": 1.0, "Google": 1.0}
assert body["stamp_scores"] == {"Ens": 0.408, "Google": 0.525}
# We just check that something != None was recorded for the last timestamp
assert body["last_score_timestamp"] is not None

Expand Down Expand Up @@ -158,11 +159,11 @@ def test_successful_authentication_and_base64_encoded_body(
assert body["evidence"] == {
"type": "ThresholdScoreCheck",
"success": False,
"rawScore": 2,
"threshold": 75.0,
"rawScore": 0.9329999999999999,
"threshold": 20.0,
}
assert body["error"] is None
assert body["stamp_scores"] == {"Ens": 1.0, "Google": 1.0}
assert body["stamp_scores"] == {"Ens": 0.408, "Google": 0.525}
# We just check that something != None was recorded for the last timestamp
assert body["last_score_timestamp"] is not None

Expand Down
5 changes: 3 additions & 2 deletions api/aws_lambdas/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from scorer.test.conftest import (
passport_holder_addresses,
scorer_account,
scorer_api_key,
scorer_community,
scorer_community_with_binary_scorer,
scorer_user,
scorer_api_key,
passport_holder_addresses,
weight_config,
)
1 change: 1 addition & 0 deletions api/ceramic_cache/test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
scorer_user, # noqa
ui_scorer, # noqa
verifiable_credential, # noqa
weight_config, # noqa
)


Expand Down
8 changes: 5 additions & 3 deletions api/ceramic_cache/test/test_cmd_scorer_dump_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@
from datetime import datetime

import pytest
from account.models import Community
from django.core.management import call_command

from account.models import Community
from registry.models import Passport, Score
from scorer_weighted.models import BinaryWeightedScorer, Scorer


class TestGetStamps:
@pytest.mark.django_db
def test_export_filtered_scored_for_scorer(self, scorer_account, mocker):
def test_export_filtered_scored_for_scorer(
self, scorer_account, mocker, weight_config
):
"""Make sure that it is not possible to have duplicate stamps in the DB"""

scorer = BinaryWeightedScorer.objects.create(type=Scorer.Type.WEIGHTED_BINARY)
Expand Down Expand Up @@ -71,7 +74,6 @@ def upload_file(self, file_name, *args, **kwargs):
"ceramic_cache.management.commands.scorer_dump_data.boto3.client",
return_value=MockS3(),
):

call_command(
"scorer_dump_data",
*[],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

class TestGetStamps:
@pytest.mark.django_db
def test_export_filtered_scored_for_scorer(self, scorer_account, mocker):
def test_export_filtered_scored_for_scorer(
self, scorer_account, mocker, weight_config
):
"""Make sure that it is not possible to have duplicate stamps in the DB"""

scorer = BinaryWeightedScorer.objects.create(type=Scorer.Type.WEIGHTED_BINARY)
Expand Down
11 changes: 6 additions & 5 deletions api/ceramic_cache/test/test_get_score.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from datetime import datetime, timezone, timedelta
from datetime import datetime, timedelta, timezone
from decimal import Decimal

import pytest
from ceramic_cache.models import CeramicCache
from registry.models import Passport, Score
from django.test import Client

from ceramic_cache.models import CeramicCache
from registry.api.schema import DetailedScoreResponse
from decimal import Decimal
from registry.models import Passport, Score

pytestmark = pytest.mark.django_db

Expand Down Expand Up @@ -44,7 +45,7 @@ def test_get_score_when_no_stamps(
"type": "ThresholdScoreCheck",
"success": False,
"rawScore": "0",
"threshold": "75.00000",
"threshold": "20.00000",
},
"error": None,
"stamp_scores": {},
Expand Down
23 changes: 19 additions & 4 deletions api/ceramic_cache/test/test_weights.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from account.models import Community
from scorer_weighted.models import Scorer, BinaryWeightedScorer
from django.test import Client
from django.conf import settings
import pytest
from django.conf import settings
from django.test import Client

from account.models import Community
from registry.weight_models import WeightConfiguration, WeightConfigurationItem
from scorer_weighted.models import BinaryWeightedScorer, Scorer

pytestmark = pytest.mark.django_db # noqa: F821

Expand All @@ -16,7 +18,20 @@ def test_get_weights(
self,
scorer_account,
):
config = WeightConfiguration.objects.create(
version="v1",
threshold=20.0,
active=True,
description="Test",
)
scorer_weights = {"provider-1": "0.5", "provider-2": "0.5"}
for provider, weight in scorer_weights.items():
WeightConfigurationItem.objects.create(
weight_configuration=config,
provider=provider,
weight=float(weight),
)

scorer = BinaryWeightedScorer.objects.create(
type=Scorer.Type.WEIGHTED_BINARY, weights=scorer_weights
)
Expand Down
15 changes: 8 additions & 7 deletions api/passport_admin/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from scorer.test.conftest import (
api_key,
sample_address,
sample_provider,
sample_token,
scorer_account,
scorer_user,
verifiable_credential,
api_key, # noqa
sample_address, # noqa
sample_provider, # noqa
sample_token, # noqa
scorer_account, # noqa
scorer_user, # noqa
verifiable_credential, # noqa
weight_config, # noqa
)
2 changes: 1 addition & 1 deletion api/passport_admin/tests/test_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def custom_notifications(current_date, sample_address, community):


@pytest.fixture
def community(scorer_account):
def community(scorer_account, weight_config):
scorer = BinaryWeightedScorer.objects.create(
type=Scorer.Type.WEIGHTED_BINARY, weights=scorer_weights
)
Expand Down
Loading

0 comments on commit a492689

Please sign in to comment.