import pytest
from django.db import connection
from django.db.migrations.executor import MigrationExecutor


@pytest.mark.django_db(transaction=True)
def test_workstation_group_migration():
    executor = MigrationExecutor(connection)
    app = "workstations"
    migrate_from = [(app, "0001_initial")]
    migrate_to = [(app, "0004_auto_20190813_1302")]

    executor.migrate(migrate_from)
    old_apps = executor.loader.project_state(migrate_from).apps

    Workstation = old_apps.get_model(app, "Workstation")
    old_ws = Workstation.objects.create(title="foo")

    assert not hasattr(old_ws, "editors_group")
    assert not hasattr(old_ws, "users_group")

    # Reload
    executor.loader.build_graph()
    # Migrate forwards
    executor.migrate(migrate_to)

    new_apps = executor.loader.project_state(migrate_to).apps

    Workstation = new_apps.get_model(app, "Workstation")
    new_ws = Workstation.objects.get(title="foo")

    assert new_ws.editors_group
    assert new_ws.users_group
    assert new_ws.slug == old_ws.slug
    assert new_ws.title == old_ws.title

import pytest
from django.db import connection
from django.db.migrations.executor import MigrationExecutor
from guardian.shortcuts import get_perms

from grandchallenge.workstations.models import Workstation
from tests.factories import UserFactory


@pytest.mark.django_db(transaction=True)
def test_workstation_group_migration():
    executor = MigrationExecutor(connection)
    app = "workstations"
    migrate_from = [(app, "0001_initial")]
    migrate_to = [(app, "0004_auto_20190813_1302")]

    executor.migrate(migrate_from)
    old_apps = executor.loader.project_state(migrate_from).apps

    user = UserFactory()
    OldWorkstation = old_apps.get_model(app, "Workstation")
    old_ws = OldWorkstation.objects.create(title="foo")

    assert not hasattr(old_ws, "editors_group")
    assert not hasattr(old_ws, "users_group")

    # Reload
    executor.loader.build_graph()
    # Migrate forwards
    executor.migrate(migrate_to)

    new_ws = Workstation.objects.get(title="foo")
    new_ws.add_user(user=user)

    assert new_ws.editors_group
    assert new_ws.users_group
    assert new_ws.slug == old_ws.slug
    assert new_ws.title == old_ws.title
    assert "view_workstation" in get_perms(user, new_ws)

Check that the permission migrations work
Check that the permission migrations work

  import pytest
  from django.db import connection
  from django.db.migrations.executor import MigrationExecutor
+ from guardian.shortcuts import get_perms
+ 
+ from grandchallenge.workstations.models import Workstation
+ from tests.factories import UserFactory
  
  
  @pytest.mark.django_db(transaction=True)
  def test_workstation_group_migration():
      executor = MigrationExecutor(connection)
      app = "workstations"
      migrate_from = [(app, "0001_initial")]
      migrate_to = [(app, "0004_auto_20190813_1302")]
  
      executor.migrate(migrate_from)
      old_apps = executor.loader.project_state(migrate_from).apps
  
+     user = UserFactory()
-     Workstation = old_apps.get_model(app, "Workstation")
+     OldWorkstation = old_apps.get_model(app, "Workstation")
-     old_ws = Workstation.objects.create(title="foo")
+     old_ws = OldWorkstation.objects.create(title="foo")
  
      assert not hasattr(old_ws, "editors_group")
      assert not hasattr(old_ws, "users_group")
  
      # Reload
      executor.loader.build_graph()
      # Migrate forwards
      executor.migrate(migrate_to)
  
-     new_apps = executor.loader.project_state(migrate_to).apps
- 
-     Workstation = new_apps.get_model(app, "Workstation")
      new_ws = Workstation.objects.get(title="foo")
+     new_ws.add_user(user=user)
  
      assert new_ws.editors_group
      assert new_ws.users_group
      assert new_ws.slug == old_ws.slug
      assert new_ws.title == old_ws.title
+     assert "view_workstation" in get_perms(user, new_ws)
  
Check that the permission migrations work
## Code Before:
import pytest
from django.db import connection
from django.db.migrations.executor import MigrationExecutor


@pytest.mark.django_db(transaction=True)
def test_workstation_group_migration():
    executor = MigrationExecutor(connection)
    app = "workstations"
    migrate_from = [(app, "0001_initial")]
    migrate_to = [(app, "0004_auto_20190813_1302")]

    executor.migrate(migrate_from)
    old_apps = executor.loader.project_state(migrate_from).apps

    Workstation = old_apps.get_model(app, "Workstation")
    old_ws = Workstation.objects.create(title="foo")

    assert not hasattr(old_ws, "editors_group")
    assert not hasattr(old_ws, "users_group")

    # Reload
    executor.loader.build_graph()
    # Migrate forwards
    executor.migrate(migrate_to)

    new_apps = executor.loader.project_state(migrate_to).apps

    Workstation = new_apps.get_model(app, "Workstation")
    new_ws = Workstation.objects.get(title="foo")

    assert new_ws.editors_group
    assert new_ws.users_group
    assert new_ws.slug == old_ws.slug
    assert new_ws.title == old_ws.title

## Instruction:
Check that the permission migrations work
## Code After:
import pytest
from django.db import connection
from django.db.migrations.executor import MigrationExecutor
from guardian.shortcuts import get_perms

from grandchallenge.workstations.models import Workstation
from tests.factories import UserFactory


@pytest.mark.django_db(transaction=True)
def test_workstation_group_migration():
    executor = MigrationExecutor(connection)
    app = "workstations"
    migrate_from = [(app, "0001_initial")]
    migrate_to = [(app, "0004_auto_20190813_1302")]

    executor.migrate(migrate_from)
    old_apps = executor.loader.project_state(migrate_from).apps

    user = UserFactory()
    OldWorkstation = old_apps.get_model(app, "Workstation")
    old_ws = OldWorkstation.objects.create(title="foo")

    assert not hasattr(old_ws, "editors_group")
    assert not hasattr(old_ws, "users_group")

    # Reload
    executor.loader.build_graph()
    # Migrate forwards
    executor.migrate(migrate_to)

    new_ws = Workstation.objects.get(title="foo")
    new_ws.add_user(user=user)

    assert new_ws.editors_group
    assert new_ws.users_group
    assert new_ws.slug == old_ws.slug
    assert new_ws.title == old_ws.title
    assert "view_workstation" in get_perms(user, new_ws)
