-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from portfoliome/inject-nulls
Inject nulls
- Loading branch information
Showing
15 changed files
with
214 additions
and
28 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
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,3 +1,3 @@ | ||
version_info = (0, 2, 2) | ||
version_info = (0, 2, 3) | ||
|
||
__version__ = '.'.join(map(str, version_info)) |
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,19 +1,3 @@ | ||
from typing import Mapping | ||
|
||
|
||
def rename_keys(record: Mapping, key_map: Mapping) -> dict: | ||
"""New record with same keys or renamed keys if key found in key_map.""" | ||
|
||
new_record = dict() | ||
|
||
for k, v in record.items(): | ||
key = key_map[k] if k in key_map else k | ||
new_record[key] = v | ||
|
||
return new_record | ||
|
||
|
||
def replace_keys(record: Mapping, key_map: Mapping) -> dict: | ||
"""New record with renamed keys including keys only found in key_map.""" | ||
|
||
return {key_map[k]: v for k, v in record.items() if k in key_map} | ||
# moved to foil/records.py | ||
from foil.records import replace_keys, rename_keys |
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 |
---|---|---|
|
@@ -24,6 +24,7 @@ | |
|
||
def passthrough(value): | ||
"""Pass through value for no conversion.""" | ||
|
||
return value | ||
|
||
|
||
|
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,32 @@ | ||
"""Functionality for manipulating dictionary records.""" | ||
|
||
from typing import Mapping | ||
|
||
|
||
def rename_keys(record: Mapping, key_map: Mapping) -> dict: | ||
"""New record with same keys or renamed keys if key found in key_map.""" | ||
|
||
new_record = dict() | ||
|
||
for k, v in record.items(): | ||
key = key_map[k] if k in key_map else k | ||
new_record[key] = v | ||
|
||
return new_record | ||
|
||
|
||
def replace_keys(record: Mapping, key_map: Mapping) -> dict: | ||
"""New record with renamed keys including keys only found in key_map.""" | ||
|
||
return {key_map[k]: v for k, v in record.items() if k in key_map} | ||
|
||
|
||
def inject_nulls(data: Mapping, field_names) -> dict: | ||
"""Insert None as value for missing fields.""" | ||
|
||
record = dict() | ||
|
||
for field in field_names: | ||
record[field] = data.get(field, None) | ||
|
||
return record |
Empty file.
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,11 @@ | ||
import unittest | ||
|
||
from foil.agent import UserAgent | ||
|
||
|
||
class TestAgent(unittest.TestCase): | ||
def test_user_agent(self): | ||
expected = 'Mozilla/4.0 (compatible; MSIE 4.01; Windows 95)' | ||
result = UserAgent.ie | ||
|
||
self.assertEqual(expected, result) |
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,35 @@ | ||
import os | ||
import textwrap | ||
import unittest | ||
from tempfile import NamedTemporaryFile | ||
|
||
from foil.dotenv import read_dotenv | ||
|
||
|
||
def mock_dotenv(): | ||
return textwrap.dedent("""\ | ||
SECRET=quiet | ||
# comment this | ||
forgot equals | ||
DB='drawer'""") | ||
|
||
|
||
class TestDotEnv(unittest.TestCase): | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
with NamedTemporaryFile(suffix='.env', delete=False) as tmp: | ||
with open(tmp.name, 'w', encoding='UTF-8') as file: | ||
file.write(mock_dotenv()) | ||
cls.path = tmp.name | ||
|
||
def test_read_dotenv(self): | ||
expected = [('SECRET', 'quiet'), ('DB', 'drawer')] | ||
result = list(read_dotenv(self.path)) | ||
|
||
self.assertEqual(expected, result) | ||
|
||
@classmethod | ||
def tearDown(cls): | ||
if os.path.exists(cls.path): | ||
os.unlink(cls.path) |
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,30 @@ | ||
import json | ||
import unittest | ||
from logging import INFO, LogRecord | ||
|
||
from foil.logger import JSONFormatter | ||
|
||
|
||
class TestLogFormatter(unittest.TestCase): | ||
def test_json_formatter(self): | ||
name = 'name' | ||
line = 42 | ||
module = 'some_module' | ||
func = 'some_function' | ||
msg = {'content': 'sample log'} | ||
|
||
log_record = LogRecord( | ||
name, INFO, module, line, msg, None, None, func=func | ||
) | ||
formatter = JSONFormatter() | ||
|
||
log_result = formatter.format(log_record) | ||
result = json.loads(log_result) | ||
|
||
# check some of the fields to ensure json formatted correctly | ||
self.assertEqual(name, result['name']) | ||
self.assertEqual(line, result['lineNumber']) | ||
self.assertEqual(func, result['functionName']) | ||
self.assertEqual(module, result['module']) | ||
self.assertEqual('INFO', result['level']) | ||
self.assertEqual(msg, result['message']) |
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
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,13 @@ | ||
import unittest | ||
|
||
from foil.util import natural_sort | ||
|
||
|
||
class TestNaturalSort(unittest.TestCase): | ||
def test_natural_sort(self): | ||
entries = ['ab127b', 'ab123b'] | ||
|
||
expected = ['ab123b', 'ab127b'] | ||
result = natural_sort(entries) | ||
|
||
self.assertEqual(expected, result) |