Skip to content
This repository was archived by the owner on Mar 13, 2020. It is now read-only.

Commit 78a8066

Browse files
authored
Merge pull request #13 from PageUpPeopleOrg/feature/OSC-1238_ci-cd
[OSC-1238] Add CI/CD Overview Integration with Travis CI! 🎉 Add Pipenv support (highly recommended to manage dependencies/venv) Add PyTest for when we do write unit tests Changes Add build status to README.md Add .travis.yml Add Pipfile and Pipfile.lock Add tests folder directory Contains integration and unit tests Rename psycopg2 dependency to psycopg2-binary Update setup.py to reflect changes Notes CI/CD Travis builds will perform two types of testing: Unit testing Integration testing Integration tests perform the following: Create stub load models a. load_model_1.json, load_model_2.json Initialise a new execution Compare load models and assert that the models changed are * (as no models have been compared) Complete execution and assert that last successful execution ID is the one returned from init Initalise another execution Modify load_model_1.json Compare load models and assert that models changed are load_model_1.json Complete execution and assert that last successful execution ID is the one returned from init
2 parents 70c85fe + c06fdb8 commit 78a8066

File tree

10 files changed

+298
-88
lines changed

10 files changed

+298
-88
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,5 @@ venv.bak/
109109
# JetBrains
110110
.idea/
111111

112-
test-models/
112+
test-models/
113+
tests/integration/models/

.travis.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
dist: xenial
2+
language: python
3+
python: 3.7
4+
env:
5+
- PIPENV_VERBOSITY=-1
6+
7+
services: docker
8+
before_install:
9+
- docker pull postgres
10+
- docker run -p 5432:5432 -e POSTGRES_PASSWORD=travisci -d postgres
11+
12+
install: make install_deps
13+
script:
14+
- make test_unit
15+
- make test_integration

Makefile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Install dependencies
2+
install_deps:
3+
pip install pipenv --upgrade
4+
pipenv install --dev
5+
6+
# Run unit tests
7+
test_unit:
8+
pipenv run pytest
9+
10+
# Run integration tests
11+
test_integration:
12+
./tests/integration/test_integration.sh
13+

Pipfile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[[source]]
2+
name = "pypi"
3+
url = "https://pypi.org/simple"
4+
verify_ssl = true
5+
6+
[dev-packages]
7+
pytest = "*"
8+
9+
[packages]
10+
psycopg2-binary = "==2.7.7"
11+
SQLAlchemy = "==1.2.17"
12+
13+
[requires]
14+
python_version = "3.7"

Pipfile.lock

Lines changed: 116 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 69 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
11
# Model Change Detector
22

3-
## About
3+
[![Build Status](https://travis-ci.com/PageUpPeopleOrg/model-change-detector.svg?branch=master)](https://travis-ci.com/PageUpPeopleOrg/model-change-detector)
44

55
A utility that persists state of a data pipeline execution and uses them to detect changes in models.
66

77
## Usage
88

9-
```commandline
10-
py mcd.py [options] <command> [command-parameters]
119
```
12-
13-
To get help,use:
14-
15-
```commandline
16-
py mcd.py -h
17-
py mcd.py <db-connection-string> <command> -h
10+
$ python mcd.py [options] {db-connection-string} <command> [command-parameters]
1811
```
1912

2013
- `options` include:
@@ -38,121 +31,113 @@ py mcd.py <db-connection-string> <command> -h
3831
- `complete-execution`: Marks the completion of an existing execution by updating a record for the same in the given database. Returns nothing unless there's an error.
3932
- `execution-id`: a GUID identifier of an existing data pipeline execution as returned by the `init` command.
4033

41-
### As a script
42-
43-
- Use a local isolated/virtual python environment for this project
44-
- Install project dependencies
45-
- `py mcd.py [options] <command> [command-parameters]`
34+
To get help, use:
4635

47-
_Windows example:_
36+
```
37+
$ python mcd.py --help
38+
$ python mcd.py <command> --help
39+
```
4840

49-
```commandline
50-
py -m venv new-env --clear
51-
new-env\scripts\activate
41+
### Usage Example
5242

53-
py -m pip install -r requirements.txt
43+
```
44+
$ pipenv install
45+
$ pipenv shell
5446
55-
py mcd.py postgresql+psycopg2://user:password@host:port/dbname init-execution
47+
$ python mcd.py postgresql+psycopg2://user:password@host:port/dbname init-execution
5648
57-
py mcd.py postgresql+psycopg2://user:password@host:port/dbname get-last-successful-execution
58-
py mcd.py postgresql+psycopg2://user:password@host:port/dbname get-execution-last-updated-timestamp id-as-returned-by-get-last-successful-execution-command
49+
$ python mcd.py postgresql+psycopg2://user:password@host:port/dbname get-last-successful-execution
50+
$ python mcd.py postgresql+psycopg2://user:password@host:port/dbname get-execution-last-updated-timestamp id-as-returned-by-get-last-successful-execution-command
5951
60-
py mcd.py postgresql+psycopg2://user:password@host:port/dbname persist-models id-as-retured-by-init-command load ./relative/path/to/load/models **/*.json
61-
py mcd.py postgresql+psycopg2://user:password@host:port/dbname compare-models id-as-retured-by-get-last-successful-execution-command id-as-retured-by-init-command load
52+
$ python mcd.py postgresql+psycopg2://user:password@host:port/dbname persist-models id-as-retured-by-init-command load ./relative/path/to/load/models **/*.json
53+
$ python mcd.py postgresql+psycopg2://user:password@host:port/dbname compare-models id-as-retured-by-get-last-successful-execution-command id-as-retured-by-init-command load
6254
63-
py mcd.py postgresql+psycopg2://user:password@host:port/dbname persist-models id-as-retured-by-init-command transform C:/absolute/path/to/transform/models group1/*.csv ./group2/**/*.sql
64-
py mcd.py postgresql+psycopg2://user:password@host:port/dbname compare-models id-as-retured-by-get-last-successful-execution-command id-as-retured-by-init-command transform
55+
$ python mcd.py postgresql+psycopg2://user:password@host:port/dbname persist-models id-as-retured-by-init-command transform C:/absolute/path/to/transform/models group1/*.csv ./group2/**/*.sql
56+
$ python mcd.py postgresql+psycopg2://user:password@host:port/dbname compare-models id-as-retured-by-get-last-successful-execution-command id-as-retured-by-init-command transform
6557
66-
py mcd.py postgresql+psycopg2://user:password@host:port/dbname complete-execution id-as-retured-by-init-command
58+
$ python mcd.py postgresql+psycopg2://user:password@host:port/dbname complete-execution id-as-retured-by-init-command
6759
```
6860

69-
### As a package
61+
## Prerequisites
7062

71-
- Use/create an empty directory
72-
- Use a local isolated/virtual python environment for this project
73-
- [Install](https://pip.pypa.io/en/stable/reference/pip_install/#editable-installs) this package
74-
- `pip install -e path/to/ProjectX`
75-
- `pip install -e git+git://github.com/ProjectX.git#egg=ProjectX`
76-
- `py -m mcd [options] <command> <command-options>`
63+
- [Python 3](https://www.python.org/downloads/)
64+
- [Pipenv](https://pipenv.readthedocs.io/en/latest/install/#installing-pipenv)
7765

78-
_Windows example:_
66+
### Verify Installation
7967

80-
```commandline
81-
mkdir new-dir
82-
cd new-dir
68+
Verify dependencies are installed by running the following commands:
8369

84-
py -m venv new-env --clear
85-
new-env\scripts\activate
86-
87-
pip install -e git+git://github.com/PageUpPeopleOrg/model-change-detector.git#egg=mcd
88-
89-
py -m mcd postgresql+psycopg2://user:password@host:port/dbname init-execution
70+
```
71+
$ python --version
72+
$ pipenv --version
9073
```
9174

92-
## Setup
75+
## Getting Started
9376

94-
1. Install pre-requisites
95-
2. Use a local isolated/virtual python environment for this project
96-
3. Install project dependencies
97-
4. Develop and test code changes
98-
5. Once done, deactivate the virtual environment
77+
### Install dependencies
9978

100-
### Install pre-requisites
79+
To install project dependencies, run the following command:
10180

102-
#### Python 3
81+
```
82+
$ pipenv install
83+
```
10384

104-
Install from [here](https://www.python.org/) _(or your choice of safe software provider)_. During installation, choose the option to _Add to PATH_ and _Custom Installation_ so you can provide the install path to not be the default `C:\Program Files\Python37\` and be outside the `Program Files` directory to say, `C:\Python37\`. This is just a suggestion since there have been issues with updating key python packages once Python is installed within `Program Files` on Windows.
85+
### Activate Virtual Environment
10586

106-
Verify your installation by running the below commands.
87+
To activate a virtual environment, run the following command:
10788

108-
```powershell
109-
py --version
110-
python --version
111-
pip --version
89+
```
90+
$ pipenv shell
11291
```
11392

114-
If you end up with multiple accidental/purposeful python installations, use the below in Windows Commandline to figure out where the executables are located.
93+
### Using MCD
11594

116-
```cmd
117-
where py
118-
where python
119-
where pip
120-
```
95+
Once the virtual environment has been activated, please refer to [usage](#Usage) for how to use MCD.
96+
97+
## Testing
98+
99+
To run integration tests locally, it is highly recommended that [Docker](https://www.docker.com/) is installed.
100+
101+
### Unit Tests
121102

122-
### Use a local isolated/virtual python environment for this project
103+
To run unit tests, run the following command:
123104

124-
`py -m venv /path/to/new/virtual/environment` _e.g._ `py -m venv new-env`
105+
```
106+
$ python pytest
107+
```
125108

126-
If you build with `--system-site-packages` directory, your virtual environment will be allowed access to packages from your global site-packages directory. Although, if you want isolation from the global system, do not use this flag. Once you've created a new environment, you need to activate the same.
109+
### Integration Tests
127110

128-
On Windows:
111+
To run integration tests, please ensure the following information is configured correctly:
129112

130-
`path\to\environment\scripts\activate` _e.g._ `new-env\scripts\activate`
113+
- `tests/integration/test_integration.sh:9`
131114

132-
On Linux / Mac OS
115+
Please ensure that the database connection string points to a valid PostgreSQL instance.
133116

134-
`source path/to/environment/bin/activate` _e.g._ `source new-env/bin/activate`
117+
#### Docker
135118

136-
You should see the name of your virtual environment in brackets on your terminal line, e.g.:
119+
If Docker is installed, running tests is as simple as running the following commands:
137120

138-
```commandline
139-
C:\path\to\working\dir: new-env\scripts\activate
140-
(new-env) C:\path\to\working\dir: _
121+
```
122+
$ docker pull postgres
123+
$ docker run --name stubdatabase -p 5432:5432 -e POSTGRES_PASSWORD=travisci -d postgres
124+
$ make test_integration
125+
$ docker stop stubdatabase
126+
$ docker remove stubdatabase
141127
```
142128

143-
Any python commands you use will now, work within your virtual environment only.
129+
#### Local PostgreSQL
144130

145-
### Install project dependencies
131+
If a local instance of PostgreSQL is installed, run integration tests with the following command:
146132

147-
```powershell
148-
pip install -r requirements.txt
133+
```
134+
$ make test_integration
149135
```
150136

151-
### Deactivate the virtual environment
137+
#### Notes
152138

153-
Once done, deactivate the virtual environment with a simple `decativate` command, e.g.:
139+
If you do not have `make` installed, you can substitute `make` with:
154140

155-
```commandline
156-
(new-env) C:\path\to\working\dir: deactivate
157-
C:\path\to\working\dir: _
141+
```
142+
$ ./tests/integration/test_integration.sh
158143
```

requirements.txt

Lines changed: 0 additions & 2 deletions
This file was deleted.

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
version='0.0.2',
55
packages=find_packages(),
66
install_requires=[
7-
'psycopg2==2.7.7',
7+
'psycopg2-binary==2.7.7',
88
'SQLAlchemy==1.2.17'
99
]
1010
)

0 commit comments

Comments
 (0)