Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
suejung-sentry committed Sep 22, 2024
1 parent c0083fd commit 3cec3b9
Show file tree
Hide file tree
Showing 9 changed files with 234 additions and 205 deletions.
48 changes: 27 additions & 21 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
__pycache__
*.pyc
*.pyo
*.pyd
.Python
env
pip-log.txt
pip-delete-this-directory.txt
.tox
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover
*.log
.git
service.json
.github
.circleci
gha-creds-*.json
**/__pycache__
**/.venv
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/bin
**/charts
**/docker-compose*
**/compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md
4 changes: 4 additions & 0 deletions .graphqlconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"schema": "./graphql_api/types/me/me.graphql",
"documents": "**/*.graphql"
}
25 changes: 25 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# For more information, please refer to https://aka.ms/vscode-docker-python
FROM python:3-slim

EXPOSE 8000

# Keeps Python from generating .pyc files in the container
ENV PYTHONDONTWRITEBYTECODE=1

# Turns off buffering for easier container logging
ENV PYTHONUNBUFFERED=1

# Install pip requirements
COPY requirements.txt .
RUN python -m pip install -r requirements.txt

WORKDIR /app
COPY . /app

# Creates a non-root user with an explicit UID and adds permission to access the /app folder
# For more info, please refer to https://aka.ms/vscode-docker-python-configure-containers
RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app
USER appuser

# During debugging, this entry point will be overridden. For more information, please refer to https://aka.ms/vscode-docker-python-debug
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "codecov.wsgi"]
2 changes: 2 additions & 0 deletions graphql_api/graphql.config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
schema: schema.graphql
documents: '**/*.graphql'
227 changes: 109 additions & 118 deletions graphql_api/tests/test_coverage_analytics.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,27 @@
import datetime

from unittest.mock import patch
from django.test import TransactionTestCase
from django.utils import timezone
from freezegun import freeze_time

from codecov_auth.tests.factories import OwnerFactory
from core.models import Repository # Import the actual model, not the factory
from core.tests.factories import (
CommitFactory,
RepositoryFactory,
RepositoryTokenFactory,
)

from .helper import GraphQLTestHelper

# Queries for reuse
query_coverage_analytics = """
query CoverageAnalytics($owner:String!, $repo: String!, $interval: MeasurementInterval!) {
owner(username:$owner) {
repository(name: $repo) {
__typename
... on Repository {
name
coverageEnabled
coverageAnalytics(interval: $interval) {
percentCovered
commitSha
hits
misses
lines
measurements(interval:$interval) {
timestamp
max
}
%s
}
}
... on ResolverError {
Expand All @@ -39,33 +32,31 @@
}
"""

default_fields = """
default_coverage_analytics_fields = """
percentCovered
coverageSha
commitSha
hits
misses
lines
measurements
measurements(interval: $interval) {
timestamp
max
min
avg
}
"""


class TestFetchCoverageAnalytics(GraphQLTestHelper, TransactionTestCase):
def fetch_coverage_analytics(self, name, interval="INTERVAL_1_DAY", fields=None):
variables = {"owner": self.owner.username, "repo": name, "interval": interval}
data = self.gql_request(
query=query_coverage_analytics,
variables=variables,
)
return data["owner"]["repository"]["coverageAnalytics"]
def fetch_coverage_analytics(self, repo_name, interval="INTERVAL_1_DAY", fields=None):
query = query_coverage_analytics % (fields or default_coverage_analytics_fields)
variables = {"owner": "codecov-user", "repo": repo_name, "interval": interval}
return self.gql_request(query=query, owner=self.owner, variables=variables)

def setUp(self):
self.owner = OwnerFactory(username="codecov-user")
self.yaml = {"test": "test"}

@freeze_time("2021-01-01")
def test_get_coverage_analytics(self):
# Create repository with an associated commit and coverage data
repo = RepositoryFactory(
self.repo = RepositoryFactory(
author=self.owner,
active=True,
private=True,
Expand All @@ -75,98 +66,98 @@ def test_get_coverage_analytics(self):
languages=[],
)

hour_ago = datetime.datetime.now() - datetime.timedelta(hours=1)
coverage_commit = CommitFactory(
repository=repo,
totals={"c": 75, "h": 30, "m": 10, "n": 40},
timestamp=hour_ago,
)
CommitFactory(repository=repo, totals={"c": 85})

# Update the repository to simulate data persistence
repo.updatestamp = datetime.datetime.now()
repo.save()

# Generate tokens
profiling_token = RepositoryTokenFactory(
repository_id=repo.repoid, token_type="profiling"
).key

# Fetch coverage analytics data
analytics_data = self.fetch_coverage_analytics(repo.name)
assert analytics_data == {
"percentCovered": 75,
"commitSha": coverage_commit.commitid,
"hits": 30,
"misses": 10,
"lines": 40,
"measurements": [], # Assuming measurements are empty for now
}

def test_coverage_analytics_with_no_commit(self):
"""Test case where there is no coverage data (no commits)"""
repo = RepositoryFactory(
author=self.owner,
active=True,
private=True,
name="empty-repo",
yaml=self.yaml,
language="erlang",
languages=[],
)

repo.save()
# Ensure the repository is saved correctly
self.repo.save()
self.assertTrue(self.repo.pk, "Repository should be saved and have a primary key.")

# Fetch coverage analytics data when no commit exists
analytics_data = self.fetch_coverage_analytics(repo.name)
assert analytics_data == {
"percentCovered": None,
"commitSha": None,
"hits": None,
"misses": None,
"lines": None,
"measurements": [],
}

def test_coverage_analytics_with_measurements(self):
"""Test case with repository having measurement data"""
repo = RepositoryFactory(
author=self.owner,
active=True,
private=True,
name="repo-with-measurements",
yaml=self.yaml,
language="python",
)

hour_ago = datetime.datetime.now() - datetime.timedelta(hours=1)
@freeze_time("2021-01-01")
def test_full_coverage_analytics(self):
"""Test with full coverage analytics fields"""
# Create commit and coverage data
hour_ago = timezone.make_aware(datetime.datetime(2020, 12, 31, 23, 0)) # Fixed
coverage_commit = CommitFactory(
repository=repo,
totals={"c": 80, "h": 50, "m": 20, "n": 70},
repository=self.repo,
totals={"c": 75, "h": 30, "m": 10, "n": 40},
timestamp=hour_ago,
)

# Simulate some measurement data for the repository
measurements = [
{"timestamp": "2021-01-01T00:00:00Z", "max": 70},
{"timestamp": "2021-01-02T00:00:00Z", "max": 80},
]

self.mock_measurements_for_repository(repo, measurements)

# Fetch coverage analytics data
analytics_data = self.fetch_coverage_analytics(repo.name)
assert analytics_data == {
"percentCovered": 80,
"commitSha": coverage_commit.commitid,
"hits": 50,
"misses": 20,
"lines": 70,
"measurements": [
{"timestamp": "2021-01-01T00:00:00Z", "max": 70},
{"timestamp": "2021-01-02T00:00:00Z", "max": 80},
],
CommitFactory(repository=self.repo, totals={"c": 85})

# Ensure the repository's timestamp is updated
self.repo.updatestamp = timezone.now() # Use timezone-aware datetime
self.repo.save()

# Query the database using the actual model (Repository)
repo_from_db = Repository.objects.get(pk=self.repo.pk) # Use the Repository model, not the factory
self.assertIsNotNone(repo_from_db.updatestamp)

# Fetch the coverage analytics data
coverage_data = self.fetch_coverage_analytics(self.repo.name)

print(coverage_data)

expected_response = {
"__typename": "Repository",
"name": self.repo.name,
"coverageAnalytics": {
"percentCovered": 75,
"commitSha": coverage_commit.commitid,
"hits": 30,
"misses": 10,
"lines": 40,
"measurements": [], # Assuming measurements are empty for now
},
}

def mock_measurements_for_repository(self, repo, measurements):
pass
assert coverage_data["owner"]["repository"] == expected_response


#
# @freeze_time("2021-01-01")
# def test_partial_coverage_analytics(self):
# """Test fetching partial fields (only percentCovered)"""
# CommitFactory(repository=self.repo, totals={"c": 75, "h": 30, "m": 10, "n": 40})
#
# self.repo.updatestamp = datetime.datetime.now()
# self.repo.save()
#
# fields = "percentCovered"
# coverage_data = self.fetch_coverage_analytics(self.repo.name, fields=fields)
# assert coverage_data["owner"]["repository"]["coverageAnalytics"]["percentCovered"] == 75
#
# @freeze_time("2021-01-01")
# def test_measurements_coverage_analytics(self):
# """Test fetching only measurements subfield"""
# CommitFactory(
# repository=self.repo,
# totals={"c": 75, "h": 30, "m": 10, "n": 40},
# timestamp=datetime.datetime.now() - datetime.timedelta(hours=1),
# )
#
# self.repo.updatestamp = datetime.datetime.now()
# self.repo.save()
#
# fields = "measurements(interval: $interval) { timestamp max }"
# coverage_data = self.fetch_coverage_analytics(self.repo.name, fields=fields)
# assert coverage_data["owner"]["repository"]["coverageAnalytics"]["measurements"] == []
#
# def test_coverage_analytics_no_commit(self):
# """Test case where no commits exist for coverage data"""
# repo = RepositoryFactory(
# author=self.owner,
# active=True,
# private=True,
# name="empty-repo",
# yaml=self.yaml,
# language="erlang",
# languages=[],
# )
# repo.save()
#
# coverage_data = self.fetch_coverage_analytics(repo.name)
# assert coverage_data["owner"]["repository"]["coverageAnalytics"] == {
# "percentCovered": None,
# "commitSha": None,
# "hits": None,
# "misses": None,
# "lines": None,
# "measurements": [],
# }
Loading

0 comments on commit 3cec3b9

Please sign in to comment.