class MigrationTests(MigrationTestCase):

    create_model = migrations.CreateModel('textdocument', [
        ('title', models.CharField(max_length=128)),
        ('body', models.TextField()),
        ('search', SearchVectorField([WeightedColumn('body', 'A')],
                                     'english')),
    ])

    delete_model = migrations.DeleteModel('textdocument')

    create_model_without_search = migrations.CreateModel(
        'textdocument', [
            ('body', models.TextField()),
        ])

    add_field = migrations.AddField(
        'textdocument', 'search',
        SearchVectorField([WeightedColumn('body', 'A')], 'english'))

    alter_field = migrations.AlterField(
        'textdocument', 'search',
        SearchVectorField(
            [WeightedColumn('title', 'A'),
             WeightedColumn('body', 'D')], 'english'))

    remove_field = migrations.RemoveField('textdocument', 'search')

    def test_create_model(self):
        self.assertFITNotExists()
        self.migrate([self.create_model])
        self.assertFITExists()

    def test_add_field(self):
        self.assertFITNotExists()
        state = self.migrate([self.create_model_without_search])
        self.assertFITNotExists()
        self.migrate([self.add_field], state)
        self.assertFITExists()

    def test_remove_field(self):
        self.assertFITNotExists()
        state = self.migrate([self.create_model])
        self.assertFITExists()
        self.migrate([self.remove_field], state)
        self.assertFITNotExists()

    def test_alter_field(self):
        state = self.migrate([self.create_model])
        self.assertFITExists()
        self.assertNotIn('title', self.get_function_src('search'))
        self.migrate([self.alter_field], state)
        self.assertFITExists()
        self.assertIn('title', self.get_function_src('search'))

    def test_delete_model(self):
        state = self.migrate([self.create_model])
        self.assertFITExists()
        self.migrate([self.delete_model], state)
        self.assertFITNotExists()
Пример #2
0
 class TextDocument(models.Model):
     title = models.CharField(max_length=128)
     title2 = models.CharField(max_length=128, db_column='body')
     search = SearchVectorField([
         WeightedColumn('title', 'A'),
         WeightedColumn('body', 'D')
     ], language='english')
 class TextDocument(models.Model):
     svf = SearchVectorField([
         WeightedColumn('name', 'A'),
         WeightedColumn('description', 'D'),
     ],
                             language='english',
                             language_column='lang',
                             force_update=True)
class DataMigrationTests(MigrationTestCase):

    create_model = migrations.CreateModel('textdocument', [
        ('body', models.TextField()),
    ])

    add_field = migrations.AddField(
        'textdocument', 'search',
        SearchVectorField([WeightedColumn('body', 'A')], 'english'))

    run_indexer = IndexSearchVector('textdocument', 'search')

    def initial_state(self):
        state = self.migrate([self.create_model])
        state.apps.get_model('tests.textdocument').objects.create(
            body="My hovercraft is full of eels.")
        return state

    def test_add_field_no_data_migration(self):
        state = self.initial_state()
        self.migrate([self.add_field], state)
        doc = state.apps.get_model('tests.textdocument').objects.first()
        self.assertIsNone(doc.search)

    def test_add_field_including_data_migration(self):
        state = self.initial_state()
        self.migrate([self.add_field, self.run_indexer], state)
        doc = state.apps.get_model('tests.textdocument').objects.first()
        self.assertEqual(doc.search, "'eel':6A 'full':4A 'hovercraft':2A")
    def test_deconstruct_default_arguments(self):

        svf = SearchVectorField([
            WeightedColumn('name', 'A'),
            WeightedColumn('description', 'D'),
        ],
                                language=None,
                                language_column=None,
                                force_update=False)

        definition, path = MigrationWriter.serialize(svf)

        self.assertEqual(
            "tsvector_field.SearchVectorField("
            "columns=["
            "tsvector_field.WeightedColumn('name', 'A'), "
            "tsvector_field.WeightedColumn('description', 'D')]"
            ")", definition)

        self.assertSetEqual({'import tsvector_field'}, path)
Пример #6
0
 class TextDocument(models.Model):
     title = models.CharField(max_length=128)
     search = SearchVectorField([
         WeightedColumn('title', 'A')
     ])
Пример #7
0
 class TextDocument(models.Model):
     search = SearchVectorField([
         WeightedColumn('title', 'A')
     ], language='english')
Пример #8
0
 class TextDocument(models.Model):
     title = models.CharField(max_length=128)
     search = SearchVectorField([
         WeightedColumn('body', 'A'),
         WeightedColumn('name', 'X')
     ], language=9, force_update=False)
Пример #9
0
 class TextDocument(models.Model):
     title = models.CharField(max_length=128)
     search = SearchVectorField([
         WeightedColumn('title', 'X')
     ], language='english')
Пример #10
0
 class WithLanguageAndLanguageColumn(models.Model):
     search = SearchVectorField([
         WeightedColumn('body', 'D'),
     ],
                                language='ukrainian',
                                language_column='lang')
Пример #11
0
 class WithLanguageTwoColumn(models.Model):
     search = SearchVectorField([
         WeightedColumn('title', 'A'),
         WeightedColumn('body', 'D'),
     ],
                                language='ukrainian')
Пример #12
0
 class TextDocumentForceUpdate(models.Model):
     search = SearchVectorField([
         WeightedColumn('body', 'D'),
     ],
                                'english',
                                force_update=True)
Пример #13
0
 class TextDocument(models.Model):
     search = SearchVectorField([
         WeightedColumn('title', 'A'),
         WeightedColumn('body', 'D'),
     ], 'english')
Пример #14
0
 class ThreeColumns(models.Model):
     search = SearchVectorField([
         WeightedColumn('name', 'A'),
         WeightedColumn('title', 'B'),
         WeightedColumn('body', 'C'),
     ])
Пример #15
0
 class OneColumn(models.Model):
     search = SearchVectorField([
         WeightedColumn('name', 'A'),
     ])