Skip to content

Commit

Permalink
Merge pull request #47 from redhatcloudx/enhance-google-endpoint
Browse files Browse the repository at this point in the history
Add optional params to /google
  • Loading branch information
major authored Jul 3, 2024
2 parents 3f9359c + d852661 commit f40a74e
Show file tree
Hide file tree
Showing 4 changed files with 232 additions and 0 deletions.
37 changes: 37 additions & 0 deletions cid/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,43 @@ def find_azure_images(
return paginate(query, page, page_size)


def find_google_images(
db: Session,
arch: Optional[str] = None,
version: Optional[str] = None,
name: Optional[str] = None,
family: Optional[str] = None,
page: int = 1,
page_size: int = 100,
) -> dict:
"""Return paginated Google images that match the given criteria.
Args:
db (Session): database session
arch (Optional[str]): architecture to search
version (Optional[str]): RHEL version to search
name (Optional[str]): image name to search
family (Optional[str]): Google image family to search
page (int): page number
page_size (int): number of images per page
Returns:
dict: paginated results
"""
query = db.query(GoogleImage).order_by(GoogleImage.creationTimestamp.desc())

if arch:
query = query.filter(GoogleImage.arch == arch)
if version:
query = query.filter(GoogleImage.version == version)
if name:
query = query.filter(GoogleImage.name.contains(name))
if family:
query = query.filter(GoogleImage.family == family)

return paginate(query, page, page_size)


def paginate(
query: Query,
page: int = 1,
Expand Down
14 changes: 14 additions & 0 deletions cid/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,20 @@ def azure_versions(db: Session = Depends(get_db)) -> list: # noqa: B008
return crud.find_available_azure_versions(db)


@app.get("/google")
def all_google_images(
db: Session = Depends(get_db), # noqa: B008
arch: Optional[str] = None,
version: Optional[str] = None,
name: Optional[str] = None,
family: Optional[str] = None,
page: int = 1,
page_size: int = 100,
) -> dict:
result = crud.find_google_images(db, arch, version, name, family, page, page_size)
return dict(jsonable_encoder(result))


@app.get("/google/versions")
def google_versions(db: Session = Depends(get_db)) -> list: # noqa: B008
return crud.find_available_google_versions(db)
Expand Down
134 changes: 134 additions & 0 deletions tests/test_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -797,3 +797,137 @@ def test_find_azure_images_paginated(db):
assert result["page_size"] == 1
assert result["total_count"] == 5
assert result["total_pages"] == 5


def find_google_images(db):
images = [
GoogleImage(
id="id-a",
arch="X86_64",
name="test_image_1a",
version="1.0",
creationTimestamp=datetime.strptime("2022-01-01", "%Y-%m-%d").date(),
),
GoogleImage(
id="id-b",
arch="X86_64",
name="test_image_2a",
version="2.0",
creationTimestamp=datetime.strptime("2022-01-01", "%Y-%m-%d").date(),
),
GoogleImage(
id="id-c",
arch="ARM64",
name="test_image_1b",
version="1.0",
creationTimestamp=datetime.strptime("2022-01-01", "%Y-%m-%d").date(),
),
GoogleImage(
id="id-d",
arch="ARM64",
name="test_image_2b",
version="2.0",
creationTimestamp=datetime.strptime("2022-01-01", "%Y-%m-%d").date(),
),
]
db.add_all(images)
db.commit()

result = crud.find_google_images(db, None, None, None, None, None)
assert len(result["results"]) == 4

result = crud.find_google_images(db, "X86_64", None, None, None, None)
assert len(result["results"]) == 2

result = crud.find_google_images(db, None, "2.0", None, None, None)
assert len(result["results"]) == 2

result = crud.find_google_images(db, None, None, "test_image_1b", None, None)
assert len(result["results"]) == 1

result = crud.find_google_images(db, None, None, None, "test_image_1b", None)
assert len(result["results"]) == 1


def test_find_google_images_paginated(db):
images = [
GoogleImage(
id="id-a",
arch="X86_64",
name="test_image_1a",
version="1.0",
creationTimestamp=datetime.strptime("2022-01-01", "%Y-%m-%d").date(),
),
GoogleImage(
id="id-b",
arch="X86_64",
name="test_image_2a",
version="2.0",
creationTimestamp=datetime.strptime("2022-01-01", "%Y-%m-%d").date(),
),
GoogleImage(
id="id-c",
arch="ARM64",
name="test_image_1b",
version="1.0",
creationTimestamp=datetime.strptime("2022-01-01", "%Y-%m-%d").date(),
),
GoogleImage(
id="id-d",
arch="ARM64",
name="test_image_2b",
version="2.0",
creationTimestamp=datetime.strptime("2022-01-01", "%Y-%m-%d").date(),
),
]
db.add_all(images)
db.commit()

result = crud.find_google_images(db, None, None, None, None)
assert len(result["results"]) == 4
assert result["page"] == 1
assert result["page_size"] == 100
assert result["total_count"] == 4
assert result["total_pages"] == 1

result = crud.find_google_images(db, None, None, None, None, 1, 1)
assert len(result["results"]) == 1
assert result["page"] == 1
assert result["page_size"] == 1
assert result["total_count"] == 4
assert result["total_pages"] == 4

result = crud.find_google_images(db, None, None, None, None, 2, 1)
assert len(result["results"]) == 1
assert result["page"] == 2
assert result["page_size"] == 1
assert result["total_count"] == 4
assert result["total_pages"] == 4

result = crud.find_google_images(db, None, None, None, None, 6, 1)
assert len(result["results"]) == 0
assert result["page"] == 6
assert result["page_size"] == 1
assert result["total_count"] == 4
assert result["total_pages"] == 4

result = crud.find_google_images(db, None, None, None, None, 1, 1000)
assert len(result["results"]) == 4
assert result["page"] == 1
assert result["page_size"] == 1000
assert result["total_count"] == 4
assert result["total_pages"] == 1

result = crud.find_google_images(db, None, None, None, None, -1, 10)
assert len(result["results"]) == 4
assert result["page"] == 1
assert result["page_size"] == 10
assert result["total_count"] == 4
assert result["total_pages"] == 1

result = crud.find_google_images(db, None, None, None, None, 1, -10)
assert len(result["results"]) == 1
assert result["page"] == 1
assert result["page_size"] == 1
assert result["total_count"] == 4
assert result["total_pages"] == 4
47 changes: 47 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,53 @@ def test_latest_azure_image(mock_azure):
assert result["urn"] == "urn-b"


def test_all_google_images():
response = client.get("/google")
assert response.status_code == 200
assert len(response.json()["results"]) == 4
assert response.json()["page"] == 1
assert response.json()["page_size"] == 100
assert response.json()["total_count"] == 4
assert response.json()["total_pages"] == 1


def test_all_google_images_with_query():
response = client.get("/google?version=7")
assert response.status_code == 200
assert response.json()["results"][0]["version"] == "7"


def test_all_google_images_with_query_arch():
response = client.get("/google?arch=X86_64")
assert response.status_code == 200
assert response.json()["results"][0]["arch"] == "X86_64"


def test_all_google_images_with_query_name():
response = client.get("/google?name=rhel-7-v20240611")
assert response.status_code == 200
assert response.json()["results"][0]["name"] == "rhel-7-v20240611"


def test_all_google_images_with_query_combination():
response = client.get(
"/google" + "?name=rhel-7-v20240611" + "&version=7" + "&arch=X86_64"
)
assert response.status_code == 200
assert len(response.json()["results"]) == 1
assert response.json()["results"][0]["name"] == "rhel-7-v20240611"
assert response.json()["results"][0]["version"] == "7"
assert response.json()["results"][0]["arch"] == "X86_64"


def test_all_google_images_with_query_combination_no_match():
response = client.get(
"/google" + "?name=does-not-exist" + "&version=7" + "&arch=X86_64"
)
assert response.status_code == 200
assert len(response.json()["results"]) == 0


@patch("cid.crud.latest_google_image")
def test_latest_google_image(mock_google):
mock_google.return_value = {
Expand Down

0 comments on commit f40a74e

Please sign in to comment.