Skip to content

Commit e05f615

Browse files
committed
test(analytics): add integration tests for old analytics API
1 parent 9839d26 commit e05f615

3 files changed

Lines changed: 355 additions & 0 deletions

File tree

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../spec_helper'
4+
require_relative 'shared_configuration_context'
5+
6+
describe Typesense::AnalyticsRuleV1 do
7+
subject(:analytics_rule_v1) { typesense.analytics_v1.rules[rule_name] }
8+
9+
include_context 'with Typesense configuration'
10+
11+
let(:rule_name) { 'test_rule' }
12+
let(:rule_configuration) do
13+
{
14+
'type' => 'popular_queries',
15+
'params' => {
16+
'source' => { 'collections' => ['products'] },
17+
'destination' => { 'collection' => 'product_queries' },
18+
'expand_query' => false,
19+
'limit' => 100
20+
}
21+
}
22+
end
23+
24+
let(:integration_client) do
25+
Typesense::Client.new(
26+
nodes: [{
27+
host: 'localhost',
28+
port: '8108',
29+
protocol: 'http'
30+
}],
31+
api_key: 'xyz',
32+
connection_timeout_seconds: 10
33+
)
34+
end
35+
36+
before do
37+
skip('Analytics is deprecated in Typesense v30+') if typesense_v30_or_above?
38+
39+
WebMock.disable!
40+
41+
# Create test collection for v1 analytics
42+
begin
43+
integration_client.collections.create({
44+
'name' => 'products',
45+
'fields' => [
46+
{ 'name' => 'title', 'type' => 'string' },
47+
{ 'name' => 'popularity', 'type' => 'int32', 'optional' => true }
48+
]
49+
})
50+
rescue Typesense::Error::ObjectAlreadyExists
51+
# Collection already exists, which is fine
52+
end
53+
54+
# Create test rule
55+
begin
56+
integration_client.analytics_v1.rules.upsert(rule_name, rule_configuration)
57+
rescue StandardError
58+
# Rule creation might fail, which is fine for testing
59+
end
60+
end
61+
62+
after do
63+
# Clean up test rules
64+
begin
65+
rules = integration_client.analytics_v1.rules.retrieve
66+
if rules.is_a?(Hash) && rules['rules']
67+
rules['rules'].each do |rule|
68+
integration_client.analytics_v1.rules[rule['name']].delete
69+
rescue StandardError
70+
# Ignore cleanup errors
71+
end
72+
end
73+
rescue StandardError
74+
# Ignore cleanup errors
75+
end
76+
77+
# Clean up test collection
78+
begin
79+
integration_client.collections['products'].delete
80+
rescue StandardError
81+
# Ignore cleanup errors
82+
end
83+
84+
WebMock.enable!
85+
end
86+
87+
describe '#retrieve' do
88+
it 'returns the specified analytics rule' do
89+
result = integration_client.analytics_v1.rules[rule_name].retrieve
90+
91+
expect(result['name']).to eq(rule_name)
92+
expect(result['type']).to eq('popular_queries')
93+
end
94+
end
95+
96+
describe '#delete' do
97+
it 'deletes the specified analytics rule' do
98+
result = integration_client.analytics_v1.rules[rule_name].delete
99+
expect(result['name']).to eq(rule_name)
100+
101+
# Verify the rule is deleted
102+
expect do
103+
integration_client.analytics_v1.rules[rule_name].retrieve
104+
end.to raise_error(Typesense::Error::ObjectNotFound)
105+
end
106+
end
107+
end
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../spec_helper'
4+
require_relative 'shared_configuration_context'
5+
6+
describe Typesense::AnalyticsRulesV1 do
7+
subject(:analytics_rules_v1) { typesense.analytics_v1.rules }
8+
9+
include_context 'with Typesense configuration'
10+
11+
let(:rule_name) { 'test_rule' }
12+
let(:rule_configuration) do
13+
{
14+
'type' => 'popular_queries',
15+
'params' => {
16+
'source' => { 'collections' => ['products'] },
17+
'destination' => { 'collection' => 'product_queries' },
18+
'expand_query' => false,
19+
'limit' => 100
20+
}
21+
}
22+
end
23+
24+
let(:integration_client) do
25+
Typesense::Client.new(
26+
nodes: [{
27+
host: 'localhost',
28+
port: '8108',
29+
protocol: 'http'
30+
}],
31+
api_key: 'xyz',
32+
connection_timeout_seconds: 10
33+
)
34+
end
35+
36+
before do
37+
skip('Analytics is deprecated in Typesense v30+') if typesense_v30_or_above?
38+
39+
WebMock.disable!
40+
41+
# Create test collection for v1 analytics
42+
begin
43+
integration_client.collections.create({
44+
'name' => 'products',
45+
'fields' => [
46+
{ 'name' => 'title', 'type' => 'string' },
47+
{ 'name' => 'popularity', 'type' => 'int32', 'optional' => true }
48+
]
49+
})
50+
rescue Typesense::Error::ObjectAlreadyExists
51+
# Collection already exists, which is fine
52+
end
53+
end
54+
55+
after do
56+
# Clean up test rules
57+
begin
58+
rules = integration_client.analytics_v1.rules.retrieve
59+
if rules.is_a?(Hash) && rules['rules']
60+
rules['rules'].each do |rule|
61+
integration_client.analytics_v1.rules[rule['name']].delete
62+
rescue StandardError
63+
# Ignore cleanup errors
64+
end
65+
end
66+
rescue StandardError
67+
# Ignore cleanup errors
68+
end
69+
70+
# Clean up test collection
71+
begin
72+
integration_client.collections['products'].delete
73+
rescue StandardError
74+
# Ignore cleanup errors
75+
end
76+
77+
WebMock.enable!
78+
end
79+
80+
describe '#upsert' do
81+
it 'creates a rule and returns it' do
82+
result = integration_client.analytics_v1.rules.upsert(rule_name, rule_configuration)
83+
84+
expect(result['name']).to eq(rule_name)
85+
expect(result['type']).to eq('popular_queries')
86+
end
87+
end
88+
89+
describe '#retrieve' do
90+
it 'retrieves all analytics rules' do
91+
# First create a rule
92+
integration_client.analytics_v1.rules.upsert(rule_name, rule_configuration)
93+
94+
result = integration_client.analytics_v1.rules.retrieve
95+
96+
expect(result).to be_a(Hash)
97+
expect(result['rules']).to be_an(Array)
98+
expect(result['rules'].length).to be >= 1
99+
end
100+
end
101+
102+
describe '#[]' do
103+
it 'creates an analytics rule object and returns it' do
104+
# First create a rule
105+
integration_client.analytics_v1.rules.upsert(rule_name, rule_configuration)
106+
107+
result = integration_client.analytics_v1.rules[rule_name]
108+
109+
expect(result).to be_a(Typesense::AnalyticsRuleV1)
110+
expect(result.instance_variable_get(:@rule_name)).to eq(rule_name)
111+
end
112+
113+
it 'memoizes the analytics rule instance' do
114+
# First create a rule
115+
integration_client.analytics_v1.rules.upsert(rule_name, rule_configuration)
116+
117+
first_call = integration_client.analytics_v1.rules[rule_name]
118+
second_call = integration_client.analytics_v1.rules[rule_name]
119+
expect(first_call).to equal(second_call)
120+
end
121+
end
122+
end
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../spec_helper'
4+
require_relative 'shared_configuration_context'
5+
6+
describe Typesense::AnalyticsV1 do
7+
subject(:analytics_v1) { typesense.analytics_v1 }
8+
9+
include_context 'with Typesense configuration'
10+
11+
let(:integration_client) do
12+
Typesense::Client.new(
13+
nodes: [{
14+
host: 'localhost',
15+
port: '8108',
16+
protocol: 'http'
17+
}],
18+
api_key: 'xyz',
19+
connection_timeout_seconds: 10
20+
)
21+
end
22+
23+
before do
24+
skip('Analytics is deprecated in Typesense v30+') if typesense_v30_or_above?
25+
end
26+
27+
describe '#rules' do
28+
it 'returns an AnalyticsRulesV1 instance' do
29+
expect(analytics_v1.rules).to be_a(Typesense::AnalyticsRulesV1)
30+
end
31+
32+
it 'memoizes the rules instance' do
33+
first_call = analytics_v1.rules
34+
second_call = analytics_v1.rules
35+
expect(first_call).to equal(second_call)
36+
end
37+
end
38+
39+
describe '#events' do
40+
it 'returns an AnalyticsEventsV1 instance' do
41+
expect(analytics_v1.events).to be_a(Typesense::AnalyticsEventsV1)
42+
end
43+
44+
it 'memoizes the events instance' do
45+
first_call = analytics_v1.events
46+
second_call = analytics_v1.events
47+
expect(first_call).to equal(second_call)
48+
end
49+
end
50+
51+
context 'with integration tests', :integration do
52+
before do
53+
WebMock.disable!
54+
55+
# Create test collections for v1 analytics
56+
begin
57+
integration_client.collections.create({
58+
'name' => 'products',
59+
'fields' => [
60+
{ 'name' => 'title', 'type' => 'string' },
61+
{ 'name' => 'popularity', 'type' => 'int32', 'optional' => true }
62+
]
63+
})
64+
rescue Typesense::Error::ObjectAlreadyExists
65+
# Collection already exists, which is fine
66+
end
67+
68+
begin
69+
integration_client.collections.create({
70+
'name' => 'product_queries',
71+
'fields' => [
72+
{ 'name' => 'query', 'type' => 'string' },
73+
{ 'name' => 'count', 'type' => 'int32' }
74+
]
75+
})
76+
rescue Typesense::Error::ObjectAlreadyExists
77+
# Collection already exists, which is fine
78+
end
79+
end
80+
81+
after do
82+
# Clean up test collections
83+
begin
84+
integration_client.collections['products'].delete
85+
rescue StandardError
86+
# Ignore cleanup errors
87+
end
88+
89+
begin
90+
integration_client.collections['product_queries'].delete
91+
rescue StandardError
92+
# Ignore cleanup errors
93+
end
94+
95+
WebMock.enable!
96+
end
97+
98+
it 'can create and use analytics rules with v1 API' do
99+
# Create a rule using v1 API
100+
rule_config = {
101+
'type' => 'popular_queries',
102+
'params' => {
103+
'source' => {
104+
'collections' => ['products']
105+
},
106+
'destination' => {
107+
'collection' => 'product_queries'
108+
},
109+
'expand_query' => false,
110+
'limit' => 100
111+
}
112+
}
113+
114+
# This should work with v1 API - name is passed separately
115+
integration_client.analytics_v1.rules.upsert('products_popularity', rule_config)
116+
117+
# Verify the rule was created
118+
rules = integration_client.analytics_v1.rules.retrieve
119+
expect(rules).to be_a(Hash)
120+
expect(rules['rules']).to be_an(Array)
121+
122+
# Clean up
123+
integration_client.analytics_v1.rules['products_popularity'].delete
124+
end
125+
end
126+
end

0 commit comments

Comments
 (0)