Skip to content

Commit

Permalink
[Bundle Analysis] Cache config service get_or_create (#464)
Browse files Browse the repository at this point in the history
* [Bundle Analysis] Cache config service get_or_create

* rename function
  • Loading branch information
JerrySentry authored Jan 6, 2025
1 parent efe4835 commit 67879c1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
6 changes: 6 additions & 0 deletions shared/django_apps/bundle_analysis/service/bundle_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@ def update_cache_option(repo_id, name, is_caching=True) -> None:
CacheConfig.objects.update_or_create(
repo_id=repo_id, bundle_name=name, defaults={"is_caching": is_caching}
)

@staticmethod
def create_if_not_exists(repo_id, name, is_caching=True) -> None:
CacheConfig.objects.get_or_create(
repo_id=repo_id, bundle_name=name, defaults={"is_caching": is_caching}
)
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,39 @@ def test_bundle_config_create_multiple(self):

query_results = CacheConfig.objects.all()
assert len(query_results) == 4

def test_bundle_config_get_or_create(self):
# Create 1 -- default as is_caching=True
BundleAnalysisCacheConfigService.create_if_not_exists(repo_id=1, name="bundleA")
query_results = CacheConfig.objects.all()
assert len(query_results) == 1
assert query_results[0].repo_id == 1
assert query_results[0].bundle_name == "bundleA"
assert query_results[0].is_caching == True

# Create 2 -- already exist don't change is_caching value
BundleAnalysisCacheConfigService.create_if_not_exists(
repo_id=1, name="bundleA", is_caching=False
)
query_results = CacheConfig.objects.all()
assert len(query_results) == 1
assert query_results[0].repo_id == 1
assert query_results[0].bundle_name == "bundleA"
assert query_results[0].is_caching == True

# Create 3 -- new bundle
BundleAnalysisCacheConfigService.create_if_not_exists(
repo_id=1, name="bundleB", is_caching=False
)
query_results = CacheConfig.objects.all()
assert len(query_results) == 2
query_results = CacheConfig.objects.filter(bundle_name="bundleA").all()
assert len(query_results) == 1
assert query_results[0].repo_id == 1
assert query_results[0].bundle_name == "bundleA"
assert query_results[0].is_caching == True
query_results = CacheConfig.objects.filter(bundle_name="bundleB").all()
assert len(query_results) == 1
assert query_results[0].repo_id == 1
assert query_results[0].bundle_name == "bundleB"
assert query_results[0].is_caching == False

0 comments on commit 67879c1

Please sign in to comment.