Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix regex check for column name #28

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion target_snowflake/snowflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ def transform():
row = next(rows_iter)

with io.StringIO() as out:
writer = csv.DictWriter(out, csv_headers)
writer = csv.DictWriter(out, csv_headers, quoting=csv.QUOTE_NONNUMERIC)
writer.writerow(row)
return out.getvalue()
except StopIteration:
Expand Down
8 changes: 4 additions & 4 deletions target_snowflake/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ def valid_identifier(x):
IDENTIFIER_FIELD_LENGTH,
len(x),
x))

if not re.match(r'^[a-zA-Z_]\w+$', x):
if not re.match(r'^[a-zA-Z_](\w+)?$', x):
raise SQLError(
'Identifier must only contain alphanumerics, or underscores, and start with alphas. Got `{}` for `{}`'.format(
re.findall(r'[^0-9]', '1234a567')[0],
'Identifier must only contain alphanumerics, or underscores, and start with alphas. Found `{}` from name `{}`'.format(
re.findall(r'[^0-9a-zA-Z_]', x),
x
))

Expand Down
24 changes: 23 additions & 1 deletion tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ def generate_record(self):
'adoption': adoption
}


class InvalidCatStream(CatStream):
def generate_record(self):
record = CatStream.generate_record(self)
Expand Down Expand Up @@ -433,6 +432,29 @@ def generate_record(self):
}


SINGLE_CHAR_SCHEMA = {
'type': 'SCHEMA',
'stream': 'root',
'schema': {
'additionalProperties': False,
'properties': {
'x': {
'type': 'integer'
}
}
},
'key_properties': []
}

class SingleCharStream(FakeStream):
stream = 'root'
schema = SINGLE_CHAR_SCHEMA

def generate_record(self):
return {
'x': random.randint(-314159265359, 314159265359)
}


def clear_schema():
with connect(**TEST_DB) as conn:
Expand Down
36 changes: 35 additions & 1 deletion tests/test_target_snowflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from psycopg2 import sql
import pytest

from fixtures import CatStream, CONFIG, db_prep, MultiTypeStream, NestedStream, S3_CONFIG, TEST_DB
from fixtures import CatStream, CONFIG, db_prep, MultiTypeStream, NestedStream, SingleCharStream, S3_CONFIG, TEST_DB
from target_postgres import singer_stream
from target_postgres.target_tools import TargetError

Expand Down Expand Up @@ -652,6 +652,40 @@ def test_loading__multi_types_columns(db_prep):
assert stream_count == len([x for x in persisted_records if isinstance(x[0], float)])


def test_loading__single_char_columns(db_prep):
stream_count = 50
main(CONFIG, input_stream=SingleCharStream(stream_count))

with connect(**TEST_DB) as conn:
with conn.cursor() as cur:
assert_columns_equal(cur,
'ROOT',
{
('_SDC_PRIMARY_KEY', 'TEXT', 'NO'),
('_SDC_BATCHED_AT', 'TIMESTAMP_TZ', 'YES'),
('_SDC_RECEIVED_AT', 'TIMESTAMP_TZ', 'YES'),
('_SDC_SEQUENCE', 'NUMBER', 'YES'),
('_SDC_TABLE_VERSION', 'NUMBER', 'YES'),
('_SDC_TARGET_SNOWFLAKE_CREATE_TABLE_PLACEHOLDER', 'BOOLEAN', 'YES'),
('X', 'NUMBER', 'YES')
})

cur.execute('''
SELECT {} FROM {}.{}.{}
'''.format(
sql.identifier('X'),
sql.identifier(CONFIG['snowflake_database']),
sql.identifier(CONFIG['snowflake_schema']),
sql.identifier('ROOT')
))
persisted_records = cur.fetchall()

## Assert that the column is has migrated data
assert stream_count == len(persisted_records)
assert stream_count == len([x for x in persisted_records if isinstance(x[0], float)])



def test_upsert(db_prep):
stream = CatStream(100)
main(CONFIG, input_stream=stream)
Expand Down