Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 9th, 2012  |  syntax: None  |  size: 4.37 KB  |  hits: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. diff --git a/lib/net_cordia/cherry_filter_util.rb b/lib/net_cordia/cherry_filter_util.rb
  2. index 1932ee7..ef162b8 100644
  3. --- a/lib/net_cordia/cherry_filter_util.rb
  4. +++ b/lib/net_cordia/cherry_filter_util.rb
  5. @@ -32,7 +32,7 @@ module NetCordia
  6.          end
  7.          return x
  8.        end
  9. -      return '' if filter['values'].blank?  
  10. +      return '' if filter['values'].blank?
  11.        if filter['operatorId']
  12.          # we are a leaf, do the right thing
  13.          statement = CONVERT_IT[filter['operatorId']]
  14. @@ -45,6 +45,10 @@ module NetCordia
  15.              rtn = rtn.gsub("like", "rlike")
  16.              rtn = rtn.gsub("%", ".*")
  17.            else
  18. +            if statement[0] =~ /.*like.*/
  19. +              # now we need to deal with wildcards: % and _
  20. +              value = value.gsub('%','\%').gsub('_','\_')
  21. +            end
  22.              values.push(value)
  23.            end
  24.          elsif statement[1] == 2
  25. diff --git a/spec/lib/cherry_filter_util_spec.rb b/spec/lib/cherry_filter_util_spec.rb
  26. new file mode 100644
  27. index 0000000..f97a4cd
  28. --- /dev/null
  29. +++ b/spec/lib/cherry_filter_util_spec.rb
  30. @@ -0,0 +1,105 @@
  31. +require File.dirname(__FILE__) + '/../spec_helper'
  32. +
  33. +describe NetCordia::CherryFilterUtil do
  34. +  def escape_test(condition,value)
  35. +    filter = {
  36. +      "fieldId"=>'test',
  37. +      "operatorId"=>condition,
  38. +      "values"=>[{
  39. +        "label"=>value,
  40. +        "value"=>value
  41. +      }]
  42. +    }.to_json
  43. +    
  44. +    NetCordia::CherryFilterUtil.sql_condition_from_filter(filter)[1].should == "\\#{value}"
  45. +  end
  46. +  
  47. +  def nonescape_test(condition,value)
  48. +    filter = {
  49. +      "fieldId"=>'test',
  50. +      "operatorId"=>condition,
  51. +      "values"=>[{
  52. +        "label"=>value,
  53. +        "value"=>value
  54. +      }]
  55. +    }.to_json
  56. +    
  57. +    RAILS_DEFAULT_LOGGER.debug NetCordia::CherryFilterUtil.sql_condition_from_filter(filter)#[1].should == value
  58. +  end
  59. +  
  60. +  it 'should escape "%" for STRING_CONTAINS' do
  61. +    escape_test('STRING_CONTAINS','%')
  62. +  end
  63. +  it 'should escape "%" for STRING_DOESNT_CONTAIN' do
  64. +    escape_test('STRING_DOESNT_CONTAIN','%')
  65. +  end
  66. +  it 'should escape "%" for STRING_STARTS_WITH' do
  67. +    escape_test('STRING_STARTS_WITH','%')
  68. +  end
  69. +  it 'should escape "%" for STRING_ENDS_WITH' do
  70. +    escape_test('STRING_ENDS_WITH','%')
  71. +  end
  72. +  
  73. +  it 'should escape "_" for STRING_CONTAINS' do
  74. +    escape_test('STRING_CONTAINS','_')
  75. +  end
  76. +  it 'should escape "_" for STRING_DOESNT_CONTAIN' do
  77. +    escape_test('STRING_DOESNT_CONTAIN','_')
  78. +  end
  79. +  it 'should escape "_" for STRING_STARTS_WITH' do
  80. +    escape_test('STRING_STARTS_WITH','_')
  81. +  end
  82. +  it 'should escape "_" for STRING_ENDS_WITH' do
  83. +    escape_test('STRING_ENDS_WITH','_')
  84. +  end
  85. +  
  86. +  
  87. +  it 'should not escape "%" for STRING_EQUAL' do
  88. +    nonescape_test('STRING_EQUAL','%')
  89. +  end
  90. +  it 'should not escape "%" for STRING_DIFFERENT' do
  91. +    nonescape_test('STRING_DIFFERENT','%')
  92. +  end
  93. +  it 'should not escape "%" for STRING_IN_LIST' do
  94. +    nonescape_test('STRING_IN_LIST','%')
  95. +  end
  96. +  it 'should not escape "%" for STRING_NOT_IN_LIST' do
  97. +    nonescape_test('STRING_NOT_IN_LIST','%')
  98. +  end
  99. +  it 'should not escape "%" for NUMBER_EQUAL' do
  100. +    nonescape_test('NUMBER_EQUAL','%')
  101. +  end
  102. +  it 'should not escape "%" for NUMBER_NOT_EQUAL' do
  103. +    nonescape_test('NUMBER_NOT_EQUAL','%')
  104. +  end
  105. +  it 'should not escape "%" for NUMBER_GREATER' do
  106. +    nonescape_test('NUMBER_GREATER','%')
  107. +  end
  108. +  it 'should not escape "%" for NUMBER_GREATER_OR_EQUAL' do
  109. +    nonescape_test('NUMBER_GREATER_OR_EQUAL','%')
  110. +  end
  111. +  it 'should not escape "%" for NUMBER_LESS' do
  112. +    nonescape_test('NUMBER_LESS','%')
  113. +  end
  114. +  it 'should not escape "%" for NUMBER_LESS_OR_EQUAL' do
  115. +    nonescape_test('NUMBER_LESS_OR_EQUAL','%')
  116. +  end
  117. +  it 'should not escape "%" for DATE_EQUAL' do
  118. +    nonescape_test('DATE_EQUAL','%')
  119. +  end
  120. +  it 'should not escape "%" for DATE_GREATER' do
  121. +    nonescape_test('DATE_GREATER','%')
  122. +  end
  123. +  it 'should not escape "%" for DATE_GREATER_OR_EQUAL' do
  124. +    nonescape_test('DATE_GREATER_OR_EQUAL','%')
  125. +  end
  126. +  it 'should not escape "%" for DATE_LESS' do
  127. +    nonescape_test('DATE_LESS','%')
  128. +  end
  129. +  it 'should not escape "%" for DATE_LESS_OR_EQUAL' do
  130. +    nonescape_test('DATE_LESS_OR_EQUAL','%')
  131. +  end
  132. +  it 'should not escape "%" for DATE_RANGE' do
  133. +    nonescape_test('DATE_RANGE','%')
  134. +  end
  135. +end
  136. \ No newline at end of file