Skip to content

Commit

Permalink
Flake
Browse files Browse the repository at this point in the history
  • Loading branch information
vanatteveldt committed Feb 5, 2025
1 parent 0217f18 commit 0d1e479
Show file tree
Hide file tree
Showing 18 changed files with 29 additions and 47 deletions.
2 changes: 1 addition & 1 deletion amcat4/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from amcat4.config import get_settings, AuthOptions, validate_settings
from amcat4.elastic import connect_elastic, get_system_version, ping
from amcat4.index import GLOBAL_ROLES, create_index, set_global_role, Role, list_global_users, upload_documents
from amcat4.models import ElasticType, FieldType
from amcat4.models import FieldType

SOTU_INDEX = "state_of_the_union"

Expand Down
2 changes: 0 additions & 2 deletions amcat4/api/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"""AmCAT4 API."""

from contextlib import asynccontextmanager
import logging
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from fastapi.middleware.cors import CORSMiddleware
Expand Down
1 change: 0 additions & 1 deletion amcat4/api/auth.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Helper methods for authentication and authorization."""

from argparse import ONE_OR_MORE
import functools
import logging
from datetime import datetime
Expand Down
5 changes: 2 additions & 3 deletions amcat4/api/index.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""API Endpoints for document and index management."""

from http import HTTPStatus
from re import U
from typing import Annotated, Any, Literal

import elasticsearch
Expand Down Expand Up @@ -184,8 +183,8 @@ def upload_documents(
description="The operation to perform. Default is create, which ignores any documents that already exist. "
"The 'update' operation behaves as an upsert (create or update). If an identical document (or document with "
"identical identifiers) already exists, the uploaded fields will be created or overwritten. If there are fields "
"in the original document that are not in the uploaded document, they will NOT be removed. since update is destructive "
"it requires admin rights."
"in the original document that are not in the uploaded document, they will NOT be removed. "
"Since update is destructive it requires admin rights."
),
] = "create",
user: str = Depends(authenticated_user),
Expand Down
7 changes: 2 additions & 5 deletions amcat4/api/query.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
"""API Endpoints for querying."""

from re import search
from typing import Annotated, Dict, List, Optional, Any, Union, Iterable, Literal

from fastapi import APIRouter, HTTPException, status, Depends, Response, Body
from pydantic import InstanceOf
from fastapi import APIRouter, HTTPException, status, Depends, Body
from pydantic.main import BaseModel

from amcat4 import query, aggregate
from amcat4.aggregate import Axis, Aggregation
from amcat4.api.auth import authenticated_user, check_fields_access, check_role
from amcat4.config import AuthOptions, get_settings
from amcat4.fields import create_fields
from amcat4.index import Role, get_role, get_fields, update_documents_by_query
from amcat4.index import Role, get_role, get_fields
from amcat4.models import FieldSpec, FilterSpec, FilterValue, SortSpec
from amcat4.query import update_query, update_tag_query

Expand Down
5 changes: 3 additions & 2 deletions amcat4/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import datetime
import json
from typing import Any, Iterator, Literal, Mapping, get_args, cast
from typing import Any, Iterator, Mapping, get_args, cast


from elasticsearch import NotFoundError
Expand Down Expand Up @@ -252,7 +252,8 @@ def update_fields(index: str, fields: dict[str, UpdateField]):
raise ValueError(f"Invalid field type: {new_settings.type}")
if current.elastic_type not in valid_es_types:
raise ValueError(
f"Field {field} has the elastic type {current.elastic_type}. A {new_settings.type} field can only have the following elastic types: {valid_es_types}."
f"Field {field} has the elastic type {current.elastic_type}. A {new_settings.type} "
"field can only have the following elastic types: {valid_es_types}."
)
current_fields[field].type = new_settings.type

Expand Down
4 changes: 2 additions & 2 deletions amcat4/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ def create_id(document: dict, field_settings: dict[str, Field]) -> str:
Create the _id for a document.
"""

identifiers = [k for k, v in field_settings.items() if v.identifier == True]
identifiers = [k for k, v in field_settings.items() if v.identifier is True]
if len(identifiers) == 0:
raise ValueError("Can only create id if identifiers are specified")

Expand Down Expand Up @@ -465,7 +465,7 @@ def es_actions(index, documents, op_type):
else:
if has_identifiers:
action["_id"] = create_id(document, field_settings)
## if no id is given, elasticsearch creates a cool unique one
# if no id is given, elasticsearch creates a cool unique one

# https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html
if op_type == "update":
Expand Down
4 changes: 1 addition & 3 deletions amcat4/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from curses import OK
from xml.dom.domreg import registered
import pydantic
from pydantic import BaseModel, field_validator, model_validator, validator
from pydantic import BaseModel, model_validator
from typing import Annotated, Any, Literal
from typing_extensions import Self

Expand Down
3 changes: 0 additions & 3 deletions amcat4/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@

from typing import (
Union,
Sequence,
Any,
Dict,
Tuple,
Literal,
)

from urllib3 import Retry

from amcat4.models import FieldSpec, FilterSpec, SortSpec

from .date_mappings import mappings
Expand Down
7 changes: 2 additions & 5 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import logging
from typing import Any, AsyncGenerator, AsyncIterable
from typing import Any
import pytest
import pytest_asyncio
import responses
from fastapi.testclient import TestClient
from httpx import ASGITransport, AsyncClient

from amcat4 import api
from amcat4.config import get_settings, AuthOptions
Expand Down Expand Up @@ -143,7 +140,7 @@ def upload(index: str, docs: list[dict[str, Any]], fields: dict[str, FieldType |
"""
Upload these docs to the index, giving them an incremental id, and flush
"""
res = upload_documents(index, docs, fields)
upload_documents(index, docs, fields)
refresh_index(index)


Expand Down
2 changes: 1 addition & 1 deletion tests/test_aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from amcat4.aggregate import query_aggregate, Axis, Aggregation
from amcat4.api.query import _standardize_queries
from amcat4.models import CreateField, Field
from amcat4.models import CreateField
from tests.conftest import upload
from tests.tools import dictset

Expand Down
5 changes: 2 additions & 3 deletions tests/test_api_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from amcat4 import elastic

from amcat4.index import GuestRole, get_guest_role, Role, set_guest_role, set_role, remove_role
from amcat4.fields import update_fields
from tests.tools import build_headers, post_json, get_json, check, refresh


Expand Down Expand Up @@ -96,9 +95,9 @@ def test_fields_upload(client: TestClient, user: str, index: str):

set_role(index, user, Role.METAREADER)

## can get fields
# can get fields
fields = get_json(client, f"/index/{index}/fields", user=user) or {}
## but should still be empty, since no fields were created
# but should still be empty, since no fields were created
assert len(set(fields.keys())) == 0
check(
client.post(f"/index/{index}/documents", headers=build_headers(user), json=body),
Expand Down
2 changes: 1 addition & 1 deletion tests/test_api_pagination.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from amcat4.index import Role, set_role
from amcat4.models import CreateField
from tests.conftest import upload
from tests.tools import get_json, post_json
from tests.tools import post_json


def test_pagination(client, index, user):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_api_query.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from amcat4.index import Role, refresh_index, set_role
from amcat4.index import Role, set_role
from amcat4.models import CreateField, FieldSpec
from amcat4.query import query_documents
from tests.conftest import upload
Expand Down
5 changes: 2 additions & 3 deletions tests/test_elastic.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from datetime import datetime
from re import I

import pytest

Expand All @@ -11,7 +10,7 @@
update_document,
update_tag_by_query,
)
from amcat4.fields import create_fields, update_fields, get_fields, field_values
from amcat4.fields import create_fields, get_fields, field_values
from amcat4.models import CreateField, FieldSpec
from amcat4.query import query_documents
from tests.conftest import upload
Expand Down Expand Up @@ -55,7 +54,7 @@ def test_fields(index):
assert fields["date"].type == "date"

# default settings
assert fields["date"].identifier == False
assert fields["date"].identifier is False
assert fields["date"].client_settings is not None

# default settings depend on the type
Expand Down
2 changes: 0 additions & 2 deletions tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
set_guest_role,
set_role,
)
from amcat4.fields import update_fields
from amcat4.models import Field
from tests.tools import refresh


Expand Down
16 changes: 8 additions & 8 deletions tests/test_middlecat.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
def test_handler_responses(client: TestClient, admin):
def test(expected=200, **payload):
token = create_token(**payload)
headers = {'Authorization': f"Bearer {token}"}
headers = {"Authorization": f"Bearer {token}"}
return get_json(client, "/users/me", headers=headers, expected=expected)

# Guests have no /me
Expand All @@ -25,16 +25,16 @@ def test(expected=200, **payload):

# A valid token needs a valid resource, expiry, and email
now = int(datetime.now().timestamp())
test(resource='http://localhost:3000', email=admin, expected=401)
test(exp=now+1000, email=admin, expected=401)
assert test(resource='http://localhost:3000', exp=now + 1000, email=admin)['email'] == admin
test(resource="http://localhost:3000", email=admin, expected=401)
test(exp=now + 1000, email=admin, expected=401)
assert test(resource="http://localhost:3000", exp=now + 1000, email=admin)["email"] == admin
# Expired tokens don't work
test(resource='http://localhost:3000', exp=now - 1000, email=admin, expected=401)
test(resource="http://localhost:3000", exp=now - 1000, email=admin, expected=401)
# Wrong resource
test(resource='http://wrong.com', exp=now + 1000, email=admin, expected=401)
test(resource="http://wrong.com", exp=now + 1000, email=admin, expected=401)


def test_config(client: TestClient):
result = get_json(client, "/config")
assert result['middlecat_url'] == get_settings().middlecat_url
assert result['authorization'] == get_settings().auth.name
assert result["middlecat_url"] == get_settings().middlecat_url
assert result["authorization"] == get_settings().auth.name
2 changes: 1 addition & 1 deletion tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import Set, Optional

from amcat4 import query
from amcat4.models import FieldSpec, FilterSpec, FilterValue, SnippetParams, UpdateField
from amcat4.models import FieldSpec, FilterSpec, FilterValue, SnippetParams
from amcat4.api.query import _standardize_queries, _standardize_filters
from tests.conftest import upload

Expand Down

0 comments on commit 0d1e479

Please sign in to comment.