|
40 | 40 | end
|
41 | 41 |
|
42 | 42 | it 'should return the correct variation ID for a given user for whom a variation has been forced' do
|
43 |
| - config.set_forced_variation('test_experiment', 'test_user', 'variation') |
| 43 | + decision_service.set_forced_variation(config, 'test_experiment', 'test_user', 'variation') |
44 | 44 | expect(decision_service.get_variation(config, 'test_experiment', 'test_user')).to eq('111129')
|
45 | 45 | # Setting forced variation should short circuit whitelist check, bucketing and audience evaluation
|
46 | 46 | expect(decision_service).not_to have_received(:get_whitelisted_variation_id)
|
|
53 | 53 | 'browser_type' => 'firefox',
|
54 | 54 | Optimizely::Helpers::Constants::CONTROL_ATTRIBUTES['BUCKETING_ID'] => 'pid'
|
55 | 55 | }
|
56 |
| - config.set_forced_variation('test_experiment_with_audience', 'test_user', 'control_with_audience') |
| 56 | + decision_service.set_forced_variation(config, 'test_experiment_with_audience', 'test_user', 'control_with_audience') |
57 | 57 | expect(decision_service.get_variation(config, 'test_experiment_with_audience', 'test_user', user_attributes)).to eq('122228')
|
58 | 58 | # Setting forced variation should short circuit whitelist check, bucketing and audience evaluation
|
59 | 59 | expect(decision_service).not_to have_received(:get_whitelisted_variation_id)
|
|
713 | 713 | expect(spy_logger).not_to have_received(:log)
|
714 | 714 | end
|
715 | 715 | end
|
| 716 | + |
| 717 | + # Only those log messages have been asserted, which are directly logged in these methods. |
| 718 | + # Messages that are logged in some internal function calls, are asserted in their respective function test cases. |
| 719 | + describe 'get_forced_variation' do |
| 720 | + user_id = 'test_user' |
| 721 | + invalid_experiment_key = 'invalid_experiment' |
| 722 | + valid_experiment = {id: '111127', key: 'test_experiment'} |
| 723 | + |
| 724 | + # User ID is not defined in the forced variation map |
| 725 | + it 'should log a message and return nil when user is not in forced variation map' do |
| 726 | + expect(decision_service.get_forced_variation(config, valid_experiment[:key], user_id)).to eq(nil) |
| 727 | + expect(spy_logger).to have_received(:log).with(Logger::DEBUG, |
| 728 | + "User '#{user_id}' is not in the forced variation map.") |
| 729 | + end |
| 730 | + # Experiment key does not exist in the datafile |
| 731 | + it 'should return nil when experiment key is not in datafile' do |
| 732 | + expect(decision_service.get_forced_variation(config, invalid_experiment_key, user_id)).to eq(nil) |
| 733 | + end |
| 734 | + end |
| 735 | + |
| 736 | + # Only those log messages have been asserted, which are directly logged in these methods. |
| 737 | + # Messages that are logged in some internal function calls, are asserted in their respective function test cases. |
| 738 | + describe 'set_forced_variation' do |
| 739 | + user_id = 'test_user' |
| 740 | + invalid_experiment_key = 'invalid_experiment' |
| 741 | + invalid_variation_key = 'invalid_variation' |
| 742 | + valid_experiment = {id: '111127', key: 'test_experiment'} |
| 743 | + valid_variation = {id: '111128', key: 'control'} |
| 744 | + |
| 745 | + # Experiment key does not exist in the datafile |
| 746 | + it 'return nil when experiment key is not in datafile' do |
| 747 | + expect(decision_service.set_forced_variation(config, invalid_experiment_key, user_id, valid_variation[:key])).to eq(false) |
| 748 | + end |
| 749 | + # Variation key does not exist in the datafile |
| 750 | + it 'return false when variation_key is not in datafile' do |
| 751 | + expect(decision_service.set_forced_variation(config, valid_experiment[:key], user_id, invalid_variation_key)).to eq(false) |
| 752 | + end |
| 753 | + end |
| 754 | + |
| 755 | + describe 'set/get forced variations multiple calls' do |
| 756 | + user_id = 'test_user' |
| 757 | + user_id_2 = 'test_user_2' |
| 758 | + valid_experiment = {id: '111127', key: 'test_experiment'} |
| 759 | + valid_variation = {id: '111128', key: 'control'} |
| 760 | + valid_variation_2 = {id: '111129', key: 'variation'} |
| 761 | + valid_experiment_2 = {id: '122227', key: 'test_experiment_with_audience'} |
| 762 | + valid_variation_for_exp_2 = {id: '122228', key: 'control_with_audience'} |
| 763 | + # Call set variation with different variations on one user/experiment to confirm that each set is expected. |
| 764 | + it 'should set and return expected variations when different variations are set and removed for one user/experiment' do |
| 765 | + expect(decision_service.set_forced_variation(config, valid_experiment[:key], user_id, valid_variation[:key])).to eq(true) |
| 766 | + variation = decision_service.get_forced_variation(config, valid_experiment[:key], user_id) |
| 767 | + expect(variation['id']).to eq(valid_variation[:id]) |
| 768 | + expect(variation['key']).to eq(valid_variation[:key]) |
| 769 | + |
| 770 | + expect(decision_service.set_forced_variation(config, valid_experiment[:key], user_id, valid_variation_2[:key])).to eq(true) |
| 771 | + variation = decision_service.get_forced_variation(config, valid_experiment[:key], user_id) |
| 772 | + expect(variation['id']).to eq(valid_variation_2[:id]) |
| 773 | + expect(variation['key']).to eq(valid_variation_2[:key]) |
| 774 | + end |
| 775 | + |
| 776 | + # Set variation on multiple experiments for one user. |
| 777 | + it 'should set and return expected variations when variation is set for multiple experiments for one user' do |
| 778 | + expect(decision_service.set_forced_variation(config, valid_experiment[:key], user_id, valid_variation[:key])).to eq(true) |
| 779 | + variation = decision_service.get_forced_variation(config, valid_experiment[:key], user_id) |
| 780 | + expect(variation['id']).to eq(valid_variation[:id]) |
| 781 | + expect(variation['key']).to eq(valid_variation[:key]) |
| 782 | + |
| 783 | + expect(decision_service.set_forced_variation(config, valid_experiment_2[:key], user_id, valid_variation_for_exp_2[:key])).to eq(true) |
| 784 | + variation = decision_service.get_forced_variation(config, valid_experiment_2[:key], user_id) |
| 785 | + expect(variation['id']).to eq(valid_variation_for_exp_2[:id]) |
| 786 | + expect(variation['key']).to eq(valid_variation_for_exp_2[:key]) |
| 787 | + end |
| 788 | + |
| 789 | + # Set variations for multiple users. |
| 790 | + it 'should set and return expected variations when variations are set for multiple users' do |
| 791 | + expect(decision_service.set_forced_variation(config, valid_experiment[:key], user_id, valid_variation[:key])).to eq(true) |
| 792 | + variation = decision_service.get_forced_variation(config, valid_experiment[:key], user_id) |
| 793 | + expect(variation['id']).to eq(valid_variation[:id]) |
| 794 | + expect(variation['key']).to eq(valid_variation[:key]) |
| 795 | + |
| 796 | + expect(decision_service.set_forced_variation(config, valid_experiment[:key], user_id_2, valid_variation[:key])).to eq(true) |
| 797 | + variation = decision_service.get_forced_variation(config, valid_experiment[:key], user_id_2) |
| 798 | + expect(variation['id']).to eq(valid_variation[:id]) |
| 799 | + expect(variation['key']).to eq(valid_variation[:key]) |
| 800 | + end |
| 801 | + end |
716 | 802 | end
|
0 commit comments