Skip to content

Commit 162f4a1

Browse files
committed
tests
1 parent 0f030ab commit 162f4a1

File tree

5 files changed

+527
-0
lines changed

5 files changed

+527
-0
lines changed

spec/conditions_spec.rb

Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,12 +305,51 @@
305305
expect(Featurevisor::Conditions.condition_is_matched(condition, context, get_regex)).to be true
306306
end
307307

308+
it "should handle semverNotEquals" do
309+
condition = { attribute: "version", operator: "semverNotEquals", value: "1.0.0" }
310+
311+
expect(
312+
Featurevisor::Conditions.condition_is_matched(condition, { version: "2.0.0" }, get_regex)
313+
).to be true
314+
expect(
315+
Featurevisor::Conditions.condition_is_matched(condition, { version: "1.0.0" }, get_regex)
316+
).to be false
317+
end
318+
319+
it "should handle semverGreaterThanOrEquals" do
320+
condition = { attribute: "version", operator: "semverGreaterThanOrEquals", value: "1.0.0" }
321+
322+
expect(
323+
Featurevisor::Conditions.condition_is_matched(condition, { version: "2.0.0" }, get_regex)
324+
).to be true
325+
expect(
326+
Featurevisor::Conditions.condition_is_matched(condition, { version: "1.0.0" }, get_regex)
327+
).to be true
328+
expect(
329+
Featurevisor::Conditions.condition_is_matched(condition, { version: "0.9.0" }, get_regex)
330+
).to be false
331+
end
332+
308333
it "should handle semverLessThan" do
309334
condition = { attribute: "version", operator: "semverLessThan", value: "1.0.0" }
310335
context = { version: "0.9.0" }
311336

312337
expect(Featurevisor::Conditions.condition_is_matched(condition, context, get_regex)).to be true
313338
end
339+
340+
it "should handle semverLessThanOrEquals" do
341+
condition = { attribute: "version", operator: "semverLessThanOrEquals", value: "1.0.0" }
342+
343+
expect(
344+
Featurevisor::Conditions.condition_is_matched(condition, { version: "1.0.0" }, get_regex)
345+
).to be true
346+
expect(
347+
Featurevisor::Conditions.condition_is_matched(condition, { version: "0.9.0" }, get_regex)
348+
).to be true
349+
expect(
350+
Featurevisor::Conditions.condition_is_matched(condition, { version: "1.1.0" }, get_regex)
351+
).to be false
352+
end
314353
end
315354

316355
describe "regex operators" do
@@ -353,6 +392,215 @@
353392
end
354393
end
355394

395+
describe "logical composition via datafile reader" do
396+
it "should match with multiple conditions inside NOT" do
397+
conditions = [
398+
{
399+
not: [
400+
{ attribute: "browser_type", operator: "equals", value: "chrome" },
401+
{ attribute: "browser_version", operator: "equals", value: "1.0" }
402+
]
403+
}
404+
]
405+
406+
expect(
407+
datafile_reader.all_conditions_are_matched(conditions, {
408+
browser_type: "firefox",
409+
browser_version: "2.0"
410+
})
411+
).to be true
412+
expect(
413+
datafile_reader.all_conditions_are_matched(conditions, {
414+
browser_type: "chrome"
415+
})
416+
).to be true
417+
expect(
418+
datafile_reader.all_conditions_are_matched(conditions, {
419+
browser_type: "chrome",
420+
browser_version: "2.0"
421+
})
422+
).to be true
423+
expect(
424+
datafile_reader.all_conditions_are_matched(conditions, {
425+
browser_type: "chrome",
426+
browser_version: "1.0"
427+
})
428+
).to be false
429+
end
430+
431+
it "should match with OR inside AND" do
432+
conditions = [
433+
{
434+
and: [
435+
{ attribute: "browser_type", operator: "equals", value: "chrome" },
436+
{
437+
or: [
438+
{ attribute: "browser_version", operator: "equals", value: "1.0" },
439+
{ attribute: "browser_version", operator: "equals", value: "2.0" }
440+
]
441+
}
442+
]
443+
}
444+
]
445+
446+
expect(
447+
datafile_reader.all_conditions_are_matched(conditions, {
448+
browser_type: "chrome",
449+
browser_version: "1.0"
450+
})
451+
).to be true
452+
expect(
453+
datafile_reader.all_conditions_are_matched(conditions, {
454+
browser_type: "chrome",
455+
browser_version: "2.0"
456+
})
457+
).to be true
458+
expect(
459+
datafile_reader.all_conditions_are_matched(conditions, {
460+
browser_type: "chrome",
461+
browser_version: "3.0"
462+
})
463+
).to be false
464+
expect(
465+
datafile_reader.all_conditions_are_matched(conditions, {
466+
browser_version: "2.0"
467+
})
468+
).to be false
469+
end
470+
471+
it "should match with plain conditions, followed by OR inside AND" do
472+
conditions = [
473+
{ attribute: "country", operator: "equals", value: "nl" },
474+
{
475+
and: [
476+
{ attribute: "browser_type", operator: "equals", value: "chrome" },
477+
{
478+
or: [
479+
{ attribute: "browser_version", operator: "equals", value: "1.0" },
480+
{ attribute: "browser_version", operator: "equals", value: "2.0" }
481+
]
482+
}
483+
]
484+
}
485+
]
486+
487+
expect(
488+
datafile_reader.all_conditions_are_matched(conditions, {
489+
country: "nl",
490+
browser_type: "chrome",
491+
browser_version: "1.0"
492+
})
493+
).to be true
494+
expect(
495+
datafile_reader.all_conditions_are_matched(conditions, {
496+
country: "nl",
497+
browser_type: "chrome",
498+
browser_version: "2.0"
499+
})
500+
).to be true
501+
expect(
502+
datafile_reader.all_conditions_are_matched(conditions, {
503+
browser_type: "chrome",
504+
browser_version: "3.0"
505+
})
506+
).to be false
507+
expect(
508+
datafile_reader.all_conditions_are_matched(conditions, {
509+
country: "us",
510+
browser_version: "2.0"
511+
})
512+
).to be false
513+
end
514+
515+
it "should match with AND inside OR" do
516+
conditions = [
517+
{
518+
or: [
519+
{ attribute: "browser_type", operator: "equals", value: "chrome" },
520+
{
521+
and: [
522+
{ attribute: "device_type", operator: "equals", value: "mobile" },
523+
{ attribute: "orientation", operator: "equals", value: "portrait" }
524+
]
525+
}
526+
]
527+
}
528+
]
529+
530+
expect(
531+
datafile_reader.all_conditions_are_matched(conditions, {
532+
browser_type: "chrome",
533+
browser_version: "2.0"
534+
})
535+
).to be true
536+
expect(
537+
datafile_reader.all_conditions_are_matched(conditions, {
538+
browser_type: "firefox",
539+
device_type: "mobile",
540+
orientation: "portrait"
541+
})
542+
).to be true
543+
expect(
544+
datafile_reader.all_conditions_are_matched(conditions, {
545+
browser_type: "firefox",
546+
browser_version: "2.0"
547+
})
548+
).to be false
549+
expect(
550+
datafile_reader.all_conditions_are_matched(conditions, {
551+
browser_type: "firefox",
552+
device_type: "desktop"
553+
})
554+
).to be false
555+
end
556+
557+
it "should match with plain conditions, followed by AND inside OR" do
558+
conditions = [
559+
{ attribute: "country", operator: "equals", value: "nl" },
560+
{
561+
or: [
562+
{ attribute: "browser_type", operator: "equals", value: "chrome" },
563+
{
564+
and: [
565+
{ attribute: "device_type", operator: "equals", value: "mobile" },
566+
{ attribute: "orientation", operator: "equals", value: "portrait" }
567+
]
568+
}
569+
]
570+
}
571+
]
572+
573+
expect(
574+
datafile_reader.all_conditions_are_matched(conditions, {
575+
country: "nl",
576+
browser_type: "chrome",
577+
browser_version: "2.0"
578+
})
579+
).to be true
580+
expect(
581+
datafile_reader.all_conditions_are_matched(conditions, {
582+
country: "nl",
583+
browser_type: "firefox",
584+
device_type: "mobile",
585+
orientation: "portrait"
586+
})
587+
).to be true
588+
expect(
589+
datafile_reader.all_conditions_are_matched(conditions, {
590+
browser_type: "firefox",
591+
browser_version: "2.0"
592+
})
593+
).to be false
594+
expect(
595+
datafile_reader.all_conditions_are_matched(conditions, {
596+
country: "de",
597+
browser_type: "firefox",
598+
device_type: "desktop"
599+
})
600+
).to be false
601+
end
602+
end
603+
356604
describe "error handling" do
357605
it "should handle errors gracefully and return false" do
358606
condition = { attribute: "name", operator: "invalid_operator", value: "test" }

spec/datafile_reader_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,33 @@
335335
).to be false
336336
end
337337

338+
it "should match dutchMobileOrDesktopUsers2" do
339+
group = groups.find { |g| g[:key] == "dutchMobileOrDesktopUsers2" }
340+
341+
# match
342+
expect(
343+
datafile_reader.all_segments_are_matched(group[:segments], {
344+
country: "nl",
345+
deviceType: "mobile"
346+
})
347+
).to be true
348+
expect(
349+
datafile_reader.all_segments_are_matched(group[:segments], {
350+
country: "nl",
351+
deviceType: "desktop"
352+
})
353+
).to be true
354+
355+
# not match
356+
expect(datafile_reader.all_segments_are_matched(group[:segments], {})).to be false
357+
expect(
358+
datafile_reader.all_segments_are_matched(group[:segments], {
359+
country: "de",
360+
deviceType: "mobile"
361+
})
362+
).to be false
363+
end
364+
338365
it "should match germanMobileUsers" do
339366
group = groups.find { |g| g[:key] == "germanMobileUsers" }
340367

0 commit comments

Comments
 (0)