Skip to content

Commit

Permalink
Merge pull request #195 from WinterFramework/custom_crud_methods
Browse files Browse the repository at this point in the history
  • Loading branch information
pristupa authored Apr 6, 2021
2 parents 2bc9dfe + cd884eb commit 5c40e8d
Show file tree
Hide file tree
Showing 21 changed files with 222 additions and 250 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [9.0.0] - 2021-03-31

### New features
- CRUDRepository now supports custom implementation extensions
### Changed
- set_factory changed to set_injector
- DomainEventDispatcher.set_handler_factory is deleted

## [8.2.0] - 2021-01-27

### New features
Expand Down
18 changes: 18 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
from pathlib import Path

import django
from injector import CallableProvider
from injector import Injector
from injector import Module
from injector import singleton
from sqlalchemy import create_engine
from sqlalchemy.engine import Engine

from winter.core import set_injector
from .entities import Guest


Expand All @@ -28,4 +35,15 @@ def pytest_configure():
'tests',
),
)
injector = Injector([Configuration])
set_injector(injector)
django.setup()


class Configuration(Module):
def configure(self, binder):
binder.bind(Engine, to=CallableProvider(make_engine), scope=singleton)


def make_engine():
return create_engine('sqlite://')
2 changes: 1 addition & 1 deletion tests/controllers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from .controller_with_exceptions import ControllerWithExceptions
from .controller_with_request_data import ControllerWithRequestData
from .controller_with_limits import ControllerWithLimits
from .controller_with_media_types_routing import ControllerWithMediaTypesRouting
from .controller_with_path_parameters import ControllerWithPathParameters
from .controller_with_problem_exceptions import ControllerWithProblemExceptions
from .controller_with_query_parameters import ControllerWithQueryParameters
from .controller_with_request_data import ControllerWithRequestData
from .controller_with_response_headers import ControllerWithResponseHeaders
from .controller_with_throttling import ControllerWithThrottling
from .no_authentication_controller import NoAuthenticationController
Expand Down
2 changes: 1 addition & 1 deletion tests/controllers/controller_with_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def handle(self, exception: CustomException) -> int:

class WithUnknownArgumentExceptionHandler(winter.web.ExceptionHandler):
@winter.response_status(400)
def handle(self, exception: CustomException, unknown_argument: int) -> str: # pragma: no cover
def handle(self, exception: WithUnknownArgumentException, unknown_argument: int) -> str: # pragma: no cover
pass


Expand Down
12 changes: 7 additions & 5 deletions tests/core/test_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,13 @@ def test_get_one():
assert annotation == SimpleAnnotation('first')


@pytest.mark.parametrize(('decorator_factory', 'error_message_template'), (
(annotate, 'Need function or class. Got: {instance}'),
(annotate_class, 'Need class. Got: {instance}'),
(annotate_method, 'Need function. Got: {instance}'),
))
@pytest.mark.parametrize(
('decorator_factory', 'error_message_template'), (
(annotate, 'Need function or class. Got: {instance}'),
(annotate_class, 'Need class. Got: {instance}'),
(annotate_method, 'Need function. Got: {instance}'),
),
)
def test_annotate_with_instance(decorator_factory, error_message_template):
instance = object()

Expand Down
4 changes: 2 additions & 2 deletions tests/core/test_component_method_argument.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

def test_parameter():
def test(number: int) -> int: # pragma: no cover
return number
pass

method = ComponentMethod(test)
argument = method.get_argument('number')
Expand All @@ -18,7 +18,7 @@ def test(number: int) -> int: # pragma: no cover

def test_default():
def test(number: int) -> int: # pragma: no cover
return number
pass

method = ComponentMethod(test)
argument = method.get_argument('number')
Expand Down
6 changes: 6 additions & 0 deletions tests/core/test_injection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from winter.core import get_injector


def test_get_injector():
injector = get_injector()
assert injector is not None
30 changes: 17 additions & 13 deletions tests/routing/test_path_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,23 @@
from tests.controllers.controller_with_path_parameters import ControllerWithPathParameters
from tests.controllers.controller_with_path_parameters import OneTwoEnum
from tests.controllers.controller_with_path_parameters import OneTwoEnumWithInt
from winter.core import Component
from winter.web.argument_resolver import ArgumentNotSupported
from winter.web.controller import get_component
from winter.core import Component
from winter.web.path_parameters_argument_resolver import PathParametersArgumentResolver

uuid_ = uuid.uuid4()


@pytest.mark.parametrize('path, arg_name, expected_value', [
(f'/controller_with_path_parameters/123/456/one/{uuid_}/2/', 'param1', '123'),
(f'/controller_with_path_parameters/123/456/one/{uuid_}/2/', 'param2', 456),
(f'/controller_with_path_parameters/123/456/one/{uuid_}/2/', 'param3', OneTwoEnum.ONE),
(f'/controller_with_path_parameters/123/456/one/{uuid_}/2/', 'param4', uuid_),
(f'/controller_with_path_parameters/123/456/one/{uuid_}/2/', 'param5', OneTwoEnumWithInt.TWO),
])
@pytest.mark.parametrize(
'path, arg_name, expected_value', [
(f'/controller_with_path_parameters/123/456/one/{uuid_}/2/', 'param1', '123'),
(f'/controller_with_path_parameters/123/456/one/{uuid_}/2/', 'param2', 456),
(f'/controller_with_path_parameters/123/456/one/{uuid_}/2/', 'param3', OneTwoEnum.ONE),
(f'/controller_with_path_parameters/123/456/one/{uuid_}/2/', 'param4', uuid_),
(f'/controller_with_path_parameters/123/456/one/{uuid_}/2/', 'param5', OneTwoEnumWithInt.TWO),
],
)
def test_resolve_path_parameter(path, arg_name, expected_value):
component = Component.get_by_cls(ControllerWithPathParameters)
argument = component.get_method('test').get_argument(arg_name)
Expand All @@ -36,11 +38,13 @@ def test_resolve_path_parameter(path, arg_name, expected_value):
assert result == expected_value


@pytest.mark.parametrize('controller_class, method_name, arg_name, expected_value', [
(ControllerWithPathParameters, 'test', 'param1', True),
(ControllerWithPathParameters, 'test', 'param2', True),
(ControllerWithPathParameters, 'test', 'param6', False),
])
@pytest.mark.parametrize(
'controller_class, method_name, arg_name, expected_value', [
(ControllerWithPathParameters, 'test', 'param1', True),
(ControllerWithPathParameters, 'test', 'param2', True),
(ControllerWithPathParameters, 'test', 'param6', False),
],
)
def test_is_supported_path_parameter(controller_class, method_name, arg_name, expected_value):
component = get_component(controller_class)
argument = component.get_method(method_name).get_argument(arg_name)
Expand Down
37 changes: 0 additions & 37 deletions tests/test_get_instance.py

This file was deleted.

61 changes: 0 additions & 61 deletions tests/winter_sqlalchemy/test_injector.py
Original file line number Diff line number Diff line change
@@ -1,61 +0,0 @@
from typing import Optional

from injector import ClassProvider
from injector import Injector
from injector import Module
from injector import singleton
from sqlalchemy import Column
from sqlalchemy import Integer
from sqlalchemy import MetaData
from sqlalchemy import String
from sqlalchemy import Table
from sqlalchemy import create_engine
from sqlalchemy.engine import Engine
from sqlalchemy.orm import mapper

from winter.data import CRUDRepository
from winter_ddd import AggregateRoot
from winter_sqlalchemy import sqla_crud


class Fixture:
def __init__(self):
class MyEntity(AggregateRoot):
pass

engine = self._engine = create_engine('sqlite://')
metadata = MetaData()
my_entity_table = Table(
'my_entities',
metadata,
Column('id', Integer, primary_key=True),
Column('name', String),
)
mapper(MyEntity, my_entity_table)

class MyRepository(CRUDRepository[MyEntity, int]):
pass

metadata.create_all(bind=self._engine)

class Configuration(Module):
def configure(self, binder):
binder.bind(Engine, engine)
binder.bind(MyRepository, to=ClassProvider(sqla_crud(MyRepository)), scope=singleton)

configuration = Configuration()
injector = Injector([configuration])
self.repository = injector.get(MyRepository)

def execute(self, sql):
return self._engine.execute(sql)


def test_count():
fixture = Fixture()
fixture.execute('INSERT INTO my_entities (id) VALUES (1), (2), (3);')

# Act
count = fixture.repository.count()

assert count == 3
Loading

0 comments on commit 5c40e8d

Please sign in to comment.