Skip to content

Commit

Permalink
add separator
Browse files Browse the repository at this point in the history
uplift coverage

Uplift coverage

fix tests
  • Loading branch information
aminalaee committed Sep 5, 2022
1 parent ab4cdf0 commit fcf9564
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 16 deletions.
10 changes: 1 addition & 9 deletions sqladmin/ajax.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import TYPE_CHECKING, Any, Dict, List

from sqlalchemy import String, and_, cast, inspect, or_, select, text
from sqlalchemy import String, cast, inspect, or_, select, text

from sqladmin.helpers import get_primary_key

Expand All @@ -24,7 +24,6 @@ def __init__(
self.model_admin = model_admin
self.fields = options.get("fields", {})
self.order_by = options.get("order_by")
self.filters = options.get("filters")

if not self.fields:
raise ValueError(
Expand Down Expand Up @@ -67,13 +66,6 @@ async def get_list(self, term: str, limit: int = DEFAULT_PAGE_SIZE) -> List[Any]

stmt = stmt.filter(or_(*filters))

if self.filters:
filter_options = [
text("%s.%s" % (self.model.__tablename__.lower(), value)) # type: ignore
for value in self.filters
]
stmt = stmt.filter(and_(*filter_options))

if self.order_by:
stmt = stmt.order_by(self.order_by)

Expand Down
1 change: 1 addition & 0 deletions sqladmin/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ def pre_validate(self, form: Form) -> None:

class AjaxSelectMultipleField(SelectFieldBase):
widget = sqladmin_widgets.AjaxSelect2Widget(multiple=True)
separator = ","

def __init__(
self,
Expand Down
3 changes: 2 additions & 1 deletion tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"TEST_DATABASE_URI_SYNC", "sqlite:///test.db?check_same_thread=False"
)
test_database_uri_async = os.environ.get(
"TEST_DATABASE_URI_ASYNC", "sqlite+aiosqlite:///test.db?check_same_thread=False"
"TEST_DATABASE_URI_ASYNC",
"postgresql+asyncpg://postgres:postgres@localhost/test_db",
)

sync_engine = create_engine(test_database_uri_sync)
Expand Down
42 changes: 36 additions & 6 deletions tests/test_ajax.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import pytest
from httpx import AsyncClient
from sqlalchemy import Column, ForeignKey, Integer, String
from sqlalchemy import Column, ForeignKey, Integer, String, select
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, sessionmaker
from sqlalchemy.orm import relationship, selectinload, sessionmaker
from starlette.applications import Starlette

from sqladmin import Admin, ModelView
Expand Down Expand Up @@ -60,6 +60,7 @@ class AddressAdmin(ModelView, model=Address):
form_ajax_refs = {
"user": {
"fields": ("name",),
"order_by": ("id"),
}
}

Expand Down Expand Up @@ -118,13 +119,13 @@ async def test_create_ajax_loader_exceptions() -> None:

async def test_create_page_template(client: AsyncClient) -> None:
response = await client.get("/admin/user/create")
assert response.status_code == 200

assert response.text.count('data-json="[]"') == 1
assert response.text.count('data-role="select2-ajax"') == 1
assert response.text.count('data-url="/admin/user/ajax/lookup"') == 1

response = await client.get("/admin/address/create")
assert response.status_code == 200

assert response.text.count('data-role="select2-ajax"') == 1
assert response.text.count('data-url="/admin/address/ajax/lookup"') == 1

Expand All @@ -140,7 +141,6 @@ async def test_edit_page_template(client: AsyncClient) -> None:
await s.commit()

response = await client.get("/admin/user/edit/1")
assert response.status_code == 200
assert (
response.text.count(
'data-json="[{"id": 1, "text": "Address 1"}]"'
Expand All @@ -151,7 +151,6 @@ async def test_edit_page_template(client: AsyncClient) -> None:
assert response.text.count('data-url="/admin/user/ajax/lookup"') == 1

response = await client.get("/admin/address/edit/1")
assert response.status_code == 200
assert (
response.text.count(
'data-json="[{"id": 1, "text": "User 1"}]"'
Expand All @@ -160,3 +159,34 @@ async def test_edit_page_template(client: AsyncClient) -> None:
)
assert response.text.count('data-role="select2-ajax"') == 1
assert response.text.count('data-url="/admin/address/ajax/lookup"') == 1


async def test_crete_and_edit_forms(client: AsyncClient) -> None:
response = await client.post("/admin/address/create", data={})
assert response.status_code == 302

data = {"addresses": ["1"], "name": "Tyrion"}
response = await client.post("/admin/user/create", data=data)
assert response.status_code == 302

data = {}
response = await client.post("/admin/address/edit/1", data=data)
assert response.status_code == 302

async with LocalSession() as s:
stmt = select(User).options(selectinload(User.addresses))
result = await s.execute(stmt)

user = result.scalar_one()
assert len(user.addresses) == 0

data = {"addresses": ["1"]}
response = await client.post("/admin/user/edit/1", data=data)
assert response.status_code == 302

async with LocalSession() as s:
stmt = select(User).options(selectinload(User.addresses))
result = await s.execute(stmt)

user = result.scalar_one()
assert len(user.addresses) == 1

0 comments on commit fcf9564

Please sign in to comment.