2020-12-23 20:38:06 +03:00
|
|
|
# Copyright 2020 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.
|
|
|
|
|
2021-03-20 03:21:54 +03:00
|
|
|
import testing_config # Must be imported first
|
2020-12-23 20:38:06 +03:00
|
|
|
|
|
|
|
import datetime
|
2022-01-11 19:20:40 +03:00
|
|
|
from unittest import mock
|
2020-12-23 20:38:06 +03:00
|
|
|
import flask
|
2022-10-20 00:08:42 +03:00
|
|
|
import werkzeug.exceptions
|
2020-12-23 20:38:06 +03:00
|
|
|
|
2021-05-07 02:37:30 +03:00
|
|
|
# from google.appengine.api import users
|
|
|
|
from framework import users
|
2022-10-18 23:55:29 +03:00
|
|
|
from framework import rediscache
|
2020-12-23 20:38:06 +03:00
|
|
|
|
2021-03-20 03:21:54 +03:00
|
|
|
from api import metricsdata
|
2022-08-13 01:44:04 +03:00
|
|
|
from internals import metrics_models
|
2020-12-23 20:38:06 +03:00
|
|
|
|
2021-09-23 23:01:43 +03:00
|
|
|
test_app = flask.Flask(__name__)
|
|
|
|
|
2020-12-23 20:38:06 +03:00
|
|
|
|
2021-06-29 05:05:04 +03:00
|
|
|
class MetricsFunctionTests(testing_config.CustomTestCase):
|
2020-12-23 20:38:06 +03:00
|
|
|
|
|
|
|
def setUp(self):
|
2022-08-13 01:44:04 +03:00
|
|
|
self.datapoint = metrics_models.StableInstance(
|
2020-12-23 20:38:06 +03:00
|
|
|
day_percentage=0.0123456789, date=datetime.date.today(),
|
|
|
|
bucket_id=1, property_name='prop')
|
|
|
|
|
|
|
|
def test_is_googler__anon(self):
|
|
|
|
testing_config.sign_out()
|
|
|
|
user = users.get_current_user()
|
2021-03-20 03:21:54 +03:00
|
|
|
self.assertFalse(metricsdata._is_googler(user))
|
2020-12-23 20:38:06 +03:00
|
|
|
|
|
|
|
def test_is_googler__nongoogler(self):
|
|
|
|
testing_config.sign_in('test@example.com', 111)
|
|
|
|
user = users.get_current_user()
|
2021-03-20 03:21:54 +03:00
|
|
|
self.assertFalse(metricsdata._is_googler(user))
|
2020-12-23 20:38:06 +03:00
|
|
|
|
|
|
|
def test_is_googler__googler(self):
|
|
|
|
testing_config.sign_in('test@google.com', 111)
|
|
|
|
user = users.get_current_user()
|
2021-03-20 03:21:54 +03:00
|
|
|
self.assertTrue(metricsdata._is_googler(user))
|
2020-12-23 20:38:06 +03:00
|
|
|
|
2022-08-16 06:11:54 +03:00
|
|
|
def test_datapoints_to_json_dicts__googler(self):
|
2020-12-23 20:38:06 +03:00
|
|
|
testing_config.sign_in('test@google.com', 111)
|
|
|
|
datapoints = [self.datapoint]
|
2022-08-16 06:11:54 +03:00
|
|
|
actual = metricsdata._datapoints_to_json_dicts(datapoints)
|
|
|
|
expected = [{
|
|
|
|
'bucket_id': 1,
|
|
|
|
'date': str(datetime.date.today()),
|
|
|
|
'day_percentage': 0.0123456789,
|
|
|
|
'property_name': 'prop',
|
|
|
|
}]
|
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
|
|
|
def test_datapoints_to_json_dicts__nongoogler(self):
|
|
|
|
testing_config.sign_in('test@example.com', 222)
|
2020-12-23 20:38:06 +03:00
|
|
|
datapoints = [self.datapoint]
|
2022-08-16 06:11:54 +03:00
|
|
|
actual = metricsdata._datapoints_to_json_dicts(datapoints)
|
|
|
|
expected = [{
|
|
|
|
'bucket_id': 1,
|
|
|
|
'date': str(datetime.date.today()),
|
|
|
|
'day_percentage': 0.01234568, # rounded
|
|
|
|
'property_name': 'prop',
|
|
|
|
}]
|
|
|
|
self.assertEqual(expected, actual)
|
2020-12-23 20:38:06 +03:00
|
|
|
|
|
|
|
|
2021-06-29 05:05:04 +03:00
|
|
|
class PopularityTimelineHandlerTests(testing_config.CustomTestCase):
|
2020-12-23 20:38:06 +03:00
|
|
|
|
|
|
|
def setUp(self):
|
2021-03-20 03:21:54 +03:00
|
|
|
self.handler = metricsdata.PopularityTimelineHandler()
|
2022-08-13 01:44:04 +03:00
|
|
|
self.datapoint = metrics_models.StableInstance(
|
2020-12-23 20:38:06 +03:00
|
|
|
day_percentage=0.0123456789, date=datetime.date.today(),
|
|
|
|
bucket_id=1, property_name='prop')
|
|
|
|
self.datapoint.put()
|
|
|
|
|
|
|
|
def tearDown(self):
|
2021-06-17 23:47:02 +03:00
|
|
|
self.datapoint.key.delete()
|
2020-12-23 20:38:06 +03:00
|
|
|
|
|
|
|
def test_make_query(self):
|
|
|
|
actual_query = self.handler.make_query(1)
|
2022-08-13 01:44:04 +03:00
|
|
|
self.assertEqual(actual_query.kind, metrics_models.StableInstance._get_kind())
|
2020-12-23 20:38:06 +03:00
|
|
|
|
2022-10-20 00:08:42 +03:00
|
|
|
@mock.patch('flask.abort')
|
|
|
|
def test_get_template_data__bad_bucket(self, mock_abort):
|
2020-12-23 20:38:06 +03:00
|
|
|
url = '/data/timeline/csspopularity?bucket_id=not-a-number'
|
2022-10-20 00:08:42 +03:00
|
|
|
mock_abort.side_effect = werkzeug.exceptions.BadRequest
|
|
|
|
|
2021-09-23 23:01:43 +03:00
|
|
|
with test_app.test_request_context(url):
|
2022-10-20 00:08:42 +03:00
|
|
|
with self.assertRaises(werkzeug.exceptions.BadRequest):
|
|
|
|
actual = self.handler.get_template_data()
|
|
|
|
mock_abort.assert_called_once_with(
|
|
|
|
400, description="Request parameter 'bucket_id' was not an int")
|
2020-12-23 20:38:06 +03:00
|
|
|
|
|
|
|
def test_get_template_data__normal(self):
|
|
|
|
testing_config.sign_out()
|
|
|
|
url = '/data/timeline/csspopularity?bucket_id=1'
|
2021-09-23 23:01:43 +03:00
|
|
|
with test_app.test_request_context(url):
|
2020-12-23 20:38:06 +03:00
|
|
|
actual_datapoints = self.handler.get_template_data()
|
|
|
|
self.assertEqual(1, len(actual_datapoints))
|
|
|
|
self.assertEqual(0.01234568, actual_datapoints[0]['day_percentage'])
|
|
|
|
|
|
|
|
|
2022-10-18 23:55:29 +03:00
|
|
|
class CSSPopularityHandlerTests(testing_config.CustomTestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.handler = metricsdata.CSSPopularityHandler()
|
|
|
|
# Set up StableInstance data.
|
|
|
|
self.datapoint = metrics_models.StableInstance(
|
|
|
|
day_percentage=0.0123456789, date=datetime.date.today(),
|
|
|
|
bucket_id=1, property_name='b prop')
|
|
|
|
self.datapoint.put()
|
|
|
|
# Set up CssPropertyHistogram data.
|
|
|
|
self.prop_1 = metrics_models.CssPropertyHistogram(
|
|
|
|
bucket_id=1, property_name='b prop')
|
|
|
|
self.prop_1.put()
|
|
|
|
self.prop_2 = metrics_models.CssPropertyHistogram(
|
|
|
|
bucket_id=2, property_name='a prop')
|
|
|
|
self.prop_2.put()
|
|
|
|
self.prop_3 = metrics_models.FeatureObserverHistogram(
|
|
|
|
bucket_id=3, property_name='b feat')
|
|
|
|
self.prop_3.put()
|
|
|
|
self.prop_4 = metrics_models.FeatureObserverHistogram(
|
|
|
|
bucket_id=4, property_name='a feat')
|
|
|
|
self.prop_4.put()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.datapoint.key.delete()
|
|
|
|
self.prop_1.key.delete()
|
|
|
|
self.prop_2.key.delete()
|
|
|
|
self.prop_3.key.delete()
|
|
|
|
self.prop_4.key.delete()
|
|
|
|
rediscache.flushall()
|
|
|
|
|
|
|
|
def test_get_top_num_cache_key(self):
|
2022-10-20 00:08:42 +03:00
|
|
|
actual = self.handler.get_top_num_cache_key(30)
|
2022-10-18 23:55:29 +03:00
|
|
|
self.assertEqual('metrics|css_popularity_30', actual)
|
|
|
|
|
|
|
|
def test_get_template_data(self):
|
|
|
|
url = '/data/csspopularity'
|
|
|
|
with test_app.test_request_context(url):
|
|
|
|
actual_datapoints = self.handler.get_template_data()
|
|
|
|
self.assertEqual(1, len(actual_datapoints))
|
|
|
|
self.assertEqual(0.01234568, actual_datapoints[0]['day_percentage'])
|
|
|
|
|
|
|
|
def test_get_template_data_from_cache(self):
|
|
|
|
url = '/data/csspopularity'
|
|
|
|
with test_app.test_request_context(url):
|
|
|
|
self.handler.get_template_data()
|
|
|
|
|
|
|
|
actual_datapoints = rediscache.get('metrics|css_popularity')
|
|
|
|
self.assertEqual(1, len(actual_datapoints))
|
|
|
|
self.assertEqual(0.0123456789, actual_datapoints[0].day_percentage)
|
|
|
|
|
|
|
|
def test_should_refresh(self):
|
|
|
|
url = '/data/csspopularity?'
|
|
|
|
with test_app.test_request_context(url):
|
|
|
|
self.assertEqual(False, self.handler.should_refresh())
|
|
|
|
|
|
|
|
def test_get_template_data_with_num(self):
|
|
|
|
self.assertEqual(None, rediscache.get('metrics|css_popularity_30'))
|
|
|
|
url = '/data/csspopularity?num=30'
|
|
|
|
with test_app.test_request_context(url):
|
|
|
|
self.handler.get_template_data()
|
|
|
|
|
|
|
|
actual_datapoints = rediscache.get('metrics|css_popularity_30')
|
|
|
|
self.assertEqual(1, len(actual_datapoints))
|
|
|
|
self.assertEqual(0.0123456789, actual_datapoints[0].day_percentage)
|
2020-12-23 20:38:06 +03:00
|
|
|
|
|
|
|
|
2021-06-29 05:05:04 +03:00
|
|
|
class FeatureBucketsHandlerTest(testing_config.CustomTestCase):
|
2020-12-23 20:38:06 +03:00
|
|
|
|
|
|
|
def setUp(self):
|
2021-03-20 03:21:54 +03:00
|
|
|
self.handler = metricsdata.FeatureBucketsHandler()
|
2022-08-13 01:44:04 +03:00
|
|
|
self.prop_1 = metrics_models.CssPropertyHistogram(
|
2020-12-23 20:38:06 +03:00
|
|
|
bucket_id=1, property_name='b prop')
|
|
|
|
self.prop_1.put()
|
2022-08-13 01:44:04 +03:00
|
|
|
self.prop_2 = metrics_models.CssPropertyHistogram(
|
2020-12-23 20:38:06 +03:00
|
|
|
bucket_id=2, property_name='a prop')
|
|
|
|
self.prop_2.put()
|
2022-08-13 01:44:04 +03:00
|
|
|
self.prop_3 = metrics_models.FeatureObserverHistogram(
|
2020-12-23 20:38:06 +03:00
|
|
|
bucket_id=3, property_name='b feat')
|
|
|
|
self.prop_3.put()
|
2022-08-13 01:44:04 +03:00
|
|
|
self.prop_4 = metrics_models.FeatureObserverHistogram(
|
2020-12-23 20:38:06 +03:00
|
|
|
bucket_id=4, property_name='a feat')
|
|
|
|
self.prop_4.put()
|
|
|
|
|
|
|
|
def tearDown(self):
|
2021-06-17 23:47:02 +03:00
|
|
|
self.prop_1.key.delete()
|
|
|
|
self.prop_2.key.delete()
|
|
|
|
self.prop_3.key.delete()
|
|
|
|
self.prop_4.key.delete()
|
2020-12-23 20:38:06 +03:00
|
|
|
|
|
|
|
def test_get_template_data__css(self):
|
2021-09-23 23:01:43 +03:00
|
|
|
with test_app.test_request_context('/data/blink/cssprops'):
|
2022-10-16 09:39:35 +03:00
|
|
|
actual_buckets = self.handler.get_template_data(prop_type='cssprops')
|
2020-12-23 20:38:06 +03:00
|
|
|
self.assertEqual(
|
|
|
|
[(2, 'a prop'), (1, 'b prop')],
|
|
|
|
actual_buckets)
|
|
|
|
|
|
|
|
def test_get_template_data__js(self):
|
2021-09-23 23:01:43 +03:00
|
|
|
with test_app.test_request_context('/data/blink/features'):
|
2022-10-16 09:39:35 +03:00
|
|
|
actual_buckets = self.handler.get_template_data(prop_type='features')
|
2020-12-23 20:38:06 +03:00
|
|
|
self.assertEqual(
|
|
|
|
[(4, 'a feat'), (3, 'b feat')],
|
2021-09-23 23:01:43 +03:00
|
|
|
actual_buckets)
|