Skip to content

Commit

Permalink
[chores] Encode created and modified to avoid false positives #66
Browse files Browse the repository at this point in the history
A comparison between a datetime object and a datetime converted
to string generates a diff, but there's no real difference in
the data, only the python representation differs.
Therefore we will now always encode these using the
JSONEncoder from REST framework.

Related to #66
  • Loading branch information
nemesifier committed Jun 26, 2020
1 parent 88a4c52 commit 6d618e3
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 7 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ install:
- pip install $DJANGO
- python setup.py -q develop
- pip install -r requirements-test.txt
# Temporary
- pip install --upgrade https://github.com/openwisp/netdiff/tarball/issue/39-detect-node-changes
# Temporary, remove when netdiff 0.8.0 is released
- pip install --upgrade https://github.com/openwisp/netdiff/tarball/master

script:
- coverage run --source=openwisp_network_topology runtests.py
Expand Down
4 changes: 2 additions & 2 deletions openwisp_network_topology/base/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ def json(self, dict=False, **kwargs):
value = getattr(self, attr)
if value or attr == 'properties':
netjson[attr] = value
netjson['properties']['created'] = self.created
netjson['properties']['modified'] = self.modified
netjson['properties']['created'] = JSONEncoder().default(self.created)
netjson['properties']['modified'] = JSONEncoder().default(self.modified)
if dict:
return netjson
return json.dumps(netjson, cls=JSONEncoder, **kwargs)
Expand Down
5 changes: 3 additions & 2 deletions openwisp_network_topology/tests/test_node.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import swapper
from django.test import TestCase
from rest_framework.utils.encoders import JSONEncoder

from .utils import CreateGraphObjectsMixin, CreateOrgMixin

Expand Down Expand Up @@ -89,8 +90,8 @@ def test_json(self):
'local_addresses': ['10.0.0.1'],
'properties': {
'gateway': True,
'created': n.created,
'modified': n.modified,
'created': JSONEncoder().default(n.created),
'modified': JSONEncoder().default(n.modified),
},
},
)
Expand Down
4 changes: 3 additions & 1 deletion openwisp_network_topology/tests/test_topology.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,9 @@ def test_update_changed(self):
self.assertEqual(link.properties, {'pretty': True})
self.assertEqual(link.cost, 1.5)
self.assertEqual(link.cost_text, '15 Mbps')
link = self.link_model.objects.last()
link = self.link_model.get_from_nodes(
source='192.168.0.1', target='192.168.0.3', topology=t
)
self.assertEqual(link.properties, {'pretty': False})
self.assertEqual(link.cost, 2.0)
self.assertEqual(link.cost_text, '20 Mbps')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Generated by Django 3.0.7 on 2020-06-25 18:25

import collections
from django.db import migrations
import jsonfield.fields
import rest_framework.utils.encoders


class Migration(migrations.Migration):

dependencies = [
('sample_network_topology', '0001_initial'),
]

operations = [
migrations.AlterField(
model_name='link',
name='properties',
field=jsonfield.fields.JSONField(
blank=True,
default=dict,
dump_kwargs={
'cls': rest_framework.utils.encoders.JSONEncoder,
'indent': 4,
},
load_kwargs={'object_pairs_hook': collections.OrderedDict},
),
),
migrations.AlterField(
model_name='node',
name='addresses',
field=jsonfield.fields.JSONField(blank=True, default=[]),
),
migrations.AlterField(
model_name='node',
name='properties',
field=jsonfield.fields.JSONField(
blank=True,
default=dict,
dump_kwargs={
'cls': rest_framework.utils.encoders.JSONEncoder,
'indent': 4,
},
load_kwargs={'object_pairs_hook': collections.OrderedDict},
),
),
]

0 comments on commit 6d618e3

Please sign in to comment.