def test_create_line_item_hash_string(self): """Test that a hash string is properly formatted.""" bill_id = self.processor._create_cost_entry_bill(self.row) cost_entry_id = self.processor._create_cost_entry(self.row, bill_id) product_id = self.processor._create_cost_entry_product(self.row) pricing_id = self.processor._create_cost_entry_pricing(self.row) reservation_id = self.processor._create_cost_entry_reservation( self.row) self.accessor.commit() self.processor._create_cost_entry_line_item(self.row, cost_entry_id, bill_id, product_id, pricing_id, reservation_id) line_item = None if self.processor.processed_report.line_items: line_item = self.processor.processed_report.line_items[-1] line_item = common_util.stringify_json_data(copy.deepcopy(line_item)) line_item.pop('hash') data = [ line_item.get(column, 'None') for column in self.processor.hash_columns ] expected = ':'.join(data) result = self.processor._create_line_item_hash_string(line_item) self.assertEqual(result, expected)
def _create_line_item_hash_string(self, data): """Build the string to be hashed using line item data. Args: data (dict): The processed line item data dictionary Returns: (str): A string representation of the data """ data = stringify_json_data(copy.deepcopy(data)) data = [data.get(column, 'None') for column in self.hash_columns] return ':'.join(data)
def test_stringify_json_data_dict(self): """Test that the dict block is covered.""" data = {"datetime": datetime.utcnow(), "float": 1.2, "int": 1, "str": "string", "Decimal": Decimal("1.2")} with self.assertRaises(TypeError): json.dumps(data) result = common_utils.stringify_json_data(data) self.assertIsInstance(result["datetime"], str) self.assertIsInstance(result["float"], str) self.assertIsInstance(result["int"], str) self.assertIsInstance(result["str"], str) self.assertIsInstance(result["Decimal"], str)
def test_stringify_json_data_list(self): """Test that each element of JSON is returned as a string.""" data = [{"datetime": datetime.utcnow(), "float": 1.2, "int": 1, "str": "string"}, {"Decimal": Decimal("1.2")}] with self.assertRaises(TypeError): json.dumps(data) result = common_utils.stringify_json_data(data) self.assertIsInstance(result[0]["datetime"], str) self.assertIsInstance(result[0]["float"], str) self.assertIsInstance(result[0]["int"], str) self.assertIsInstance(result[0]["str"], str) self.assertIsInstance(result[1]["Decimal"], str)
def test_stringify_json_data_list(self): """Test that each element of JSON is returned as a string.""" data = [ {'datetime': datetime.utcnow(), 'float': 1.2, 'int': 1, 'str': 'string'}, {'Decimal': Decimal('1.2')}, ] with self.assertRaises(TypeError): json.dumps(data) result = common_utils.stringify_json_data(data) self.assertIsInstance(result[0]['datetime'], str) self.assertIsInstance(result[0]['float'], str) self.assertIsInstance(result[0]['int'], str) self.assertIsInstance(result[0]['str'], str) self.assertIsInstance(result[1]['Decimal'], str)
def test_stringify_json_data_dict(self): """Test that the dict block is covered.""" data = { 'datetime': datetime.utcnow(), 'float': 1.2, 'int': 1, 'str': 'string', 'Decimal': Decimal('1.2') } with self.assertRaises(TypeError): json.dumps(data) result = common_utils.stringify_json_data(data) self.assertIsInstance(result['datetime'], str) self.assertIsInstance(result['float'], str) self.assertIsInstance(result['int'], str) self.assertIsInstance(result['str'], str) self.assertIsInstance(result['Decimal'], str)