Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type part of test suite utils #1483

Merged
merged 1 commit into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions tests/cell_test.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
from typing import Generator

import pytest
from pytest import FixtureRequest

import gspread
from gspread.client import Client
from gspread.spreadsheet import Spreadsheet
from gspread.worksheet import Worksheet

from .conftest import GspreadTest


class CellTest(GspreadTest):
"""Test for gspread.Cell."""

spreadsheet: Spreadsheet
sheet: Worksheet

@pytest.fixture(scope="function", autouse=True)
def init(self, client, request):
def init(
self: "CellTest", client: Client, request: FixtureRequest
) -> Generator[None, None, None]:
# User current test name in spreadsheet name
name = self.get_temporary_spreadsheet_title(request.node.name)
CellTest.spreadsheet = client.create(name)
Expand Down Expand Up @@ -70,7 +81,12 @@ def test_a1_value(self):
cell = self.sheet.cell(4, 4)
self.assertEqual(cell.address, "D4")
self.sheet.update_acell("B1", "Dummy")
cell = self.sheet.find("Dummy")
result = self.sheet.find("Dummy")
if result is None:
self.fail("did not find cell with matching text 'Dummy'")
else:
cell = result

self.assertEqual(cell.address, "B1")
self.assertEqual(cell.value, "Dummy")
cell = gspread.cell.Cell(1, 2, "Foo Bar")
Expand Down
11 changes: 10 additions & 1 deletion tests/client_test.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
import time
from typing import Generator

import pytest
from pytest import FixtureRequest

import gspread
from gspread.client import Client
from gspread.spreadsheet import Spreadsheet

from .conftest import GspreadTest


class ClientTest(GspreadTest):
"""Test for gspread.client."""

gc: Client
spreadsheet: Spreadsheet

@pytest.fixture(scope="function", autouse=True)
def init(self, client, request):
def init(
self: "ClientTest", client: Client, request: FixtureRequest
) -> Generator[None, None, None]:
ClientTest.gc = client
name = self.get_temporary_spreadsheet_title(request.node.name)
ClientTest.spreadsheet = client.create(name)
Expand Down
23 changes: 13 additions & 10 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import itertools
import os
import unittest
from typing import Any
from typing import Any, Dict, Generator, Optional

import pytest
from google.auth.credentials import Credentials
from google.oauth2.credentials import Credentials as UserCredentials
from google.oauth2.service_account import Credentials as ServiceAccountCredentials
from requests import Response
Expand All @@ -26,21 +27,23 @@
I18N_STR = "Iñtërnâtiônàlizætiøn" # .encode('utf8')


def read_credentials(filename):
def read_credentials(filename: str) -> Credentials:
return ServiceAccountCredentials.from_service_account_file(filename, scopes=SCOPE)


def prefixed_counter(prefix, start=1):
def prefixed_counter(prefix: str, start: int = 1) -> Generator[str, None, None]:
c = itertools.count(start)
for value in c:
yield "{} {}".format(prefix, value)


def get_method_name(self_id):
def get_method_name(self_id: str) -> str:
return self_id.split(".")[-1]


def ignore_retry_requests(response):
def ignore_retry_requests(
response: Dict[str, Dict[str, int]]
) -> Optional[Dict[str, Dict[str, int]]]:
SKIP_RECORD = [408, 429]
if response["status"]["code"] in SKIP_RECORD:
return None # do not record
Expand Down Expand Up @@ -72,14 +75,14 @@ class DummyCredentials(UserCredentials):

class GspreadTest(unittest.TestCase):
@classmethod
def get_temporary_spreadsheet_title(cls, suffix=""):
def get_temporary_spreadsheet_title(cls, suffix: str = "") -> str:
return "Test {} {}".format(cls.__name__, suffix)

@classmethod
def get_cassette_name(cls):
def get_cassette_name(cls) -> str:
return cls.__name__

def _sequence_generator(self):
def _sequence_generator(self) -> Generator[str, None, None]:
return prefixed_counter(get_method_name(self.id()))


Expand All @@ -105,8 +108,8 @@ def request(self, *args: Any, **kwargs: Any) -> Response:


@pytest.fixture(scope="module")
def client():
if CREDS_FILENAME:
def client() -> Client:
if CREDS_FILENAME is not None:
auth_credentials = read_credentials(CREDS_FILENAME)
else:
auth_credentials = DummyCredentials(DUMMY_ACCESS_TOKEN)
Expand Down
16 changes: 12 additions & 4 deletions tests/worksheet_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,34 @@
import random
import re
from inspect import signature
from typing import Generator

import pytest
from pytest import FixtureRequest

import gspread
from gspread import utils
from gspread.client import Client
from gspread.exceptions import APIError, GSpreadException
from gspread.spreadsheet import Spreadsheet
from gspread.worksheet import Worksheet

from .conftest import I18N_STR, GspreadTest


class WorksheetTest(GspreadTest):
"""Test for gspread.Worksheet."""

spreadsheet: Spreadsheet
sheet: Worksheet

@pytest.fixture(scope="function", autouse=True)
def init(self, client, request):
def init(
self: "WorksheetTest", client: Client, request: FixtureRequest
) -> Generator[None, None, None]:
name = self.get_temporary_spreadsheet_title(request.node.name)
WorksheetTest.spreadsheet = client.create(name)
WorksheetTest.sheet: gspread.worksheet.Worksheet = (
WorksheetTest.spreadsheet.sheet1
)
WorksheetTest.sheet = WorksheetTest.spreadsheet.sheet1

yield

Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ commands = black --check --diff --extend-exclude "./env" .
codespell --skip=".tox,.git,./docs/build,.mypy_cache,./env" .
flake8 .
isort --check-only .
mypy --install-types --non-interactive --ignore-missing-imports ./gspread
mypy --install-types --non-interactive --ignore-missing-imports ./gspread ./tests

# Used by developers to format code, best advised to be run before commit
[testenv:format]
Expand Down