def test_bad_assignment(self): class Blog(BabushkaModel): pass class Post(BabushkaModel): blog = model.KeyProperty(kind='Blog', required=False) title = model.StringProperty(required=True) _cache_break = 'badattr' blog = Blog.get_or_insert('main') post = Post(parent=blog.key, title='title') self.assertRaises(BabushkaModelError, post.put) Post._cache_break = 'title' post = Post(parent=blog.key, title='title') self.assertRaises(BabushkaModelError, post.put) Post._cache_break = 'blog' post = Post(parent=blog.key, title='title') post.put()
def test_method_name_default(): class BlogSerializer(serializers.ModelSerializer): one_entry = SerializerMethodResourceRelatedField(model=Entry) class Meta: model = Blog fields = ['one_entry'] def get_one_entry(self, instance): return Entry(id=100) serializer = BlogSerializer(instance=Blog()) assert serializer.data['one_entry']['id'] == '100'
def test_method_name_custom(): class BlogSerializer(serializers.ModelSerializer): one_entry = SerializerMethodResourceRelatedField( model=Entry, method_name="get_custom_entry") class Meta: model = Blog fields = ["one_entry"] def get_custom_entry(self, instance): return Entry(id=100) serializer = BlogSerializer(instance=Blog()) assert serializer.data["one_entry"]["id"] == "100"
def test_creation(self): self.register_model('Blog', Blog) self.register_model('Post', Post) data = yaml.load(open('example/data.yaml')) blog = Blog.get_or_insert(data['blog']['name'], name=data['blog']['name']) for post in data['blog']['posts']: post = Post(parent=blog.key, **post) post.put() for post in data['blog']['posts']: post = Post(blog=blog.key, **post) post.put()
def test_cache_keys(self): class Blog(BabushkaModel): pass class Post(BabushkaModel): blog = model.KeyProperty(kind='Blog', required=False) title = model.StringProperty(required=True) blog = Blog.get_or_insert('main') post = Post(parent=blog.key, title='title') post.put() old_blog_cache_key = blog.cache_key old_post_cache_key = post.cache_key post.title = 'new title' post.put() blog = Blog.get_by_id('main') self.assertNotEqual(blog.cache_key, old_blog_cache_key) self.assertNotEqual(post.cache_key, old_post_cache_key) class Blog(BabushkaModel): pass class Post(BabushkaModel): blog = model.KeyProperty(kind='Blog', required=False) title = model.StringProperty(required=True) blog = Blog.get_or_insert('main') post = Post(blog=blog.key, title='title') post.put() old_blog_cache_key = blog.cache_key old_post_cache_key = post.cache_key post.title = 'new title' post.put() blog = Blog.get_by_id('main') self.assertEqual(blog.cache_key, old_blog_cache_key) self.assertNotEqual(post.cache_key, old_post_cache_key) class Blog(BabushkaModel): pass class Post(BabushkaModel): blog = model.KeyProperty(kind='Blog', required=False) title = model.StringProperty(required=True) _cache_break = 'blog' blog = Blog.get_or_insert('main') post = Post(blog=blog.key, title='title') post.put() old_blog_cache_key = blog.cache_key old_post_cache_key = post.cache_key post.title = 'new title' post.put() blog = Blog.get_by_id('main') self.assertNotEqual(blog.cache_key, old_blog_cache_key) self.assertNotEqual(post.cache_key, old_post_cache_key)