-
Notifications
You must be signed in to change notification settings - Fork 384
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Webdx Feature IDs datastore and endpoint (#4722)
* Complete api and datastore implementation * Address comments
- Loading branch information
Showing
7 changed files
with
210 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Copyright 2025 Google Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License") | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import logging | ||
from collections import OrderedDict | ||
|
||
from framework import basehandlers | ||
from internals.webdx_feature_models import WebdxFeatures | ||
|
||
MISSING_FEATURE_ID = 'N/A' | ||
TBD_FEATURE_ID = 'TBD' | ||
|
||
|
||
class WebdxFeatureAPI(basehandlers.APIHandler): | ||
"""The list of ordered webdx feature ID populates the "Web Feature ID" select field | ||
in the guide form""" | ||
|
||
def do_get(self, **kwargs): | ||
"""Returns an ordered dict with Webdx feature id as both keys and values.""" | ||
webdx_features = WebdxFeatures.get_webdx_feature_id_list() | ||
if not webdx_features: | ||
logging.error('Webdx feature id list is empty.') | ||
return {} | ||
|
||
feature_ids_dict = OrderedDict() | ||
# The first key, value pair is the id when features are missing from the list. | ||
feature_ids_dict[MISSING_FEATURE_ID] = [MISSING_FEATURE_ID, MISSING_FEATURE_ID] | ||
feature_ids_dict[TBD_FEATURE_ID] = [TBD_FEATURE_ID, TBD_FEATURE_ID] | ||
|
||
feature_list = webdx_features.feature_ids | ||
feature_list.sort() | ||
for id in feature_list: | ||
feature_ids_dict[id] = [id, id] | ||
|
||
return feature_ids_dict |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# Copyright 2025 Google Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License") | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import testing_config | ||
from collections import OrderedDict | ||
|
||
import flask | ||
|
||
from api.webdx_feature_api import WebdxFeatureAPI | ||
from internals.webdx_feature_models import WebdxFeatures | ||
|
||
test_app = flask.Flask(__name__) | ||
|
||
class WebdxFeatureAPITest(testing_config.CustomTestCase): | ||
|
||
def setUp(self): | ||
self.webdx_features = WebdxFeatures(feature_ids = ['zzz', 'aaa']) | ||
self.webdx_features.put() | ||
|
||
self.handler = WebdxFeatureAPI() | ||
self.request_path = '/api/v0/webdxfeatures' | ||
|
||
def tearDown(self): | ||
for entity in WebdxFeatures.query(): | ||
entity.key.delete() | ||
|
||
def test_do_get__success(self): | ||
testing_config.sign_out() | ||
|
||
with test_app.test_request_context(self.request_path): | ||
actual = self.handler.do_get() | ||
|
||
expected = OrderedDict( | ||
[ | ||
('N/A', ['N/A', 'N/A']), | ||
('TBD', ['TBD', 'TBD']), | ||
('aaa', ['aaa', 'aaa']), | ||
('zzz', ['zzz', 'zzz']), | ||
] | ||
) | ||
self.assertEqual(actual, expected) | ||
|
||
def test_do_get__empty_data(self): | ||
testing_config.sign_out() | ||
self.webdx_features.key.delete() | ||
|
||
with test_app.test_request_context(self.request_path): | ||
actual = self.handler.do_get() | ||
|
||
self.assertEqual(actual, {}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Copyright 2025 Google Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License") | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from google.cloud import ndb # type: ignore | ||
|
||
|
||
class WebdxFeatures(ndb.Model): | ||
"""A singleton model to store Webdx feature IDs""" | ||
feature_ids = ndb.StringProperty(repeated=True) | ||
|
||
@classmethod | ||
def get_webdx_feature_id_list(cls): | ||
fetch_results = cls.query().fetch(1) | ||
if not fetch_results: | ||
return None | ||
|
||
return fetch_results[0] | ||
|
||
@classmethod | ||
def store_webdx_feature_id_list(cls, new_list: list[str]): | ||
webdx_features = WebdxFeatures.get_webdx_feature_id_list() | ||
if not webdx_features: | ||
webdx_features = WebdxFeatures(feature_ids=new_list) | ||
else: | ||
webdx_features.feature_ids = new_list | ||
webdx_features.put() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Copyright 2025 Google Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License") | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import testing_config | ||
|
||
from internals.webdx_feature_models import WebdxFeatures | ||
|
||
|
||
class WebdxFeaturesTest(testing_config.CustomTestCase): | ||
def setUp(self): | ||
self.webdx = WebdxFeatures(feature_ids=['abc']) | ||
self.webdx.put() | ||
|
||
def tearDown(self): | ||
self.webdx.key.delete() | ||
|
||
def test_get_webdx_feature_id_list(self): | ||
result = WebdxFeatures.get_webdx_feature_id_list() | ||
|
||
self.assertIsNotNone(result) | ||
self.assertEqual(len(result.feature_ids), 1) | ||
self.assertEqual(result.feature_ids[0], 'abc') | ||
|
||
def test_store_webdx_feature_id_list__success(self): | ||
WebdxFeatures.store_webdx_feature_id_list(['foo']) | ||
|
||
result = WebdxFeatures.query().fetch() | ||
self.assertIsNotNone(result) | ||
self.assertEqual(len(result), 1) | ||
self.assertEqual(result[0].feature_ids[0], 'foo') | ||
|
||
def test_store_webdx_feature_id_list__success_from_empty(self): | ||
self.webdx.key.delete() | ||
|
||
WebdxFeatures.store_webdx_feature_id_list(['foo']) | ||
|
||
result = WebdxFeatures.query().fetch() | ||
self.assertIsNotNone(result) | ||
self.assertEqual(len(result), 1) | ||
self.assertEqual(result[0].feature_ids[0], 'foo') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters