Skip to content

Commit

Permalink
use ndb builtin lib
Browse files Browse the repository at this point in the history
  • Loading branch information
phil-lopreiato committed Nov 26, 2021
1 parent 58e9f6c commit 2aa6bfa
Show file tree
Hide file tree
Showing 396 changed files with 8,345 additions and 6,138 deletions.
4 changes: 2 additions & 2 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ exclude =
application_import_names = backend
import-order-style = edited
banned-modules =
google.appengine.api.datastore = Use google.cloud.datastore
google.appengine.ext.ndb = Use google.cloud.ndb
google.cloud.datastore = Use google.appengine.api.datastore
google.cloud.ndb = Use google.appengine.ext.ndb

[flake8:local-plugins]
extension =
Expand Down
3 changes: 0 additions & 3 deletions .pyre_configuration
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
{"site-package": "flask_cors"},
{"site-package": "freezegun"},
{"site-package": "google/appengine"},
{"site-package": "google/cloud/datastore_v1"},
{"site-package": "google/cloud/tasks_v2/"},
{"site-package": "google/cloud/storage"},
{"site-package": "InMemoryCloudDatastoreStub"},
{"site-package": "numpy"},
{"site-package": "pytest"},
{"site-package": "_pytest"},
Expand Down
2 changes: 1 addition & 1 deletion ops/shell/lib/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import contextlib
import os

from google.cloud import ndb
from google.appengine.ext import ndb


@contextlib.contextmanager
Expand Down
2 changes: 0 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ flake8-tidy-imports
blinker==1.4
fakeredis
freezegun==1.1.0
InMemoryCloudDatastoreStub
pre-commit
pyre-check==0.9.8
pytest
pytest-cov
pytest-custom_exit_code
pytest-testmon
PyYAML==6.0
redis==3.5.3
hiredis==2.0.0
requests-mock
shellcheck-py
61 changes: 29 additions & 32 deletions src/backend/api/handlers/helpers/tests/model_properties_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest

from google.cloud import ndb
from google.appengine.ext import ndb

from backend.api.handlers.helpers.model_properties import (
filter_event_properties,
Expand All @@ -23,16 +23,15 @@
from backend.common.queries.dict_converters.team_converter import TeamConverter


def test_filter_event_properties(ndb_client: ndb.Client) -> None:
with ndb_client.context():
event = EventConverter(
Event(
id="2020casj",
event_type_enum=EventType.REGIONAL,
event_short="casj",
year=2020,
)
).convert(ApiMajorVersion.API_V3)
def test_filter_event_properties(ndb_stub) -> None:
event = EventConverter(
Event(
id="2020casj",
event_type_enum=EventType.REGIONAL,
event_short="casj",
year=2020,
)
).convert(ApiMajorVersion.API_V3)

assert set(event.keys()).difference(set(simple_event_properties)) != set()

Expand All @@ -47,24 +46,23 @@ def test_filter_event_properties(ndb_client: ndb.Client) -> None:
filter_event_properties([event], ModelType("bad_type"))


def test_filter_match_properties(ndb_client: ndb.Client) -> None:
with ndb_client.context():
match = MatchConverter(
Match(
id="2020casj_qm1",
year=2020,
event=ndb.Key("Event", "2020casj"),
comp_level="qm",
match_number=1,
set_number=1,
alliances_json=json.dumps(
{
"red": {"score": 0, "teams": []},
"blue": {"score": 0, "teams": []},
}
),
)
).convert(ApiMajorVersion.API_V3)
def test_filter_match_properties(ndb_stub) -> None:
match = MatchConverter(
Match(
id="2020casj_qm1",
year=2020,
event=ndb.Key("Event", "2020casj"),
comp_level="qm",
match_number=1,
set_number=1,
alliances_json=json.dumps(
{
"red": {"score": 0, "teams": []},
"blue": {"score": 0, "teams": []},
}
),
)
).convert(ApiMajorVersion.API_V3)

assert set(match.keys()).difference(set(simple_match_properties)) != set()

Expand All @@ -79,9 +77,8 @@ def test_filter_match_properties(ndb_client: ndb.Client) -> None:
filter_match_properties([match], ModelType("bad_type"))


def test_filter_team_properties(ndb_client: ndb.Client) -> None:
with ndb_client.context():
team = TeamConverter(Team(id="frc604")).convert(ApiMajorVersion.API_V3)
def test_filter_team_properties(ndb_stub) -> None:
team = TeamConverter(Team(id="frc604")).convert(ApiMajorVersion.API_V3)

assert set(team.keys()).difference(set(simple_team_properties)) != set()

Expand Down
54 changes: 24 additions & 30 deletions src/backend/api/handlers/tests/add_match_video_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
from typing import Dict, List

from google.cloud import ndb
from google.appengine.ext import ndb
from werkzeug.test import Client

from backend.api.trusted_api_auth_helper import TrustedApiAuthHelper
Expand Down Expand Up @@ -68,19 +68,17 @@ def get_auth_headers(request_path: str, request_body) -> Dict[str, str]:
}


def test_no_auth(ndb_client: ndb.Client, api_client: Client) -> None:
with ndb_client.context():
setup_event()
def test_no_auth(ndb_stub, api_client: Client) -> None:
setup_event()

resp = api_client.post(REQUEST_PATH, data=json.dumps({}))
assert resp.status_code == 401


def test_set_video(ndb_client: ndb.Client, api_client: Client, taskqueue_stub) -> None:
with ndb_client.context():
setup_event()
setup_auth(access_types=[AuthType.MATCH_VIDEO])
setup_matches()
def test_set_video(ndb_stub, api_client: Client, taskqueue_stub) -> None:
setup_event()
setup_auth(access_types=[AuthType.MATCH_VIDEO])
setup_matches()

request_body = json.dumps({"qm1": "aFZy8iibMD0", "sf1m1": "RpSgUrsghv4"})

Expand All @@ -91,22 +89,19 @@ def test_set_video(ndb_client: ndb.Client, api_client: Client, taskqueue_stub) -
)
assert response.status_code == 200

with ndb_client.context():
assert set(Match.get_by_id("2014casj_qm1").youtube_videos) == {
"abcdef",
"aFZy8iibMD0",
}
assert set(Match.get_by_id("2014casj_sf1m1").youtube_videos) == {"RpSgUrsghv4"}
assert set(Match.get_by_id("2014casj_qm1").youtube_videos) == {
"abcdef",
"aFZy8iibMD0",
}
assert set(Match.get_by_id("2014casj_sf1m1").youtube_videos) == {"RpSgUrsghv4"}


def test_bad_match_id(ndb_client: ndb.Client, api_client: Client) -> None:
with ndb_client.context():
setup_event()
setup_auth(access_types=[AuthType.MATCH_VIDEO])
setup_matches()
def test_bad_match_id(ndb_stub, api_client: Client) -> None:
setup_event()
setup_auth(access_types=[AuthType.MATCH_VIDEO])
setup_matches()

request_body = json.dumps({"qm1": "aFZy8iibMD0", "qm2": "abc123"})

response = api_client.post(
REQUEST_PATH,
headers=get_auth_headers(REQUEST_PATH, request_body),
Expand All @@ -115,15 +110,15 @@ def test_bad_match_id(ndb_client: ndb.Client, api_client: Client) -> None:
assert response.status_code == 404

# make sure the valid match is unchnaged
with ndb_client.context():
assert set(Match.get_by_id("2014casj_qm1").youtube_videos) == {"abcdef"}
assert set(Match.get_by_id("2014casj_qm1", use_cache=False).youtube_videos) == {
"abcdef"
}


def test_malformed_match_id(ndb_client: ndb.Client, api_client: Client) -> None:
with ndb_client.context():
setup_event()
setup_auth(access_types=[AuthType.MATCH_VIDEO])
setup_matches()
def test_malformed_match_id(ndb_stub, api_client: Client) -> None:
setup_event()
setup_auth(access_types=[AuthType.MATCH_VIDEO])
setup_matches()

request_body = json.dumps({"qm1": "aFZy8iibMD0", "zzz": "abc123"})

Expand All @@ -136,5 +131,4 @@ def test_malformed_match_id(ndb_client: ndb.Client, api_client: Client) -> None:
assert response.json["Error"] == "Invalid match IDs provided: ['zzz']"

# make sure the valid match is unchnaged
with ndb_client.context():
assert set(Match.get_by_id("2014casj_qm1").youtube_videos) == {"abcdef"}
assert set(Match.get_by_id("2014casj_qm1").youtube_videos) == {"abcdef"}

0 comments on commit 2aa6bfa

Please sign in to comment.