class CreateArticleDTO: title = String(required=True, max_length=250) description = Text(required=True) body = Text(required=True) tag_list = List() author = CustomObject(User, required=True)
def test_init(self): """Test successful List Field initialization""" tags = List() assert tags is not None assert tags._load(['x', 'y', 'z']) == ['x', 'y', 'z']
class ArticleDTO: slug = String(max_length=250) title = String(required=True, max_length=250) description = Text(required=True) body = Text(required=True) tag_list = List() created_at = DateTime(default=datetime.now()) updated_at = DateTime(default=datetime.now()) favorited = Boolean(default=False) favorites_count = Integer() @classmethod def for_article(cls, article: Article, user: User): favorited = article in [ favorite.article for favorite in user.favorites ] return ArticleDTO(slug=article.slug, title=article.title, description=article.description, body=article.body, tag_list=article.tag_list, created_at=article.created_at, updated_at=article.updated_at, author=article.author, favorited=favorited)
def test_init(self): """Test successful List Field initialization""" tags = List() assert tags is not None assert tags._load(["x", "y", "z"]) == ["x", "y", "z"]
def test_that_only_specific_primitive_types_are_allowed_as_content_types( test_domain): List(content_type=String) List(content_type=Identifier) List(content_type=Integer) List(content_type=Float) List(content_type=Boolean) List(content_type=Date) List(content_type=DateTime) with pytest.raises(ValidationError) as error: List(content_type=Auto) assert error.value.messages == { "content_type": ["Content type not supported"] }
def test_choice(self): """ Test choices validations for the list field """ class StatusChoices(enum.Enum): """ Set of choices for the status""" PENDING = 'Pending' SUCCESS = 'Success' ERROR = 'Error' status = List(choices=StatusChoices) assert status is not None # Test loading of values to the status field assert status._load(['Pending']) == ['Pending'] with pytest.raises(ValidationError) as e_info: status._load(['Pending', 'Failure']) assert e_info.value.messages == { 'unlinked': [ "Value `'Failure'` is not a valid choice. " "Must be one of ['Pending', 'Success', 'Error']" ] }
class CreateArticleCommand: token = String(required=True, max_length=1024) title = String(required=True, max_length=250) description = Text(required=True) body = Text(required=True) tag_list = List()
class Lottery(BaseEntity): jackpot = Boolean() numbers = List(content_type=Integer, required=True)
class Lottery(BaseEntity): numbers = List(content_type=Integer)
class IntegerListUser(BaseAggregate): email = String(max_length=255, required=True, unique=True) roles = List(content_type=Integer)
def test_type_validation(self): """ Test type checking validation for the Field""" numbers = List(content_type=Integer) with pytest.raises(ValidationError): numbers._load("x")
class GenericPostgres(BaseAggregate): ids = List() role = String()
class Article: slug = String(max_length=250) title = String(required=True, max_length=250) description = Text(required=True) body = Text(required=True) tag_list = List() created_at = DateTime(default=datetime.now()) updated_at = DateTime(default=datetime.now()) author = Reference(User, required=True) comments = HasMany('realworld.model.article.Comment') @classmethod def create(self, article_dto: CreateArticleDTO): article = Article(title=article_dto.title, slug=slugify(article_dto.title), description=article_dto.description, body=article_dto.body, tag_list=article_dto.tag_list, author=article_dto.author) current_domain.publish(TagsAdded(tag_list=article_dto.tag_list)) return article def update(self, **kwargs): valid_fields = [ field for field in kwargs if field in ['title', 'description', 'body'] ] for field in valid_fields: setattr(self, field, kwargs[field]) if 'title' in valid_fields: setattr(self, 'slug', slugify(self.title)) ################### # Comment methods # ################### def add_comment(self, body: String, logged_in_user: User): new_comment = Comment(body=body, article=self, author=logged_in_user) self.comments.add(new_comment) return self, new_comment def delete_comment(self, comment_identifier): [old_comment] = [ comment for comment in self.comments if comment.id == comment_identifier ] self.comments.remove(old_comment) return self, old_comment def get_comment_by_identifier(self, comment_identifier): [comment] = [ comment for comment in self.comments if comment.id == comment_identifier ] return comment
class TagsAdded: tag_list = List() added_at = DateTime(default=datetime.now())
class ArrayUser(BaseAggregate): email = String(max_length=255, required=True, unique=True) roles = List() # Defaulted to Text Content Type integers = List(content_type=Integer)
class NewTagsCommand: tag_list = List() added_at = DateTime(default=datetime.now())
class ListUser(BaseAggregate): email = String(max_length=255, required=True, unique=True) roles = List() # Defaulted to String Content Type