-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for populating the db with test data
--------- Co-authored-by: Christoph Pirkl <[email protected]>
- Loading branch information
Showing
8 changed files
with
10,298 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import sys | ||
import csv | ||
import decimal | ||
import random | ||
import datetime | ||
import argparse | ||
|
||
from faker import Faker | ||
from faker.providers import BaseProvider | ||
|
||
|
||
class UserDataProvider(BaseProvider): | ||
|
||
def date(self): | ||
start_date = datetime.date(2018, 1, 1) | ||
return start_date + datetime.timedelta(random.randint(1, 365)) | ||
|
||
def timestamp(self): | ||
date = self.date() | ||
time = datetime.time( | ||
random.randint(0, 23), | ||
random.randint(0, 59), | ||
random.randint(0, 59), | ||
random.randint(0, 999) * 1000 | ||
) | ||
return datetime.datetime.combine(date, time) | ||
|
||
def boolean(self): | ||
return self.random_element([True, False]) | ||
|
||
def status(self): | ||
return self.random_element( | ||
['ACTIVE', 'PENDING', 'SUSPENDED', 'DISABLED'] | ||
) | ||
|
||
def decimal(self): | ||
return decimal.Decimal(random.randint(0, 100)) / 100 | ||
|
||
def score(self): | ||
value = random.randint(0, 10) | ||
return None if value == 10 else random.randint(0, 10000) / 100 | ||
|
||
|
||
def generate_users(count): | ||
fake = Faker() | ||
fake.add_provider(UserDataProvider) | ||
|
||
for i in range(count): | ||
yield ( | ||
i, | ||
fake.name(), | ||
fake.date(), | ||
fake.timestamp(), | ||
fake.boolean(), | ||
fake.decimal(), | ||
fake.score(), | ||
fake.status() | ||
) | ||
|
||
|
||
def _create_parser(): | ||
parser = argparse.ArgumentParser( | ||
description="Generate a CSV file containing users", | ||
formatter_class=argparse.ArgumentDefaultsHelpFormatter | ||
) | ||
parser.add_argument( | ||
'filename', | ||
type=argparse.FileType('w', encoding='utf8'), | ||
help='file the resulting CSV should be written to.' | ||
) | ||
parser.add_argument( | ||
'-n', '--count', | ||
type=int, default=10000, | ||
help='Number of users to create.' | ||
) | ||
|
||
return parser | ||
|
||
|
||
FAILURE = -1 | ||
SUCCESS = 0 | ||
|
||
|
||
def main(argv=None): | ||
parser = _create_parser() | ||
args = parser.parse_args(argv) | ||
try: | ||
writer = csv.writer(args.filename, delimiter=',') | ||
for user in generate_users(count=args.count): | ||
writer.writerow(user) | ||
except Exception as ex: | ||
print(f"Error while generating users, details: {ex}") | ||
return FAILURE | ||
return SUCCESS | ||
|
||
|
||
if __name__ == '__main__': | ||
sys.exit(main()) |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
DROP SCHEMA IF EXISTS PYEXASOL_TEST CASCADE; | ||
CREATE SCHEMA PYEXASOL_TEST; | ||
|
||
CREATE OR REPLACE TABLE PYEXASOL_TEST.USERS | ||
( | ||
user_id DECIMAL(18,0), | ||
user_name VARCHAR(255), | ||
register_dt DATE, | ||
last_visit_ts TIMESTAMP, | ||
is_female BOOLEAN, | ||
user_rating DECIMAL(10,5), | ||
user_score DOUBLE, | ||
status VARCHAR(50) | ||
); | ||
|
||
IMPORT INTO PYEXASOL_TEST.USERS FROM LOCAL CSV FILE 'users.csv' COLUMN SEPARATOR = ','; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,19 @@ | ||
def test_smoke(connection): | ||
def test_static_select(connection): | ||
result = connection.execute("SELECT 1;") | ||
expected = (1,) | ||
actual = result.fetchall()[0] | ||
expected = [(1,)] | ||
actual = result.fetchall() | ||
assert expected == actual | ||
|
||
|
||
def test_sorted_select_and_limited_select(connection): | ||
statement = f"SELECT * FROM USERS ORDER BY USER_ID LIMIT 5;" | ||
result = connection.execute(statement) | ||
expected = [ | ||
(0, 'Amy Marquez', '2018-10-04', '2018-03-06 21:44:36.142000', True, '0.76', 30.11, 'PENDING\r'), | ||
(1, 'John Lawson', '2018-05-17', '2018-05-28 02:58:29.079000', True, '0.04', 71.72, 'DISABLED\r'), | ||
(2, 'Jessica Clark', '2018-05-23', '2018-05-22 04:19:51.098000', False, '0.72', 29.13, 'PENDING\r'), | ||
(3, 'Jennifer Taylor', '2018-05-01', '2018-03-03 08:12:52.685000', True, '0.43', 8.46, 'SUSPENDED\r'), | ||
(4, 'Tristan Romero', '2018-10-04', '2018-03-31 20:21:50.199000', True, '0.23', 62.980000000000004, 'PENDING\r') | ||
] | ||
actual = result.fetchall() | ||
assert expected == actual |