airbyte-source-faker 4.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- airbyte-source-faker-4.0.0/PKG-INFO +7 -0
- airbyte-source-faker-4.0.0/README.md +168 -0
- airbyte-source-faker-4.0.0/airbyte_source_faker.egg-info/PKG-INFO +7 -0
- airbyte-source-faker-4.0.0/airbyte_source_faker.egg-info/SOURCES.txt +28 -0
- airbyte-source-faker-4.0.0/airbyte_source_faker.egg-info/dependency_links.txt +1 -0
- airbyte-source-faker-4.0.0/airbyte_source_faker.egg-info/requires.txt +6 -0
- airbyte-source-faker-4.0.0/airbyte_source_faker.egg-info/top_level.txt +2 -0
- airbyte-source-faker-4.0.0/integration_tests/__init__.py +3 -0
- airbyte-source-faker-4.0.0/integration_tests/abnormal_state.json +35 -0
- airbyte-source-faker-4.0.0/integration_tests/acceptance.py +14 -0
- airbyte-source-faker-4.0.0/integration_tests/catalog.json +40 -0
- airbyte-source-faker-4.0.0/integration_tests/configured_catalog.json +115 -0
- airbyte-source-faker-4.0.0/integration_tests/invalid_config.json +3 -0
- airbyte-source-faker-4.0.0/integration_tests/sample_config.json +6 -0
- airbyte-source-faker-4.0.0/integration_tests/sample_state.json +24 -0
- airbyte-source-faker-4.0.0/setup.cfg +10 -0
- airbyte-source-faker-4.0.0/setup.py +23 -0
- airbyte-source-faker-4.0.0/source_faker/__init__.py +8 -0
- airbyte-source-faker-4.0.0/source_faker/airbyte_message_with_cached_json.py +22 -0
- airbyte-source-faker-4.0.0/source_faker/purchase_generator.py +106 -0
- airbyte-source-faker-4.0.0/source_faker/record_data/products.json +802 -0
- airbyte-source-faker-4.0.0/source_faker/schemas/products.json +21 -0
- airbyte-source-faker-4.0.0/source_faker/schemas/purchases.json +34 -0
- airbyte-source-faker-4.0.0/source_faker/schemas/users.json +42 -0
- airbyte-source-faker-4.0.0/source_faker/source.py +32 -0
- airbyte-source-faker-4.0.0/source_faker/spec.json +49 -0
- airbyte-source-faker-4.0.0/source_faker/streams.py +185 -0
- airbyte-source-faker-4.0.0/source_faker/user_generator.py +78 -0
- airbyte-source-faker-4.0.0/source_faker/utils.py +33 -0
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
# Faker Source
|
|
2
|
+
|
|
3
|
+
This is the repository for the Faker source connector, written in Python.
|
|
4
|
+
For information about how to use this connector within Airbyte, see [the documentation](https://docs.airbyte.io/integrations/sources/faker).
|
|
5
|
+
|
|
6
|
+
## Local development
|
|
7
|
+
|
|
8
|
+
### Prerequisites
|
|
9
|
+
|
|
10
|
+
**To iterate on this connector, make sure to complete this prerequisites section.**
|
|
11
|
+
|
|
12
|
+
#### Minimum Python version required `= 3.9.0`
|
|
13
|
+
|
|
14
|
+
#### Build & Activate Virtual Environment and install dependencies
|
|
15
|
+
|
|
16
|
+
From this connector directory, create a virtual environment:
|
|
17
|
+
|
|
18
|
+
```
|
|
19
|
+
python -m venv .venv
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
This will generate a virtualenv for this module in `.venv/`. Make sure this venv is active in your
|
|
23
|
+
development environment of choice. To activate it from the terminal, run:
|
|
24
|
+
|
|
25
|
+
```
|
|
26
|
+
source .venv/bin/activate
|
|
27
|
+
pip install -r requirements.txt
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
If you are in an IDE, follow your IDE's instructions to activate the virtualenv.
|
|
31
|
+
|
|
32
|
+
Note that while we are installing dependencies from `requirements.txt`, you should only edit `setup.py` for your dependencies. `requirements.txt` is
|
|
33
|
+
used for editable installs (`pip install -e`) to pull in Python dependencies from the monorepo and will call `setup.py`.
|
|
34
|
+
If this is mumbo jumbo to you, don't worry about it, just put your deps in `setup.py` but install using `pip install -r requirements.txt` and everything
|
|
35
|
+
should work as you expect.
|
|
36
|
+
|
|
37
|
+
#### Building via Gradle
|
|
38
|
+
|
|
39
|
+
From the Airbyte repository root, run:
|
|
40
|
+
|
|
41
|
+
```
|
|
42
|
+
./gradlew :airbyte-integrations:connectors:source-faker:build
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
#### Create credentials
|
|
46
|
+
|
|
47
|
+
**If you are a community contributor**, follow the instructions in the [documentation](https://docs.airbyte.io/integrations/sources/faker)
|
|
48
|
+
to generate the necessary credentials. Then create a file `secrets/config.json` conforming to the `source_faker/spec.json` file.
|
|
49
|
+
Note that the `secrets` directory is gitignored by default, so there is no danger of accidentally checking in sensitive information.
|
|
50
|
+
See `integration_tests/sample_config.json` for a sample config file.
|
|
51
|
+
|
|
52
|
+
**If you are an Airbyte core member**, copy the credentials in Lastpass under the secret name `source faker test creds`
|
|
53
|
+
and place them into `secrets/config.json`.
|
|
54
|
+
|
|
55
|
+
### Locally running the connector
|
|
56
|
+
|
|
57
|
+
```
|
|
58
|
+
python main.py spec
|
|
59
|
+
python main.py check --config secrets/config.json
|
|
60
|
+
python main.py discover --config secrets/config.json
|
|
61
|
+
python main.py read --config secrets/config.json --catalog integration_tests/configured_catalog.json
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### Locally running the connector docker image
|
|
65
|
+
|
|
66
|
+
#### Build
|
|
67
|
+
|
|
68
|
+
First, make sure you build the latest Docker image:
|
|
69
|
+
|
|
70
|
+
```
|
|
71
|
+
docker build . -t airbyte/source-faker:dev
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
You can also build the connector image via Gradle:
|
|
75
|
+
|
|
76
|
+
```
|
|
77
|
+
./gradlew :airbyte-integrations:connectors:source-faker:airbyteDocker
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
When building via Gradle, the docker image name and tag, respectively, are the values of the `io.airbyte.name` and `io.airbyte.version` `LABEL`s in
|
|
81
|
+
the Dockerfile.
|
|
82
|
+
|
|
83
|
+
#### Run
|
|
84
|
+
|
|
85
|
+
Then run any of the connector commands as follows:
|
|
86
|
+
|
|
87
|
+
```
|
|
88
|
+
docker run --rm airbyte/source-faker:dev spec
|
|
89
|
+
docker run --rm -v $(pwd)/secrets:/secrets airbyte/source-faker:dev check --config /secrets/config.json
|
|
90
|
+
docker run --rm -v $(pwd)/secrets:/secrets airbyte/source-faker:dev discover --config /secrets/config.json
|
|
91
|
+
docker run --rm -v $(pwd)/secrets:/secrets -v $(pwd)/integration_tests:/integration_tests airbyte/source-faker:dev read --config /secrets/config.json --catalog /integration_tests/configured_catalog.json
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Testing
|
|
95
|
+
|
|
96
|
+
Make sure to familiarize yourself with [pytest test discovery](https://docs.pytest.org/en/latest/goodpractices.html#test-discovery) to know how your test files and methods should be named.
|
|
97
|
+
First install test dependencies into your virtual environment:
|
|
98
|
+
|
|
99
|
+
```
|
|
100
|
+
pip install ".[tests]"
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### Unit Tests
|
|
104
|
+
|
|
105
|
+
To run unit tests locally, from the connector directory run:
|
|
106
|
+
|
|
107
|
+
```
|
|
108
|
+
python -m pytest unit_tests
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### Integration Tests
|
|
112
|
+
|
|
113
|
+
There are two types of integration tests: Acceptance Tests (Airbyte's test suite for all source connectors) and custom integration tests (which are specific to this connector).
|
|
114
|
+
|
|
115
|
+
#### Custom Integration tests
|
|
116
|
+
|
|
117
|
+
Place custom tests inside `integration_tests/` folder, then, from the connector root, run
|
|
118
|
+
|
|
119
|
+
```
|
|
120
|
+
python -m pytest integration_tests
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
#### Acceptance Tests
|
|
124
|
+
|
|
125
|
+
Customize `acceptance-test-config.yml` file to configure tests. See [Connector Acceptance Tests](https://docs.airbyte.io/connector-development/testing-connectors/connector-acceptance-tests-reference) for more information.
|
|
126
|
+
If your connector requires to create or destroy resources for use during acceptance tests create fixtures for it and place them inside integration_tests/acceptance.py.
|
|
127
|
+
To run your integration tests with acceptance tests, from the connector root, run
|
|
128
|
+
|
|
129
|
+
```
|
|
130
|
+
python -m pytest integration_tests -p integration_tests.acceptance
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
To run your integration tests with docker
|
|
134
|
+
|
|
135
|
+
### Using gradle to run tests
|
|
136
|
+
|
|
137
|
+
All commands should be run from airbyte project root.
|
|
138
|
+
To run unit tests:
|
|
139
|
+
|
|
140
|
+
```
|
|
141
|
+
./gradlew :airbyte-integrations:connectors:source-faker:unitTest
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
To run acceptance and custom integration tests:
|
|
145
|
+
|
|
146
|
+
```
|
|
147
|
+
./gradlew :airbyte-integrations:connectors:source-faker:integrationTest
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
## Dependency Management
|
|
151
|
+
|
|
152
|
+
All of your dependencies should go in `setup.py`, NOT `requirements.txt`. The requirements file is only used to connect internal Airbyte dependencies in the monorepo for local development.
|
|
153
|
+
We split dependencies between two groups, dependencies that are:
|
|
154
|
+
|
|
155
|
+
- required for your connector to work need to go to `MAIN_REQUIREMENTS` list.
|
|
156
|
+
- required for the testing need to go to `TEST_REQUIREMENTS` list
|
|
157
|
+
|
|
158
|
+
### Publishing a new version of the connector
|
|
159
|
+
|
|
160
|
+
You've checked out the repo, implemented a million dollar feature, and you're ready to share your changes with the world. Now what?
|
|
161
|
+
|
|
162
|
+
1. Make sure your changes are passing unit and integration tests.
|
|
163
|
+
1. Bump the connector version in `Dockerfile` -- just increment the value of the `LABEL io.airbyte.version` appropriately (we use [SemVer](https://semver.org/)).
|
|
164
|
+
1. Create a Pull Request.
|
|
165
|
+
1. Pat yourself on the back for being an awesome contributor.
|
|
166
|
+
1. Someone from Airbyte will take a look at your PR and iterate with you to merge it into master.
|
|
167
|
+
|
|
168
|
+
The end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
README.md
|
|
2
|
+
setup.cfg
|
|
3
|
+
setup.py
|
|
4
|
+
airbyte_source_faker.egg-info/PKG-INFO
|
|
5
|
+
airbyte_source_faker.egg-info/SOURCES.txt
|
|
6
|
+
airbyte_source_faker.egg-info/dependency_links.txt
|
|
7
|
+
airbyte_source_faker.egg-info/requires.txt
|
|
8
|
+
airbyte_source_faker.egg-info/top_level.txt
|
|
9
|
+
integration_tests/__init__.py
|
|
10
|
+
integration_tests/abnormal_state.json
|
|
11
|
+
integration_tests/acceptance.py
|
|
12
|
+
integration_tests/catalog.json
|
|
13
|
+
integration_tests/configured_catalog.json
|
|
14
|
+
integration_tests/invalid_config.json
|
|
15
|
+
integration_tests/sample_config.json
|
|
16
|
+
integration_tests/sample_state.json
|
|
17
|
+
source_faker/__init__.py
|
|
18
|
+
source_faker/airbyte_message_with_cached_json.py
|
|
19
|
+
source_faker/purchase_generator.py
|
|
20
|
+
source_faker/source.py
|
|
21
|
+
source_faker/spec.json
|
|
22
|
+
source_faker/streams.py
|
|
23
|
+
source_faker/user_generator.py
|
|
24
|
+
source_faker/utils.py
|
|
25
|
+
source_faker/record_data/products.json
|
|
26
|
+
source_faker/schemas/products.json
|
|
27
|
+
source_faker/schemas/purchases.json
|
|
28
|
+
source_faker/schemas/users.json
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
[
|
|
2
|
+
{
|
|
3
|
+
"type": "STREAM",
|
|
4
|
+
"stream": {
|
|
5
|
+
"stream_state": {
|
|
6
|
+
"updated_at": 11
|
|
7
|
+
},
|
|
8
|
+
"stream_descriptor": {
|
|
9
|
+
"name": "users"
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
"type": "STREAM",
|
|
15
|
+
"stream": {
|
|
16
|
+
"stream_state": {
|
|
17
|
+
"updated_at": 11
|
|
18
|
+
},
|
|
19
|
+
"stream_descriptor": {
|
|
20
|
+
"name": "purchases"
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
"type": "STREAM",
|
|
26
|
+
"stream": {
|
|
27
|
+
"stream_state": {
|
|
28
|
+
"updated_at": 101
|
|
29
|
+
},
|
|
30
|
+
"stream_descriptor": {
|
|
31
|
+
"name": "products"
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
]
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#
|
|
2
|
+
# Copyright (c) 2023 Airbyte, Inc., all rights reserved.
|
|
3
|
+
#
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
import pytest
|
|
7
|
+
|
|
8
|
+
pytest_plugins = ("connector_acceptance_test.plugin",)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
@pytest.fixture(scope="session", autouse=True)
|
|
12
|
+
def connector_setup():
|
|
13
|
+
"""This fixture is a placeholder for external resources that acceptance test might require."""
|
|
14
|
+
yield
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"streams": [
|
|
3
|
+
{
|
|
4
|
+
"name": "users",
|
|
5
|
+
"json_schema": {
|
|
6
|
+
"$schema": "http://json-schema.org/draft-07/schema#",
|
|
7
|
+
"type": "object",
|
|
8
|
+
"properties": {
|
|
9
|
+
"id": { "type": "number" },
|
|
10
|
+
"created_at": {
|
|
11
|
+
"type": "string",
|
|
12
|
+
"format": "date-time",
|
|
13
|
+
"airbyte_type": "timestamp_with_timezone"
|
|
14
|
+
},
|
|
15
|
+
"updated_at": {
|
|
16
|
+
"type": "string",
|
|
17
|
+
"format": "date-time",
|
|
18
|
+
"airbyte_type": "timestamp_with_timezone"
|
|
19
|
+
},
|
|
20
|
+
"name": { "type": "string" },
|
|
21
|
+
"title": { "type": "string" },
|
|
22
|
+
"age": { "type": "integer" },
|
|
23
|
+
"email": { "type": "string" },
|
|
24
|
+
"telephone": { "type": "string" },
|
|
25
|
+
"gender": { "type": "string" },
|
|
26
|
+
"language": { "type": "string" },
|
|
27
|
+
"academic_degree": { "type": "string" },
|
|
28
|
+
"nationality": { "type": "string" },
|
|
29
|
+
"occupation": { "type": "string" },
|
|
30
|
+
"height": { "type": "string" },
|
|
31
|
+
"blood_type": { "type": "string" },
|
|
32
|
+
"weight": { "type": "integer" }
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"supported_sync_modes": ["incremental", "full_refresh"],
|
|
36
|
+
"source_defined_cursor": true,
|
|
37
|
+
"default_cursor_field": ["created_at"]
|
|
38
|
+
}
|
|
39
|
+
]
|
|
40
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
{
|
|
2
|
+
"streams": [
|
|
3
|
+
{
|
|
4
|
+
"stream": {
|
|
5
|
+
"name": "users",
|
|
6
|
+
"json_schema": {
|
|
7
|
+
"$schema": "http://json-schema.org/draft-07/schema#",
|
|
8
|
+
"type": "object",
|
|
9
|
+
"properties": {
|
|
10
|
+
"id": { "type": "number" },
|
|
11
|
+
"created_at": {
|
|
12
|
+
"type": "string",
|
|
13
|
+
"format": "date-time",
|
|
14
|
+
"airbyte_type": "timestamp_with_timezone"
|
|
15
|
+
},
|
|
16
|
+
"updated_at": {
|
|
17
|
+
"type": "string",
|
|
18
|
+
"format": "date-time",
|
|
19
|
+
"airbyte_type": "timestamp_with_timezone"
|
|
20
|
+
},
|
|
21
|
+
"name": { "type": "string" },
|
|
22
|
+
"title": { "type": "string" },
|
|
23
|
+
"age": { "type": "integer" },
|
|
24
|
+
"email": { "type": "string" },
|
|
25
|
+
"telephone": { "type": "string" },
|
|
26
|
+
"gender": { "type": "string" },
|
|
27
|
+
"language": { "type": "string" },
|
|
28
|
+
"academic_degree": { "type": "string" },
|
|
29
|
+
"nationality": { "type": "string" },
|
|
30
|
+
"occupation": { "type": "string" },
|
|
31
|
+
"height": { "type": "string" },
|
|
32
|
+
"blood_type": { "type": "string" },
|
|
33
|
+
"weight": { "type": "integer" }
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
"supported_sync_modes": ["incremental", "full_refresh"],
|
|
37
|
+
"source_defined_cursor": true,
|
|
38
|
+
"default_cursor_field": ["created_at"]
|
|
39
|
+
},
|
|
40
|
+
"sync_mode": "incremental",
|
|
41
|
+
"destination_sync_mode": "overwrite"
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
"stream": {
|
|
45
|
+
"name": "purchases",
|
|
46
|
+
"json_schema": {
|
|
47
|
+
"properties": {
|
|
48
|
+
"id": { "type": "number" },
|
|
49
|
+
"user_id": { "type": "number" },
|
|
50
|
+
"product_id": { "type": "number" },
|
|
51
|
+
"created_at": {
|
|
52
|
+
"type": "string",
|
|
53
|
+
"format": "date-time",
|
|
54
|
+
"airbyte_type": "timestamp_with_timezone"
|
|
55
|
+
},
|
|
56
|
+
"updated_at": {
|
|
57
|
+
"type": "string",
|
|
58
|
+
"format": "date-time",
|
|
59
|
+
"airbyte_type": "timestamp_with_timezone"
|
|
60
|
+
},
|
|
61
|
+
"added_to_cart_at": {
|
|
62
|
+
"type": ["null", "string"],
|
|
63
|
+
"format": "date-time",
|
|
64
|
+
"airbyte_type": "timestamp_with_timezone"
|
|
65
|
+
},
|
|
66
|
+
"purchased_at": {
|
|
67
|
+
"type": ["null", "string"],
|
|
68
|
+
"format": "date-time",
|
|
69
|
+
"airbyte_type": "timestamp_with_timezone"
|
|
70
|
+
},
|
|
71
|
+
"returned_at": {
|
|
72
|
+
"type": ["null", "string"],
|
|
73
|
+
"format": "date-time",
|
|
74
|
+
"airbyte_type": "timestamp_with_timezone"
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
"supported_sync_modes": ["incremental", "full_refresh"],
|
|
79
|
+
"source_defined_cursor": true,
|
|
80
|
+
"default_cursor_field": ["created_at"]
|
|
81
|
+
},
|
|
82
|
+
"sync_mode": "incremental",
|
|
83
|
+
"destination_sync_mode": "overwrite"
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
"stream": {
|
|
87
|
+
"name": "products",
|
|
88
|
+
"json_schema": {
|
|
89
|
+
"properties": {
|
|
90
|
+
"id": { "type": "number" },
|
|
91
|
+
"make": { "type": "string" },
|
|
92
|
+
"model": { "type": "string" },
|
|
93
|
+
"year": { "type": "number" },
|
|
94
|
+
"price": { "type": "number" },
|
|
95
|
+
"created_at": {
|
|
96
|
+
"type": "string",
|
|
97
|
+
"format": "date-time",
|
|
98
|
+
"airbyte_type": "timestamp_with_timezone"
|
|
99
|
+
},
|
|
100
|
+
"updated_at": {
|
|
101
|
+
"type": "string",
|
|
102
|
+
"format": "date-time",
|
|
103
|
+
"airbyte_type": "timestamp_with_timezone"
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
},
|
|
107
|
+
"supported_sync_modes": ["incremental", "full_refresh"],
|
|
108
|
+
"source_defined_cursor": true,
|
|
109
|
+
"default_cursor_field": ["created_at"]
|
|
110
|
+
},
|
|
111
|
+
"sync_mode": "incremental",
|
|
112
|
+
"destination_sync_mode": "overwrite"
|
|
113
|
+
}
|
|
114
|
+
]
|
|
115
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
[
|
|
2
|
+
{
|
|
3
|
+
"type": "STREAM",
|
|
4
|
+
"stream": {
|
|
5
|
+
"stream_state": {
|
|
6
|
+
"id": 0
|
|
7
|
+
},
|
|
8
|
+
"stream_descriptor": {
|
|
9
|
+
"name": "users"
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
"type": "STREAM",
|
|
15
|
+
"stream": {
|
|
16
|
+
"stream_state": {
|
|
17
|
+
"user_id": 0
|
|
18
|
+
},
|
|
19
|
+
"stream_descriptor": {
|
|
20
|
+
"name": "purchases"
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
]
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
#
|
|
2
|
+
# Copyright (c) 2023 Airbyte, Inc., all rights reserved.
|
|
3
|
+
#
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
from setuptools import find_packages, setup
|
|
7
|
+
|
|
8
|
+
MAIN_REQUIREMENTS = ["airbyte-cdk~=0.2", "mimesis==6.1.1"]
|
|
9
|
+
|
|
10
|
+
TEST_REQUIREMENTS = [
|
|
11
|
+
"pytest~=6.2",
|
|
12
|
+
"connector-acceptance-test",
|
|
13
|
+
]
|
|
14
|
+
|
|
15
|
+
setup(
|
|
16
|
+
description="Source implementation for fake but realistic looking data.",
|
|
17
|
+
packages=find_packages(),
|
|
18
|
+
install_requires=MAIN_REQUIREMENTS,
|
|
19
|
+
package_data={"": ["*.json", "schemas/*.json", "record_data/*.json"]},
|
|
20
|
+
extras_require={
|
|
21
|
+
"tests": TEST_REQUIREMENTS,
|
|
22
|
+
},
|
|
23
|
+
)
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
#
|
|
2
|
+
# Copyright (c) 2023 Airbyte, Inc., all rights reserved.
|
|
3
|
+
#
|
|
4
|
+
|
|
5
|
+
from airbyte_cdk.models import AirbyteMessage
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class AirbyteMessageWithCachedJSON(AirbyteMessage):
|
|
9
|
+
"""
|
|
10
|
+
I a monkeypatch to AirbyteMessage which pre-renders the JSON-representation of the object upon initialization.
|
|
11
|
+
This allows the JSON to be calculated in the process that builds the object rather than the main process.
|
|
12
|
+
|
|
13
|
+
Note: We can't use @cache here because the LRU cache is not serializable when passed to child workers.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
def __init__(self, **kwargs):
|
|
17
|
+
super().__init__(**kwargs)
|
|
18
|
+
self._json = self.json(exclude_unset=True)
|
|
19
|
+
self.json = self.get_json
|
|
20
|
+
|
|
21
|
+
def get_json(self, **kwargs):
|
|
22
|
+
return self._json
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
#
|
|
2
|
+
# Copyright (c) 2023 Airbyte, Inc., all rights reserved.
|
|
3
|
+
#
|
|
4
|
+
|
|
5
|
+
import datetime
|
|
6
|
+
from multiprocessing import current_process
|
|
7
|
+
from typing import Dict, List
|
|
8
|
+
|
|
9
|
+
from airbyte_cdk.models import AirbyteRecordMessage, Type
|
|
10
|
+
from mimesis import Datetime, Numeric
|
|
11
|
+
|
|
12
|
+
from .airbyte_message_with_cached_json import AirbyteMessageWithCachedJSON
|
|
13
|
+
from .utils import format_airbyte_time, now_millis
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class PurchaseGenerator:
|
|
17
|
+
def __init__(self, stream_name: str, seed: int) -> None:
|
|
18
|
+
self.stream_name = stream_name
|
|
19
|
+
self.seed = seed
|
|
20
|
+
|
|
21
|
+
def prepare(self):
|
|
22
|
+
"""
|
|
23
|
+
Note: the instances of the mimesis generators need to be global.
|
|
24
|
+
Yes, they *should* be able to be instance variables on this class, which should only instantiated once-per-worker, but that's not quite the case:
|
|
25
|
+
* relying only on prepare as a pool initializer fails because we are calling the parent process's method, not the fork
|
|
26
|
+
* Calling prepare() as part of generate() (perhaps checking if self.person is set) and then `print(self, current_process()._identity, current_process().pid)` reveals multiple object IDs in the same process, resetting the internal random counters
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
seed_with_offset = self.seed
|
|
30
|
+
if self.seed is not None and len(current_process()._identity) > 0:
|
|
31
|
+
seed_with_offset = self.seed + current_process()._identity[0]
|
|
32
|
+
|
|
33
|
+
global dt
|
|
34
|
+
global numeric
|
|
35
|
+
|
|
36
|
+
dt = Datetime(seed=seed_with_offset)
|
|
37
|
+
numeric = Numeric(seed=seed_with_offset)
|
|
38
|
+
|
|
39
|
+
def random_date_in_range(
|
|
40
|
+
self, start_date: datetime.datetime, end_date: datetime.datetime = datetime.datetime.now()
|
|
41
|
+
) -> datetime.datetime:
|
|
42
|
+
time_between_dates = end_date - start_date
|
|
43
|
+
days_between_dates = time_between_dates.days
|
|
44
|
+
if days_between_dates < 2:
|
|
45
|
+
days_between_dates = 2
|
|
46
|
+
random_number_of_days = numeric.integer_number(0, days_between_dates)
|
|
47
|
+
random_date = start_date + datetime.timedelta(days=random_number_of_days)
|
|
48
|
+
return random_date
|
|
49
|
+
|
|
50
|
+
def generate(self, user_id: int) -> List[Dict]:
|
|
51
|
+
"""
|
|
52
|
+
Because we are doing this work in parallel processes, we need a deterministic way to know what a purchase's ID should be given on the input of a user_id.
|
|
53
|
+
tldr; Every 10 user_ids produce 10 purchases. User ID x5 has no purchases, User ID mod x7 has 2, and everyone else has 1
|
|
54
|
+
"""
|
|
55
|
+
|
|
56
|
+
purchases: List[Dict] = []
|
|
57
|
+
last_user_id_digit = int(repr(user_id)[-1])
|
|
58
|
+
purchase_count = 1
|
|
59
|
+
id_offset = 0
|
|
60
|
+
if last_user_id_digit - 1 == 5:
|
|
61
|
+
purchase_count = 0
|
|
62
|
+
elif last_user_id_digit - 1 == 6:
|
|
63
|
+
id_offset = 1
|
|
64
|
+
elif last_user_id_digit - 1 == 7:
|
|
65
|
+
id_offset = 1
|
|
66
|
+
purchase_count = 2
|
|
67
|
+
|
|
68
|
+
total_products = 100
|
|
69
|
+
i = 0
|
|
70
|
+
|
|
71
|
+
while purchase_count > 0:
|
|
72
|
+
id = user_id + i + 1 - id_offset
|
|
73
|
+
time_a = dt.datetime()
|
|
74
|
+
time_b = dt.datetime()
|
|
75
|
+
updated_at = format_airbyte_time(datetime.datetime.now())
|
|
76
|
+
created_at = time_a if time_a <= time_b else time_b
|
|
77
|
+
product_id = numeric.integer_number(1, total_products)
|
|
78
|
+
added_to_cart_at = self.random_date_in_range(created_at)
|
|
79
|
+
purchased_at = (
|
|
80
|
+
self.random_date_in_range(added_to_cart_at)
|
|
81
|
+
if added_to_cart_at is not None and numeric.integer_number(1, 100) <= 70
|
|
82
|
+
else None
|
|
83
|
+
) # 70% likely to purchase the item in the cart
|
|
84
|
+
returned_at = (
|
|
85
|
+
self.random_date_in_range(purchased_at) if purchased_at is not None and numeric.integer_number(1, 100) <= 15 else None
|
|
86
|
+
) # 15% likely to return the item
|
|
87
|
+
|
|
88
|
+
purchase = {
|
|
89
|
+
"id": id,
|
|
90
|
+
"product_id": product_id,
|
|
91
|
+
"user_id": user_id + 1,
|
|
92
|
+
"created_at": created_at,
|
|
93
|
+
"updated_at": updated_at,
|
|
94
|
+
"added_to_cart_at": format_airbyte_time(added_to_cart_at) if added_to_cart_at is not None else None,
|
|
95
|
+
"purchased_at": format_airbyte_time(purchased_at) if purchased_at is not None else None,
|
|
96
|
+
"returned_at": format_airbyte_time(returned_at) if returned_at is not None else None,
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
record = AirbyteRecordMessage(stream=self.stream_name, data=purchase, emitted_at=now_millis())
|
|
100
|
+
message = AirbyteMessageWithCachedJSON(type=Type.RECORD, record=record)
|
|
101
|
+
purchases.append(message)
|
|
102
|
+
|
|
103
|
+
purchase_count = purchase_count - 1
|
|
104
|
+
i += 1
|
|
105
|
+
|
|
106
|
+
return purchases
|