Skip to content

Commit

Permalink
update to latest version of pyairtable (fix for issue #1)
Browse files Browse the repository at this point in the history
  • Loading branch information
drkane committed Jan 9, 2024
1 parent 1c6e9fc commit ca82743
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 61 deletions.
76 changes: 25 additions & 51 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ readme = "README.md"
requires-python = ">=3.7"
license = "MIT"
keywords = []
authors = [
{ name = "David Kane", email = "[email protected]" },
]
authors = [{ name = "David Kane", email = "[email protected]" }]
classifiers = [
"Development Status :: 4 - Beta",
"Programming Language :: Python",
Expand All @@ -26,7 +24,7 @@ classifiers = [
]
dependencies = [
"click",
"pyairtable",
"pyairtable==2.2.*",
"python-dotenv",
"diskcache",
"sqlite-utils",
Expand All @@ -45,27 +43,13 @@ airtable-to-sqlite = "airtable_to_sqlite.cli:airtable_to_sqlite"
path = "src/airtable_to_sqlite/__about__.py"

[tool.hatch.envs.default]
dependencies = [
"coverage[toml]>=6.5",
"pytest",
"pytest-mock",
]
dependencies = ["coverage[toml]>=6.5", "pytest", "pytest-mock"]
[tool.hatch.envs.default.scripts]
test = "pytest {args:tests}"
test-cov = "coverage run -m pytest {args:tests}"
cov-report = [
"- coverage combine",
"coverage report",
]
cov = [
"test-cov",
"cov-report",
]
cov-fail = [
"test-cov",
"- coverage combine",
"coverage report --fail-under=95",
]
cov-report = ["- coverage combine", "coverage report"]
cov = ["test-cov", "cov-report"]
cov-fail = ["test-cov", "- coverage combine", "coverage report --fail-under=95"]
cov-html = [
"test-cov",
"- coverage combine",
Expand All @@ -79,26 +63,12 @@ python = ["3.7", "3.8", "3.9", "3.10", "3.11"]

[tool.hatch.envs.lint]
detached = true
dependencies = [
"black>=23.1.0",
"mypy>=1.0.0",
"ruff>=0.0.243",
]
dependencies = ["mypy>=1.0.0", "ruff>=0.1.11"]
[tool.hatch.envs.lint.scripts]
typing = "mypy --install-types --non-interactive {args:src/airtable_to_sqlite tests}"
style = [
"ruff {args:.}",
"black --check --diff {args:.}",
]
fmt = [
"black {args:.}",
"ruff --fix {args:.}",
"style",
]
all = [
"style",
"typing",
]
style = ["ruff {args:.}", "ruff format --check --diff {args:.}"]
fmt = ["ruff format {args:.}", "ruff --fix {args:.}", "style"]
all = ["style", "typing"]

[tool.mypy]
ignore_missing_imports = true
Expand Down Expand Up @@ -144,9 +114,16 @@ ignore = [
# Allow boolean positional values in function calls, like `dict.get(... True)`
"FBT003",
# Ignore checks for possible passwords
"S105", "S106", "S107",
"S105",
"S106",
"S107",
# Ignore complexity
"C901", "PLR0911", "PLR0912", "PLR0913", "PLR0915",
"C901",
"PLR0911",
"PLR0912",
"PLR0913",
"PLR0915",
"ISC001",
]
unfixable = [
# Don't touch unused imports
Expand All @@ -167,17 +144,14 @@ ban-relative-imports = "all"
source_pkgs = ["airtable_to_sqlite", "tests"]
branch = true
parallel = true
omit = [
"src/airtable_to_sqlite/__about__.py",
]
omit = ["src/airtable_to_sqlite/__about__.py"]

[tool.coverage.paths]
airtable_to_sqlite = ["src/airtable_to_sqlite", "*/airtable-to-sqlite/src/airtable_to_sqlite"]
airtable_to_sqlite = [
"src/airtable_to_sqlite",
"*/airtable-to-sqlite/src/airtable_to_sqlite",
]
tests = ["tests", "*/airtable-to-sqlite/tests"]

[tool.coverage.report]
exclude_lines = [
"no cov",
"if __name__ == .__main__.:",
"if TYPE_CHECKING:",
]
exclude_lines = ["no cov", "if __name__ == .__main__.:", "if TYPE_CHECKING:"]
2 changes: 1 addition & 1 deletion src/airtable_to_sqlite/__about__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SPDX-FileCopyrightText: 2023-present David Kane <[email protected]>
#
# SPDX-License-Identifier: MIT
__version__ = "0.1.0"
__version__ = "0.2.0"
4 changes: 2 additions & 2 deletions src/airtable_to_sqlite/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import pyairtable.metadata
import sqlite_utils
from pyairtable.api.api import Api as AirtableApi
from pyairtable.api.base import Base as AirtableBase
from sqlite_utils import Database
from tqdm import tqdm

Expand Down Expand Up @@ -50,7 +49,8 @@ def __init__(
) -> None:
self._base: BaseRecord = base
self._db: Database = db
self._base_api = AirtableBase(personal_access_token, base.id)
self._api = AirtableApi(personal_access_token)
self._base_api = self._api.base(base.id)
self.prefer_ids = prefer_ids
self.foreign_keys: ForeignKeySet = set()
self.table_meta: List[TableSchema] = []
Expand Down
8 changes: 5 additions & 3 deletions src/airtable_to_sqlite/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import Any, Dict, Generator, List, Optional

from pyairtable.api.base import Base as AirtableBase
from pyairtable.api.table import Table as AirtableTable

from airtable_to_sqlite.constants import (
NUMBER_FIELD_TYPES,
Expand Down Expand Up @@ -107,8 +108,9 @@ def db_name(self, prefers_ids: PreferedNamingMethod = PreferedNamingMethod.NAME)
return self.id
return self.name

def get_table_data(self, api: AirtableBase) -> Generator[Dict[str, Any], None, None]:
def get_table_data(self, base: AirtableBase) -> Generator[Dict[str, Any], None, None]:
logger.info(f"Fetching table data for {self.name} from Airtable...")
for page in api.iterate(self.name):
table: AirtableTable = base.table(self.name)
for page in table.iterate():
for record in page:
yield record
yield dict(record)
15 changes: 13 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
from unittest.mock import Mock

import pytest
from pyairtable.api.api import Api as AirtableApi
from pyairtable.api.base import Base as AirtableBase

from .dummy_returns import BASE_SCHEMA, DUMMY_RECORDS, GET_API_BASES


@pytest.fixture(name="_mock_api")
def mock_api(mocker):
mock = Mock(spec=AirtableApi)
mock.base.return_value.table.return_value.iterate.return_value = DUMMY_RECORDS

mocker.patch("airtable_to_sqlite.main.AirtableApi", return_value=mock)
return mock


@pytest.fixture(name="_mock_base")
def mock_base(mocker):
mock = Mock(spec=AirtableBase)
mock.iterate.return_value = DUMMY_RECORDS
mocker.patch("airtable_to_sqlite.main.AirtableBase", return_value=mock)
mock.table.return_value.iterate.return_value = DUMMY_RECORDS

mocker.patch("airtable_to_sqlite.schema.AirtableBase", return_value=mock)
return mock


Expand Down
4 changes: 2 additions & 2 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,10 @@ def test_viewschema_dbname():
assert f.db_name() == "View"


def test_tableschema_get_table_data(_mock_api):
def test_tableschema_get_table_data(_mock_base):
t = TableSchema(id="tbl123", name="Table", fields=[], views=[], primaryFieldId="fld123")

table_data = list(t.get_table_data(_mock_api))
table_data = list(t.get_table_data(_mock_base))
assert len(table_data) == 4
assert table_data[0] == {
"id": "rec123",
Expand Down

0 comments on commit ca82743

Please sign in to comment.