Skip to content

Commit

Permalink
Test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
judtinzhang committed Apr 25, 2024
1 parent 09ba11e commit 7af3c14
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions tests/analytics/test_analytics.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import random
from unittest import mock

from django.test import TestCase

from analytics.analytics import AnalyticsTxn, LabsAnalytics, Product
from identity.identity import attest


class AnalyticsTxnTestCase(TestCase):
Expand All @@ -21,25 +23,29 @@ def test_analytics_txn(self):


class AnalyticsSubmission(TestCase):

# TODO: Add mocked test cases for Analytics

def setUp(self):
attest()
# NOTE: use attest this for real testing
# from identity.identity import attest
# attest()
self.analytics_wrapper = LabsAnalytics()
self.NUM_TRIES = 1000

def test_submit(self):
data = {
def rand_int(self):
return random.randint(1, self.NUM_TRIES)

def generate_data(self):
return {
"product": Product.MOBILE_BACKEND,
"pennkey": "judtin",
"data": [{"key": "backend", "value": "some data"}],
"pennkey": None,
"data": [{"key": f"{self.rand_int()}", "value": f"{self.rand_int()}"}],
}

for _ in range(20):
data["product"] = Product((data["product"].value + 1) % len(Product))
txn = AnalyticsTxn(**data)
@mock.patch("analytics.analytics.LabsAnalytics.submit")
def test_submit(self, mock_submit):
for _ in range(self.NUM_TRIES):
txn = AnalyticsTxn(**self.generate_data())
self.analytics_wrapper.submit(txn)

self.analytics_wrapper.executor.shutdown(wait=True)
self.assertEqual(self.NUM_TRIES, mock_submit.call_count)

assert False
self.analytics_wrapper.executor.shutdown(wait=True)

0 comments on commit 7af3c14

Please sign in to comment.