1+ from abc import ABC
2+
13from django import forms
24from django .test import TestCase
35
46from tom_dataservices .dataservices import BaseDataService , MissingDataException
57from tom_dataservices .forms import BaseQueryForm
8+ from tom_targets .models import Target
69
710
811test_query_results = {'ra' : 24 , 'dec' : 77 , 'name' : 'faketarget' , 'type' : 'SIDEREAL' }
912
1013
1114class TestDataServiceForm (BaseQueryForm ):
12- """ All brokers must have a form which will be used to construct and save queries
13- to the broker. They should subclass `BaseQueryForm` which includes some required
14- fields and contains logic for serializing and persisting the query parameters to the
15- database. This test form will only have one field.
15+ """ A DataService must implement a form in order to be displayed in a TOM.
16+ They should subclass `BaseQueryForm`. This test form will only have one field.
1617 """
1718 name = forms .CharField (required = True )
1819
@@ -25,27 +26,71 @@ def query_service(self, term):
2526 if term == 'notfound' :
2627 raise MissingDataException
2728 self .query_results = test_query_results
29+ return
2830
2931 def get_form_class (self ):
3032 return TestDataServiceForm
3133
32- def pre_query_validation (self , query_parameters ):
33- pass
34+ def create_target_from_query (self , query_results , ** kwargs ):
35+ return Target (** query_results )
36+
37+
38+ class EmptyTestDataService (BaseDataService ):
39+ name = 'TEST'
40+ service_notes = "This is a test DataService."
3441
35- def to_target (self , query_results ):
36- target = super ().to_target (query_results )
37- target .name = query_results ['name' ]
38- target .type = query_results ['type' ]
39- target .ra = query_results ['ra' ]
40- target .dec = query_results ['dec' ]
41- return target
42+ def query_service (self , term ):
43+ if term == 'notfound' :
44+ raise MissingDataException
45+ self .query_results = test_query_results
46+ return
4247
4348
4449class TestDataServiceClass (TestCase ):
4550 """
46- Test the functionality of the TestDataService.
51+ Test the functionality of the DataService class via the TestDataService.
4752 """
53+ def test_query_service (self ):
54+ new_test_query = TestDataService ()
55+ self .assertEqual (new_test_query .query_results ,{})
56+ new_test_query .query_service ('mytarget' )
57+ self .assertEqual (new_test_query .query_results , test_query_results )
4858
4959 def test_to_target (self ):
50- target = TestDataService .to_target (self , test_query_results )
60+ new_test_query = TestDataService ()
61+ # Show to_target() returns error with no query_results
62+ with self .assertRaises (MissingDataException ):
63+ new_test_query .to_target ()
64+ # Show to_target() works with the default query_results
65+ new_test_query .query_targets ('mytarget' )
66+ target = new_test_query .to_target ()
5167 self .assertEqual (target .name , test_query_results ['name' ])
68+ # Show to_target() works independently of the query.
69+ new_test_query_results = test_query_results .copy ()
70+ new_test_query_results ['name' ] = 'target2'
71+ target2 = new_test_query .to_target (new_test_query_results )
72+ self .assertEqual (target2 .name , 'target2' )
73+
74+
75+ class TestUnimplementedDataServiceClass (TestCase ):
76+ """
77+ Test the functionality of a DataService with unimplemented methods.
78+ """
79+
80+ def test_no_create_data_product_from_query (self ):
81+ new_test_query = EmptyTestDataService ()
82+ # Show to_data_product() returns error when create_data_product_from_query undefined
83+ with self .assertRaises (NotImplementedError ):
84+ new_test_query .to_data_product (test_query_results )
85+
86+ def test_no_create_target_from_query (self ):
87+ new_test_query = EmptyTestDataService ()
88+ # Show to_data_product() returns error when create_data_product_from_query undefined
89+ with self .assertRaises (NotImplementedError ):
90+ new_test_query .to_target (test_query_results )
91+
92+ def test_no_create_reduced_datums_from_query (self ):
93+ new_test_query = EmptyTestDataService ()
94+ # Show to_data_product() returns error when create_data_product_from_query undefined
95+ with self .assertRaises (NotImplementedError ):
96+ new_test_query .to_reduced_datums (test_query_results )
0 commit comments