TOMToolkit / tom_base

The base Django project for a Target and Observation Manager
https://tom-toolkit.readthedocs.io
GNU General Public License v3.0
23 stars 42 forks source link

SiderealTargetFactory creates BaseTargets but not custom Targets? #963

Closed rachel3834 closed 2 weeks ago

rachel3834 commented 2 weeks ago

I'm upgrading my TOM to use a custom Target model, including updating my unittests. These tests make use of tom_target's SiderealTargetFactory to create targets for testing purposes, e.g.:

t1 = SiderealTargetFactory.create()

I then have a unittest which tests one of the methods on the custom target, store_model_parameters by calling it with a dictionary of input parameters, i.e.:

t1.store_model_parameters(test_parameter_dict)
...

But this produces the following error:

AttributeError: 'BaseTarget' object has no attribute 'store_model_parameters'

Does the SiderealTargetFactory support the new custom Target models?

rachel3834 commented 2 weeks ago

Let me summarize the cause and solution here for the record:

Yes, the SiderealTargetFactory supports the new custom Target models. It should pick up the customized model automatically, so the test as originally written was fine.

The custom target model is declared in the TOM's settings.py list of INSTALLED_APPs. However, the error indicated that the model associated with this app wasn't being loaded correctly. This was diagnosed by adding print statements to the settings.py to report the value of the TARGET_MODEL_CLASS parameter.

While the TOM system itself was picking up the right settings, the DB used for the unittests is created separately, and the test runner code loads additional settings specific to the test DB. It was this test settings.py that had not been updated to use the custom target model and hence the test failed as described above.

The solution was to update the test settings file to include the custom target model in the INSTALLED_APPs and TARGET_MODEL_CLASS parameter. After this, the tests ran as expected.

Thanks very much @jchate6 for all your help!