Skip to content

Commit

Permalink
Update aws line item aggregate table test to use defined dates (proje…
Browse files Browse the repository at this point in the history
…ct-koku#220)

* Update aws line item aggregate table test to use defined dates

* Trying the suggestion from the github issue

getmoto/moto#1771
  • Loading branch information
adberglund authored Oct 1, 2018
1 parent 9a22cc7 commit 673bcef
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 20 deletions.
26 changes: 20 additions & 6 deletions helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import random
from decimal import Decimal

from dateutil import relativedelta
from faker import Faker

from masu.config import Config
Expand All @@ -41,11 +42,14 @@ def __init__(self, db_accessor, column_map, column_types):
self.column_map = column_map
self.column_types = column_types

def create_cost_entry(self, bill):
def create_cost_entry(self, bill, entry_datetime=None):
"""Create a cost entry database object for test."""
table_name = AWS_CUR_TABLE_MAP['cost_entry']
row = self.db_accessor.create_db_object(table_name, {})
start_datetime = self.fake.past_datetime(start_date='-60d')
if entry_datetime:
start_datetime = entry_datetime
else:
start_datetime = self.fake.past_datetime(start_date='-60d')
end_datetime = start_datetime + datetime.timedelta(hours=1)
row.interval_start = self.stringify_datetime(start_datetime)
row.interval_end = self.stringify_datetime(end_datetime)
Expand All @@ -56,18 +60,25 @@ def create_cost_entry(self, bill):

return row

def create_cost_entry_bill(self):
def create_cost_entry_bill(self, bill_date=None):
"""Create a cost entry bill database object for test."""
table_name = AWS_CUR_TABLE_MAP['bill']
data = self.create_columns_for_table(table_name)

if bill_date:
bill_start = bill_date.replace(day=1).date()
bill_end = bill_start + relativedelta.relativedelta(months=1)

data['billing_period_start'] = bill_start
data['billing_period_end'] = bill_end

row = self.db_accessor.create_db_object(table_name, data)

self.db_accessor._session.add(row)
self.db_accessor._session.commit()

return row


def create_cost_entry_pricing(self):
"""Create a cost entry pricing database object for test."""
table_name = AWS_CUR_TABLE_MAP['pricing']
Expand All @@ -79,12 +90,15 @@ def create_cost_entry_pricing(self):

return row

def create_cost_entry_product(self):
def create_cost_entry_product(self, product_family=None):
"""Create a cost entry product database object for test."""
table_name = AWS_CUR_TABLE_MAP['product']
data = self.create_columns_for_table(table_name)
row = self.db_accessor.create_db_object(table_name, data)
row.product_family = random.choice(AWS_PRODUCT_FAMILY)
if product_family:
row.product_family = product_family
else:
row.product_family = random.choice(AWS_PRODUCT_FAMILY)
self.db_accessor._session.add(row)
self.db_accessor._session.commit()

Expand Down
34 changes: 20 additions & 14 deletions test_report_db_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import uuid

import psycopg2
from dateutil import relativedelta
from sqlalchemy.orm.query import Query
from sqlalchemy.sql import func

Expand Down Expand Up @@ -804,22 +805,27 @@ def test_populate_line_item_aggregates_table(self):
ce_table = getattr(self.accessor.report_schema, ce_table_name)
agg_table = getattr(self.accessor.report_schema, agg_table_name)

expected_time_scope_values = [-1, -2, -10]
expected_time_scope_values = [-1, -2, -10, -30]
expected_report_types = ['storage', 'instance_type', 'costs']

for _ in range(25):
bill = self.creator.create_cost_entry_bill()
cost_entry = self.creator.create_cost_entry(bill)
product = self.creator.create_cost_entry_product()
pricing = self.creator.create_cost_entry_pricing()
reservation = self.creator.create_cost_entry_reservation()
self.creator.create_cost_entry_line_item(
bill,
cost_entry,
product,
pricing,
reservation
)
today = datetime.datetime.utcnow()
last_month = today - relativedelta.relativedelta(months=1)

for cost_entry_date in (today, last_month):
bill = self.creator.create_cost_entry_bill(cost_entry_date)
cost_entry = self.creator.create_cost_entry(bill, cost_entry_date)
for family in ['Storage', 'Compute Instance', 'Database Storage',
'Database Instance']:
product = self.creator.create_cost_entry_product(family)
pricing = self.creator.create_cost_entry_pricing()
reservation = self.creator.create_cost_entry_reservation()
self.creator.create_cost_entry_line_item(
bill,
cost_entry,
product,
pricing,
reservation
)

start_date, end_date = self.accessor._session.query(
func.min(ce_table.interval_start),
Expand Down

0 comments on commit 673bcef

Please sign in to comment.