Skip to content

Commit aea4772

Browse files
Add tests for empty entity ID in experiment traffic allocation (#37)
1 parent 368afe3 commit aea4772

File tree

5 files changed

+39
-25
lines changed

5 files changed

+39
-25
lines changed

CONTRIBUTING.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
#Contributing to the Optimizely Python SDK
1+
# Contributing to the Optimizely Python SDK
22
We welcome contributions and feedback! All contributors must sign our [Contributor License Agreement (CLA)](https://docs.google.com/a/optimizely.com/forms/d/e/1FAIpQLSf9cbouWptIpMgukAKZZOIAhafvjFCV8hS00XJLWQnWDFtwtA/viewform) to be eligible to contribute. Please read the [README](README.md) to set up your development environment, then read the guidelines below for information on submitting your code.
33

4-
##Development process
4+
## Development process
55

66
1. Create a branch off of `devel`: `git checkout -b YOUR_NAME/branch_name`.
77
2. Commit your changes. Make sure to add tests!
@@ -11,22 +11,22 @@ We welcome contributions and feedback! All contributors must sign our [Contribut
1111
6. Open a pull request from `YOUR_NAME/branch_name` to `devel`.
1212
7. A repository maintainer will review your pull request and, if all goes well, merge it!
1313

14-
##Pull request acceptance criteria
14+
## Pull request acceptance criteria
1515

1616
* **All code must have test coverage.** We use unittest. Changes in functionality should have accompanying unit tests. Bug fixes should have accompanying regression tests.
1717
* Tests are located in `/tests` with one file per class.
1818
* Please don't change the `__version__`. We'll take care of bumping the version when we next release.
1919
* Lint your code with PEP-8 before submitting.
2020

21-
##Style
21+
## Style
2222
We enforce PEP-8 rules with a few minor deviations.
2323

24-
##License
24+
## License
2525

2626
All contributions are under the CLA mentioned above. For this project, Optimizely uses the Apache 2.0 license, and so asks that by contributing your code, you agree to license your contribution under the terms of the [Apache License v2.0](http://www.apache.org/licenses/LICENSE-2.0). Your contributions should also include the following header:
2727

2828
```
29-
# Copyright 2016, Optimizely
29+
# Copyright 2017, Optimizely
3030
# Licensed under the Apache License, Version 2.0 (the "License");
3131
# you may not use this file except in compliance with the License.
3232
# You may obtain a copy of the License at
@@ -42,5 +42,5 @@ All contributions are under the CLA mentioned above. For this project, Optimizel
4242

4343
The YEAR above should be the year of the contribution. If work on the file has been done over multiple years, list each year in the section above. Example: Optimizely writes the file and releases it in 2014. No changes are made in 2015. Change made in 2016. YEAR should be “2014, 2016”.
4444

45-
##Contact
45+
## Contact
4646
If you have questions, please contact [email protected].

README.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
#Optimizely Python SDK
1+
# Optimizely Python SDK
22
[![Build Status](https://travis-ci.org/optimizely/python-sdk.svg?branch=master)](https://travis-ci.org/optimizely/python-sdk)
33
[![Coverage Status](https://coveralls.io/repos/github/optimizely/python-sdk/badge.svg)](https://coveralls.io/github/optimizely/python-sdk)
44
[![Apache 2.0](https://img.shields.io/github/license/nebula-plugins/gradle-extra-configurations-plugin.svg)](http://www.apache.org/licenses/LICENSE-2.0)
55

66
This repository houses the Python SDK for Optimizely Full Stack.
77

8-
##Getting Started
8+
## Getting Started
99

10-
###Installing the SDK
10+
### Installing the SDK
1111

1212
The SDK is available through [PyPi](https://pypi.python.org/pypi?name=optimizely-sdk&:action=display). To install:
1313

1414
```
1515
pip install optimizely-sdk
1616
```
1717

18-
###Using the SDK
18+
### Using the SDK
1919
See the Optimizely Full Stack [developer documentation](http://developers.optimizely.com/server/reference/index.html) to learn how to set up your first Python project and use the SDK.
2020

21-
##Development
21+
## Development
2222

23-
###Building the SDK
23+
### Building the SDK
2424

2525
Build the SDK using the following command:
2626

@@ -36,16 +36,16 @@ You can then install the SDK and its dependencies with:
3636
pip install dist/optimizely-sdk-{VERSION}.tar.gz
3737
```
3838

39-
###Unit tests
39+
### Unit tests
4040

41-
#####Running all tests
41+
##### Running all tests
4242
You can run all unit tests with:
4343

4444
```
4545
nosetests
4646
```
4747

48-
#####Running all tests in a file
48+
##### Running all tests in a file
4949
To run all tests under a particular test file you can use the following command:
5050

5151
```
@@ -58,7 +58,7 @@ For example, to run all tests under `test_event`, the command would be:
5858
nosetests tests.test_event
5959
```
6060

61-
#####Running all tests under a class
61+
##### Running all tests under a class
6262
To run all tests under a particular class of tests you can use the following command:
6363

6464
```
@@ -70,7 +70,7 @@ For example, to run all tests under `test_event.EventTest`, the command would be
7070
nosetests tests.test_event:EventTest
7171
```
7272

73-
#####Running a single test
73+
##### Running a single test
7474
To run a single test you can use the following command:
7575

7676
```
@@ -83,6 +83,6 @@ For example, to run `test_event.EventTest.test_dispatch`, the command would be:
8383
nosetests tests.test_event:EventTest.test_dispatch
8484
```
8585

86-
###Contributing
86+
### Contributing
8787

8888
Please see [CONTRIBUTING](CONTRIBUTING.md).

tests/base.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@ def setUp(self):
160160
'trafficAllocation': [{
161161
'entityId': '111128',
162162
'endOfRange': 4000
163+
}, {
164+
'entityId': '',
165+
'endOfRange': 5000
163166
}, {
164167
'entityId': '111129',
165168
'endOfRange': 9000

tests/test_bucketing.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@
2626
from . import base
2727

2828

29-
class BucketerTest(base.BaseTestV1):
29+
class BucketerTest(base.BaseTestV2):
3030

3131
def setUp(self):
32-
base.BaseTestV1.setUp(self)
32+
base.BaseTestV2.setUp(self)
3333
self.bucketer = bucketer.Bucketer(self.project_config)
3434

3535
def test_bucket(self):
@@ -44,9 +44,17 @@ def test_bucket(self):
4444
))
4545
mock_generate_bucket_value.assert_called_once_with('test_user111127')
4646

47-
# Variation 2
47+
# Empty entity ID
4848
with mock.patch('optimizely.bucketer.Bucketer._generate_bucket_value',
4949
return_value=4242) as mock_generate_bucket_value:
50+
self.assertIsNone(self.bucketer.bucket(
51+
self.project_config.get_experiment_from_key('test_experiment'), 'test_user'
52+
))
53+
mock_generate_bucket_value.assert_called_once_with('test_user111127')
54+
55+
# Variation 2
56+
with mock.patch('optimizely.bucketer.Bucketer._generate_bucket_value',
57+
return_value=5042) as mock_generate_bucket_value:
5058
self.assertEqual(entities.Variation('111129', 'variation'),
5159
self.bucketer.bucket(self.project_config.get_experiment_from_key('test_experiment'),
5260
'test_user'))

tests/test_event_builder.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,8 @@ def test_create_conversion_event__with_attributes(self):
352352
'clientEngine': 'python-sdk',
353353
'clientVersion': version.__version__
354354
}
355-
with mock.patch('time.time', return_value=42.123):
355+
with mock.patch('time.time', return_value=42.123), \
356+
mock.patch('optimizely.bucketer.Bucketer._generate_bucket_value', return_value=5042):
356357
event_obj = self.event_builder.create_conversion_event(
357358
'test_event', 'test_user', {'test_attribute': 'test_value'}, None,
358359
[self.project_config.get_experiment_from_key('test_experiment')]
@@ -439,7 +440,8 @@ def test_create_conversion_event__with_event_value(self):
439440
'clientEngine': 'python-sdk',
440441
'clientVersion': version.__version__
441442
}
442-
with mock.patch('time.time', return_value=42.123):
443+
with mock.patch('time.time', return_value=42.123), \
444+
mock.patch('optimizely.bucketer.Bucketer._generate_bucket_value', return_value=5042):
443445
event_obj = self.event_builder.create_conversion_event(
444446
'test_event', 'test_user', {'test_attribute': 'test_value'}, {'revenue': 4200, 'non-revenue': 'abc'},
445447
[self.project_config.get_experiment_from_key('test_experiment')]
@@ -496,7 +498,8 @@ def test_create_conversion_event__with_invalid_event_value(self):
496498
'clientEngine': 'python-sdk',
497499
'clientVersion': version.__version__
498500
}
499-
with mock.patch('time.time', return_value=42.123):
501+
with mock.patch('time.time', return_value=42.123), \
502+
mock.patch('optimizely.bucketer.Bucketer._generate_bucket_value', return_value=5042):
500503
event_obj = self.event_builder.create_conversion_event(
501504
'test_event', 'test_user', {'test_attribute': 'test_value'}, {'revenue': '4200', 'non-revenue': 'abc'},
502505
[self.project_config.get_experiment_from_key('test_experiment')]

0 commit comments

Comments
 (0)