def test_model_weight_for_complex_must_be_none(self): with app.app_context(): action = add_simple_action(db.session) analysis = add_analysis_with_coordinates(db.session, action.id) comb_model = add_complex_model_comb(db.session, analysis.id) with self.assertRaises(AssertionError): comb_model.create_weights(weight=3)
def test_cs_profiles_representation(self): with app.app_context(): action = add_simple_action(db.session) analysis = add_analysis_with_coordinates(db.session, action.id) person_type_tourist = PersonType.query.filter_by( name='tourist').first() person_type_climber = PersonType.query.filter_by( name='climber').first() weight_tourist, weight_climber = 3, 7 tourist = Profile(analysis_id=analysis.id, person_type_id=person_type_tourist.id, weight=weight_tourist) climber = Profile(analysis_id=analysis.id, person_type_id=person_type_climber.id, weight=weight_climber) db.session.add(tourist) db.session.add(climber) db.session.flush() expected = {'tourist': weight_tourist, 'climber': weight_climber} self.assertEquals(len(analysis.cs_profiles()), 2) self.assertEquals(analysis.cs_profiles(), expected)
def test_starting_analysis_with_complex_models(self): result_ids = { "HorDistIPP": "3897890975230495710", "ElevChgIPP": "4709831647259734695", "HorChgIPP": "4375692374598762347", "TrackOffset": "3476273469987234659", "DispAngle": "3892345235423495710", "FindLocation": "4709831647263434695", "Mobility": "4375692374598762346", } expected_content = { "CombProb": "3897890975230495710", "SearchSeg": "3476273469987234659", } httpretty.register_uri(httpretty.POST, server_path('complex_analysis'), body=json.dumps(expected_content), content_type="application/json") with app.app_context(): action = add_simple_action(db.session) analysis = add_complex_models_analysis(db.session, action.id) for model in analysis.simple_models(): model.update_result(result_ids[model.name]) draft_simple_models = analysis.draft_models().join( Model.model_type).filter(ModelType.complex == False) assert draft_simple_models.count() == 0 analysis.start_computation() for model in analysis.complex_models(): self.assertEquals(model.result_id, expected_content[model.name])
def test_updating_layers(self): expected_content = { "status": "finished", "layer_ids": ["432542345", "785642345", "434567678", "969824586"] } layers_count = 4 result_id = "3897890975230495710" path = server_path('analysis/%s' % result_id) httpretty.register_uri(httpretty.GET, path, body=json.dumps(expected_content), content_type="application/json") with app.app_context(): action = add_simple_action(db.session) analysis = add_analysis_with_coordinates(db.session, action.id) model = add_simple_model(db.session, analysis.id) # mocking starting computation model.update_result(result_id) analysis.update_result() finished_id = ModelStatus.by_name(ModelStatus.FINISHED).id assert model.status_id == finished_id assert analysis.analysis_status_id == finished_id assert len(model.layer_urls()) == layers_count layer_paths = [ layer_path(layers_id) for layers_id in expected_content['layer_ids'] ] for l in model.layer_urls(): assert l in layer_paths
def test_duplicating_analysis_deep(self): with app.app_context(): action = add_simple_action(db.session) analysis = add_simple_models_analysis(db.session, action.id) duplicated = analysis.duplicate({'name': 'Another'}) self.assertNotEqual(analysis.id, duplicated.id) self.assertEqual(analysis.lost_time, duplicated.lost_time) self.assertEqual(analysis.models.count(), duplicated.models.count())
def test_duplicating_analysis_shallow(self): with app.app_context(): action = add_simple_action(db.session) analysis = add_analysis_with_coordinates(db.session, action.id) duplicated = analysis.duplicate({'ipp_latitude': 10}) self.assertNotEqual(analysis.id, duplicated.id) self.assertEqual(analysis.name, duplicated.name) self.assertEqual(analysis.lost_time, duplicated.lost_time) self.assertNotEqual(duplicated.ipp_latitude, analysis.ipp_latitude) self.assertEqual(duplicated.ipp_latitude, 10)
def test_analysis_updating_unfinished(self): with app.app_context(): action = add_simple_action(db.session) analysis = add_simple_models_analysis(db.session, action.id) processing_id = ModelStatus.by_name(ModelStatus.PROCESSING).id for model in analysis.models: model.status_id = processing_id with self.assertRaises(ValueError): analysis_data = {'ipp_latitude': 1123123} analysis.update(analysis_data, None, None)
def test_model_default_status(self): with app.app_context(): action = add_simple_action(db.session) analysis = add_analysis_with_coordinates(db.session, action.id) model_type = ModelType.query.filter_by(complex=False).first() model = Model(analysis_id=analysis.id, model_type_id=model_type.id) db.session.add(model) db.session.flush() model_status = ModelStatus.query.filter_by(name='draft').one() self.assertEquals(model.status_id, model_status.id)
def test_model_weight_for_complex_types(self): with app.app_context(): action = add_simple_action(db.session) analysis = add_analysis_with_coordinates(db.session, action.id) model_type = ModelType.query.filter_by(complex=True).first() model = Model(analysis_id=analysis.id, model_type_id=model_type.id) db.session.add(model) db.session.flush() self.assertEquals(analysis.models.count(), 1) self.assertIsNone(model.weight) with self.assertRaises(AssertionError): model.create_weights()
def test_model_weight_must_be_associated_with_non_complex_types(self): with app.app_context(): action = add_simple_action(db.session) analysis = add_analysis_with_coordinates(db.session, action.id) model_type = ModelType.query.filter_by(complex=False).first() model = Model(analysis_id=analysis.id, model_type_id=model_type.id) db.session.add(model) db.session.flush() self.assertEquals(analysis.models.count(), 1) self.assertEquals(model.model_weights.count(), 0) with self.assertRaises(AssertionError): getattr(model, 'weight')
def test_action_status_id_no_analyses(self): with app.app_context(): action = add_simple_action(db.session) analyses_count = Analysis.query.count() # only one analysis, no models self.assertEquals(analyses_count, 0) draft_id = ModelStatus.draft_id() # and the one has status draft on class level draft_count = Action.query.filter( Action.action_status_id == draft_id).count() self.assertEquals(draft_count, 1) # as well as on instance level self.assertEquals(action.action_status_id, draft_id)
def test_analysis_status_id_finished(self): with app.app_context(): action = add_simple_action(db.session) analysis = add_simple_models_analysis(db.session, action.id) finished_id = ModelStatus.by_name(ModelStatus.FINISHED).id for model in analysis.models: model.status_id = finished_id # only one analysis, all finished analyses_count = Analysis.query.count() self.assertEquals(analyses_count, 1) # and the one has status finished on class level analyses_status_count = Analysis.query.filter( Analysis.analysis_status_id == finished_id).count() self.assertEquals(analyses_status_count, 1) # as well as on instance level self.assertEquals(analysis.analysis_status_id, finished_id)
def test_model_weight_for_non_complex_types(self): with app.app_context(): action = add_simple_action(db.session) analysis = add_analysis_with_coordinates(db.session, action.id) model_type = ModelType.query.filter_by(complex=False).first() model = Model(analysis_id=analysis.id, model_type_id=model_type.id) db.session.add(model) db.session.flush() model.create_weights() db.session.flush() self.assertEquals(analysis.models.count(), 1) self.assertEquals(model.model_weights.count(), 1) self.assertEquals(model.weight, app.config['DEFAULT_WEIGHT']) new_weight = 5 model.update_weights(new_weight) self.assertEquals(model.model_weights.count(), 1) self.assertEquals(model.weight, new_weight)
def test_updating_state(self): expected_content = { "status": "converting", } result_ids = { "HorDistIPP": "3897890975230495710", "ElevChgIPP": "4709831647259734695", "HorChgIPP": "4375692374598762347", "TrackOffset": "3476273469987234659", "DispAngle": "3892345235423495710", "FindLocation": "4709831647263434695", "Mobility": "4375692374598762346", "CombProb": "3897890975230495710", "SearchSeg": "3476273469987234659", } for id in result_ids.values(): path = server_path('analysis/%s' % id) httpretty.register_uri(httpretty.GET, path, body=json.dumps(expected_content), content_type="application/json") with app.app_context(): action = add_simple_action(db.session) analysis = add_complex_models_analysis(db.session, action.id) draft_id = ModelStatus.draft_id() for model in analysis.models: assert model.model_status.id == draft_id assert analysis.analysis_status_id == draft_id for model in analysis.models: model.update_result(result_ids[model.name]) waiting_status_id = ModelStatus.by_name(ModelStatus.WAITING).id for model in analysis.models: assert model.status_id == waiting_status_id assert analysis.analysis_status_id == waiting_status_id analysis.update_result() processing_id = ModelStatus.by_name(ModelStatus.PROCESSING).id for model in analysis.models: assert model.status_id == processing_id assert analysis.analysis_status_id == processing_id
def test_model_weight_for_multiple_complex_analyses(self): with app.app_context(): action = add_simple_action(db.session) analysis = add_analysis_with_coordinates(db.session, action.id) model = add_simple_model(db.session, analysis.id) comb_model = add_complex_model_comb(db.session, analysis.id) seg_model = add_complex_model_seg(db.session, analysis.id) model.create_weights(parent_model_id=comb_model.id) model.create_weights(parent_model_id=seg_model.id) db.session.flush() self.assertEquals(analysis.models.count(), 3) self.assertEquals(model.model_weights.count(), 2) self.assertEquals(model.child_model_weights.count(), 0) self.assertEquals(comb_model.model_weights.count(), 0) self.assertEquals(seg_model.model_weights.count(), 0) self.assertEquals(comb_model.child_model_weights.count(), 1) self.assertEquals(seg_model.child_model_weights.count(), 1)
def test_analysis_status_id_draft(self): with app.app_context(): action = add_simple_action(db.session) analysis = add_simple_models_analysis(db.session, action.id) analyses_count = Analysis.query.count() draft_id = ModelStatus.draft_id() for model in analysis.models: model.status_id = draft_id # only one analysis, no models self.assertEquals(analyses_count, 1) # and the one has status draft on class level draft_count = Analysis.query.filter( Analysis.analysis_status_id == draft_id).count() self.assertEquals(draft_count, 1) # as well as on instance level self.assertEquals(analysis.analysis_status_id, draft_id)
def test_analysis_status_id_waiting(self): with app.app_context(): action = add_simple_action(db.session) analysis = add_simple_models_analysis(db.session, action.id) processing_id = ModelStatus.by_name(ModelStatus.PROCESSING).id waiting_id = ModelStatus.by_name(ModelStatus.WAITING).id for model in analysis.models: model.status_id = processing_id analysis.models.first().status_id = waiting_id # only one analysis, all processing but one waiting analyses_count = Analysis.query.count() self.assertEquals(analyses_count, 1) # and the one has status waiting on class level analyses_status_count = Analysis.query.filter( Analysis.analysis_status_id == waiting_id).count() self.assertEquals(analyses_status_count, 1) # as well as on instance level self.assertEquals(analysis.analysis_status_id, waiting_id)
def test_action_status_id_processing(self): with app.app_context(): action = add_simple_action(db.session) processing_id = ModelStatus.by_name(ModelStatus.PROCESSING).id finished_id = ModelStatus.by_name(ModelStatus.FINISHED).id analysis = add_simple_models_analysis(db.session, action.id) for model in analysis.models: model.status_id = finished_id analysis = add_simple_models_analysis(db.session, action.id) for model in analysis.models: model.status_id = processing_id action_status_count = Action.query.filter( Action.action_status_id == processing_id).count() self.assertEquals(action_status_count, 1) self.assertEquals(action.action_status_id, processing_id)
def test_action_status_id_error(self): with app.app_context(): action = add_simple_action(db.session) waiting_id = ModelStatus.by_name(ModelStatus.WAITING).id error_id = ModelStatus.by_name(ModelStatus.ERROR).id analysis = add_simple_models_analysis(db.session, action.id) for model in analysis.models: model.status_id = waiting_id analysis = add_simple_models_analysis(db.session, action.id) for model in analysis.models: model.status_id = error_id action_status_count = Action.query.filter( Action.action_status_id == error_id).count() self.assertEquals(action_status_count, 1) self.assertEquals(action.action_status_id, error_id)
def test_model_weight_for_child_model(self): with app.app_context(): action = add_simple_action(db.session) analysis = add_analysis_with_coordinates(db.session, action.id) model_type = ModelType.query.filter_by(complex=False).first() complex_model_type = ModelType.query.filter_by( complex=True).first() model = Model(analysis_id=analysis.id, model_type_id=model_type.id) complex_model = Model(analysis_id=analysis.id, model_type_id=complex_model_type.id) db.session.add(model) db.session.add(complex_model) db.session.flush() model.create_weights(parent_model_id=complex_model.id) db.session.flush() self.assertEquals(analysis.models.count(), 2) self.assertEquals(model.model_weights.count(), 1) self.assertEquals(model.child_model_weights.count(), 0) self.assertEquals(complex_model.model_weights.count(), 0) self.assertEquals(complex_model.child_model_weights.count(), 1)
def test_cs_simple_models_representation(self): with app.app_context(): action = add_simple_action(db.session) analysis = add_analysis_with_coordinates(db.session, action.id) # add simple models model_type_names = [ 'HorDistIPP', 'ElevChgIPP', 'HorChgIPP', 'DispAngle', 'TrackOffset', 'FindLocation', 'Mobility' ] for mtn in model_type_names: model_type = ModelType.query.filter_by(name=mtn).first() model = Model(analysis_id=analysis.id, model_type_id=model_type.id) db.session.add(model) db.session.flush() expected = model_type_names self.assertEquals(len(analysis.cs_simple_models()), len(expected)) self.assertEquals(analysis.cs_simple_models(), expected)
def test_starting_analysis_with_simple_models(self): expected_content = { "HorDistIPP": "3897890975230495710", "ElevChgIPP": "4709831647259734695", "HorChgIPP": "4375692374598762347", "TrackOffset": "3476273469987234659", "DispAngle": "3892345235423495710", "FindLocation": "4709831647263434695", "Mobility": "4375692374598762346", } httpretty.register_uri(httpretty.POST, server_path('analysis'), body=json.dumps(expected_content), content_type="application/json") with app.app_context(): action = add_simple_action(db.session) analysis = add_simple_models_analysis(db.session, action.id) analysis.start_computation() for model in analysis.simple_models(): self.assertEquals(model.result_id, expected_content[model.name])
def test_cs_complex_models_representation(self): with app.app_context(): action = add_simple_action(db.session) analysis = add_analysis_with_coordinates(db.session, action.id) # add comb model model_type = ModelType.query.filter_by(name='CombProb').first() comb_model = Model(analysis_id=analysis.id, model_type_id=model_type.id) db.session.add(comb_model) db.session.flush() # add search seg model model_type = ModelType.query.filter_by(name='SearchSeg').first() seg_model = Model(analysis_id=analysis.id, model_type_id=model_type.id) db.session.add(seg_model) db.session.flush() # add simple models model_type_names = ['HorDistIPP', 'FindLocation', 'Mobility'] result_ids = ['213', '543', '234'] weight = 4 for mtn, result_id in zip(model_type_names, result_ids): model_type = ModelType.query.filter_by(name=mtn).first() model = Model(analysis_id=analysis.id, model_type_id=model_type.id) db.session.add(model) db.session.flush() model.create_weights(parent_model_id=comb_model.id, weight=weight) model.create_weights(parent_model_id=seg_model.id, weight=weight) model.update_result(result_id) expected_complex_analyses = ['CombProb', 'SearchSeg'] expected_model_weights = { 'HorDistIPP': { 'id': '213', 'weight': weight }, 'FindLocation': { 'id': '543', 'weight': weight }, 'Mobility': { 'id': '234', 'weight': weight }, } cs_complex_models = analysis.cs_complex_models() cs_model_weights = analysis.cs_complex_model_weights() self.assertEquals(len(cs_complex_models), len(expected_complex_analyses)) self.assertEquals(sorted(cs_complex_models), sorted(expected_complex_analyses)) self.assertEquals(len(cs_model_weights), len(expected_model_weights)) self.assertEquals(sorted(cs_model_weights), sorted(expected_model_weights))
def test_starting_analysis_requires_complete_data(self): with app.app_context(): action = add_simple_action(db.session) analysis = add_analysis_with_coordinates(db.session, action.id) with self.assertRaises(AnalysisDataIncomplete): analysis.start_computation()