Skip to content

Commit

Permalink
Added usage_cutoff_date to summary report for all months (#65)
Browse files Browse the repository at this point in the history
* Added usage_cutoff_date to summary report for all months

* Added __init__.py
  • Loading branch information
mroloux committed Oct 17, 2023
1 parent b54a42c commit 44c4fe5
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 20 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ jobs:
with:
timezone: Europe/Brussels
- run: python setup.py test
env:
DEMO_COMPANY_SECRET_KEY: ${{ secrets.DEMO_COMPANY_SECRET_KEY }}
24 changes: 13 additions & 11 deletions seatsio/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,8 @@ def __init__(self, item_data):

class UsageSummaryForAllMonths:
def __init__(self, json):
self.items = list(map(lambda x: UsageSummaryForMonth(x), json))
self.usage = list(map(lambda x: UsageSummaryForMonth(x), json.get("usage")))
self.usage_cutoff_date = parse_date(json.get("usageCutoffDate"))


class UsageSummaryForMonth(object):
Expand All @@ -320,8 +321,9 @@ def from_json(cls, json):


class UsageDetailsForMonth:
def __init__(self, json):
self.items = list(map(lambda x: UsageDetails(x), json))
@classmethod
def from_json(cls, json):
return list(map(lambda x: UsageDetails(x), json))


class UsageDetails:
Expand All @@ -334,7 +336,7 @@ class UsageByChart:
def __init__(self, json):
if json.get("chart") is not None:
self.chart = UsageChart(json.get("chart"))
self.usageByEvent = list(map(lambda x: UsageByEvent(x), json.get("usageByEvent")))
self.usage_by_event = list(map(lambda x: UsageByEvent(x), json.get("usageByEvent")))


class UsageChart:
Expand All @@ -355,9 +357,13 @@ def __init__(self, json):
self.key = json.get("key")


class UsageDetailsForEventInMonthV1:
def __init__(self, json):
self.items = list(map(lambda x: UsageForObjectV1(x), json))
class UsageDetailsForEventInMonth:
@classmethod
def from_json(cls, json):
if len(json) == 0 or "usageByReason" not in json[0]:
return list(map(lambda x: UsageForObjectV1(x), json))
else:
return list(map(lambda x: UsageForObjectV2(x), json))


class UsageForObjectV1:
Expand All @@ -368,10 +374,6 @@ def __init__(self, json):
self.num_first_selections = json.get("numFirstSelections")
self.num_first_bookings_or_selections = json.get("numFirstBookingsOrSelections")

class UsageDetailsForEventInMonthV2:
def __init__(self, json):
self.items = list(map(lambda x: UsageForObjectV2(x), json))


class UsageForObjectV2:
def __init__(self, json):
Expand Down
14 changes: 5 additions & 9 deletions seatsio/reports/usage/usageReports.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from seatsio.domain import UsageDetailsForMonth
from seatsio.domain import UsageSummaryForAllMonths, UsageDetailsForEventInMonthV1, \
UsageDetailsForEventInMonthV2
from seatsio.domain import UsageDetailsForMonth, UsageDetailsForEventInMonth
from seatsio.domain import UsageSummaryForAllMonths


class UsageReports:
Expand All @@ -9,19 +8,16 @@ def __init__(self, http_client):
self.http_client = http_client

def summary_for_all_months(self):
url = "/reports/usage"
url = "/reports/usage?version=2"
body = self.http_client.url(url).get()
return UsageSummaryForAllMonths(body)

def details_for_month(self, month):
url = "/reports/usage/month/" + month.serialize()
body = self.http_client.url(url).get()
return UsageDetailsForMonth(body)
return UsageDetailsForMonth.from_json(body)

def details_for_event_in_month(self, event_id, month):
url = "/reports/usage/month/" + month.serialize() + "/event/" + str(event_id)
body = self.http_client.url(url).get()
if len(body) == 0 or "usageByReason" not in body[0]:
return UsageDetailsForEventInMonthV1(body)
else:
return UsageDetailsForEventInMonthV2(body)
return UsageDetailsForEventInMonth.from_json(body)
Empty file added tests/reports/usage/__init__.py
Empty file.
37 changes: 37 additions & 0 deletions tests/reports/usage/testUsageReports.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from seatsio import Month
from tests.seatsioClientTest import SeatsioClientTest
from tests.util.asserts import assert_that


class UsageReportTest(SeatsioClientTest):

def test_usage_report_for_all_months(self):
self.assert_demo_company_secret_key_set()
client = self.create_client(self.demo_company_secret_key(), None)

report = client.usage_reports.summary_for_all_months()

assert_that(report.usage_cutoff_date).is_not_none()
assert_that(len(report.usage) > 0).is_true()
assert_that(report.usage[0].month.year).is_equal_to(2014)
assert_that(report.usage[0].month.month).is_equal_to(2)

def test_usage_report_month(self):
self.assert_demo_company_secret_key_set()
client = self.create_client(self.demo_company_secret_key(), None)

report = client.usage_reports.details_for_month(Month(2021, 11))

assert_that(len(report) > 0).is_true()
assert_that(len(report[0].usage_by_chart) > 0).is_true()
assert_that(report[0].usage_by_chart[0].usage_by_event[0].num_used_objects).is_equal_to(143)


def test_usage_report_event_in_month(self):
self.assert_demo_company_secret_key_set()
client = self.create_client(self.demo_company_secret_key(), None)

report = client.usage_reports.details_for_event_in_month(580293, Month(2021, 11))

assert_that(len(report) > 0).is_true()
assert_that(report[0].num_first_selections).is_equal_to(1)
7 changes: 7 additions & 0 deletions tests/seatsioClientTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,10 @@ def wait_for_status_changes(self, event, num_status_changes):
time.sleep(1)
else:
return status_changes

def demo_company_secret_key(self):
return os.environ["DEMO_COMPANY_SECRET_KEY"]

def assert_demo_company_secret_key_set(self):
if "DEMO_COMPANY_SECRET_KEY" not in os.environ:
self.skipTest("DEMO_COMPANY_SECRET_KEY environment variable not set, skipping test")

0 comments on commit 44c4fe5

Please sign in to comment.