nql 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,117 +1,117 @@
1
- require 'spec_helper'
2
-
3
- describe 'SQL generation' do
4
-
5
- before :all do
6
- ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ":memory:"
7
- ActiveRecord::Base.connection
8
- ActiveRecord::Migrator.migrate ActiveRecord::Migrator.migrations_path
9
- end
10
-
11
- context 'Single comparisons' do
12
-
13
- it 'Equals' do
14
- q = 'name = abcd'
15
- Country.search(NQL.to_ransack(q)).result.should produce_sql "SELECT \"countries\".* FROM \"countries\" WHERE \"countries\".\"name\" = 'abcd'"
16
- end
17
-
18
- it 'Not equals' do
19
- q = 'name != abcd'
20
- Country.search(NQL.to_ransack(q)).result.should produce_sql "SELECT \"countries\".* FROM \"countries\" WHERE (\"countries\".\"name\" != 'abcd')"
21
- end
22
-
23
- it 'Greater than' do
24
- q = 'name > abcd'
25
- Country.search(NQL.to_ransack(q)).result.should produce_sql "SELECT \"countries\".* FROM \"countries\" WHERE (\"countries\".\"name\" > 'abcd')"
26
- end
27
-
28
- it 'Greater or equals than' do
29
- q = 'name >= abcd'
30
- Country.search(NQL.to_ransack(q)).result.should produce_sql "SELECT \"countries\".* FROM \"countries\" WHERE (\"countries\".\"name\" >= 'abcd')"
31
- end
32
-
33
- it 'Less than' do
34
- q = 'name < abcd'
35
- Country.search(NQL.to_ransack(q)).result.should produce_sql "SELECT \"countries\".* FROM \"countries\" WHERE (\"countries\".\"name\" < 'abcd')"
36
- end
37
-
38
- it 'Less or equals than' do
39
- q = 'name <= abcd'
40
- Country.search(NQL.to_ransack(q)).result.should produce_sql "SELECT \"countries\".* FROM \"countries\" WHERE (\"countries\".\"name\" <= 'abcd')"
41
- end
42
-
43
- it 'Contains' do
44
- q = 'name : abcd'
45
- Country.search(NQL.to_ransack(q)).result.should produce_sql "SELECT \"countries\".* FROM \"countries\" WHERE (\"countries\".\"name\" LIKE '%abcd%')"
46
- end
47
-
48
- end
49
-
50
- context 'Coordinated comparisons' do
51
-
52
- it 'And' do
53
- q = 'id > 1234 & name = abcd'
54
- Country.search(NQL.to_ransack(q)).result.should produce_sql "SELECT \"countries\".* FROM \"countries\" WHERE ((\"countries\".\"id\" > 1234 AND \"countries\".\"name\" = 'abcd'))"
55
- end
56
-
57
- it 'Or' do
58
- q = 'id < 1234 | name : abcd'
59
- Country.search(NQL.to_ransack(q)).result.should produce_sql "SELECT \"countries\".* FROM \"countries\" WHERE ((\"countries\".\"id\" < 1234 OR \"countries\".\"name\" LIKE '%abcd%'))"
60
- end
61
-
62
- it 'And then Or' do
63
- q = 'id > 1234 & name = abcd | name : efgh'
64
- Country.search(NQL.to_ransack(q)).result.should produce_sql "SELECT \"countries\".* FROM \"countries\" WHERE ((\"countries\".\"id\" > 1234 AND (\"countries\".\"name\" = 'abcd' OR \"countries\".\"name\" LIKE '%efgh%')))"
65
- end
66
-
67
- it 'With parentheses' do
68
- q = '(id > 1234 & name = abcd) | name : efgh'
69
- Country.search(NQL.to_ransack(q)).result.should produce_sql "SELECT \"countries\".* FROM \"countries\" WHERE ((\"countries\".\"name\" LIKE '%efgh%' OR (\"countries\".\"id\" > 1234 AND \"countries\".\"name\" = 'abcd')))"
70
- end
71
-
72
- end
73
-
74
- context 'Model joins' do
75
-
76
- it 'Parent join' do
77
- q = 'country.name : abcd'
78
- City.search(NQL.to_ransack(q)).result.should produce_sql "SELECT \"cities\".* FROM \"cities\" LEFT OUTER JOIN \"countries\" ON \"countries\".\"id\" = \"cities\".\"country_id\" WHERE (\"countries\".\"name\" LIKE '%abcd%')"
79
- end
80
-
81
- it 'Children join' do
82
- q = 'cities.name : abcd'
83
- Country.search(NQL.to_ransack(q)).result.should produce_sql "SELECT \"countries\".* FROM \"countries\" LEFT OUTER JOIN \"cities\" ON \"cities\".\"country_id\" = \"countries\".\"id\" WHERE (\"cities\".\"name\" LIKE '%abcd%')"
84
- end
85
-
86
- it 'Children join distinct' do
87
- q = 'cities.name : abcd'
88
- Country.search(NQL.to_ransack(q)).result(distinct: true).should produce_sql "SELECT DISTINCT \"countries\".* FROM \"countries\" LEFT OUTER JOIN \"cities\" ON \"cities\".\"country_id\" = \"countries\".\"id\" WHERE (\"cities\".\"name\" LIKE '%abcd%')"
89
- end
90
-
91
- end
92
-
93
- context 'Invalid queries' do
94
-
95
- it 'Nil' do
96
- q = nil
97
- Country.search(NQL.to_ransack(q)).result.should produce_sql "SELECT \"countries\".* FROM \"countries\""
98
- end
99
-
100
- it 'Empty' do
101
- q = ''
102
- Country.search(NQL.to_ransack(q)).result.should produce_sql "SELECT \"countries\".* FROM \"countries\""
103
- end
104
-
105
- it 'Empty with spaces' do
106
- q = ' '
107
- Country.search(NQL.to_ransack(q)).result.should produce_sql "SELECT \"countries\".* FROM \"countries\""
108
- end
109
-
110
- it 'Partial expression' do
111
- q = 'id ='
112
- expect{Country.search(NQL.to_ransack(q)).result}.should raise_exception NQL::InvalidExpressionError
113
- end
114
-
115
- end
116
-
1
+ require 'spec_helper'
2
+
3
+ describe 'SQL generation' do
4
+
5
+ before :all do
6
+ ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ":memory:"
7
+ ActiveRecord::Base.connection
8
+ ActiveRecord::Migrator.migrate ActiveRecord::Migrator.migrations_path
9
+ end
10
+
11
+ context 'Single comparisons' do
12
+
13
+ it 'Equals' do
14
+ q = 'name = abcd'
15
+ Country.search(NQL.to_ransack(q)).result.should produce_sql "SELECT \"countries\".* FROM \"countries\" WHERE \"countries\".\"name\" = 'abcd'"
16
+ end
17
+
18
+ it 'Not equals' do
19
+ q = 'name != abcd'
20
+ Country.search(NQL.to_ransack(q)).result.should produce_sql "SELECT \"countries\".* FROM \"countries\" WHERE (\"countries\".\"name\" != 'abcd')"
21
+ end
22
+
23
+ it 'Greater than' do
24
+ q = 'name > abcd'
25
+ Country.search(NQL.to_ransack(q)).result.should produce_sql "SELECT \"countries\".* FROM \"countries\" WHERE (\"countries\".\"name\" > 'abcd')"
26
+ end
27
+
28
+ it 'Greater or equals than' do
29
+ q = 'name >= abcd'
30
+ Country.search(NQL.to_ransack(q)).result.should produce_sql "SELECT \"countries\".* FROM \"countries\" WHERE (\"countries\".\"name\" >= 'abcd')"
31
+ end
32
+
33
+ it 'Less than' do
34
+ q = 'name < abcd'
35
+ Country.search(NQL.to_ransack(q)).result.should produce_sql "SELECT \"countries\".* FROM \"countries\" WHERE (\"countries\".\"name\" < 'abcd')"
36
+ end
37
+
38
+ it 'Less or equals than' do
39
+ q = 'name <= abcd'
40
+ Country.search(NQL.to_ransack(q)).result.should produce_sql "SELECT \"countries\".* FROM \"countries\" WHERE (\"countries\".\"name\" <= 'abcd')"
41
+ end
42
+
43
+ it 'Contains' do
44
+ q = 'name : abcd'
45
+ Country.search(NQL.to_ransack(q)).result.should produce_sql "SELECT \"countries\".* FROM \"countries\" WHERE (\"countries\".\"name\" LIKE '%abcd%')"
46
+ end
47
+
48
+ end
49
+
50
+ context 'Coordinated comparisons' do
51
+
52
+ it 'And' do
53
+ q = 'id > 1234 & name = abcd'
54
+ Country.search(NQL.to_ransack(q)).result.should produce_sql "SELECT \"countries\".* FROM \"countries\" WHERE ((\"countries\".\"id\" > 1234 AND \"countries\".\"name\" = 'abcd'))"
55
+ end
56
+
57
+ it 'Or' do
58
+ q = 'id < 1234 | name : abcd'
59
+ Country.search(NQL.to_ransack(q)).result.should produce_sql "SELECT \"countries\".* FROM \"countries\" WHERE ((\"countries\".\"id\" < 1234 OR \"countries\".\"name\" LIKE '%abcd%'))"
60
+ end
61
+
62
+ it 'And then Or' do
63
+ q = 'id > 1234 & name = abcd | name : efgh'
64
+ Country.search(NQL.to_ransack(q)).result.should produce_sql "SELECT \"countries\".* FROM \"countries\" WHERE ((\"countries\".\"id\" > 1234 AND (\"countries\".\"name\" = 'abcd' OR \"countries\".\"name\" LIKE '%efgh%')))"
65
+ end
66
+
67
+ it 'With parentheses' do
68
+ q = '(id > 1234 & name = abcd) | name : efgh'
69
+ Country.search(NQL.to_ransack(q)).result.should produce_sql "SELECT \"countries\".* FROM \"countries\" WHERE ((\"countries\".\"name\" LIKE '%efgh%' OR (\"countries\".\"id\" > 1234 AND \"countries\".\"name\" = 'abcd')))"
70
+ end
71
+
72
+ end
73
+
74
+ context 'Model joins' do
75
+
76
+ it 'Parent join' do
77
+ q = 'country.name : abcd'
78
+ City.search(NQL.to_ransack(q)).result.should produce_sql "SELECT \"cities\".* FROM \"cities\" LEFT OUTER JOIN \"countries\" ON \"countries\".\"id\" = \"cities\".\"country_id\" WHERE (\"countries\".\"name\" LIKE '%abcd%')"
79
+ end
80
+
81
+ it 'Children join' do
82
+ q = 'cities.name : abcd'
83
+ Country.search(NQL.to_ransack(q)).result.should produce_sql "SELECT \"countries\".* FROM \"countries\" LEFT OUTER JOIN \"cities\" ON \"cities\".\"country_id\" = \"countries\".\"id\" WHERE (\"cities\".\"name\" LIKE '%abcd%')"
84
+ end
85
+
86
+ it 'Children join distinct' do
87
+ q = 'cities.name : abcd'
88
+ Country.search(NQL.to_ransack(q)).result(distinct: true).should produce_sql "SELECT DISTINCT \"countries\".* FROM \"countries\" LEFT OUTER JOIN \"cities\" ON \"cities\".\"country_id\" = \"countries\".\"id\" WHERE (\"cities\".\"name\" LIKE '%abcd%')"
89
+ end
90
+
91
+ end
92
+
93
+ context 'Invalid queries' do
94
+
95
+ it 'Nil' do
96
+ q = nil
97
+ Country.search(NQL.to_ransack(q)).result.should produce_sql "SELECT \"countries\".* FROM \"countries\""
98
+ end
99
+
100
+ it 'Empty' do
101
+ q = ''
102
+ Country.search(NQL.to_ransack(q)).result.should produce_sql "SELECT \"countries\".* FROM \"countries\""
103
+ end
104
+
105
+ it 'Empty with spaces' do
106
+ q = ' '
107
+ Country.search(NQL.to_ransack(q)).result.should produce_sql "SELECT \"countries\".* FROM \"countries\""
108
+ end
109
+
110
+ it 'Partial expression' do
111
+ q = 'id ='
112
+ expect { Country.search(NQL.to_ransack(q)).result }.to raise_exception NQL::InvalidExpressionError
113
+ end
114
+
115
+ end
116
+
117
117
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-09 00:00:00.000000000 Z
12
+ date: 2012-11-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: treetop
16
- requirement: &28678476 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,15 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *28678476
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
25
30
  - !ruby/object:Gem::Dependency
26
31
  name: activerecord
27
- requirement: &28678152 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
28
33
  none: false
29
34
  requirements:
30
35
  - - ! '>='
@@ -32,10 +37,15 @@ dependencies:
32
37
  version: 3.2.0
33
38
  type: :runtime
34
39
  prerelease: false
35
- version_requirements: *28678152
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 3.2.0
36
46
  - !ruby/object:Gem::Dependency
37
47
  name: activesupport
38
- requirement: &28677828 !ruby/object:Gem::Requirement
48
+ requirement: !ruby/object:Gem::Requirement
39
49
  none: false
40
50
  requirements:
41
51
  - - ! '>='
@@ -43,10 +53,15 @@ dependencies:
43
53
  version: 3.2.0
44
54
  type: :runtime
45
55
  prerelease: false
46
- version_requirements: *28677828
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 3.2.0
47
62
  - !ruby/object:Gem::Dependency
48
63
  name: ransack
49
- requirement: &28677600 !ruby/object:Gem::Requirement
64
+ requirement: !ruby/object:Gem::Requirement
50
65
  none: false
51
66
  requirements:
52
67
  - - ! '>='
@@ -54,10 +69,15 @@ dependencies:
54
69
  version: '0'
55
70
  type: :runtime
56
71
  prerelease: false
57
- version_requirements: *28677600
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
58
78
  - !ruby/object:Gem::Dependency
59
79
  name: sqlite3
60
- requirement: &28677324 !ruby/object:Gem::Requirement
80
+ requirement: !ruby/object:Gem::Requirement
61
81
  none: false
62
82
  requirements:
63
83
  - - ! '>='
@@ -65,10 +85,15 @@ dependencies:
65
85
  version: '0'
66
86
  type: :development
67
87
  prerelease: false
68
- version_requirements: *28677324
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
69
94
  - !ruby/object:Gem::Dependency
70
95
  name: rspec
71
- requirement: &28677060 !ruby/object:Gem::Requirement
96
+ requirement: !ruby/object:Gem::Requirement
72
97
  none: false
73
98
  requirements:
74
99
  - - ! '>='
@@ -76,7 +101,12 @@ dependencies:
76
101
  version: '0'
77
102
  type: :development
78
103
  prerelease: false
79
- version_requirements: *28677060
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
80
110
  description: Natural Query Language built on top of ActiveRecord and Ransack
81
111
  email:
82
112
  - gabynaiman@gmail.com
@@ -124,7 +154,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
124
154
  version: '0'
125
155
  requirements: []
126
156
  rubyforge_project:
127
- rubygems_version: 1.8.16
157
+ rubygems_version: 1.8.24
128
158
  signing_key:
129
159
  specification_version: 3
130
160
  summary: Natural Query Language built on top of ActiveRecord and Ransack