from datetime import datetime

from freezegun import freeze_time

from app.models import Post, Tag
from tests.general import AppTestCase


class TestModels(AppTestCase):
    @freeze_time(datetime.now())
    def test_post_initialization(self):
        post = Post(title='foo', short_text='bar')
        post.save()

        self.assertEqual(datetime.now(), post.timestamp)
        self.assertEqual(post.title, 'foo')
        self.assertEqual(post.short_text, 'bar')

    def test_posts_tags_relationship(self):
        post_1 = Post(title='foo', short_text='bar')
        post_2 = Post(title='foo', short_text='bar')
        tag_1 = Tag(name='tag 1')
        tag_2 = Tag(name='tag 2')
        post_1.tags.append(tag_1)
        post_1.tags.append(tag_2)
        post_2.tags.append(tag_1)
        post_1.save()
        post_2.save()

        self.assertIn(tag_1, post_1.tags)
        self.assertIn(tag_2, post_1.tags)
        self.assertNotIn(tag_2, post_2.tags)
        self.assertIn(post_1, tag_1.posts)
        self.assertIn(post_2, tag_1.posts)
        self.assertNotIn(post_2, tag_2.posts)

    def test_post_save(self):
        post_1 = Post(title=u'Hello World! ß', short_text='foobar')
        post_1.save()

        post_2 = Post(title=u'Hello World! ß', short_text='foobar')
        post_2.save()

        self.assertEquals(post_1.slug, 'hello-world-ss')
        self.assertEquals(post_2.slug, 'hello-world-ss')

@@ -53,24 +53,66 @@
  freeze_time
+%0Afrom sqlalchemy.exc import IntegrityError
 %0A%0Afrom app.m
@@ -188,14 +188,12 @@
 Test
-Models
+Post
 (App
@@ -249,21 +249,16 @@
 ef test_
-post_
 initiali
@@ -290,35 +290,38 @@
 t = Post(title='
-foo
+title 
 ', short_text='b
@@ -319,34 +319,51 @@
 t_text='
-bar')%0A        post
+short text', long_text='long text')
 .save()%0A
@@ -423,101 +423,8 @@
 amp)
-%0A        self.assertEqual(post.title, 'foo')%0A        self.assertEqual(post.short_text, 'bar')
 %0A%0A  
@@ -438,14 +438,8 @@
 est_
-posts_
 tags
@@ -480,35 +480,38 @@
 1 = Post(title='
-foo
+post 1
 ', short_text='b
@@ -501,36 +501,50 @@
 1', short_text='
-bar'
+short text').save(
 )%0A        post_2
@@ -558,19 +558,22 @@
 (title='
-foo
+post 2
 ', short
@@ -579,20 +579,34 @@
 t_text='
-bar'
+short text').save(
 )%0A      
@@ -612,16 +612,23 @@
    tag_1
+, tag_2
  = Tag(n
@@ -643,24 +643,9 @@
  1')
-%0A        tag_2 =
+,
  Tag
@@ -683,54 +683,29 @@
 ags.
-app
+ext
 end(
+%5B
 tag_1
-)%0A        post_1.tags.append(
+, 
 tag_2
+%5D
 )%0A  
@@ -726,64 +726,22 @@
 ags.
-app
+ext
 end(
+%5B
 tag_1
-)%0A        post_1.save()%0A        post_2.save(
+%5D
 )%0A%0A 
@@ -771,32 +771,38 @@
 g_1, post_1.tags
+.all()
 )%0A        self.a
@@ -831,53 +831,14 @@
 tags
-)%0A        self.assertNotIn(tag_2, post_2.tags
+.all()
 )%0A  
@@ -868,32 +868,38 @@
 t_1, tag_1.posts
+.all()
 )%0A        self.a
@@ -925,16 +925,22 @@
 _1.posts
+.all()
 )%0A      
@@ -977,16 +977,22 @@
 _2.posts
+.all()
 )%0A%0A    d
@@ -999,21 +999,16 @@
 ef test_
-post_
 save(sel
@@ -1031,33 +1031,32 @@
 _1 = Post(title=
-u
 'Hello World! %C3%9F'
@@ -1081,31 +1081,16 @@
 ar')
-%0A        post_1
 .save()%0A
 %0A   
@@ -1077,33 +1077,32 @@
 foobar').save()%0A
-%0A
         post_2 =
@@ -1113,17 +1113,16 @@
 t(title=
-u
 'Hello W
@@ -1151,31 +1151,16 @@
 foobar')
-%0A        post_2
 .save()%0A
@@ -1254,28 +1254,179 @@
 t_2.slug, 'hello-world-ss')%0A
+%0A    def test_short_text_or_long_text_is_not_null(self):%0A        post_1 = Post(title='post 1')%0A%0A        self.assertRaises(IntegrityError, post_1.save)%0A

Rewrite Post tests