Skip to content

Commit

Permalink
add test coverage to makefile command, start refactoring cache for si…
Browse files Browse the repository at this point in the history
…ngle and multifile options
  • Loading branch information
jaydeluca committed Aug 7, 2023
1 parent e90db0d commit 06c4c4f
Show file tree
Hide file tree
Showing 14 changed files with 233 additions and 173 deletions.
2 changes: 2 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[run]
omit = tests/*
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ cache/*
.idea/*

*DS_Store
*__pycache__/*
__pycache__/*
*/__pycache__/*
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ install:

.PHONY: test
test:
pytest
pytest --cov=./ --cov-config=.coveragerc

.PHONY: lint
lint:
Expand Down
47 changes: 7 additions & 40 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
from collections import defaultdict
from typing import List
from datetime import datetime, timedelta
from datetime import datetime
import matplotlib.pyplot as plt
import argparse
from utilities import count_by_file_extension, get_dates_between

from file_cache import FileCache
from single_file_cache import SingleFileCache
from github_client import GithubClient

COMMIT_CACHE_FILE = 'cache/date-commit-cache.json'
REPO_CACHE_FILE = 'cache/repo-cache.json'


def count_by_file_extension(files: List[str], file_extensions: List[str]) -> dict:
file_counts = defaultdict(int)
for file in files:
for ext in file_extensions:
if file.endswith(ext):
file_counts[ext] += 1
return file_counts


def get_commit_by_date(gh_client: GithubClient, cache: FileCache, repository, date):
def get_commit_by_date(gh_client: GithubClient, cache: SingleFileCache, repository, date):
find_commit = cache.retrieve_value(date)
if not find_commit:
find_commit = gh_client.get_most_recent_commit(repository, date)
Expand All @@ -30,7 +21,7 @@ def get_commit_by_date(gh_client: GithubClient, cache: FileCache, repository, da
return find_commit


def get_repository_by_commit(gh_client: GithubClient, cache: FileCache, repository,
def get_repository_by_commit(gh_client: GithubClient, cache: SingleFileCache, repository,
commit):
find_repo = cache.retrieve_value(commit)

Expand All @@ -41,30 +32,6 @@ def get_repository_by_commit(gh_client: GithubClient, cache: FileCache, reposito
return find_repo


def get_dates_between(start_date_str, end_date, interval):
date_format = "%Y-%m-%d"
output_format = "%Y-%m-%dT%H:%M:%SZ"

# Parse the input date string
start_date = datetime.strptime(start_date_str, date_format).date()

# Convert end date to date if string
if type(end_date) is str:
end_date = datetime.strptime(end_date, '%Y-%m-%d').date()

# Calculate the difference in days
days_diff = (end_date - start_date).days

# Generate the list of dates
date_list = []
for i in range(0, days_diff + 1, int(interval)):
date_item = start_date + timedelta(days=i)
start_date_str = date_item.strftime(output_format)
date_list.append(start_date_str)

return date_list


def main(args):
client = GithubClient()

Expand All @@ -73,8 +40,8 @@ def main(args):
".groovy"
]

commit_cache = FileCache(COMMIT_CACHE_FILE)
repo_cache = FileCache(REPO_CACHE_FILE)
commit_cache = SingleFileCache(COMMIT_CACHE_FILE)
repo_cache = SingleFileCache(REPO_CACHE_FILE)

timeframe = get_dates_between(args.start, datetime.now().date(), args.interval)
result = defaultdict(dict)
Expand Down
32 changes: 32 additions & 0 deletions multi_file_cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import os
import json

CACHE_LOCATION = "cache"


class MultiFileCache:
def __init__(self, location):
self.location = location
if not os.path.exists(location):
with open(location, 'w') as file:
json.dump({}, file)

def add_to_cache(self, key, value):
with open(self.location, 'r+') as file:
cache = json.load(file)
cache[key] = value
file.seek(0)
json.dump(cache, file)
file.truncate()

def retrieve_value(self, key):
with open(self.location, 'r') as file:
cache = json.load(file)
result = cache.get(key)
if result is not None:
print(f"cache hit. key:{key}")
return result

def delete_cache(self):
if os.path.exists(self.location):
os.remove(self.location)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ requests
matplotlib
argparse
pytest
pytest-cov
ruff
2 changes: 1 addition & 1 deletion file_cache.py → single_file_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import json


class FileCache:
class SingleFileCache:
def __init__(self, location):
self.location = location
if not os.path.exists(location):
Expand Down
54 changes: 0 additions & 54 deletions test_data_filter.py

This file was deleted.

68 changes: 0 additions & 68 deletions test_mocks/tree_data.json

This file was deleted.

Empty file added tests/__init__.py
Empty file.
Loading

0 comments on commit 06c4c4f

Please sign in to comment.