def test_histogram_negative_values(self): # range is [-9, -4), so it is divided into 5 buckets of width 1 specs = [ (-9, -8, [("foo", 3)]), (-5, -4, [("foo", 1)]), ] self.populate_events(specs) for array_column in ARRAY_COLUMNS: alias = get_array_column_alias(array_column) query = { "project": [self.project.id], "field": [f"{alias}.foo"], "numBuckets": 5, } response = self.do_request(query) assert response.status_code == 200, f"failing for {array_column}" expected = [ (-9, -8, [(f"{alias}.foo", 3)]), (-8, -7, [(f"{alias}.foo", 0)]), (-7, -6, [(f"{alias}.foo", 0)]), (-6, -5, [(f"{alias}.foo", 0)]), (-5, -4, [(f"{alias}.foo", 1)]), ] assert response.data == self.as_response_data( expected), f"failing for {array_column}"
def test_histogram_missing_measurement_data_with_explicit_bounds(self): # make sure there is at least one transaction specs = [ (0, 1, [("foo", 1)]), ] self.populate_events(specs) for array_column in ARRAY_COLUMNS: alias = get_array_column_alias(array_column) query = { "project": [self.project.id], # make sure to query a measurement that does not exist "field": [f"{alias}.bar"], "numBuckets": 5, "dataFilter": "exclude_outliers", "min": 10, } response = self.do_request(query) assert response.status_code == 200, f"failing for {array_column}" expected = [ (10, 11, [(f"{alias}.bar", 0)]), (11, 11, [(f"{alias}.bar", 0)]), (12, 12, [(f"{alias}.bar", 0)]), (13, 13, [(f"{alias}.bar", 0)]), (14, 14, [(f"{alias}.bar", 0)]), ] assert response.data == self.as_response_data( expected), f"failing for {array_column}"
def test_histogram_increased_precision_large_buckets(self): # range is [10.0000, 59.9999] so it is divided into 5 buckets of width 10 specs = [ (10.0000, 10.0000, [("foo", 1)]), (30.0000, 40.0000, [("foo", 1)]), (59.9999, 59.9999, [("foo", 2)]), ] self.populate_events(specs) for array_column in ARRAY_COLUMNS: alias = get_array_column_alias(array_column) query = { "project": [self.project.id], "field": [f"{alias}.foo"], "numBuckets": 5, "precision": 4, } response = self.do_request(query) assert response.status_code == 200, f"failing for {array_column}" expected = [ (10.0000, 20.0000, [(f"{alias}.foo", 1)]), (20.0000, 30.0000, [(f"{alias}.foo", 0)]), (30.0000, 40.0000, [(f"{alias}.foo", 1)]), (40.0000, 50.0000, [(f"{alias}.foo", 0)]), (50.0000, 60.0000, [(f"{alias}.foo", 2)]), ] assert response.data == self.as_response_data( expected), f"failing for {array_column}"
def test_histogram_extra_data(self): # range is [11, 16), so it is divided into 5 buckets of width 1 # make sure every bin has some value specs = [ (10, 11, [("foo", 1)]), (11, 12, [("foo", 1)]), (12, 13, [("foo", 1)]), (13, 14, [("foo", 1)]), (14, 15, [("foo", 1)]), (15, 16, [("foo", 1)]), (16, 17, [("foo", 1)]), ] self.populate_events(specs) for array_column in ARRAY_COLUMNS: alias = get_array_column_alias(array_column) query = { "project": [self.project.id], "field": [f"{alias}.foo"], "numBuckets": 5, "min": 11, "max": 16, } response = self.do_request(query) assert response.status_code == 200, f"failing for {array_column}" expected = [ (11, 12, [(f"{alias}.foo", 1)]), (12, 13, [(f"{alias}.foo", 1)]), (13, 14, [(f"{alias}.foo", 1)]), (14, 15, [(f"{alias}.foo", 1)]), (15, 16, [(f"{alias}.foo", 1)]), ] assert response.data == self.as_response_data( expected), f"failing for {array_column}"
def test_histogram_increased_precision_with_min_max(self): # range is [1.25, 2.24], so it is divided into 5 buckets of width 0.25 specs = [ (1.00, 1.25, [("foo", 3)]), (2.00, 2.25, [("foo", 1)]), ] self.populate_events(specs) for array_column in ARRAY_COLUMNS: alias = get_array_column_alias(array_column) query = { "project": [self.project.id], "field": [f"{alias}.foo"], "numBuckets": 3, "precision": 2, "min": 1.25, "max": 2.00, } response = self.do_request(query) assert response.status_code == 200, f"failing for {array_column}" expected = [ (1.25, 1.50, [(f"{alias}.foo", 0)]), (1.50, 1.75, [(f"{alias}.foo", 0)]), (1.75, 2.00, [(f"{alias}.foo", 0)]), ] assert response.data == self.as_response_data( expected), f"failing for {array_column}"
def test_histogram_large_buckets(self): # make sure that it works for large width buckets # range is [0, 99], so it is divided into 5 buckets of width 20 specs = [ (0, 0, [("foo", 2)]), (99, 99, [("foo", 2)]), ] self.populate_events(specs) for array_column in ARRAY_COLUMNS: alias = get_array_column_alias(array_column) query = { "project": [self.project.id], "field": [f"{alias}.foo"], "numBuckets": 5, } response = self.do_request(query) assert response.status_code == 200, f"failing for {array_column}" expected = [ (0, 20, [(f"{alias}.foo", 2)]), (20, 40, [(f"{alias}.foo", 0)]), (40, 60, [(f"{alias}.foo", 0)]), (60, 80, [(f"{alias}.foo", 0)]), (80, 100, [(f"{alias}.foo", 2)]), ] assert response.data == self.as_response_data( expected), f"failing for {array_column}"
def test_histogram_non_zero_offset(self): # range is [10, 15), so it is divided into 5 buckets of width 1 specs = [ (10, 11, [("foo", 1)]), (12, 13, [("foo", 1)]), (13, 14, [("foo", 1)]), (14, 15, [("foo", 1)]), ] self.populate_events(specs) for array_column in ARRAY_COLUMNS: alias = get_array_column_alias(array_column) query = { "project": [self.project.id], "field": [f"{alias}.foo"], "numBuckets": 5, } response = self.do_request(query) assert response.status_code == 200, f"failing for {array_column}" expected = [ (10, 11, [(f"{alias}.foo", 1)]), (11, 12, [(f"{alias}.foo", 0)]), (12, 13, [(f"{alias}.foo", 1)]), (13, 14, [(f"{alias}.foo", 1)]), (14, 15, [(f"{alias}.foo", 1)]), ] assert response.data == self.as_response_data( expected), f"failing for {array_column}"
def test_histogram_simple_using_given_max_below_queried_min(self): # All these events are out of range of the query parameters, # and should not appear in the results. specs = [ (6, 7, [("foo", 1)]), (8, 9, [("foo", 1)]), (10, 11, [("foo", 1)]), (12, 13, [("foo", 1)]), ] self.populate_events(specs) for array_column in ARRAY_COLUMNS: alias = get_array_column_alias(array_column) query = { "project": [self.project.id], "field": [f"{alias}.foo"], "numBuckets": 5, "max": 6, } response = self.do_request(query) assert response.status_code == 200, f"failing for {array_column}" expected = [ (5, 6, [(f"{alias}.foo", 0)]), ] assert response.data == self.as_response_data( expected), f"failing for {array_column}"
def test_histogram_simple_using_min_max(self): # range is [0, 5), so it is divided into 5 buckets of width 1 specs = [ (0, 1, [("foo", 1)]), (1, 2, [("foo", 1)]), (2, 3, [("foo", 1)]), (4, 5, [("foo", 1)]), ] self.populate_events(specs) for array_column in ARRAY_COLUMNS: alias = get_array_column_alias(array_column) query = { "project": [self.project.id], "field": [f"{alias}.foo"], "numBuckets": 5, "min": 0, "max": 5, } response = self.do_request(query) assert response.status_code == 200, f"failing for {array_column}" expected = [ (0, 1, [(f"{alias}.foo", 1)]), (1, 2, [(f"{alias}.foo", 1)]), (2, 3, [(f"{alias}.foo", 1)]), (3, 4, [(f"{alias}.foo", 0)]), (4, 5, [(f"{alias}.foo", 1)]), ] assert response.data == self.as_response_data( expected), f"failing for {array_column}"
def test_histogram_all_data_filter(self): specs = [ (0, 1, [("foo", 4)]), (4000, 5000, [("foo", 1)]), ] self.populate_events(specs) for array_column in ARRAY_COLUMNS: alias = get_array_column_alias(array_column) query = { "project": [self.project.id], "field": [f"{alias}.foo"], "numBuckets": 5, "dataFilter": "all", } response = self.do_request(query) assert response.status_code == 200, f"failing for {array_column}" expected = [ (0, 1000, [(f"{alias}.foo", 4)]), (1000, 2000, [(f"{alias}.foo", 0)]), (2000, 3000, [(f"{alias}.foo", 0)]), (3000, 4000, [(f"{alias}.foo", 0)]), (4000, 5000, [(f"{alias}.foo", 1)]), ] assert response.data == self.as_response_data( expected), f"failing for {array_column}"
def test_histogram_positive_and_negative_values(self): # range is [-50, 49], so it is divided into 5 buckets of width 10 specs = [ (-50, -50, [("foo", 1)]), (-10, 10, [("foo", 2)]), (49, 49, [("foo", 1)]), ] self.populate_events(specs) for array_column in ARRAY_COLUMNS: alias = get_array_column_alias(array_column) query = { "project": [self.project.id], "field": [f"{alias}.foo"], "numBuckets": 5, } response = self.do_request(query) assert response.status_code == 200, f"failing for {array_column}" expected = [ (-50, -30, [(f"{alias}.foo", 1)]), (-30, -10, [(f"{alias}.foo", 0)]), (-10, 10, [(f"{alias}.foo", 2)]), (10, 30, [(f"{alias}.foo", 0)]), (30, 50, [(f"{alias}.foo", 1)]), ] assert response.data == self.as_response_data( expected), f"failing for {array_column}"
def test_good_params(self): for array_column in ARRAY_COLUMNS: alias = get_array_column_alias(array_column) query = { "query": "event.type:transaction", "project": [self.project.id], "field": [f"{alias}.foo", f"{alias}.bar"], "numBuckets": 10, } response = self.do_request(query) assert response.status_code == 200, f"failing for {array_column}"
def test_bad_params_invalid_num_buckets(self): for array_column in ARRAY_COLUMNS: alias = get_array_column_alias(array_column) query = { "project": [self.project.id], "field": [f"{alias}.foo", f"{alias}.bar"], "numBuckets": "baz", } response = self.do_request(query) assert response.status_code == 400, f"failing for {array_column}" assert response.data == { "numBuckets": ["A valid integer is required."], }, f"failing for {array_column}"
def test_histogram_max_value_on_edge(self): # range is [11, 21] so it is divided into 5 buckets of width 5 # because using buckets of width 2 will exclude 21, and the next # nice number is 5 specs = [ (11, 11, [("bar", 0), ("baz", 0), ("foo", 1)]), (21, 21, [("bar", 1), ("baz", 1), ("foo", 1)]), ] self.populate_events(specs) for array_column in ARRAY_COLUMNS: alias = get_array_column_alias(array_column) query = { "project": [self.project.id], "field": [f"{alias}.bar", f"{alias}.baz", f"{alias}.foo"], "numBuckets": 5, } response = self.do_request(query) assert response.status_code == 200, f"failing for {array_column}" expected = [ ( 10, 15, [ (f"{alias}.bar", 0), (f"{alias}.baz", 0), (f"{alias}.foo", 1), ], ), ( 15, 20, [ (f"{alias}.bar", 0), (f"{alias}.baz", 0), (f"{alias}.foo", 0), ], ), ( 20, 25, [ (f"{alias}.bar", 1), (f"{alias}.baz", 1), (f"{alias}.foo", 1), ], ), ] assert response.data == self.as_response_data( expected), f"failing for {array_column}"
def test_bad_params_num_buckets_too_large(self): for array_column in ARRAY_COLUMNS: alias = get_array_column_alias(array_column) query = { "project": [self.project.id], "field": [f"{alias}.foo", f"{alias}.bar"], "numBuckets": 150, } response = self.do_request(query) assert response.status_code == 400, f"failing for {array_column}" assert response.data == { "numBuckets": ["Ensure this value is less than or equal to 100."], }, f"failing for {array_column}"
def test_histogram_bins_exceed_max(self): specs = [ (10, 15, [("bar", 0), ("baz", 0), ("foo", 1)]), (30, 30, [("bar", 1), ("baz", 1), ("foo", 1)]), ] self.populate_events(specs) for array_column in ARRAY_COLUMNS: alias = get_array_column_alias(array_column) query = { "project": [self.project.id], "field": [f"{alias}.bar", f"{alias}.baz", f"{alias}.foo"], "numBuckets": 5, "min": 10, "max": 21, } response = self.do_request(query) assert response.status_code == 200, f"failing for {array_column}" expected = [ ( 10, 15, [ (f"{alias}.bar", 0), (f"{alias}.baz", 0), (f"{alias}.foo", 1), ], ), ( 15, 20, [ (f"{alias}.bar", 0), (f"{alias}.baz", 0), (f"{alias}.foo", 0), ], ), ( 20, 25, [ (f"{alias}.bar", 0), (f"{alias}.baz", 0), (f"{alias}.foo", 0), ], ), ] assert response.data == self.as_response_data( expected), f"failing for {array_column}"
def test_histogram_empty(self): for array_column in ARRAY_COLUMNS: alias = get_array_column_alias(array_column) query = { "project": [self.project.id], "field": [f"{alias}.foo", f"{alias}.bar"], "numBuckets": 5, } response = self.do_request(query) assert response.status_code == 200, f"failing for {array_column}" expected = [(i, i + 1, [(f"{alias}.foo", 0), (f"{alias}.bar", 0)]) for i in range(5)] assert response.data == self.as_response_data( expected), f"failing for {array_column}"
def test_bad_params_invalid_data_filter(self): for array_column in ARRAY_COLUMNS: alias = get_array_column_alias(array_column) query = { "project": [self.project.id], "field": [f"{alias}.foo", f"{alias}.bar"], "numBuckets": 10, "dataFilter": "invalid", } response = self.do_request(query) assert response.status_code == 400, f"failing for {array_column}" assert response.data == { "dataFilter": ['"invalid" is not a valid choice.'], }, f"failing for {array_column}"
def test_bad_params_invalid_precision_too_small(self): for array_column in ARRAY_COLUMNS: alias = get_array_column_alias(array_column) query = { "project": [self.project.id], "field": [f"{alias}.foo", f"{alias}.bar"], "numBuckets": 10, "precision": -1, } response = self.do_request(query) assert response.status_code == 400, f"failing for {array_column}" assert response.data == { "precision": ["Ensure this value is greater than or equal to 0."], }, f"failing for {array_column}"
def test_bad_params_reverse_min_max(self): for array_column in ARRAY_COLUMNS: alias = get_array_column_alias(array_column) query = { "query": "event.type:transaction", "project": [self.project.id], "field": [f"{alias}.foo", f"{alias}.bar"], "numBuckets": 10, "precision": 0, "min": 10, "max": 5, } response = self.do_request(query) assert response.data == { "non_field_errors": ["min cannot be greater than max."] }
def test_histogram_multiple_measures(self): # range is [10, 59] so it is divided into 5 buckets of width 10 specs = [ (10, 10, [("bar", 0), ("baz", 0), ("foo", 1)]), (30, 40, [("bar", 2), ("baz", 0), ("foo", 0)]), (59, 59, [("bar", 0), ("baz", 1), ("foo", 0)]), ] self.populate_events(specs) for array_column in ARRAY_COLUMNS: alias = get_array_column_alias(array_column) query = { "project": [self.project.id], "field": [f"{alias}.bar", f"{alias}.baz", f"{alias}.foo"], "numBuckets": 5, } response = self.do_request(query) assert response.status_code == 200, f"failing for {array_column}" expected = [ ( 10, 20, [ (f"{alias}.bar", 0), (f"{alias}.baz", 0), (f"{alias}.foo", 1), ], ), ( 20, 30, [ (f"{alias}.bar", 0), (f"{alias}.baz", 0), (f"{alias}.foo", 0), ], ), ( 30, 40, [ (f"{alias}.bar", 2), (f"{alias}.baz", 0), (f"{alias}.foo", 0), ], ), ( 40, 50, [ (f"{alias}.bar", 0), (f"{alias}.baz", 0), (f"{alias}.foo", 0), ], ), ( 50, 60, [ (f"{alias}.bar", 0), (f"{alias}.baz", 1), (f"{alias}.foo", 0), ], ), ] assert response.data == self.as_response_data( expected), f"failing for {array_column}"