import pytest

from tests.utils import is_postgresql_env_with_json_field


@pytest.mark.skipif(not is_postgresql_env_with_json_field(),
                    reason="requires postgresql and Django 1.9+")
@pytest.mark.django_db
def test_dirty_json_field():
    from tests.models import TestModelWithJSONField

    tm = TestModelWithJSONField.objects.create(json_field={'data': 'dummy_data'})
    assert tm.get_dirty_fields() == {}

    tm.json_field = {'data': 'foo'}
    assert tm.get_dirty_fields() == {'json_field': {'data': 'dummy_data'}}

import pytest

from tests.utils import is_postgresql_env_with_json_field


@pytest.mark.skipif(not is_postgresql_env_with_json_field(),
                    reason="requires postgresql and Django 1.9+")
@pytest.mark.django_db
def test_dirty_json_field():
    from tests.models import TestModelWithJSONField

    tm = TestModelWithJSONField.objects.create(json_field={'data': [1, 2, 3]})

    data = tm.json_field['data']
    data.append(4)

    assert tm.get_dirty_fields(verbose=True) == {
        'json_field': {
            'current': {'data': [1, 2, 3, 4]},
            'saved': {'data': [1, 2, 3]}
        }
    }

Update postgresql json_field to reflect deepcopy fix
Update postgresql json_field to reflect deepcopy fix

  import pytest
  
  from tests.utils import is_postgresql_env_with_json_field
  
  
  @pytest.mark.skipif(not is_postgresql_env_with_json_field(),
                      reason="requires postgresql and Django 1.9+")
  @pytest.mark.django_db
  def test_dirty_json_field():
      from tests.models import TestModelWithJSONField
  
-     tm = TestModelWithJSONField.objects.create(json_field={'data': 'dummy_data'})
+     tm = TestModelWithJSONField.objects.create(json_field={'data': [1, 2, 3]})
-     assert tm.get_dirty_fields() == {}
  
-     tm.json_field = {'data': 'foo'}
-     assert tm.get_dirty_fields() == {'json_field': {'data': 'dummy_data'}}
+     data = tm.json_field['data']
+     data.append(4)
  
+     assert tm.get_dirty_fields(verbose=True) == {
+         'json_field': {
+             'current': {'data': [1, 2, 3, 4]},
+             'saved': {'data': [1, 2, 3]}
+         }
+     }
+ 
Update postgresql json_field to reflect deepcopy fix
## Code Before:
import pytest

from tests.utils import is_postgresql_env_with_json_field


@pytest.mark.skipif(not is_postgresql_env_with_json_field(),
                    reason="requires postgresql and Django 1.9+")
@pytest.mark.django_db
def test_dirty_json_field():
    from tests.models import TestModelWithJSONField

    tm = TestModelWithJSONField.objects.create(json_field={'data': 'dummy_data'})
    assert tm.get_dirty_fields() == {}

    tm.json_field = {'data': 'foo'}
    assert tm.get_dirty_fields() == {'json_field': {'data': 'dummy_data'}}

## Instruction:
Update postgresql json_field to reflect deepcopy fix
## Code After:
import pytest

from tests.utils import is_postgresql_env_with_json_field


@pytest.mark.skipif(not is_postgresql_env_with_json_field(),
                    reason="requires postgresql and Django 1.9+")
@pytest.mark.django_db
def test_dirty_json_field():
    from tests.models import TestModelWithJSONField

    tm = TestModelWithJSONField.objects.create(json_field={'data': [1, 2, 3]})

    data = tm.json_field['data']
    data.append(4)

    assert tm.get_dirty_fields(verbose=True) == {
        'json_field': {
            'current': {'data': [1, 2, 3, 4]},
            'saved': {'data': [1, 2, 3]}
        }
    }
