Skip to content
This repository has been archived by the owner on Jan 23, 2021. It is now read-only.

Commit

Permalink
Feature/crear asalto (#87)
Browse files Browse the repository at this point in the history
* Crear asalto use case + entity business logic

* Extract crear asalto params to crear asalto request

* cambios en caso de uso llamada desde controller

* refactor variable

* WIP: Ajustes en caso de uso y pruebas con pytest

* WIP: crear asalto + tests

* WIP: crear asalto cambios en todas las capas

* FIX: Added tests to test_crear_asalto
changes on test_calificar_asalto, test_puntuar_asalto
changes on mysql_asalto_repository

* Raid modificaciones en entidad para integrar caso de uso create_raid
all tests passed

* WIP: banda_mock_repo deleted, test_crear_asalto deleted

* DOC: create_raid examples

* FEAT: create_raid (all tests passed)

* Deleted non used files

* limpieza de código

Co-authored-by: Diego Zavaleta <diegozav92@gmail.com>
  • Loading branch information
diegozavsalle and diegozav committed Dec 23, 2019
1 parent d036f0d commit 658305b
Show file tree
Hide file tree
Showing 11 changed files with 161 additions and 5 deletions.
12 changes: 12 additions & 0 deletions examples/json/create_raid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"raid": {
"id": 1,
"name": "Asalto 1",
"date": "2019-12-31 23:59:00",
"location": "Barcelona",
"gang": {},
"sheriff": {},
"outlaws": [],
"rates": []
}
}
Binary file added tests/__init__.pyc
Binary file not shown.
5 changes: 4 additions & 1 deletion tests/mock_gang_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@

class MockGangRepository(GangRepository):

def __init__(self):
self.gang = None

def of_id(self, gang_id: int) -> Gang:
pass
return self.gang

def add(self, new_gang: Gang) -> NoReturn:
pass
Expand Down
6 changes: 6 additions & 0 deletions tests/mock_outlaw_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,9 @@ def get_friends(self, outlaw_id: int) -> List[Outlaw]:

return [outlaw_1, outlaw_2, outlaw_3]

def all(self) -> [Outlaw]:
return [
Outlaw(1, "Outlaw1", "b1@yopmail.com"),
Outlaw(2, "Outlaw2", "b2@yopmail.com"),
Outlaw(3, "Outlaw3", "b3@yopmail.com")
]
30 changes: 30 additions & 0 deletions tests/test_create_raid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from tests.mock_gang_repository import MockGangRepository
from tests.mock_outlaw_repository import MockOutlawRepository
from tests.mock_raid_repository import MockRaidRepository
from thesheriff.application.raid.create_raid import CreateRaid
from thesheriff.application.raid.create_raid_request import CreateRaidRequest
from thesheriff.domain.outlaw.outlaw import Outlaw


def test_create_raid():
name = 'Asalto test'
date = '2019-12-24 23:59:00'
location = 'Barcelona'
gang_id = 1
sheriff_id = 3
outlaw_ids = [1, 2, 3]

request = CreateRaidRequest(name, date, location, gang_id, sheriff_id, outlaw_ids)

outlaw_repository = MockOutlawRepository()
outlaw_repository.outlaw = Outlaw(3, 'Sheriff', 'sheriff@banda.com')

gang_repository = MockGangRepository()
gang_repository.gang = gang_repository.all()[0]

service = CreateRaid(outlaw_repository, gang_repository, MockRaidRepository())

raid = service.execute(request)

assert raid.name == name
assert raid.sheriff.id == sheriff_id
Empty file.
72 changes: 72 additions & 0 deletions thesheriff/application/raid/create_raid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
from datetime import datetime

from thesheriff.application.raid.create_raid_request import CreateRaidRequest
from thesheriff.domain.gang.gang import Gang
from thesheriff.domain.gang.repository.gang_repository import GangRepository
from thesheriff.domain.outlaw.outlaw import Outlaw
from thesheriff.domain.outlaw.repository.outlaw_repository \
import OutlawRepository
from thesheriff.domain.outlaw.sheriff import Sheriff
from thesheriff.domain.raid.raid import Raid, DEFAULT_DATETIME_FORMAT
from thesheriff.domain.raid.repository.raid_repository import RaidRepository


class CreateRaid:
def __init__(
self,
outlaw_repository: OutlawRepository,
gang_repository: GangRepository,
raid_repository: RaidRepository
):
self.__outlaw_repository = outlaw_repository
self.__gang_repository = gang_repository
self.__raid_repository = raid_repository

def execute(self, request: CreateRaidRequest):
sheriff = self.get_sheriff_or_fail(request)
gang = self.get_gang_or_fail(request)
outlaws = self.get_outlaws_or_fail(request)

date = datetime.strptime(request.date, DEFAULT_DATETIME_FORMAT)
raid_id = 0

raid = Raid(
request.name,
request.location,
sheriff,
gang,
date,
raid_id,
outlaws
)

self.__raid_repository.add(raid)

return raid

def get_sheriff_or_fail(self, request: CreateRaidRequest) -> Sheriff:
sheriff = self.__outlaw_repository.of_id(request.sheriff_id)
if sheriff is None:
raise Exception('Sheriff with id: {0} does not exist.'
.format(request.sheriff_id))
sheriff = Sheriff(sheriff)
return sheriff

def get_gang_or_fail(self, request: CreateRaidRequest) -> Gang:
gang = self.__gang_repository.of_id(request.gang_id)
if gang is None:
raise Exception('Gang with id: {0} does not exist.'
.format(request.gang_id))
return gang

def get_outlaws_or_fail(self, request: CreateRaidRequest) -> [Outlaw]:
outlaws = []
all_outlaws = self.__outlaw_repository.all()
if outlaws is None or len(all_outlaws) < 1:
raise Exception('Outlaws does not exist')

for outlaw in all_outlaws:
if outlaw.id in request.outlaw_ids:
outlaws.append(outlaw)

return outlaws
18 changes: 18 additions & 0 deletions thesheriff/application/raid/create_raid_request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from datetime import datetime


class CreateRaidRequest:
def __init__(self,
name: str,
date: datetime,
location: str,
gang_id: str,
sheriff_id: int,
outlaw_ids: list
):
self.name = name
self.date = date
self.location = location
self.gang_id = gang_id
self.sheriff_id = sheriff_id
self.outlaw_ids = outlaw_ids
4 changes: 4 additions & 0 deletions thesheriff/domain/outlaw/repository/outlaw_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ def update(self, mod_outlaw: Outlaw) -> NoReturn:
@abc.abstractmethod
def remove(self, outlaw_id: int) -> NoReturn:
pass

@abc.abstractmethod
def all(self) -> [Outlaw]:
pass
16 changes: 12 additions & 4 deletions thesheriff/domain/raid/raid.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from thesheriff.domain.outlaw.sheriff import Sheriff
from typing import NoReturn, Optional, List

DEFAULT_DATETIME_FORMAT = '%Y-%m-%d %H:%M:%S'


class Raid:
"""Class Raid represents the Raid entity.
Expand All @@ -25,17 +27,23 @@ class Raid:
"""

def __init__(
self, name: str, location: str,
sheriff: Sheriff, gang: Gang, date: datetime,
raid_id: Optional[int] = None
self,
name: str,
location: str,
sheriff: Sheriff,
gang: Gang,
date: datetime,
raid_id: Optional[int] = None,
outlaws: Optional[Outlaw] = []
):
self.name = name
self.outlaws = list()
self.location = location
self.sheriff = sheriff
self.gang = gang
self.date = date
self.rates = []
self.id = raid_id
self.outlaws = outlaws

def add_rate(self, rate: float) -> NoReturn:
"""Method add_rate, adds a new rate for the Raid.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,6 @@ def get_friends(self, outlaw_id: int) -> List[Outlaw]:
query = self.__outlaw_table.select().where(
self.__outlaw_table.c.id == outlaw_id)
return self.__connection.execute(query)

def all(self) -> [Outlaw]:
raise Exception('to implement')

0 comments on commit 658305b

Please sign in to comment.