示例#1
0
class Invitation(Entity):
    id: Id('email', 'organization')
    token: InvitationToken
    email: Email
    inviter: User
    organization: Organization
    sent_at: datetime.datetime
    expires_at: datetime.date

    is_inviter_from_same_organization = PredicateDescriptor(
        where('inviter.organization') == where('organization'))
    is_valid_inviter = PredicateDescriptor(
        is_inviter_from_same_organization | where('inviter.is_staff') == True  # noqa: E712
    )

    @property
    def _today(self):
        return datetime.date.today()

    @property
    def is_active(self):
        return self._today <= self.expires_at

    def accept(self):
        raise NotImplementedError
def test_where_usage(example_dict):
    predicate1 = where('foo') == 1  # type: Predicate
    predicate2 = where('bar.baz') == {'a': 1}  # type: Predicate
    assert predicate1.var_name is None
    assert predicate2.var_name is None
    assert predicate1(example_dict)
    assert predicate2(example_dict)
示例#3
0
def test_where_usage(example_dict):
    predicate1 = where('foo') == 1  # type: Predicate
    predicate2 = where('bar.baz') == {'a': 1}  # type: Predicate
    assert predicate1.var_name is None
    assert predicate2.var_name is None
    assert predicate1(example_dict)
    assert predicate2(example_dict)
示例#4
0
def test_where_usage(example_dict):
    predicate1 = where("foo") == 1  # type: Predicate
    predicate2 = where("bar.baz") == {"a": 1}  # type: Predicate
    assert predicate1.var_name is None
    assert predicate2.var_name is None
    assert predicate1(example_dict)
    assert predicate2(example_dict)
示例#5
0
class User(Entity):
    id: Id
    first_name: str
    last_name: str
    email: Email
    organization: t.List[Organization]
    is_staff: bool  # has additional privileges to invite

    is_active = PredicateDescriptor(where('active') == True)  # noqa: E712
    is_deleted = PredicateDescriptor(where('active') == False)  # noqa: E712

    @property
    def full_name(self):
        return f"{self.first_name} f{self.last_name}"
 def get_by_email(self, email):
     data = self.dao.filter(
         where('email') == email &
         entities.User.is_active &
         self.is_from_my_organization
     ).one()
     return self.create(**data)
class InvitationRepo(Repository):
    schema: Schema = Schema(entity=entities.Invitation)

    q_inviter_from_same_organization = where('inviter.organization') == where(
        'organization')
    q_valid_inviter = q_inviter_from_same_organization | \
                      where('inviter.is_staff') == True  # noqa: E712

    def get_by_email(self, email: entities.Email,
                     organization: entities.Organization):
        data = self.dao.filter((where('email') == email) &
                               (where('organization') == organization)).one()
        return self.create(**data)

    def get_by_token(self, token: entities.InvitationToken):
        data = self.dao.filter(where('token') == token).one()
        return self.create(**data)
示例#8
0
 def test_batch_insert(self, dao: TinyDbDao):
     batch = [{'foo': 'bar'}, {'foo': 'baz'}]
     result = dao.batch_insert(batch)
     assert result == (4, 5)
     assert list(dao.filter(where('foo').exists())) == batch
 def get_by_token(self, token: entities.InvitationToken):
     data = self.dao.filter(where('token') == token).one()
     return self.create(**data)
 def get_by_email(self, email: entities.Email,
                  organization: entities.Organization):
     data = self.dao.filter((where('email') == email) &
                            (where('organization') == organization)).one()
     return self.create(**data)
 def is_email_taken(self, email):
     return self.dao.filter(where('email') == email).exists()
 def is_from_my_organization(self):
     organization = self.authentication.user.organization
     return where('organization.id') == organization.id
示例#13
0
import typing as t

import pytest

from pca.data.dao import InMemoryDao
from pca.data.errors import (
    QueryError,
    QueryErrors,
)
from pca.data.predicate import where

pred_a = where("char") == "a"
pred_not_a = ~(where("char") == "a")
pred_c = where("char") == "c"
pred_z = where("char") == "z"


def get_ids(objects: t.Iterable):
    return [dto.id for dto in objects]


@pytest.fixture
def content():
    return [{"char": c, "is_a": c == "a"} for c in "abc"]


class TestConstruction:
    def test_initial_content(self, mock_container, content):
        dao = InMemoryDao(initial_content=content)
        assert list(dao.all()) == content
示例#14
0
        dao = TinyDbDao(mock_container, table_name='table_name')
        assert isinstance(dao._table._storage._storage, tinydb.storages.MemoryStorage)

    def test_db_cache(self, mock_container, path, json_dao: TinyDbDao):
        cache = TinyDbDao._db_cache
        assert cache.get(path) is json_dao._db
        second_dao = TinyDbDao(mock_container, path=path, table_name='another_table')
        assert second_dao._db is json_dao._db

    def test_no_table_name(self, mock_container):
        with pytest.raises(ConfigError) as error_info:
            TinyDbDao(mock_container)
        assert error_info.value == IntegrationErrors.NO_TABLE_NAME_PROVIDED


pred_a = where('char') == 'a'
pred_not_a = ~(where('char') == 'a')
pred_c = where('char') == 'c'
pred_z = where('char') == 'z'


class TestApi:

    @pytest.fixture
    def dao(self, mock_container):
        """
        In-memory table that has three documents pre-assigned:
            {'char': 'a', 'is_a': True},
            {'char': 'b', 'is_a': False},
            {'char': 'c', 'is_a': False}
        """
示例#15
0
 def test_batch_insert(self, dao: InMemoryDao):
     batch = [{'foo': 'bar'}, {'foo': 'baz'}]
     result = dao.batch_insert(batch)
     assert result == (4, 5)
     assert list(dao.filter(where('foo').exists())) == batch
示例#16
0
import typing as t

import pytest

from pca.data.dao import InMemoryDao
from pca.data.errors import QueryError, QueryErrors
from pca.data.predicate import where

pred_a = where('char') == 'a'
pred_not_a = ~(where('char') == 'a')
pred_c = where('char') == 'c'
pred_z = where('char') == 'z'


def get_ids(objects: t.Iterable):
    return [dto.id for dto in objects]


@pytest.fixture
def content():
    return [{'char': c, 'is_a': c == 'a'} for c in 'abc']


class TestConstruction:
    def test_initial_content(self, mock_container, content):
        dao = InMemoryDao(mock_container, initial_content=content)
        assert list(dao.all()) == content


class TestApi:
    @pytest.fixture
示例#17
0
 def test_batch_insert(self, dao: InMemoryDao):
     batch = [{"foo": "bar"}, {"foo": "baz"}]
     result = dao.batch_insert(batch)
     assert result == (4, 5)
     assert list(dao.filter(where("foo").exists())) == batch