Skip to content

Commit

Permalink
Add unit test
Browse files Browse the repository at this point in the history
Signed-off-by: Zhiwei Liang <[email protected]>
  • Loading branch information
zliang-akamai committed May 10, 2023
1 parent d6c3842 commit efedf30
Showing 1 changed file with 52 additions and 1 deletion.
53 changes: 52 additions & 1 deletion tests/unit/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,42 @@
from __future__ import annotations

import copy
import math
import re
from typing import TYPE_CHECKING

import pytest
import requests
from pytest import MonkeyPatch

if TYPE_CHECKING:
from linodecli import CLI, CLIOperation


class MockResponse:
def __init__(
self, page: int, pages: int, results: int, status_code: int = 200
):
self.page = page
self.pages = pages
self.results = results
self.status_code = status_code

def json(self):
return {
"data": ["test_data" for _ in range(500)],
"page": self.page,
"pages": self.pages,
"results": self.results,
}


class TestCLI:
"""
Unit tests for linodecli.cli
"""

def test_find_operation(self, mock_cli, list_operation):
def test_find_operation(self, mock_cli: CLI, list_operation: CLIOperation):
target_operation = list_operation
target_operation.command = "foo"
target_operation.action = "list"
Expand All @@ -35,3 +63,26 @@ def test_find_operation(self, mock_cli, list_operation):
with pytest.raises(ValueError, match=r"No action *"):
mock_cli.find_operation("foo", "cool")
mock_cli.find_operation("cool", "cool")


def test_get_all_pages(
mock_cli: CLI, list_operation: CLIOperation, monkeypatch: MonkeyPatch
):
TOTAL_DATA = 2000

def mock_get(url: str, *args, **kwargs):
# assume page_size is always 500
page = int(re.search(r"\?page=(.*?)&page_size", url).group(1))
pages = math.ceil(TOTAL_DATA / 500)
if page > pages:
page = pages
return MockResponse(page, pages, pages * 500)

monkeypatch.setattr(requests, "get", mock_get)

merged_result = mock_cli.get_all_pages(list_operation, [])

assert len(merged_result["data"]) == TOTAL_DATA
assert merged_result["page"] == 1
assert merged_result["pages"] == 1
assert merged_result["results"] == TOTAL_DATA

0 comments on commit efedf30

Please sign in to comment.