class StationViewApiTest(TestCase): """ Tests the Station View API """ station = None def setUp(self): self.antenna = AntennaFactory() self.encoder = JSONEncoder() self.station = StationFactory.create(antennas=[self.antenna]) def test_station_view_api(self): ants = self.station.antenna.all() ser_ants = [" ".join([ant.band, ant.get_antenna_type_display()]) for ant in ants] station_serialized = { u'altitude': self.station.alt, u'antenna': ser_ants, u'created': self.encoder.default(self.station.created), u'description': self.station.description, u'id': self.station.id, u'last_seen': self.encoder.default(self.station.last_seen), u'lat': self.station.lat, u'lng': self.station.lng, u'location': self.station.location, u'min_horizon': self.station.horizon, u'name': self.station.name, u'observations': 0, u'qthlocator': self.station.qthlocator, u'status': self.station.get_status_display()} response = self.client.get('/api/stations/') response_json = json.loads(response.content) self.assertEqual(response_json, [station_serialized])
def default(self, obj): # pylint: disable=method-hidden # Handle Decimal NaN values if isinstance(obj, decimal.Decimal) and math.isnan(obj): return None return JSONEncoder.default(self, obj)
def default(self, obj): # Handle Decimal NaN values if isinstance(obj, decimal.Decimal) and math.isnan(obj): return None return JSONEncoder.default(self, obj)
class JSONEncoderTests(TestCase): """ Tests the JSONEncoder method """ def setUp(self): self.encoder = JSONEncoder() def test_encode_decimal(self): """ Tests encoding a decimal """ d = Decimal(3.14) assert d == float(d) def test_encode_datetime(self): """ Tests encoding a datetime object """ current_time = datetime.now() assert self.encoder.default(current_time) == current_time.isoformat() def test_encode_time(self): """ Tests encoding a timezone """ current_time = datetime.now().time() assert self.encoder.default(current_time) == current_time.isoformat()[:12] def test_encode_time_tz(self): """ Tests encoding a timezone aware timestamp """ class UTC(tzinfo): """ Class extending tzinfo to mimic UTC time """ def utcoffset(self, dt): return timedelta(0) def tzname(self, dt): return "UTC" def dst(self, dt): return timedelta(0) current_time = datetime.now().time() current_time = current_time.replace(tzinfo=UTC()) with self.assertRaises(ValueError): self.encoder.default(current_time) def test_encode_date(self): """ Tests encoding a date object """ current_date = date.today() assert self.encoder.default(current_date) == current_date.isoformat() def test_encode_timedelta(self): """ Tests encoding a timedelta object """ delta = timedelta(hours=1) assert self.encoder.default(delta) == str(delta.total_seconds()) def test_encode_uuid(self): """ Tests encoding a UUID object """ unique_id = uuid4() assert self.encoder.default(unique_id) == str(unique_id)
class JSONEncoderTests(TestCase): """ Tests the JSONEncoder method """ def setUp(self): self.encoder = JSONEncoder() def test_encode_decimal(self): """ Tests encoding a decimal """ d = Decimal(3.14) assert self.encoder.default(d) == float(d) def test_encode_datetime(self): """ Tests encoding a datetime object """ current_time = datetime.now() assert self.encoder.default(current_time) == current_time.isoformat() current_time_utc = current_time.replace(tzinfo=utc) assert self.encoder.default(current_time_utc) == current_time.isoformat() + 'Z' def test_encode_time(self): """ Tests encoding a timezone """ current_time = datetime.now().time() assert self.encoder.default(current_time) == current_time.isoformat() def test_encode_time_tz(self): """ Tests encoding a timezone aware timestamp """ current_time = datetime.now().time() current_time = current_time.replace(tzinfo=utc) with pytest.raises(ValueError): self.encoder.default(current_time) def test_encode_date(self): """ Tests encoding a date object """ current_date = date.today() assert self.encoder.default(current_date) == current_date.isoformat() def test_encode_timedelta(self): """ Tests encoding a timedelta object """ delta = timedelta(hours=1) assert self.encoder.default(delta) == str(delta.total_seconds()) def test_encode_uuid(self): """ Tests encoding a UUID object """ unique_id = uuid4() assert self.encoder.default(unique_id) == str(unique_id) @pytest.mark.skipif(not coreapi, reason='coreapi is not installed') def test_encode_coreapi_raises_error(self): """ Tests encoding a coreapi objects raises proper error """ with pytest.raises(RuntimeError): self.encoder.default(coreapi.Document()) with pytest.raises(RuntimeError): self.encoder.default(coreapi.Error()) def test_encode_object_with_tolist(self): """ Tests encoding a object with tolist method """ foo = MockList() assert self.encoder.default(foo) == [1, 2, 3]
class JSONEncoderTests(TestCase): """ Tests the JSONEncoder method """ def setUp(self): self.encoder = JSONEncoder() def test_encode_decimal(self): """ Tests encoding a decimal """ d = Decimal(3.14) assert self.encoder.default(d) == float(d) def test_encode_datetime(self): """ Tests encoding a datetime object """ current_time = datetime.now() assert self.encoder.default(current_time) == current_time.isoformat() current_time_utc = current_time.replace(tzinfo=utc) assert self.encoder.default( current_time_utc) == current_time.isoformat() + 'Z' def test_encode_time(self): """ Tests encoding a timezone """ current_time = datetime.now().time() assert self.encoder.default(current_time) == current_time.isoformat() def test_encode_time_tz(self): """ Tests encoding a timezone aware timestamp """ current_time = datetime.now().time() current_time = current_time.replace(tzinfo=utc) with pytest.raises(ValueError): self.encoder.default(current_time) def test_encode_date(self): """ Tests encoding a date object """ current_date = date.today() assert self.encoder.default(current_date) == current_date.isoformat() def test_encode_timedelta(self): """ Tests encoding a timedelta object """ delta = timedelta(hours=1) assert self.encoder.default(delta) == str(delta.total_seconds()) def test_encode_uuid(self): """ Tests encoding a UUID object """ unique_id = uuid4() assert self.encoder.default(unique_id) == str(unique_id) @pytest.mark.skipif(not coreapi, reason='coreapi is not installed') def test_encode_coreapi_raises_error(self): """ Tests encoding a coreapi objects raises proper error """ with pytest.raises(RuntimeError): self.encoder.default(coreapi.Document()) with pytest.raises(RuntimeError): self.encoder.default(coreapi.Error()) def test_encode_object_with_tolist(self): """ Tests encoding a object with tolist method """ foo = MockList() assert self.encoder.default(foo) == [1, 2, 3]
def default(self, obj): if isinstance(obj, (Server, Tenant, QuotaSet)): return obj.to_dict() return JSONEncoder.default(self, obj)