Passed
Push — master ( 5ba9be...1ef026 )
by Swen
01:55
created

get_fake_model()   B

Complexity

Conditions 2

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
dl 0
loc 25
rs 8.8571
c 2
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A TestProject.clone() 0 2 1
A TestProject.apps() 0 3 1
1
from django.db import connection, migrations
2
from django.db.migrations.executor import MigrationExecutor
3
from django.contrib.postgres.operations import HStoreExtension
4
5
from localized_fields import LocalizedModel
6
7
8
def define_fake_model(name='TestModel', fields=None):
9
    attributes = {
10
        'app_label': 'localized_fields',
11
        '__module__': __name__,
12
        '__name__': name
13
    }
14
15
    if fields:
16
        attributes.update(fields)
17
    model = type(name, (LocalizedModel,), attributes)
18
19
    return model
20
21
22
def get_fake_model(name='TestModel', fields=None):
23
    """Creates a fake model to use during unit tests."""
24
25
    model = define_fake_model(name, fields)
26
27
    class TestProject:
28
29
        def clone(self, *_args, **_kwargs):
30
            return self
31
32
        @property
33
        def apps(self):
34
            return self
35
36
    class TestMigration(migrations.Migration):
37
        operations = [HStoreExtension()]
38
39
    with connection.schema_editor() as schema_editor:
40
        migration_executor = MigrationExecutor(schema_editor.connection)
41
        migration_executor.apply_migration(
42
            TestProject(), TestMigration('eh', 'localized_fields'))
43
44
        schema_editor.create_model(model)
45
46
    return model
47