influxdb-arel 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +22 -0
  3. data/.rspec +3 -0
  4. data/.travis.yml +8 -0
  5. data/Gemfile +6 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +350 -0
  8. data/Rakefile +6 -0
  9. data/influxdb-arel.gemspec +24 -0
  10. data/lib/influxdb-arel.rb +1 -0
  11. data/lib/influxdb.rb +4 -0
  12. data/lib/influxdb/arel.rb +38 -0
  13. data/lib/influxdb/arel/alias_predication.rb +9 -0
  14. data/lib/influxdb/arel/attributes.rb +1 -0
  15. data/lib/influxdb/arel/attributes/attribute.rb +74 -0
  16. data/lib/influxdb/arel/core_extensions.rb +49 -0
  17. data/lib/influxdb/arel/expressions.rb +73 -0
  18. data/lib/influxdb/arel/math.rb +21 -0
  19. data/lib/influxdb/arel/nodes.rb +27 -0
  20. data/lib/influxdb/arel/nodes/and.rb +32 -0
  21. data/lib/influxdb/arel/nodes/binary.rb +47 -0
  22. data/lib/influxdb/arel/nodes/duration.rb +30 -0
  23. data/lib/influxdb/arel/nodes/equality.rb +11 -0
  24. data/lib/influxdb/arel/nodes/function.rb +47 -0
  25. data/lib/influxdb/arel/nodes/grouping.rb +11 -0
  26. data/lib/influxdb/arel/nodes/in.rb +8 -0
  27. data/lib/influxdb/arel/nodes/infix_operation.rb +51 -0
  28. data/lib/influxdb/arel/nodes/node.rb +19 -0
  29. data/lib/influxdb/arel/nodes/now.rb +15 -0
  30. data/lib/influxdb/arel/nodes/select_statement.rb +59 -0
  31. data/lib/influxdb/arel/nodes/sql_literal.rb +23 -0
  32. data/lib/influxdb/arel/nodes/table_alias.rb +23 -0
  33. data/lib/influxdb/arel/nodes/time.rb +13 -0
  34. data/lib/influxdb/arel/nodes/unary.rb +35 -0
  35. data/lib/influxdb/arel/predications.rb +137 -0
  36. data/lib/influxdb/arel/select_manager.rb +129 -0
  37. data/lib/influxdb/arel/table.rb +219 -0
  38. data/lib/influxdb/arel/tree_manager.rb +30 -0
  39. data/lib/influxdb/arel/version.rb +5 -0
  40. data/lib/influxdb/arel/visitor.rb +287 -0
  41. data/spec/lib/influxdb/arel/core_extensions_spec.rb +49 -0
  42. data/spec/lib/influxdb/arel/nodes/and_spec.rb +17 -0
  43. data/spec/lib/influxdb/arel/nodes/binary_spec.rb +49 -0
  44. data/spec/lib/influxdb/arel/nodes/duration_spec.rb +72 -0
  45. data/spec/lib/influxdb/arel/nodes/equality_spec.rb +5 -0
  46. data/spec/lib/influxdb/arel/nodes/function_spec.rb +69 -0
  47. data/spec/lib/influxdb/arel/nodes/grouping_spec.rb +10 -0
  48. data/spec/lib/influxdb/arel/nodes/in_spec.rb +13 -0
  49. data/spec/lib/influxdb/arel/nodes/now_spec.rb +8 -0
  50. data/spec/lib/influxdb/arel/nodes/sql_literal_spec.rb +28 -0
  51. data/spec/lib/influxdb/arel/nodes/table_alias_spec.rb +36 -0
  52. data/spec/lib/influxdb/arel/nodes/time_spec.rb +5 -0
  53. data/spec/lib/influxdb/arel/nodes/unary_spec.rb +25 -0
  54. data/spec/lib/influxdb/arel/select_manager_spec.rb +459 -0
  55. data/spec/lib/influxdb/arel/table_spec.rb +193 -0
  56. data/spec/lib/influxdb/arel_spec.rb +11 -0
  57. data/spec/spec_helper.rb +20 -0
  58. data/spec/support/examples/binary_node.rb +10 -0
  59. data/spec/support/examples/function_node.rb +14 -0
  60. data/spec/support/examples/node_as.rb +8 -0
  61. data/spec/support/examples/node_expressions.rb +145 -0
  62. data/spec/support/examples/node_math.rb +29 -0
  63. data/spec/support/examples/node_predications.rb +248 -0
  64. data/spec/support/examples/node_to_sql.rb +5 -0
  65. data/spec/support/examples/unary_node.rb +10 -0
  66. data/spec/support/fabrics.rb +21 -0
  67. metadata +177 -0
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+ require './lib/influxdb/arel/core_extensions'
3
+
4
+ describe Integer do
5
+ describe '#u' do
6
+ specify{ expect(1.u).to eq(node(:Duration, 1, sql('u'))) }
7
+ end
8
+
9
+ describe '#s' do
10
+ specify{ expect(1.s).to eq(node(:Duration, 1, sql('s'))) }
11
+ end
12
+
13
+ describe '#m' do
14
+ specify{ expect(1.m).to eq(node(:Duration, 1, sql('m'))) }
15
+ end
16
+
17
+ describe '#h' do
18
+ specify{ expect(1.h).to eq(node(:Duration, 1, sql('h'))) }
19
+ end
20
+
21
+ describe '#d' do
22
+ specify{ expect(1.d).to eq(node(:Duration, 1, sql('d'))) }
23
+ end
24
+
25
+ describe '#w' do
26
+ specify{ expect(1.w).to eq(node(:Duration, 1, sql('w'))) }
27
+ end
28
+ end
29
+
30
+
31
+ describe String do
32
+ describe '#as' do
33
+ specify{ expect('MEAN(value)'.as('mean')).to eq(node(:As, sql('MEAN(value)'), 'mean')) }
34
+ end
35
+
36
+ describe '#to_arel' do
37
+ specify{ expect('events'.to_arel).to eq(sql('events')) }
38
+ end
39
+ end
40
+
41
+ describe Symbol do
42
+ describe '#as' do
43
+ specify{ expect(:events.as('alias')).to eq(node(:TableAlias, table(:events), 'alias')) }
44
+ end
45
+
46
+ describe '#to_arel' do
47
+ specify{ expect(:events.to_arel).to eq(table(:events)) }
48
+ end
49
+ end
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ describe Influxdb::Arel::Nodes::And do
4
+ describe '#to_sql' do
5
+ subject{ node(:And, ['first', sql('second'), 'third']).to_sql }
6
+
7
+ specify{ expect(subject).to eq("'first' AND second AND 'third'") }
8
+ end
9
+
10
+ describe '#eql?' do
11
+ subject{ node(:And, ['first', sql('second'), 'third']) }
12
+
13
+ specify{ expect(subject.eql?(node(:And, ['first', sql('second'), 'third']))).to be_truthy }
14
+ specify{ expect(subject.eql?(node(:And, ['first', 'second', 'third']))).to be_truthy }
15
+ specify{ expect(subject.eql?(node(:And, ['first', 'second', 'third', 'fourth']))).to be_falsy }
16
+ end
17
+ end
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+
3
+ describe Influxdb::Arel::Nodes::Binary do
4
+ it_should_behave_like :binary_node, :Binary
5
+ end
6
+
7
+ describe Influxdb::Arel::Nodes::As do
8
+ it_should_behave_like :binary_node, :As, 'left AS right'
9
+ end
10
+
11
+ describe Influxdb::Arel::Nodes::DoesNotMatch do
12
+ it_should_behave_like :binary_node, :DoesNotMatch, 'left !~ right'
13
+ end
14
+
15
+ describe Influxdb::Arel::Nodes::GreaterThan do
16
+ it_should_behave_like :binary_node, :GreaterThan, 'left > right'
17
+ end
18
+
19
+ describe Influxdb::Arel::Nodes::GreaterThanOrEqual do
20
+ it_should_behave_like :binary_node, :GreaterThanOrEqual, 'left >= right'
21
+ end
22
+
23
+ describe Influxdb::Arel::Nodes::Join do
24
+ it_should_behave_like :binary_node, :Join, 'left INNER JOIN right'
25
+ end
26
+
27
+ describe Influxdb::Arel::Nodes::LessThan do
28
+ it_should_behave_like :binary_node, :LessThan, 'left < right'
29
+ end
30
+
31
+ describe Influxdb::Arel::Nodes::LessThanOrEqual do
32
+ it_should_behave_like :binary_node, :LessThanOrEqual, 'left <= right'
33
+ end
34
+
35
+ describe Influxdb::Arel::Nodes::Matches do
36
+ it_should_behave_like :binary_node, :Matches, 'left =~ right'
37
+ end
38
+
39
+ describe Influxdb::Arel::Nodes::Merge do
40
+ it_should_behave_like :binary_node, :Merge, 'left MERGE right'
41
+ end
42
+
43
+ describe Influxdb::Arel::Nodes::NotEqual do
44
+ it_should_behave_like :binary_node, :NotEqual, 'left <> right'
45
+ end
46
+
47
+ describe Influxdb::Arel::Nodes::Or do
48
+ it_should_behave_like :binary_node, :Or, 'left OR right'
49
+ end
@@ -0,0 +1,72 @@
1
+ require 'spec_helper'
2
+
3
+ describe Influxdb::Arel::Nodes::Duration do
4
+ let(:described_node){ node(:Duration, 10, 'h') }
5
+
6
+ it_should_behave_like :node_to_sql, '10h'
7
+
8
+ describe '#eql?' do
9
+ specify{ expect(described_node.eql?(node(:Duration, 10, 'h'))).to be_truthy }
10
+ specify{ expect(described_node.eql?(node(:Duration, 10, 's'))).to be_falsy }
11
+ specify{ expect(described_node.eql?(node(:Duration, 11, 'h'))).to be_falsy }
12
+ end
13
+
14
+ describe 'initialization' do
15
+ context 'with invalid suffix' do
16
+ subject{ node(:Duration, 10, 'hours') }
17
+
18
+ specify{ expect(subject).to eq(node(:Duration, 10, 'u')) }
19
+ end
20
+
21
+ context 'with nulled suffix' do
22
+ subject{ node(:Duration, 10, nil) }
23
+
24
+ specify{ expect(subject).to eq(node(:Duration, 10, 'u')) }
25
+ end
26
+
27
+ context 'with value as string' do
28
+ subject{ node(:Duration, '10', 'h') }
29
+
30
+ specify{ expect(subject).to eq(node(:Duration, 10, 'h')) }
31
+ end
32
+
33
+ context 'with value as float' do
34
+ subject{ node(:Duration, 10.34, 'h') }
35
+
36
+ specify{ expect(subject).to eq(node(:Duration, 10, 'h')) }
37
+ end
38
+
39
+ context 'with invalid value' do
40
+ subject{ node(:Duration, 'one', 'h') }
41
+
42
+ specify{ expect(subject).to eq(node(:Duration, 0, 'h')) }
43
+ end
44
+
45
+ context 'with nulled value' do
46
+ subject{ node(:Duration, nil, 'h') }
47
+
48
+ specify{ expect(subject).to eq(node(:Duration, 0, 'h')) }
49
+ end
50
+ end
51
+
52
+ describe '#time' do
53
+ subject{ node(:Duration, 10, 'h').time }
54
+
55
+ specify{ expect(subject).to eq(node(:Time, node(:Duration, 10, 'h'))) }
56
+ specify{ expect(subject.to_sql).to eq('time(10h)') }
57
+ end
58
+
59
+ describe '#ago' do
60
+ subject{ node(:Duration, 10, 'h').ago }
61
+
62
+ specify{ expect(subject).to eq(node(:Grouping, node(:Subtraction, node(:Now), node(:Duration, 10, 'h')))) }
63
+ specify{ expect(subject.to_sql).to eq('(now() - 10h)') }
64
+ end
65
+
66
+ describe '#since' do
67
+ subject{ node(:Duration, 10, 'h').since }
68
+
69
+ specify{ expect(subject).to eq(node(:Grouping, node(:Addition, node(:Now), node(:Duration, 10, 'h')))) }
70
+ specify{ expect(subject.to_sql).to eq('(now() + 10h)') }
71
+ end
72
+ end
@@ -0,0 +1,5 @@
1
+ require 'spec_helper'
2
+
3
+ describe Influxdb::Arel::Nodes::Equality do
4
+ it_should_behave_like :binary_node, :Equality, 'left = right'
5
+ end
@@ -0,0 +1,69 @@
1
+ require 'spec_helper'
2
+
3
+ describe Influxdb::Arel::Nodes::Count do
4
+ it_should_behave_like :function_node, :Count, 'COUNT(expression)'
5
+ end
6
+
7
+ describe Influxdb::Arel::Nodes::Sum do
8
+ it_should_behave_like :function_node, :Sum, 'SUM(expression)'
9
+ end
10
+
11
+ describe Influxdb::Arel::Nodes::Max do
12
+ it_should_behave_like :function_node, :Max, 'MAX(expression)'
13
+ end
14
+
15
+ describe Influxdb::Arel::Nodes::Min do
16
+ it_should_behave_like :function_node, :Min, 'MIN(expression)'
17
+ end
18
+
19
+ describe Influxdb::Arel::Nodes::Mean do
20
+ it_should_behave_like :function_node, :Mean, 'MEAN(expression)'
21
+ end
22
+
23
+ describe Influxdb::Arel::Nodes::Mode do
24
+ it_should_behave_like :function_node, :Mode, 'MODE(expression)'
25
+ end
26
+
27
+ describe Influxdb::Arel::Nodes::Median do
28
+ it_should_behave_like :function_node, :Median, 'MEDIAN(expression)'
29
+ end
30
+
31
+ describe Influxdb::Arel::Nodes::Distinct do
32
+ it_should_behave_like :function_node, :Distinct, 'DISTINCT(expression)'
33
+ end
34
+
35
+ describe Influxdb::Arel::Nodes::Percentile do
36
+ it_should_behave_like :function_node, :Percentile, 'PERCENTILE(expression, 99)', [99]
37
+ end
38
+
39
+ describe Influxdb::Arel::Nodes::Histogram do
40
+ it_should_behave_like :function_node, :Histogram, 'HISTOGRAM(expression, 50)', [50]
41
+ end
42
+
43
+ describe Influxdb::Arel::Nodes::Derivative do
44
+ it_should_behave_like :function_node, :Derivative, 'DERIVATIVE(expression)'
45
+ end
46
+
47
+ describe Influxdb::Arel::Nodes::Stddev do
48
+ it_should_behave_like :function_node, :Stddev, 'STDDEV(expression)'
49
+ end
50
+
51
+ describe Influxdb::Arel::Nodes::First do
52
+ it_should_behave_like :function_node, :First, 'FIRST(expression)'
53
+ end
54
+
55
+ describe Influxdb::Arel::Nodes::Last do
56
+ it_should_behave_like :function_node, :Last, 'LAST(expression)'
57
+ end
58
+
59
+ describe Influxdb::Arel::Nodes::Difference do
60
+ it_should_behave_like :function_node, :Difference, 'DIFFERENCE(expression)'
61
+ end
62
+
63
+ describe Influxdb::Arel::Nodes::Top do
64
+ it_should_behave_like :function_node, :Top, 'TOP(expression, 10)', [10]
65
+ end
66
+
67
+ describe Influxdb::Arel::Nodes::Bottom do
68
+ it_should_behave_like :function_node, :Bottom, 'BOTTOM(expression, 10)', [10]
69
+ end
@@ -0,0 +1,10 @@
1
+ require 'spec_helper'
2
+
3
+ describe Influxdb::Arel::Nodes::Grouping do
4
+ let(:described_node){ node(:Grouping, sql('value')) }
5
+
6
+ it_should_behave_like :unary_node, :Grouping, '(value)'
7
+ it_should_behave_like :node_as, '(value)'
8
+ it_should_behave_like :node_expressions, '(value)'
9
+ it_should_behave_like :node_predications, '(value)'
10
+ end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ describe Influxdb::Arel::Nodes::In do
4
+ let(:described_node){ node(:In, sql('left'), [1, 2, 3]) }
5
+
6
+ it_should_behave_like :node_to_sql, 'left IN (1, 2, 3)'
7
+
8
+ describe '#eql?' do
9
+ specify{ expect(described_node.eql?(node(:In, sql('left'), [1, 2, 3]))).to be_truthy }
10
+ specify{ expect(described_node.eql?(node(:In, sql('left'), [1, 2]))).to be_falsy }
11
+ specify{ expect(described_node.eql?(node(:In, sql('left1'), [1, 2, 3]))).to be_falsy }
12
+ end
13
+ end
@@ -0,0 +1,8 @@
1
+ require 'spec_helper'
2
+
3
+ describe Influxdb::Arel::Nodes::Now do
4
+ let(:described_node){ node(:Now) }
5
+
6
+ it_should_behave_like :node_to_sql, 'now()'
7
+ it_should_behave_like :node_math, 'now()'
8
+ end
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ describe Influxdb::Arel::Nodes::SqlLiteral do
4
+ let(:described_node){ node(:SqlLiteral, 'expressions') }
5
+
6
+ describe '#to_sql' do
7
+ specify{ expect(visitor.accept(described_node)).to eq('expressions') }
8
+ end
9
+
10
+ it_should_behave_like :node_as, 'expressions'
11
+ it_should_behave_like :node_expressions, 'expressions'
12
+ it_should_behave_like :node_predications, 'expressions'
13
+
14
+ describe '#name' do
15
+ specify{ expect(described_node.name).to eq(described_node) }
16
+ end
17
+
18
+ describe '#unalias' do
19
+ specify{ expect(described_node.unalias).to eq(described_node) }
20
+ end
21
+
22
+ describe '#eql?' do
23
+ specify{ expect(described_node.eql?(node(:SqlLiteral, 'expressions'))).to be_truthy }
24
+ specify{ expect(described_node.eql?(table('expressions'))).to be_truthy }
25
+ specify{ expect(described_node.eql?(node(:SqlLiteral, 'expressions1'))).to be_falsy }
26
+ specify{ expect(described_node.eql?(node(:Fill, 'expressions'))).to be_falsy }
27
+ end
28
+ end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ describe Influxdb::Arel::Nodes::TableAlias do
4
+ let(:described_node){ node(:TableAlias, table('events'), 'alias') }
5
+
6
+ subject{ described_node }
7
+
8
+ it_should_behave_like :binary_node, :TableAlias, 'left AS right'
9
+
10
+ describe '#relation' do
11
+ specify{ expect(subject.relation).to eq(table('events')) }
12
+ end
13
+
14
+ describe '#table_alias' do
15
+ specify{ expect(subject.table_alias).to eq('alias') }
16
+ end
17
+
18
+ describe '#name' do
19
+ specify{ expect(subject.name).to eq('alias') }
20
+ end
21
+
22
+ describe '#[]' do
23
+ subject{ described_node[:name] }
24
+
25
+ specify{ expect(subject).to be_instance_of(Influxdb::Arel::Attribute) }
26
+ specify{ expect(visitor.accept(subject)).to eq('alias.name') }
27
+ end
28
+
29
+ describe '#unalias' do
30
+ specify{ expect(subject.unalias).to eq(table('events')) }
31
+ end
32
+
33
+ describe '#table_name' do
34
+ specify{ expect(subject.table_name).to eq('events') }
35
+ end
36
+ end
@@ -0,0 +1,5 @@
1
+ require 'spec_helper'
2
+
3
+ describe Influxdb::Arel::Nodes::Time do
4
+ it_should_behave_like :unary_node, :Time, 'time(value)'
5
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe Influxdb::Arel::Nodes::Unary do
4
+ it_should_behave_like :unary_node, :Unary
5
+ end
6
+
7
+ describe Influxdb::Arel::Nodes::Group do
8
+ it_should_behave_like :unary_node, :Group, 'value'
9
+ end
10
+
11
+ describe Influxdb::Arel::Nodes::Limit do
12
+ it_should_behave_like :unary_node, :Limit, 'LIMIT value'
13
+ end
14
+
15
+ describe Influxdb::Arel::Nodes::Fill do
16
+ it_should_behave_like :unary_node, :Fill, 'fill(value)'
17
+ end
18
+
19
+ describe Influxdb::Arel::Nodes::Ordering do
20
+ it_should_behave_like :unary_node, :Ordering, 'ORDER VALUE'
21
+ end
22
+
23
+ describe Influxdb::Arel::Nodes::Into do
24
+ it_should_behave_like :unary_node, :Into, 'INTO value'
25
+ end
@@ -0,0 +1,459 @@
1
+ require 'spec_helper'
2
+
3
+ describe Influxdb::Arel::SelectManager do
4
+ let(:manager){ Influxdb::Arel::SelectManager.new(table('events')) }
5
+
6
+ describe '#group' do
7
+ subject{ manager.group('time(1s)', 'name', :type) }
8
+
9
+ specify{ expect(subject).to be_instance_of(Influxdb::Arel::SelectManager) }
10
+ specify{ expect(subject.ast.groups).to eq([
11
+ node(:Group, sql('time(1s)')), node(:Group, sql('name')), node(:Group, sql('type'))
12
+ ]) }
13
+ specify{ expect(subject.to_sql).to eq('SELECT * FROM events GROUP BY time(1s), name, type') }
14
+
15
+ context 'chaining' do
16
+ subject{ table('events').group('time(1s)').group('name', :type) }
17
+
18
+ specify{ expect(subject).to be_instance_of(Influxdb::Arel::SelectManager) }
19
+ specify{ expect(subject.ast.groups).to eq([
20
+ node(:Group, sql('time(1s)')), node(:Group, sql('name')), node(:Group, sql('type'))
21
+ ]) }
22
+ specify{ expect(subject.to_sql).to eq('SELECT * FROM events GROUP BY time(1s), name, type') }
23
+ end
24
+ end
25
+
26
+ describe '#fill' do
27
+ context 'with empty groups' do
28
+ subject{ manager.fill(0) }
29
+
30
+ specify{ expect(subject).to be_instance_of(Influxdb::Arel::SelectManager) }
31
+ specify{ expect(subject.ast.fill).to eq(node(:Fill, 0)) }
32
+ specify{ expect(subject.to_sql).to eq('SELECT * FROM events') }
33
+ end
34
+
35
+ context 'filling empty values with integer' do
36
+ subject{ manager.group('time(1s)').fill(0) }
37
+
38
+ specify{ expect(subject).to be_instance_of(Influxdb::Arel::SelectManager) }
39
+ specify{ expect(subject.ast.fill).to eq(node(:Fill, 0)) }
40
+ specify{ expect(subject.to_sql).to eq('SELECT * FROM events GROUP BY time(1s) fill(0)') }
41
+ end
42
+
43
+ context 'filling empty values with string' do
44
+ subject{ manager.group('time(1s)').fill('empty') }
45
+
46
+ specify{ expect(subject).to be_instance_of(Influxdb::Arel::SelectManager) }
47
+ specify{ expect(subject.ast.fill).to eq(node(:Fill, 'empty')) }
48
+ specify{ expect(subject.to_sql).to eq("SELECT * FROM events GROUP BY time(1s) fill('empty')") }
49
+ end
50
+
51
+ context 'filling empty values with sql node' do
52
+ subject{ manager.group('time(1s)').fill(sql('name')) }
53
+
54
+ specify{ expect(subject).to be_instance_of(Influxdb::Arel::SelectManager) }
55
+ specify{ expect(subject.ast.fill).to eq(node(:Fill, sql('name'))) }
56
+ specify{ expect(subject.to_sql).to eq("SELECT * FROM events GROUP BY time(1s) fill(name)") }
57
+ end
58
+ end
59
+
60
+ describe '#from' do
61
+ context 'with table' do
62
+ subject{ manager.from(table('table')) }
63
+
64
+ specify{ expect(subject).to be_instance_of(Influxdb::Arel::SelectManager) }
65
+ specify{ expect(subject.ast.series).to eq([table('table')]) }
66
+ specify{ expect(subject.to_sql).to eq('SELECT * FROM table') }
67
+ end
68
+
69
+ context 'with symbol' do
70
+ subject{ manager.from(:table) }
71
+
72
+ specify{ expect(subject).to be_instance_of(Influxdb::Arel::SelectManager) }
73
+ specify{ expect(subject.ast.series).to eq([sql('table')]) }
74
+ specify{ expect(subject.to_sql).to eq('SELECT * FROM table') }
75
+ end
76
+
77
+ context 'with string' do
78
+ subject{ manager.from('table') }
79
+
80
+ specify{ expect(subject).to be_instance_of(Influxdb::Arel::SelectManager) }
81
+ specify{ expect(subject.ast.series).to eq([sql('table')]) }
82
+ specify{ expect(subject.to_sql).to eq('SELECT * FROM table') }
83
+ end
84
+
85
+ context 'with sql node' do
86
+ subject{ manager.from(sql('table')) }
87
+
88
+ specify{ expect(subject).to be_instance_of(Influxdb::Arel::SelectManager) }
89
+ specify{ expect(subject.ast.series).to eq([sql('table')]) }
90
+ specify{ expect(subject.to_sql).to eq('SELECT * FROM table') }
91
+ end
92
+
93
+ context 'with table alias' do
94
+ subject{ manager.from(table('table').as('alias')) }
95
+
96
+ specify{ expect(subject).to be_instance_of(Influxdb::Arel::SelectManager) }
97
+ specify{ expect(subject.ast.series).to eq([table('table').as('alias')]) }
98
+ specify{ expect(subject.to_sql).to eq('SELECT * FROM table') }
99
+ end
100
+
101
+ context 'with regexp' do
102
+ subject{ manager.from(/events\..*/) }
103
+
104
+ specify{ expect(subject).to be_instance_of(Influxdb::Arel::SelectManager) }
105
+ specify{ expect(subject.ast.series).to eq([sql('/events\..*/')]) }
106
+ specify{ expect(subject.to_sql).to eq('SELECT * FROM /events\..*/') }
107
+ end
108
+
109
+ context 'with nil' do
110
+ subject{ manager.from(nil) }
111
+
112
+ specify{ expect(subject).to be_instance_of(Influxdb::Arel::SelectManager) }
113
+ specify{ expect(subject.ast.series).to eq([table('events')]) }
114
+ specify{ expect(subject.to_sql).to eq('SELECT * FROM events') }
115
+ end
116
+
117
+ context 'with several tables' do
118
+ subject{ manager.from('table1', table('table2'), table('table2').as('alias')) }
119
+
120
+ specify{ expect(subject).to be_instance_of(Influxdb::Arel::SelectManager) }
121
+ specify{ expect(subject.ast.series).to eq([sql('table1'), table('table2'), table('table2').as('alias')]) }
122
+ specify{ expect(subject.to_sql).to eq('SELECT * FROM table1, table2') }
123
+ end
124
+
125
+ context 'with several non unique tables' do
126
+ subject{ manager.from('table1', table('table1'), 'table1') }
127
+
128
+ specify{ expect(subject).to be_instance_of(Influxdb::Arel::SelectManager) }
129
+ specify{ expect(subject.ast.series).to eq([sql('table1'), table('table1'), sql('table1')]) }
130
+ specify{ expect(subject.to_sql).to eq('SELECT * FROM table1') }
131
+ end
132
+
133
+ context 'chaining' do
134
+ subject{ manager.from('table1').from('table2') }
135
+
136
+ specify{ expect(subject).to be_instance_of(Influxdb::Arel::SelectManager) }
137
+ specify{ expect(subject.ast.series).to eq([sql('table2')]) }
138
+ specify{ expect(subject.to_sql).to eq('SELECT * FROM table2') }
139
+ end
140
+ end
141
+
142
+ describe '#join' do
143
+ let(:table_alias1){ table('events').as('events1') }
144
+ let(:table_alias2){ table('events').as('events2') }
145
+
146
+ subject{ manager.join('errors') }
147
+
148
+ specify{ expect(subject).to be_instance_of(Influxdb::Arel::SelectManager) }
149
+ specify{ expect(subject.ast.join).to eq(node(:Join, table('events'), sql('errors'))) }
150
+ specify{ expect(subject.to_sql).to eq('SELECT * FROM events INNER JOIN errors') }
151
+
152
+ context 'when table as argument' do
153
+ subject{ manager.join(table('errors')) }
154
+
155
+ specify{ expect(subject).to be_instance_of(Influxdb::Arel::SelectManager) }
156
+ specify{ expect(subject.ast.join).to eq(node(:Join, table('events'), table('errors'))) }
157
+ specify{ expect(subject.to_sql).to eq('SELECT * FROM events INNER JOIN errors') }
158
+ end
159
+
160
+ context 'when alias as argument' do
161
+ let(:table_alias){ table('errors').as('fatal_errors') }
162
+
163
+ subject{ manager.join(table_alias) }
164
+
165
+ specify{ expect(subject).to be_instance_of(Influxdb::Arel::SelectManager) }
166
+ specify{ expect(subject.ast.join).to eq(node(:Join, table('events'), table_alias)) }
167
+ specify{ expect(subject.to_sql).to eq('SELECT * FROM events INNER JOIN errors AS fatal_errors') }
168
+ end
169
+
170
+ context 'with two aliases' do
171
+ subject{ manager.from(table_alias1).join(table_alias2) }
172
+
173
+ specify{ expect(subject).to be_instance_of(Influxdb::Arel::SelectManager) }
174
+ specify{ expect(subject.ast.join).to eq(node(:Join, table_alias1, table_alias2)) }
175
+ specify{ expect(subject.to_sql).to eq('SELECT * FROM events AS events1 INNER JOIN events AS events2') }
176
+ end
177
+
178
+ context 'without argument' do
179
+ subject{ manager.join }
180
+
181
+ specify{ expect(subject).to be_instance_of(Influxdb::Arel::SelectManager) }
182
+ specify{ expect(subject.ast.join).to be_nil }
183
+ specify{ expect(subject.to_sql).to eq('SELECT * FROM events') }
184
+ end
185
+
186
+ context 'without argument with many series' do
187
+ subject{ manager.from('user_events', 'errors').join }
188
+
189
+ specify{ expect(subject).to be_instance_of(Influxdb::Arel::SelectManager) }
190
+ specify{ expect(subject.ast.join).to eq(node(:Join, sql('user_events'), sql('errors'))) }
191
+ specify{ expect(subject.to_sql).to eq('SELECT * FROM user_events INNER JOIN errors') }
192
+ end
193
+
194
+ context 'without argument with many aliases' do
195
+ subject{ manager.from(table_alias1, table_alias2).join }
196
+
197
+ specify{ expect(subject).to be_instance_of(Influxdb::Arel::SelectManager) }
198
+ specify{ expect(subject.ast.join).to eq(node(:Join, table_alias1, table_alias2)) }
199
+ specify{ expect(subject.to_sql).to eq('SELECT * FROM events AS events1 INNER JOIN events AS events2') }
200
+ end
201
+
202
+ context 'with merging' do
203
+ subject{ manager.from(table_alias1, table_alias2).merge.join }
204
+
205
+ specify{ expect(subject).to be_instance_of(Influxdb::Arel::SelectManager) }
206
+ specify{ expect(subject.ast.merge).to eq(node(:Merge, table('events'), table('events'))) }
207
+ specify{ expect(subject.ast.join).to eq(node(:Join, table_alias1, table_alias2)) }
208
+ specify{ expect(subject.to_sql).to eq('SELECT * FROM events AS events1 INNER JOIN events AS events2') }
209
+ end
210
+ end
211
+
212
+ describe '#merge' do
213
+ let(:table_alias1){ table('events').as('events1') }
214
+ let(:table_alias2){ table('errors').as('errors1') }
215
+
216
+ subject{ manager.merge('errors') }
217
+
218
+ specify{ expect(subject).to be_instance_of(Influxdb::Arel::SelectManager) }
219
+ specify{ expect(subject.ast.merge).to eq(node(:Merge, table('events'), sql('errors'))) }
220
+ specify{ expect(subject.to_sql).to eq('SELECT * FROM events MERGE errors') }
221
+
222
+ context 'when table as argument' do
223
+ subject{ manager.merge(table('errors')) }
224
+
225
+ specify{ expect(subject).to be_instance_of(Influxdb::Arel::SelectManager) }
226
+ specify{ expect(subject.ast.merge).to eq(node(:Merge, table('events'), table('errors'))) }
227
+ specify{ expect(subject.to_sql).to eq('SELECT * FROM events MERGE errors') }
228
+ end
229
+
230
+ context 'when alias as argument' do
231
+ let(:table_alias){ table('errors').as('fatal_errors') }
232
+
233
+ subject{ manager.merge(table_alias) }
234
+
235
+ specify{ expect(subject).to be_instance_of(Influxdb::Arel::SelectManager) }
236
+ specify{ expect(subject.ast.merge).to eq(node(:Merge, table('events'), table('errors'))) }
237
+ specify{ expect(subject.to_sql).to eq('SELECT * FROM events MERGE errors') }
238
+ end
239
+
240
+ context 'when alias as argument' do
241
+ let(:table_alias){ table('errors').as('fatal_errors') }
242
+
243
+ subject{ manager.merge(table_alias) }
244
+
245
+ specify{ expect(subject).to be_instance_of(Influxdb::Arel::SelectManager) }
246
+ specify{ expect(subject.ast.merge).to eq(node(:Merge, table('events'), table('errors'))) }
247
+ specify{ expect(subject.to_sql).to eq('SELECT * FROM events MERGE errors') }
248
+ end
249
+
250
+ context 'with two aliases' do
251
+ subject{ manager.from(table_alias1).merge(table_alias2) }
252
+
253
+ specify{ expect(subject).to be_instance_of(Influxdb::Arel::SelectManager) }
254
+ specify{ expect(subject.ast.merge).to eq(node(:Merge, table('events'), table('errors'))) }
255
+ specify{ expect(subject.to_sql).to eq('SELECT * FROM events MERGE errors') }
256
+ end
257
+
258
+ context 'without argument' do
259
+ subject{ manager.merge }
260
+
261
+ specify{ expect(subject).to be_instance_of(Influxdb::Arel::SelectManager) }
262
+ specify{ expect(subject.ast.merge).to be_nil }
263
+ specify{ expect(subject.to_sql).to eq('SELECT * FROM events') }
264
+ end
265
+
266
+ context 'without argument with many series' do
267
+ subject{ manager.from('user_events', 'errors').merge }
268
+
269
+ specify{ expect(subject).to be_instance_of(Influxdb::Arel::SelectManager) }
270
+ specify{ expect(subject.ast.merge).to eq(node(:Merge, sql('user_events'), sql('errors'))) }
271
+ specify{ expect(subject.to_sql).to eq('SELECT * FROM user_events MERGE errors') }
272
+ end
273
+ end
274
+
275
+ describe '#column' do
276
+ subject{ manager.column(table('events')[:time], 'name', :type) }
277
+
278
+ specify{ expect(subject).to be_instance_of(Influxdb::Arel::SelectManager) }
279
+ specify{ expect(subject.ast.columns).to eq([table('events')[:time], sql('name'), sql('type')]) }
280
+ specify{ expect(subject.to_sql).to eq('SELECT time, name, type FROM events') }
281
+
282
+ context 'chaining' do
283
+ subject{ manager.column(table('events')[:time]).column('name', :type) }
284
+
285
+ specify{ expect(subject).to be_instance_of(Influxdb::Arel::SelectManager) }
286
+ specify{ expect(subject.ast.columns).to eq([table('events')[:time], sql('name'), sql('type')]) }
287
+ specify{ expect(subject.to_sql).to eq('SELECT time, name, type FROM events') }
288
+ end
289
+ end
290
+
291
+ describe '#order' do
292
+ let(:asc){ node(:Ordering, 'asc') }
293
+ let(:desc){ node(:Ordering, 'desc') }
294
+
295
+ context 'when sort by ascending order' do
296
+ subject{ manager.order(:asc) }
297
+
298
+ specify{ expect(subject).to be_instance_of(Influxdb::Arel::SelectManager) }
299
+ specify{ expect(subject.ordering).to eq(asc) }
300
+ specify{ expect(subject.to_sql).to eq('SELECT * FROM events ORDER ASC') }
301
+ end
302
+
303
+ context 'when sort by ascending order' do
304
+ subject{ manager.order('asc') }
305
+
306
+ specify{ expect(subject).to be_instance_of(Influxdb::Arel::SelectManager) }
307
+ specify{ expect(subject.ordering).to eq(asc) }
308
+ specify{ expect(subject.to_sql).to eq('SELECT * FROM events ORDER ASC') }
309
+ end
310
+
311
+ context 'when sort by ascending order' do
312
+ subject{ manager.order(asc) }
313
+
314
+ specify{ expect(subject).to be_instance_of(Influxdb::Arel::SelectManager) }
315
+ specify{ expect(subject.ordering).to eq(asc) }
316
+ specify{ expect(subject.to_sql).to eq('SELECT * FROM events ORDER ASC') }
317
+ end
318
+
319
+ context 'when sort by ascending order' do
320
+ subject{ manager.asc }
321
+
322
+ specify{ expect(subject).to be_instance_of(Influxdb::Arel::SelectManager) }
323
+ specify{ expect(subject.ordering).to eq(asc) }
324
+ specify{ expect(subject.to_sql).to eq('SELECT * FROM events ORDER ASC') }
325
+ end
326
+
327
+ context 'when sort by descending order' do
328
+ subject{ manager.order(:desc) }
329
+
330
+ specify{ expect(subject).to be_instance_of(Influxdb::Arel::SelectManager) }
331
+ specify{ expect(subject.ordering).to eq(desc) }
332
+ specify{ expect(subject.to_sql).to eq('SELECT * FROM events ORDER DESC') }
333
+ end
334
+
335
+ context 'when sort by descending order' do
336
+ subject{ manager.order('desc') }
337
+
338
+ specify{ expect(subject).to be_instance_of(Influxdb::Arel::SelectManager) }
339
+ specify{ expect(subject.ordering).to eq(desc) }
340
+ specify{ expect(subject.to_sql).to eq('SELECT * FROM events ORDER DESC') }
341
+ end
342
+
343
+ context 'when sort by descending order' do
344
+ subject{ manager.order(desc) }
345
+
346
+ specify{ expect(subject).to be_instance_of(Influxdb::Arel::SelectManager) }
347
+ specify{ expect(subject.ordering).to eq(desc) }
348
+ specify{ expect(subject.to_sql).to eq('SELECT * FROM events ORDER DESC') }
349
+ end
350
+
351
+ context 'when sort by descending order' do
352
+ subject{ manager.desc }
353
+
354
+ specify{ expect(subject).to be_instance_of(Influxdb::Arel::SelectManager) }
355
+ specify{ expect(subject.ordering).to eq(desc) }
356
+ specify{ expect(subject.to_sql).to eq('SELECT * FROM events ORDER DESC') }
357
+ end
358
+
359
+ describe 'chaining' do
360
+ subject{ manager.order(desc).order(asc) }
361
+
362
+ specify{ expect(subject).to be_instance_of(Influxdb::Arel::SelectManager) }
363
+ specify{ expect(subject.ordering).to eq(asc) }
364
+ specify{ expect(subject.to_sql).to eq('SELECT * FROM events ORDER ASC') }
365
+ end
366
+ end
367
+
368
+ describe '#take' do
369
+ subject{ manager.take(100) }
370
+
371
+ specify{ expect(subject).to be_instance_of(Influxdb::Arel::SelectManager) }
372
+ specify{ expect(subject.taken).to eq(node(:Limit, 100)) }
373
+ specify{ expect(subject.to_sql).to eq('SELECT * FROM events LIMIT 100') }
374
+
375
+ context 'chaining' do
376
+ subject{ manager.take(100).take(1) }
377
+
378
+ specify{ expect(subject).to be_instance_of(Influxdb::Arel::SelectManager) }
379
+ specify{ expect(subject.taken).to eq(node(:Limit, 1)) }
380
+ specify{ expect(subject.to_sql).to eq('SELECT * FROM events LIMIT 1') }
381
+ end
382
+ end
383
+
384
+ describe '#where' do
385
+ context 'with conditions as string' do
386
+ subject{ manager.where("name = 'Undr'") }
387
+
388
+ specify{ expect(subject).to be_instance_of(Influxdb::Arel::SelectManager) }
389
+ specify{ expect(subject.wheres).to eq([sql("name = 'Undr'")]) }
390
+ specify{ expect(subject.to_sql).to eq("SELECT * FROM events WHERE name = 'Undr'") }
391
+ end
392
+
393
+ context 'with conditions as sql leteral' do
394
+ subject{ manager.where(sql("name = 'Undr'")) }
395
+
396
+ specify{ expect(subject).to be_instance_of(Influxdb::Arel::SelectManager) }
397
+ specify{ expect(subject.wheres).to eq([sql("name = 'Undr'")]) }
398
+ specify{ expect(subject.to_sql).to eq("SELECT * FROM events WHERE name = 'Undr'") }
399
+ end
400
+
401
+ context 'with conditions as sql node' do
402
+ subject{ manager.where(table('events')[:name].eq('Undr')) }
403
+
404
+ specify{ expect(subject).to be_instance_of(Influxdb::Arel::SelectManager) }
405
+ specify{ expect(subject.wheres).to eq([node(:Equality, table('events')[:name], 'Undr')]) }
406
+ specify{ expect(subject.to_sql).to eq("SELECT * FROM events WHERE name = 'Undr'") }
407
+ end
408
+
409
+ context 'chaining' do
410
+ subject{ manager.where("name = 'Undr'").where("email = 'undr@gmail.com'") }
411
+
412
+ specify{ expect(subject).to be_instance_of(Influxdb::Arel::SelectManager) }
413
+ specify{ expect(subject.wheres).to eq([sql("name = 'Undr'"), sql("email = 'undr@gmail.com'")]) }
414
+ specify{ expect(subject.to_sql).to eq("SELECT * FROM events WHERE name = 'Undr' AND email = 'undr@gmail.com'") }
415
+ end
416
+ end
417
+
418
+ describe '#into' do
419
+ context 'with string as argument' do
420
+ subject{ manager.into('events.all') }
421
+
422
+ specify{ expect(subject).to be_instance_of(Influxdb::Arel::SelectManager) }
423
+ specify{ expect(subject.ast.into).to eq(node(:Into, sql('events.all'))) }
424
+ specify{ expect(subject.to_sql).to eq("SELECT * FROM events INTO events.all") }
425
+ end
426
+
427
+ context 'with symbol as argument' do
428
+ subject{ manager.into(:'events.all') }
429
+
430
+ specify{ expect(subject).to be_instance_of(Influxdb::Arel::SelectManager) }
431
+ specify{ expect(subject.ast.into).to eq(node(:Into, sql('events.all'))) }
432
+ specify{ expect(subject.to_sql).to eq("SELECT * FROM events INTO events.all") }
433
+ end
434
+
435
+ context 'with sql node as argument' do
436
+ subject{ manager.into(sql('events.all')) }
437
+
438
+ specify{ expect(subject).to be_instance_of(Influxdb::Arel::SelectManager) }
439
+ specify{ expect(subject.ast.into).to eq(node(:Into, sql('events.all'))) }
440
+ specify{ expect(subject.to_sql).to eq("SELECT * FROM events INTO events.all") }
441
+ end
442
+
443
+ context 'fanout placeholder' do
444
+ subject{ manager.into('events.[host]') }
445
+
446
+ specify{ expect(subject).to be_instance_of(Influxdb::Arel::SelectManager) }
447
+ specify{ expect(subject.ast.into).to eq(node(:Into, sql('events.[host]'))) }
448
+ specify{ expect(subject.to_sql).to eq("SELECT * FROM events INTO events.[host]") }
449
+ end
450
+
451
+ context 'many series placeholder' do
452
+ subject{ manager.into('events.:series_name') }
453
+
454
+ specify{ expect(subject).to be_instance_of(Influxdb::Arel::SelectManager) }
455
+ specify{ expect(subject.ast.into).to eq(node(:Into, sql('events.:series_name'))) }
456
+ specify{ expect(subject.to_sql).to eq("SELECT * FROM events INTO events.:series_name") }
457
+ end
458
+ end
459
+ end