Skip to content

Commit 18827f3

Browse files
Update the code according to comments to PR
1 parent 870b89c commit 18827f3

File tree

4 files changed

+80
-65
lines changed

4 files changed

+80
-65
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ build
1010
.vscode/
1111
.idea/
1212
.python-version
13-
.venv
13+
.venv
14+
.DS_Store

e2e-test/.env

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
APP_NAME='e2e-test'
22
DEBUG_MODE = False
33
SEND_EVENTS = True
4-
LOG_DIRECTORY='logs'
5-
LOG_FILENAME_SUFFIX='e2e-test'
64
LOG_FORMAT='%(asctime)s - %(name)s - %(levelname)s - %(message)s'

e2e-test/README.md

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,49 +40,56 @@ This tool is created for the purpose of E2E Testing.
4040

4141
## Usage Examples with Sample Payloads
4242

43-
### 1. Identify
43+
| Command Option | Type | Description | Required |
44+
|--|--|--|--|
45+
| --writeKey | String | Segment Write Key | Yes |
46+
| --apiHost | String | Custom Host | No |
47+
| --payload | JSON | Event Payload | Yes |
48+
49+
### Example: Passing Multiple Events as JSON Array
50+
51+
```bash
52+
$ e2e-test:run --writeKey='YOUR_WRITE_KEY' --apiHost='' --payload='"[{\"anonymousId\":\"507f191e810c89729de960ea\",\"channel\":\"browser\",\"context\":{\"app\":\"ecommerce\"},\"integrations\":{\"All\":false,\"Mixpanel\":true,\"Salesforce\":true},\"messageId\":\"022ty90c-bbac-11e4-8dfc-aa07a5b093q8\",\"traits\":{\"name\":\"Clark Kent\",\"email\":\"[email protected]\",\"plan\":\"premium\",\"logins\":5,\"address\":{\"street\":\"6th St\",\"city\":\"San Francisco\",\"state\":\"CA\",\"postalCode\":\"94103\",\"country\":\"USA\"}},\"type\":\"identify\",\"userId\":\"AiUGstSDIg\",\"version\":\"2.0\"},{\"messageId\":\"122bb9ui-bbac-11e4-8dfc-aa07z5b098ip\",\"userId\":\"AiUGstSDIg\",\"type\":\"track\",\"event\":\"Course Clicked\",\"context\":{\"page\":{\"path\":\"/academy/\",\"referrer\":\"\",\"search\":\"\",\"title\":\"Analytics Academy\",\"url\":\"https://segment.com/academy/\"}},\"integrations\":{},\"properties\":{\"title\":\"Intro to Analytics\"}}]"'
53+
```
54+
55+
### Example: Passing Individual Events
56+
57+
#### 1. Identify
4458

4559
``` bash
46-
$ e2e-test:run --writeKey='YOUR_WRITE_KEY' --payload='"{\"anonymousId\":\"507f191e810c89729de960ea\",\"channel\":\"browser\",\"context\":{\"ip\":\"8.8.8.8\",\"userAgent\":\"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36\"},\"integrations\":{\"All\":false,\"Mixpanel\":true,\"Salesforce\":true},\"messageId\":\"022bb90c-bbac-11e4-8dfc-aa07a5b093q8\",\"traits\":{\"name\":\"Clark Kent\",\"email\":\"[email protected]\",\"plan\":\"premium\",\"logins\":5,\"address\":{\"street\":\"6th St\",\"city\":\"San Francisco\",\"state\":\"CA\",\"postalCode\":\"94103\",\"country\":\"USA\"}},\"type\":\"identify\",\"userId\":\"97980cfea0062\",\"version\":\"2.0\"}"'
60+
$ e2e-test:run --writeKey='YOUR_WRITE_KEY' --payload='"{\"anonymousId\":\"507f191e810c89729de960ea\",\"channel\":\"browser\",\"context\":{\"app\":\"ecommerce\"},\"integrations\":{\"All\":false,\"Mixpanel\":true,\"Salesforce\":true},\"messageId\":\"022bb90c-bbac-11e4-8dfc-aa07a5b093q8\",\"traits\":{\"name\":\"Clark Kent\",\"email\":\"[email protected]\",\"plan\":\"premium\",\"logins\":5,\"address\":{\"street\":\"6th St\",\"city\":\"San Francisco\",\"state\":\"CA\",\"postalCode\":\"94103\",\"country\":\"USA\"}},\"type\":\"identify\",\"userId\":\"97980cfea0062\",\"version\":\"2.0\"}"'
4761
```
4862

49-
### 2. Track
63+
#### 2. Track
5064

5165
``` bash
52-
$ e2e-test:run --writeKey='YOUR_WRITE_KEY' --payload='"{\"messageId\":\"122bb90c-bbac-11e4-8dfc-aa07z5b098ip\",\"userId\":\"AiUGstSDIg\",\"type\":\"track\",\"event\":\"Course Clicked\",\"context\":{\"page\":{\"path\":\"/academy/\",\"referrer\":\"\",\"search\":\"\",\"title\":\"Analytics Academy\",\"url\":\"https://segment.com/academy/\"},\"userAgent\":\"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36\",\"ip\":\"108.0.78.21\"},\"integrations\":{},\"properties\":{\"title\":\"Intro to Analytics\"}}"'
66+
$ e2e-test:run --writeKey='YOUR_WRITE_KEY' --payload='"{\"messageId\":\"122bb90c-bbac-11e4-8dfc-aa07z5b098ip\",\"userId\":\"AiUGstSDIg\",\"type\":\"track\",\"event\":\"Course Clicked\",\"context\":{\"page\":{\"path\":\"/academy/\",\"referrer\":\"\",\"search\":\"\",\"title\":\"Analytics Academy\",\"url\":\"https://segment.com/academy/\"}},\"integrations\":{},\"properties\":{\"title\":\"Intro to Analytics\"}}"'
5367
```
5468

55-
### 3. Page
69+
#### 3. Page
5670

5771
``` bash
58-
$ e2e-test:run --writeKey='YOUR_WRITE_KEY' --payload='"{\"anonymousId\":\"507f191e810c19729de860ea\",\"channel\":\"browser\",\"context\":{\"ip\":\"8.8.8.8\",\"userAgent\":\"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36\"},\"integrations\":{\"All\":true,\"Mixpanel\":false,\"Salesforce\":false},\"messageId\":\"022bb90c-bbac-11e8-8dfc-aa07a5b090ol\",\"name\":\"Home\",\"properties\":{\"title\":\"Welcome | Initech\",\"url\":\"http://www.example.com\"},\"type\":\"page\",\"userId\":\"97980cfea0067\"}"'
72+
$ e2e-test:run --writeKey='YOUR_WRITE_KEY' --payload='"{\"anonymousId\":\"507f191e810c19729de860ea\",\"channel\":\"browser\",\"integrations\":{\"All\":true,\"Mixpanel\":false,\"Salesforce\":false},\"messageId\":\"022bb90c-bbac-11e8-8dfc-aa07a5b090ol\",\"name\":\"Home\",\"properties\":{\"title\":\"Welcome | Initech\",\"url\":\"http://www.example.com\"},\"type\":\"page\",\"userId\":\"97980cfea0067\"}"'
5973
```
6074

61-
### 4. Screen
75+
#### 4. Screen
6276

6377
``` bash
64-
$ e2e-test:run --writeKey='YOUR_WRITE_KEY' --payload='"{\"anonymousId\":\"507f191e810c19729de860ea\",\"channel\":\"browser\",\"context\":{\"ip\":\"8.8.8.8\",\"userAgent\":\"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36\"},\"integrations\":{\"All\":true,\"Mixpanel\":false,\"Salesforce\":false},\"messageId\":\"022bb90c-bbac-11e4-8dfc-aa07a5b090op\",\"name\":\"Registration\",\"properties\":{\"title\":\"Welcome | Initech\",\"url\":\"http://www.example.com\"},\"type\":\"screen\",\"userId\":\"97980cfea0067\"}"'
78+
$ e2e-test:run --writeKey='YOUR_WRITE_KEY' --payload='"{\"anonymousId\":\"507f191e810c19729de860ea\",\"channel\":\"browser\",\"integrations\":{\"All\":true,\"Mixpanel\":false,\"Salesforce\":false},\"messageId\":\"022bb90c-bbac-11e8-8dfc-aa07a5b090ol\",\"name\":\"Registration\",\"properties\":{\"title\":\"Welcome | Initech\",\"url\":\"http://www.example.com\"},\"type\":\"Screen\",\"userId\":\"97980cfea0067\"}"'
6579
```
6680

67-
### 5. Alias
81+
#### 5. Alias
6882

6983
``` bash
70-
$ e2e-test:run --writeKey='YOUR_WRITE_KEY' --payload='"{\"anonymousId\":\"507f191e810c19729de800ea\",\"channel\":\"browser\",\"context\":{\"ip\":\"8.8.8.8\",\"userAgent\":\"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36\"},\"integrations\":{\"All\":true,\"Mixpanel\":false,\"Salesforce\":false},\"messageId\":\"022bb90c-bbac-11e4-8dfc-aa07u5b093lk\",\"previousId\":\"12345-239239-239239-23923\",\"type\":\"alias\",\"userId\":\"507f191e81\",\"version\":\"1.9\"}"'
84+
$ e2e-test:run --writeKey='YOUR_WRITE_KEY' --payload='"{\"anonymousId\":\"507f191e810c19729de800ea\",\"channel\":\"browser\",\"integrations\":{\"All\":true,\"Mixpanel\":false,\"Salesforce\":false},\"messageId\":\"022bb90c-bbac-11e4-8dfc-aa07u5b093lk\",\"previousId\":\"12345-239239-239239-23923\",\"type\":\"alias\",\"userId\":\"507f191e81\",\"version\":\"1.9\"}"'
7185
```
7286

73-
### 6. Group
87+
#### 6. Group
7488

7589
``` bash
76-
% e2e-test:run --writeKey='YOUR_WRITE_KEY' --payload='"{\"type\":\"group\",\"groupId\":\"0e8c78ea9d97a7b8185e8632\",\"userId\":\"EMP12345\",\"traits\":{\"name\":\"Initech\",\"industry\":\"Technology\",\"employees\":329,\"plan\":\"enterprise\",\"total billed\":830}}"'
90+
% e2e-test:run --writeKey='YOUR_WRITE_KEY' --payload='"{\"anonymousId\":\"507f191e810c19729de800ea\",\"channel\":\"browser\",\"integrations\":{\"All\":true,\"Mixpanel\":false,\"Salesforce\":false},\"messageId\":\"022bb90c-bbac-11e4-8dfc-aa07u5b093lk\",\"previousId\":\"12345-239239-239239-23923\",\"type\":\"alias\",\"userId\":\"507f191e81\",\"version\":\"1.9\"}"'
7791
```
7892

79-
## Error Logs
80-
81-
1. Errors logs are written under ```logs```directory.
82-
2. Each day, a new log directory is created under ```logs``` in ```YearMonthDay``` format.
83-
3. Log files are named as ```e2e-test_<hour-of-the-day>.log```
84-
4. Log file is rotated is maximum allowed file size is reached.
85-
8693
## Configuration Options
8794

8895
A few configuration options are available in the ```.env``` file.

e2e-test/src/cli.py

Lines changed: 52 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
import os
44
import sys
55
import logging
6-
from logging.handlers import RotatingFileHandler
76
import json
8-
from datetime import datetime
7+
from collections.abc import Iterable
98

109
load_dotenv()
1110

@@ -16,48 +15,65 @@
1615

1716
@click.command()
1817
@click.option('--writeKey', type=str, help='Segment write key')
18+
@click.option('--apiHost', type=str, help='Custom host')
1919
@click.option('--payload', type=str, help='A JSON string that specifies the event payload.')
20-
def run(writekey, payload):
20+
def run(writekey, payload, apihost=None):
2121
analytics.write_key = writekey
22+
23+
if apihost is not None:
24+
analytics.host = apihost # Set custom host
25+
2226
analytics.debug = os.getenv('DEBUG_MODE')
2327
analytics.send = os.getenv('SEND_EVENTS')
2428
logger = log_config()
2529

2630
try:
2731
# Decode the JSON payload
2832
decodedJson = json.loads(payload)
29-
data = json.loads(decodedJson)
30-
31-
specType = data.get('type') if data.get('type') is not None else None
32-
messageId = data.get('messageId') if data.get('messageId') is not None else None
33-
userId = data.get('userId') if data.get('userId') is not None else ''
34-
eventName = data.get('event') if data.get('event') is not None else None
35-
traits = data.get('traits') if data.get('traits') is not None else None
36-
properties = data.get('properties') if data.get('properties') is not None else None
37-
context = data.get('context') if data.get('context') is not None else None
38-
integrations = data.get('integrations') if data.get('integrations') is not None else None
39-
groupId = data.get('groupId') if data.get('groupId') is not None else None
40-
pageOrScreenName = data.get('name') if data.get('name') is not None else None
41-
pageOrScreenCategory = data.get('category') if data.get('category') is not None else None
42-
timestamp = data.get('timestamp') if data.get('timestamp') is not None else None
43-
anonymousId = data.get('anonymousId') if data.get('anonymousId') is not None else ''
44-
previousId = data.get('previousId') if data.get('previousId') is not None else None
45-
if specType == 'identify':
46-
analytics.identify(userId, traits, context, timestamp, anonymousId, integrations, messageId)
47-
elif specType == 'track':
48-
analytics.track(userId, eventName, properties, context, timestamp, anonymousId, integrations, messageId)
49-
elif specType == 'page':
50-
analytics.page(userId, pageOrScreenCategory, pageOrScreenName, properties,
51-
context, timestamp, anonymousId, integrations, messageId)
52-
elif specType == 'screen':
53-
analytics.screen(userId, pageOrScreenCategory, pageOrScreenName, properties,
54-
context, timestamp, anonymousId, integrations, messageId)
55-
elif specType == 'alias':
56-
analytics.alias(previousId, userId, context, timestamp, integrations, messageId)
57-
elif specType == 'group':
58-
analytics.group(userId, groupId, traits, context, timestamp, anonymousId, integrations, messageId)
59-
else:
60-
raise Exception
33+
dataObject = json.loads(decodedJson)
34+
35+
# To ensure for loop do not raise exception when individual JSON is passed
36+
# we are converting the payload to a List having a single Dictionary as its item
37+
38+
if not isinstance(dataObject, Iterable): # Check if dataObject is non-iterable
39+
dataObject = [dataObject]
40+
elif isinstance(dataObject, dict): # Check if dataObject is a dictionary
41+
dataObject = [dataObject]
42+
43+
# Iterate over each item in the payload JSON
44+
for data in dataObject:
45+
46+
specType = data.get('type') if data.get('type') is not None else None
47+
messageId = data.get('messageId') if data.get('messageId') is not None else None
48+
userId = data.get('userId') if data.get('userId') is not None else ''
49+
eventName = data.get('event') if data.get('event') is not None else None
50+
traits = data.get('traits') if data.get('traits') is not None else None
51+
properties = data.get('properties') if data.get('properties') is not None else None
52+
context = data.get('context') if data.get('context') is not None else None
53+
integrations = data.get('integrations') if data.get('integrations') is not None else None
54+
groupId = data.get('groupId') if data.get('groupId') is not None else None
55+
pageOrScreenName = data.get('name') if data.get('name') is not None else None
56+
pageOrScreenCategory = data.get('category') if data.get('category') is not None else None
57+
timestamp = data.get('timestamp') if data.get('timestamp') is not None else None
58+
anonymousId = data.get('anonymousId') if data.get('anonymousId') is not None else ''
59+
previousId = data.get('previousId') if data.get('previousId') is not None else None
60+
61+
if specType == 'identify':
62+
analytics.identify(userId, traits, context, timestamp, anonymousId, integrations, messageId)
63+
elif specType == 'track':
64+
analytics.track(userId, eventName, properties, context, timestamp, anonymousId, integrations, messageId)
65+
elif specType == 'page':
66+
analytics.page(userId, pageOrScreenCategory, pageOrScreenName, properties,
67+
context, timestamp, anonymousId, integrations, messageId)
68+
elif specType == 'screen':
69+
analytics.screen(userId, pageOrScreenCategory, pageOrScreenName, properties,
70+
context, timestamp, anonymousId, integrations, messageId)
71+
elif specType == 'alias':
72+
analytics.alias(previousId, userId, context, timestamp, integrations, messageId)
73+
elif specType == 'group':
74+
analytics.group(userId, groupId, traits, context, timestamp, anonymousId, integrations, messageId)
75+
else:
76+
raise Exception
6177
except Exception as e:
6278
logger.exception(e)
6379
finally:
@@ -69,14 +85,7 @@ def log_config():
6985
logger = logging.getLogger(os.getenv('APP_NAME'))
7086
logger.setLevel(logging.DEBUG)
7187

72-
# Create a file handler to log messages to a file
73-
log_directory = f"{os.getenv('LOG_DIRECTORY')}/{datetime.now().strftime('%Y%m%d')}"
74-
os.makedirs(log_directory, exist_ok=True)
75-
log_filename = f"{os.getenv('LOG_FILENAME_SUFFIX')}_{datetime.now().strftime('%H')}00.log"
76-
77-
# Create a rotating file handler
78-
handler = RotatingFileHandler(os.path.join(log_directory, log_filename),
79-
mode='a', maxBytes=1024*1024, backupCount=100)
88+
handler = logging.StreamHandler()
8089
handler.setLevel(logging.DEBUG)
8190

8291
# Define the log message format

0 commit comments

Comments
 (0)