Skip to content

Commit

Permalink
Test connector templates (airbytehq#2081)
Browse files Browse the repository at this point in the history
  • Loading branch information
michel-tricot authored Feb 17, 2021
1 parent d2218b1 commit 9a2ec03
Show file tree
Hide file tree
Showing 22 changed files with 508 additions and 45 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ jobs:
with:
python-version: '3.7'

- name: Generate Template scaffold
run: ./gradlew :airbyte-integrations:connector-templates:generator:testScaffoldTemplates --scan

- name: Format
run: ./gradlew --no-daemon format --scan

Expand Down
32 changes: 31 additions & 1 deletion airbyte-integrations/connector-templates/generator/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,34 @@ node {

assemble.dependsOn(npmInstall)

// TODO add gradle tasks which generate one module for each generator we support and tests that the resulting code is valid.
task testScaffoldTemplates

def addScaffoldTemplateTask(name, scaffoldParams=[]) {
def taskName = "testScaffoldTemplate_${name}"
def outputDir = "airbyte-integrations/connectors/source-scaffold-${name}"

def task = tasks.create(taskName) {
inputs.files rootProject.fileTree("airbyte-integrations/connector-templates/${name}")

doLast {
exec {
workingDir rootDir
commandLine 'rm', '-rf', outputDir
}
exec {
workingDir rootDir
def cmd = ['./tools/integrations/manage.sh', 'scaffold', name, "scaffold-${name}"]
cmd.addAll(scaffoldParams)
commandLine cmd
}
}

outputs.dir rootProject.file(outputDir)
}

testScaffoldTemplates.dependsOn task
}

addScaffoldTemplateTask('source-python')
// TODO: enable Singer template testing
//addScaffoldTask('source-python-singer', ['tap-exchangeratesapi'])
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ module.exports = function (plop) {

plop.setGenerator('source-python-singer', {
description: 'Generate an Airbyte Source written on top of a Singer Tap.',
prompts: [{type: 'input', name: 'name', message: 'Source name, without the "source-" prefix e.g: "google-analytics"'}],
prompts: [
{type: 'input', name: 'name', message: 'Source name, without the "source-" prefix e.g: "google-analytics"', filter: function (name) {
return name.endsWith('-singer') ? name.replace(/-singer$/, '') : name;
}},
{type: 'input', name: 'tap_name', message: 'Singer tap package'},
],
actions: [
{
abortOnFail: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ airbyteStandardSourceTestFile {

// All these input paths must live inside this connector's directory (or subdirectories)
// TODO update the spec JSON file
specPath = "source_{{snakeCase name}}_singer/spec.json"
specPath = "source_{{snakeCase name}}/spec.json"

// configPath points to a config file which matches the spec.json supplied above. secrets/ is gitignored by default, so place your config file
// there (in case it contains any credentials)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,20 @@
"streams": [
{
"stream": {
"name": "TODO fix this file",
"supported_sync_modes": ["full_refresh", "incremental"],
"source_defined_cursor": true,
"default_cursor_field": "column1",
"name": "table_name",
"json_schema": {
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"column1": {
"column_name": {
"type": "string"
},
"column2": {
"type": "number"
}
}
}
},
"sync_mode": "full_refresh"
},
{
"stream": {
"name": "table1",
"supported_sync_modes": ["full_refresh", "incremental"],
"source_defined_cursor": false,
"json_schema": {
"$schema": "http://json-schema.org/draft-07/schema#",
},
"type": "object",
"properties": {
"column1": {
"type": "string"
},
"column2": {
"type": "number"
}
}
}
},
"sync_mode": "incremental",
"cursor_field": ["column1"]
"additionalProperties": false
},
"supported_sync_modes": ["full_refresh"],
"source_defined_cursor": false,
"default_cursor_field": ["column_name"]
}
}
]
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{
// TODO populate with needed configuration for integration tests or delete this file and any references to it
// the schema of this file should match what is in your spec.json
"fix-me": "TODO populate with needed configuration for integration tests or delete this file and any references to it. The schema of this file should match what is in your spec.json"
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,12 @@ setup(
author="Airbyte",
author_email="[email protected]",
packages=find_packages(),
install_requires=["airbyte-protocol"],
install_requires=["{{ tap_name }}", "airbyte-protocol", "base-singer", "base-python"],
package_data={"": ["*.json"]},
setup_requires=["pytest-runner"],
tests_require=["pytest"],
extras_require={
# Dependencies required by the main package but not tests should go in main. Deps required by
# integration tests but not the main package go in tests. Deps required by both should go in
# install_requires.
"main": ["base-singer", "base-python"],
"main": [],
"tests": ["airbyte-python-test", "pytest"],
},
)
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ from base_singer import SingerSource, Status


class Source{{properCase name}}Singer(SingerSource):
TAP_CMD = "tap-{{properCase name}}"
TAP_CMD = "{{ tap_name }}"

def check_config(self, logger: AirbyteLogger, config_path: str, config: json) -> AirbyteConnectionStatus:
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*
!Dockerfile
!Dockerfile.test
!source_scaffold_source_python
!setup.py
!secrets
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NEW_SOURCE_CHECKLIST.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM airbyte/integration-base-python:dev

# Bash is installed for more convenient debugging.
RUN apt-get update && apt-get install -y bash && rm -rf /var/lib/apt/lists/*

ENV CODE_PATH="source_scaffold_source_python"
ENV AIRBYTE_IMPL_MODULE="source_scaffold_source_python"
ENV AIRBYTE_IMPL_PATH="SourceScaffoldSourcePython"

WORKDIR /airbyte/integration_code
COPY $CODE_PATH ./$CODE_PATH
COPY setup.py ./
RUN pip install ".[main]"

LABEL io.airbyte.version=0.1.0
LABEL io.airbyte.name=airbyte/source-scaffold-source-python
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Scaffold Source Python Source

This is the repository for the Scaffold Source Python source connector, written in Python.
For information about how to use this connector within Airbyte, see [the documentation](https://docs.airbyte.io/integrations/sources/scaffold-source-python).

## Local development

### Prerequisites
**To iterate on this connector, make sure to complete this prerequisites section.**

#### Build & Activate Virtual Environment
First, build the module by running the following from the `airbyte` project root directory:
```
./gradlew :airbyte-integrations:connectors:source-scaffold-source-python:build
```

This will generate a virtualenv for this module in `source-scaffold-source-python/.venv`. Make sure this venv is active in your
development environment of choice. To activate the venv from the terminal, run:
```
cd airbyte-integrations/connectors/source-scaffold-source-python # cd into the connector directory
source .venv/bin/activate
```
If you are in an IDE, follow your IDE's instructions to activate the virtualenv.

#### Create credentials
**If you are a community contributor**, follow the instructions in the [documentation](https://docs.airbyte.io/integrations/sources/scaffold-source-python)
to generate the necessary credentials. Then create a file `secrets/config.json` conforming to the `source_scaffold_source_python/spec.json` file.
See `sample_files/sample_config.json` for a sample config file.

**If you are an Airbyte core member**, copy the credentials in RPass under the secret name `source-scaffold-source-python-integration-test-config`
and place them into `secrets/config.json`.


### Locally running the connector
```
python main_dev.py spec
python main_dev.py check --config secrets/config.json
python main_dev.py discover --config secrets/config.json
python main_dev.py read --config secrets/config.json --catalog sample_files/configured_catalog.json
```

### Unit Tests
To run unit tests locally, from the connector directory run:
```
pytest unit_tests
```

### Locally running the connector docker image
```
# in airbyte root directory
./gradlew :airbyte-integrations:connectors:source-scaffold-source-python:airbyteDocker
docker run --rm airbyte/source-scaffold-source-python:dev spec
docker run --rm -v $(pwd)/airbyte-integrations/connectors/source-scaffold-source-python/secrets:/secrets airbyte/source-scaffold-source-python:dev check --config /secrets/config.json
docker run --rm -v $(pwd)/airbyte-integrations/connectors/source-scaffold-source-python/secrets:/secrets airbyte/source-scaffold-source-python:dev discover --config /secrets/config.json
docker run --rm -v $(pwd)/airbyte-integrations/connectors/source-scaffold-source-python/secrets:/secrets -v $(pwd)/airbyte-integrations/connectors/source-scaffold-source-python/sample_files:/sample_files airbyte/source-scaffold-source-python:dev read --config /secrets/config.json --catalog /sample_files/configured_catalog.json
```

### Integration Tests
1. From the airbyte project root, run `./gradlew :airbyte-integrations:connectors:source-scaffold-source-python:standardSourceTestPython` to run the standard integration test suite.
1. To run additional integration tests, place your integration tests in a new directory `integration_tests` and run them with `pytest integration_tests`.
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.

## Dependency Management
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.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
plugins {
id 'airbyte-python'
id 'airbyte-docker'
id 'airbyte-standard-source-test-file'
}

airbytePython {
moduleDirectory 'source_scaffold_source_python'
}

airbyteStandardSourceTestFile {
// For more information on standard source tests, see https://docs.airbyte.io/contributing-to-airbyte/building-new-connector/testing-connectors

// All these input paths must live inside this connector's directory (or subdirectories)
// TODO update the spec JSON file
specPath = "source_scaffold_source_python/spec.json"

// configPath points to a config file which matches the spec.json supplied above. secrets/ is gitignored by default, so place your config file
// there (in case it contains any credentials)
// TODO update the config file to contain actual credentials
configPath = "secrets/config.json"
// TODO update the sample configured_catalog JSON for use in testing
// Note: If your source supports incremental syncing, then make sure that the catalog that is returned in the get_catalog method is configured
// for incremental syncing (e.g. include cursor fields, etc).
configuredCatalogPath = "sample_files/configured_catalog.json"
}


dependencies {
implementation files(project(':airbyte-integrations:bases:base-standard-source-test-file').airbyteDocker.outputs)
implementation files(project(':airbyte-integrations:bases:base-python').airbyteDocker.outputs)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""
MIT License
Copyright (c) 2020 Airbyte
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""

import sys

from base_python.entrypoint import launch
from source_scaffold_source_python import SourceScaffoldSourcePython

if __name__ == "__main__":
source = SourceScaffoldSourcePython()
launch(source, sys.argv[1:])
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-e ../../bases/airbyte-protocol
-e ../../bases/base-python
-e ../../bases/base-python-test
-e .
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"streams": [
{
"name": "TODO fix this file",
"supported_sync_modes": ["full_refresh", "incremental"],
"source_defined_cursor": true,
"default_cursor_field": "column1",
"json_schema": {
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"column1": {
"type": "string"
},
"column2": {
"type": "number"
}
}
}
},
{
"name": "table1",
"supported_sync_modes": ["full_refresh", "incremental"],
"source_defined_cursor": false,
"json_schema": {
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"column1": {
"type": "string"
},
"column2": {
"type": "number"
}
}
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"streams": [
{
"stream": {
"name": "table_name",
"json_schema": {
"properties": {
"column_name": {
"type": "string"
}
},
"type": "object",
"additionalProperties": false
},
"supported_sync_modes": ["full_refresh"],
"source_defined_cursor": false,
"default_cursor_field": ["column_name"]
}
}
]
}
Loading

0 comments on commit 9a2ec03

Please sign in to comment.