def test_detail_setter_booleans(): detail = divelog.DiveDetail() for value in [True, 'True', 'T', 1]: detail.ascent_rate_violation = value assert detail.ascent_rate_violation is True for value in [False, 'False', 'No', 'Nada', 'F', 0]: detail.ascent_rate_violation = value assert detail.ascent_rate_violation is False
def test_detail_slots(): slots = ['elapsed_time', 'depth', 'current_ceiling', 'water_temperature', 'ascent_rate', 'gas_switch', 'current_PO2', 'main_cylinder_pressure', 'diluent_cylinder_pressure', 'oxygen_flow_rate', 'CNS_toxicity', 'OUT', 'warning_number', 'ascent_rate_violation', 'decompression_violation'] detail = divelog.DiveDetail() for slot in slots: assert slot in dir(detail) for slot in dir(detail): if not slot[0:2] == '__': assert slot in slots
def test_detail_setter_errors(): detail = divelog.DiveDetail() with pytest.raises(ValueError): detail.elapsed_time = 'awhile' assert detail.elapsed_time == 0.0 with pytest.raises(ValueError): detail.depth = 'deep' assert detail.depth == 0.0 with pytest.raises(ValueError): detail.water_temperature = 'cold' assert detail.water_temperature == 0.0 with pytest.raises(ValueError): detail.ascent_rate = 'fast' assert detail.ascent_rate == 0 with pytest.raises(ValueError): detail.warning_number = 'nope' assert detail.warning_number == 0
def create_record(iterations): mid = iterations / 2 depth = 0 record = [] for i in range(iterations): detail = divelog.DiveDetail() detail.elapsed_time = i if i < mid: depth = depth + 10 else: depth = depth - 10 detail.depth = depth detail.water_temperature = 26.0 if i == iterations-1: detail.ascent_rate_violation = True detail.decompression_violation = True record.append(detail) return record
def parse(file): """ Parse this file object and return either a new top-level Log object, or None. """ def copy_obj(src, tgt, ignore): for key in src: if not key == ignore: setattr(tgt, key, src[key]) log = divelog.Log() jobj = json.load(file) copy_obj(jobj, log, 'dives') for jdive in jobj['dives']: dive = divelog.Dive() log.dives.append(dive) copy_obj(jdive, dive, 'record') for jdet in jdive['record']: detail = divelog.DiveDetail() dive.record.append(detail) copy_obj(jdet, detail, None) return log
def test_detail_setter_unknown(): detail = divelog.DiveDetail() assert ('water_color' not in dir(detail)) with pytest.raises(AttributeError): detail.water_color = 'greenish' assert ('water_color' not in dir(detail))
def test_dive_setter_dives(): dive = divelog.Dive() dive.record = [divelog.DiveDetail()] assert len(dive.record) == 1 with pytest.raises(ValueError): dive.record = {}