def test_book_entity_should_be_identified_by_id(self): book_1 = Book( id="book_01", isbn=Isbn("978-0321125217"), title= "Domain-Driven Design: Tackling Complexity in the Heart of Softwares", page=560, read_page=50, ) book_2 = Book( id="book_01", isbn=Isbn("978-0321125217"), title= "Domain-Driven Design: Tackling Complexity in the Heart of Softwares", page=560, read_page=120, ) book_3 = Book( id="book_02", isbn=Isbn("978-0321125217"), title= "Domain-Driven Design: Tackling Complexity in the Heart of Softwares", page=560, read_page=50, ) assert book_1 == book_2 assert book_1 != book_3
def test_is_already_read_should_true_when_read_page_has_reached_last_page( self, read_page, expected): book = Book( id="book_01", isbn=Isbn("978-0321125217"), title= "Domain-Driven Design: Tackling Complexity in the Heart of Softwares", page=560, ) book.read_page = read_page assert book.is_already_read() == expected
def test_read_page_setter_should_update_value(self, read_page): book = Book( id="book_01", isbn=Isbn("978-0321125217"), title= "Domain-Driven Design: Tackling Complexity in the Heart of Softwares", page=560, ) book.read_page = read_page assert book.read_page == read_page
def update_book(self, id: str, data: BookUpdateModel) -> Optional[BookReadModel]: try: existing_book = self.uow.book_repository.find_by_id(id) if existing_book is None: raise BookNotFoundError book = Book( id=id, isbn=existing_book.isbn, title=data.title, page=data.page, read_page=data.read_page, ) self.uow.book_repository.update(book) updated_book = self.uow.book_repository.find_by_id(book.id) self.uow.commit() except: self.uow.rollback() raise return BookReadModel.from_entity(cast(Book, updated_book))
def to_entity(self) -> Book: return Book( id=self.id, isbn=Isbn(self.isbn), title=self.title, page=self.page, read_page=self.read_page, created_at=self.created_at, updated_at=self.updated_at, )
def test_constructor_should_create_instance(self): book = Book( id="book_01", isbn=Isbn("978-0321125217"), title= "Domain-Driven Design: Tackling Complexity in the Heart of Softwares", page=560, ) assert book.id == "book_01" assert book.isbn == Isbn("978-0321125217") assert ( book.title == "Domain-Driven Design: Tackling Complexity in the Heart of Softwares" ) assert book.page == 560 assert book.read_page == 0
def create_book(self, data: BookCreateModel) -> Optional[BookReadModel]: try: uuid = shortuuid.uuid() isbn = Isbn(data.isbn) book = Book(id=uuid, isbn=isbn, title=data.title, page=data.page) existing_book = self.uow.book_repository.find_by_isbn(isbn.value) if existing_book is not None: raise BookIsbnAlreadyExistsError self.uow.book_repository.create(book) self.uow.commit() created_book = self.uow.book_repository.find_by_id(uuid) except: self.uow.rollback() raise return BookReadModel.from_entity(cast(Book, created_book))
def test_from_entity_should_create_dto_instance(self): book = Book( id="book_01", isbn=Isbn("978-0321125217"), title="Domain-Driven Design: Tackling Complexity in the Heart of Softwares", page=560, read_page=120, created_at=1614007224642, updated_at=1614007224642, ) book_dto = BookDTO.from_entity(book) assert book_dto.id == "book_01" assert book_dto.isbn == "978-0321125217" assert ( book_dto.title == "Domain-Driven Design: Tackling Complexity in the Heart of Softwares" ) assert book_dto.page == 560 assert book_dto.read_page == 120