def test_order_by(self): """Test that order_by returns properly sorted data.""" # Do not need to fill db url = "?filter[time_scope_units]=month&filter[time_scope_value]=-1&filter[resolution]=monthly" query_params = self.mocked_query_params(url, OCPAzureCostView) handler = OCPAzureReportQueryHandler(query_params) unordered_data = [ {"date": self.dh.today, "delta_percent": 8, "total": 6.2, "rank": 2}, {"date": self.dh.yesterday, "delta_percent": 4, "total": 2.2, "rank": 1}, {"date": self.dh.today, "delta_percent": 7, "total": 8.2, "rank": 1}, {"date": self.dh.yesterday, "delta_percent": 4, "total": 2.2, "rank": 2}, ] order_fields = ["date", "rank"] expected = [ {"date": self.dh.yesterday, "delta_percent": 4, "total": 2.2, "rank": 1}, {"date": self.dh.yesterday, "delta_percent": 4, "total": 2.2, "rank": 2}, {"date": self.dh.today, "delta_percent": 7, "total": 8.2, "rank": 1}, {"date": self.dh.today, "delta_percent": 8, "total": 6.2, "rank": 2}, ] ordered_data = handler.order_by(unordered_data, order_fields) self.assertEqual(ordered_data, expected) order_fields = ["date", "-delta"] expected = [ {"date": self.dh.yesterday, "delta_percent": 4, "total": 2.2, "rank": 1}, {"date": self.dh.yesterday, "delta_percent": 4, "total": 2.2, "rank": 2}, {"date": self.dh.today, "delta_percent": 8, "total": 6.2, "rank": 2}, {"date": self.dh.today, "delta_percent": 7, "total": 8.2, "rank": 1}, ] ordered_data = handler.order_by(unordered_data, order_fields) self.assertEqual(ordered_data, expected)
def test_order_by_null_values(self): """Test that order_by returns properly sorted data with null data.""" # Do not need to fill db url = "?filter[time_scope_units]=month&filter[time_scope_value]=-1&filter[resolution]=monthly" query_params = self.mocked_query_params(url, OCPAzureCostView) handler = OCPAzureReportQueryHandler(query_params) unordered_data = [ {"node": None, "cluster": "cluster-1"}, {"node": "alpha", "cluster": "cluster-2"}, {"node": "bravo", "cluster": "cluster-3"}, {"node": "oscar", "cluster": "cluster-4"}, ] order_fields = ["node"] expected = [ {"node": "alpha", "cluster": "cluster-2"}, {"node": "bravo", "cluster": "cluster-3"}, {"node": "no-node", "cluster": "cluster-1"}, {"node": "oscar", "cluster": "cluster-4"}, ] ordered_data = handler.order_by(unordered_data, order_fields) self.assertEqual(ordered_data, expected)