Переглянути джерело

Add Movie model, test and migrations

Benoît Hubert 7 роки тому
батько
коміт
04ebb7850b

+ 13 - 5
functional_tests.py

@@ -11,6 +11,11 @@ class NewVisitorTest(unittest.TestCase):
     def tearDown(self):
         self.browser.quit()
 
+    def check_for_row_in_list_table(self, row_text):
+        table = self.browser.find_element_by_id('movie-table')
+        rows = table.find_elements_by_tag_name('tr')
+        self.assertIn(row_text, [row.text for row in rows])
+
     def test_can_start_a_list_and_retrieve_it_later(self):
         # Edith has heard about a cool new online to-do app. She goes
         # to check out its homepage
@@ -35,12 +40,15 @@ class NewVisitorTest(unittest.TestCase):
         # "1: Inception" as an item in a to-do list table
         inputbox.send_keys(Keys.ENTER)
         time.sleep(1)
+        self.check_for_row_in_list_table('1: Inception')
 
-        table = self.browser.find_element_by_id('movie-table')
-        rows = table.find_elements_by_tag_name('tr')
-        self.assertTrue(
-            any(row.text == '1: Inception' for row in rows)
-        )
+        inputbox = self.browser.find_element_by_id('input-new-movie')
+        inputbox.send_keys('The Sixth Sense')
+        inputbox.send_keys(Keys.ENTER)
+        time.sleep(1)
+
+        self.check_for_row_in_list_table('1: Inception')
+        self.check_for_row_in_list_table('2: The Sixth Sense')
 
         # There is still a text box inviting her to add another item. She
         # enters "Armageddon"

+ 20 - 0
movies/migrations/0001_initial.py

@@ -0,0 +1,20 @@
+# Generated by Django 2.0.2 on 2018-02-10 17:59
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    initial = True
+
+    dependencies = [
+    ]
+
+    operations = [
+        migrations.CreateModel(
+            name='Movie',
+            fields=[
+                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
+            ],
+        ),
+    ]

+ 18 - 0
movies/migrations/0002_movie_title.py

@@ -0,0 +1,18 @@
+# Generated by Django 2.0.2 on 2018-02-10 18:00
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('movies', '0001_initial'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='movie',
+            name='title',
+            field=models.TextField(default=''),
+        ),
+    ]

+ 4 - 0
movies/models.py

@@ -1,3 +1,7 @@
 from django.db import models
 
 # Create your models here.
+from django.db import models
+
+class Movie(models.Model):
+    title = models.TextField(default='')

+ 21 - 1
movies/tests.py

@@ -3,6 +3,7 @@ from django.test import TestCase
 from django.http import HttpRequest
 
 from movies.views import home_page
+from movies.models import Movie
 
 class HomePageTest(TestCase):
 
@@ -24,4 +25,23 @@ class HomePageTest(TestCase):
 
     def test_can_save_a_POST_request(self):
         response = self.client.post('/', data={'movie-title': 'X-Men'})
-        self.assertIn('X-Men', response.content.decode())
+        self.assertIn('X-Men', response.content.decode())
+
+class MovieModelTest(TestCase):
+
+    def test_saving_and_retrieving_movies(self):
+        first_movie = Movie()
+        first_movie.title = 'The first (ever) movie'
+        first_movie.save()
+
+        second_movie = Movie()
+        second_movie.title = 'The second movie'
+        second_movie.save()
+
+        saved_movies = Movie.objects.all()
+        self.assertEqual(saved_movies.count(), 2)
+
+        first_saved_movie = saved_movies[0]
+        second_saved_movie = saved_movies[1]
+        self.assertEqual(first_saved_movie.title, 'The first (ever) movie')
+        self.assertEqual(second_saved_movie.title, 'The second movie')