Эх сурвалжийг харах

[oozie]: fix random order of start end time fields (#4293)

* [oozie]: fix random order of start end time fields
* [oozie] update test to reflect sorted parameters

---------

Co-authored-by: Harsh Gupta <42064744+Harshg999@users.noreply.github.com>
Bjorn Alm - personal 3 долоо хоног өмнө
parent
commit
be06cfbf99

+ 12 - 2
apps/oozie/src/oozie/forms.py

@@ -15,7 +15,6 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-import sys
 import logging
 from builtins import object
 from datetime import datetime, timedelta
@@ -23,7 +22,6 @@ from functools import partial
 from time import mktime, struct_time
 
 from django import forms
-from django.core.exceptions import ValidationError
 from django.forms.widgets import TextInput
 from django.utils.translation import gettext_lazy as _t
 
@@ -86,6 +84,18 @@ class ParameterForm(forms.Form):
   @staticmethod
   def get_initial_params(conf_dict):
     params = [key for key in list(conf_dict.keys()) if key not in ParameterForm.NON_PARAMETERS]
+
+    # Sort parameters: start_date first, end_date second, then alphabetically
+    def param_sort_key(name):
+      if name == 'start_date':
+        return (0, name)
+      elif name == 'end_date':
+        return (1, name)
+      else:
+        return (2, name)
+
+    params.sort(key=param_sort_key)
+
     return [{'name': name, 'value': conf_dict[name]} for name in params]
 
   @staticmethod

+ 65 - 2
apps/oozie/src/oozie/tests.py

@@ -954,6 +954,68 @@ class TestEditor(OozieMockBase):
     assert set(["b", "foo", "food", "time"]) == reduce(lambda x, y: x | set(y), result, set())
 
 
+  def test_get_initial_params_sorting(self):
+    """Test that ParameterForm.get_initial_params() sorts parameters correctly"""
+    from oozie.forms import ParameterForm
+    
+    # Test data with mixed order parameters
+    conf_dict = {
+        'end_date': '2024-01-01T10:00:00Z',
+        'start_date': '2024-01-01T09:00:00Z',
+        'oozie.use.system.libpath': 'true',
+        'custom_param': 'value',
+        'another_param': 'another_value',
+        'nominal_time': '2024-01-01T09:30:00Z'
+    }
+    
+    result = ParameterForm.get_initial_params(conf_dict)
+    
+    # Should be sorted: start_date first, end_date second, then alphabetically
+    assert len(result) == 6, "Expected 6 parameters, got {}".format(len(result))
+    assert result[0]['name'] == 'start_date'
+    assert result[0]['value'] == '2024-01-01T09:00:00Z'
+    assert result[1]['name'] == 'end_date'
+    assert result[1]['value'] == '2024-01-01T10:00:00Z'
+    assert result[2]['name'] == 'another_param'
+    assert result[2]['value'] == 'another_value'
+    assert result[3]['name'] == 'custom_param'
+    assert result[3]['value'] == 'value'
+    assert result[4]['name'] == 'nominal_time'
+    assert result[4]['value'] == '2024-01-01T09:30:00Z'
+    assert result[5]['name'] == 'oozie.use.system.libpath'
+    assert result[5]['value'] == 'true'
+
+  def test_get_initial_params_edge_cases(self):
+    """Test ParameterForm.get_initial_params() with edge cases"""
+    from oozie.forms import ParameterForm
+    
+    # Test empty dict
+    result = ParameterForm.get_initial_params({})
+    assert result == []
+    
+    # Test with oozie parameters (they are included, not filtered out)
+    conf_dict = {
+        'oozie.use.system.libpath': 'true',
+        'oozie.some.other.param': 'value'
+    }
+    result = ParameterForm.get_initial_params(conf_dict)
+    assert len(result) == 2
+    # Should be sorted alphabetically
+    assert result[0]['name'] == 'oozie.some.other.param'
+    assert result[0]['value'] == 'value'
+    assert result[1]['name'] == 'oozie.use.system.libpath'
+    assert result[1]['value'] == 'true'
+    
+    # Test with only start_date and end_date
+    conf_dict = {
+        'end_date': '2024-01-01T10:00:00Z',
+        'start_date': '2024-01-01T09:00:00Z'
+    }
+    result = ParameterForm.get_initial_params(conf_dict)
+    assert len(result) == 2
+    assert result[0]['name'] == 'start_date'
+    assert result[1]['name'] == 'end_date'
+
   def test_find_all_parameters(self):
     self.wf.data = json.dumps({'sla': [
         {'key': 'enabled', 'value': False},
@@ -1955,8 +2017,9 @@ class TestEditor(OozieMockBase):
 
     # Check param popup, SLEEP is set by coordinator so not shown in the popup
     response = self.c.get(reverse('oozie:submit_coordinator', args=[coord.id]))
-    assert ([{'name': u'output', 'value': ''},
-                  {'name': u'market', 'value': u'US'}
+    # Parameters are sorted alphabetically after start_date and end_date
+    assert ([{'name': u'market', 'value': u'US'},
+                  {'name': u'output', 'value': ''}
                   ] ==
                   response.context[0]['params_form'].initial)