|
9 | 9 | from app_analytics.analytics_db_service import ( |
10 | 10 | get_feature_evaluation_data, |
11 | 11 | get_feature_evaluation_data_from_local_db, |
| 12 | + get_top_organisations_from_local_db, |
12 | 13 | get_total_events_count, |
13 | 14 | get_usage_data, |
14 | 15 | get_usage_data_from_local_db, |
@@ -731,3 +732,83 @@ def test_get_usage_data__previous_billing_period__passes_correct_date_range( |
731 | 732 | date_stop=datetime(2022, 12, 30, 9, 9, 47, 325132, tzinfo=UTC), |
732 | 733 | labels_filter=None, |
733 | 734 | ) |
| 735 | + |
| 736 | + |
| 737 | +@pytest.mark.use_analytics_db |
| 738 | +@pytest.mark.freeze_time("2023-01-19T09:09:47.325132+00:00") |
| 739 | +def test_get_top_organisations_from_local_db__with_data__returns_correct_mapping( |
| 740 | + organisation: Organisation, |
| 741 | + environment: Environment, |
| 742 | + settings: SettingsWrapper, |
| 743 | +) -> None: |
| 744 | + # Given |
| 745 | + now = timezone.now() |
| 746 | + read_bucket_size = 15 |
| 747 | + settings.ANALYTICS_BUCKET_SIZE = read_bucket_size |
| 748 | + date_start = now - timedelta(days=30) |
| 749 | + |
| 750 | + for i in range(3): |
| 751 | + APIUsageBucket.objects.create( |
| 752 | + environment_id=environment.id, |
| 753 | + resource=Resource.FLAGS, |
| 754 | + total_count=100, |
| 755 | + bucket_size=read_bucket_size, |
| 756 | + created_at=now - timedelta(days=i), |
| 757 | + ) |
| 758 | + |
| 759 | + # When |
| 760 | + result = get_top_organisations_from_local_db(date_start) |
| 761 | + |
| 762 | + # Then |
| 763 | + assert result == {organisation.id: 300} |
| 764 | + |
| 765 | + |
| 766 | +@pytest.mark.use_analytics_db |
| 767 | +@pytest.mark.freeze_time("2023-01-19T09:09:47.325132+00:00") |
| 768 | +def test_get_top_organisations_from_local_db__buckets_before_date_start__excluded( |
| 769 | + organisation: Organisation, |
| 770 | + environment: Environment, |
| 771 | + settings: SettingsWrapper, |
| 772 | +) -> None: |
| 773 | + # Given |
| 774 | + now = timezone.now() |
| 775 | + read_bucket_size = 15 |
| 776 | + settings.ANALYTICS_BUCKET_SIZE = read_bucket_size |
| 777 | + date_start = now - timedelta(days=7) |
| 778 | + |
| 779 | + # Bucket within range |
| 780 | + APIUsageBucket.objects.create( |
| 781 | + environment_id=environment.id, |
| 782 | + resource=Resource.FLAGS, |
| 783 | + total_count=50, |
| 784 | + bucket_size=read_bucket_size, |
| 785 | + created_at=now - timedelta(days=3), |
| 786 | + ) |
| 787 | + # Bucket outside range (before date_start) |
| 788 | + APIUsageBucket.objects.create( |
| 789 | + environment_id=environment.id, |
| 790 | + resource=Resource.FLAGS, |
| 791 | + total_count=200, |
| 792 | + bucket_size=read_bucket_size, |
| 793 | + created_at=now - timedelta(days=10), |
| 794 | + ) |
| 795 | + |
| 796 | + # When |
| 797 | + result = get_top_organisations_from_local_db(date_start) |
| 798 | + |
| 799 | + # Then |
| 800 | + assert result == {organisation.id: 50} |
| 801 | + |
| 802 | + |
| 803 | +def test_get_top_organisations_from_local_db__saas_mode__raises_runtime_error( |
| 804 | + mocker: MockerFixture, |
| 805 | +) -> None: |
| 806 | + # Given |
| 807 | + mocker.patch( |
| 808 | + "app_analytics.analytics_db_service.is_saas", |
| 809 | + return_value=True, |
| 810 | + ) |
| 811 | + |
| 812 | + # When / Then |
| 813 | + with pytest.raises(RuntimeError, match="Must not run in SaaS mode"): |
| 814 | + get_top_organisations_from_local_db(timezone.now() - timedelta(days=30)) |
0 commit comments