-
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 #19 from bsa7/SMLP-018-implement-candles-model
SMLP-018 Implement Candles model
- Loading branch information
Showing
19 changed files
with
192 additions
and
51 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
API_EXMO_HOST=https://api.exmo.com/v1.1 | ||
MONGO_USER_NAME= | ||
MONGO_USER_PASSWORD= | ||
MONGO_DATABASE_NAME=smlp | ||
MONGO_HOST=localhost:45845 |
This file was deleted.
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
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,24 @@ | ||
''' This file contains implementation of ServiceFactory class ''' | ||
from app.lib.singleton import Singleton | ||
from app.lib.env import Env | ||
from app.lib.mongo_client import MongoClient | ||
from app.lib.test.mongo_client import MongoClient as TestMongoClient | ||
|
||
class ServiceFactory(metaclass = Singleton): | ||
''' This class produces client classes for various services ''' | ||
@property | ||
def mongo_client(self): | ||
''' Returns class for mongo_client ''' | ||
return self.__client_by_env_name(production = MongoClient, test = TestMongoClient) | ||
|
||
def __client_by_env_name(self, development = None, production = None, test = None): | ||
''' Returns one from given attributes depending on env name ''' | ||
env_name = Env().name | ||
print(f'{env_name=}') | ||
if env_name == 'test': | ||
return test or development or production | ||
|
||
if env_name == 'development': | ||
return development or production | ||
|
||
return production or development |
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,38 @@ | ||
''' This file contains self-writed implementation for inmemory mongo client ''' | ||
|
||
class Collection: | ||
''' This class contains emulation of mongodb collection ''' | ||
__storage = [] | ||
|
||
@classmethod | ||
def find_many(cls, filter_attributes): | ||
''' Returns list of filtered documents ''' | ||
def filter_lambda(item): | ||
return item.items() | filter_attributes.items() == item.items() | ||
|
||
result = filter(filter_lambda, cls.__storage) | ||
return list(result) | ||
|
||
@classmethod | ||
def count_documents(cls, filter_attributes) -> int: | ||
''' Returns count of filtered documents ''' | ||
return len(cls.find_many(filter_attributes)) | ||
|
||
@classmethod | ||
def insert_one(cls, record_attributes): | ||
''' Insert one record to storage ''' | ||
return cls.__storage.append(record_attributes) | ||
|
||
@classmethod | ||
def find_one(cls, filter_attributes): | ||
''' Find first record with exact attributes ''' | ||
return cls.find_many(filter_attributes)[0] | ||
|
||
@classmethod | ||
def cleanup(cls): | ||
''' Clears internal storage ''' | ||
cls.__storage = [] | ||
|
||
class MongoClient: | ||
''' This class implements mongo db client ''' | ||
collection = Collection |
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,34 @@ | ||
''' This file contains ApplicationRecord abstract class definition ''' | ||
from app.lib.service_factory import ServiceFactory | ||
|
||
class ApplicationRecord(): | ||
''' This abstract class contains base methods for data operation in mongo db ''' | ||
collection = ServiceFactory().mongo_client().collection | ||
|
||
def __init__(self): | ||
''' Initializes instance ''' | ||
|
||
@classmethod | ||
def count(cls, **filter_attributes) -> int: | ||
''' Returns count of model's records ''' | ||
return cls.collection.count_documents({ 'model': cls.__name__, **filter_attributes }) | ||
|
||
@classmethod | ||
def find_one(cls, **attributes): | ||
''' find record by filter ''' | ||
return cls.collection.find_one({ 'model': cls.__name__, **attributes }) | ||
|
||
@classmethod | ||
def insert_one(cls, **record_attributes): | ||
''' Insert one record model's records ''' | ||
return cls.collection.insert_one({ 'model': cls.__name__, **record_attributes }) | ||
|
||
@classmethod | ||
def upsert_one(cls, find_by, data): | ||
''' Update existed or create new record ''' | ||
existed_item = cls.find_one(**find_by) | ||
if existed_item is None: | ||
return cls.insert_one(**find_by, **data) | ||
|
||
print(f'{find_by=}, {data=}') | ||
return cls.collection.update_one({ 'model': cls.__name__, **find_by }, { '$set': data }) |
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,5 @@ | ||
''' This file contains Candle model class definition ''' | ||
from app.models.application_record import ApplicationRecord | ||
|
||
class Candle(ApplicationRecord): | ||
'''This class contains Candle class definition''' |
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,6 @@ | ||
[pytest] | ||
env_files = | ||
.env | ||
.test.env | ||
testpaths = | ||
test |
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 @@ | ||
#!/usr/bin/env bash | ||
|
||
python -m pytest test --capture=sys | ||
PYTHON_ENV=test python -m pytest --capture=sys --envfile=.test.env |
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 |
---|---|---|
@@ -1,26 +1,18 @@ | ||
''' This file contains unit tests for mongo client class instance ''' | ||
import unittest | ||
from mockupdb import going, MockupDB | ||
from app.lib.mongo_client import MongoClient | ||
from app.lib.service_factory import ServiceFactory | ||
|
||
class TestMongoClient(unittest.TestCase): | ||
''' This class contains unit tests for mongo client ''' | ||
def setUp(self): | ||
''' Initializes mock server for mongodb client ''' | ||
self.__server = MockupDB(auto_ismaster = True) | ||
self.__server.autoresponds('ismaster', maxWireVersion = 6) | ||
self.__server.run() | ||
self.addCleanup(self.__server.stop) | ||
self.__client = MongoClient(self.__server.uri) | ||
self.__collection = ServiceFactory().mongo_client().collection | ||
|
||
def test_insert_one(self): | ||
''' This case checks if the mongo client inserts one record to collection ''' | ||
expected_document = { '_id': 'id-100500' } | ||
with going(self.__client.database.collection.insert_one, expected_document) as future: | ||
self.__server.receives().ok() | ||
|
||
result = future() | ||
self.assertEqual('id-100500', result.inserted_id) | ||
self.__collection.insert_one(expected_document) | ||
result = self.__collection.find_one(expected_document) | ||
self.assertEqual(expected_document, result) | ||
|
||
def test_read_collection_size(self): | ||
''' This case checks the mongo client receive correct size of collection ''' |
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,31 @@ | ||
''' This file contains test for Candle model tests ''' | ||
import unittest | ||
from app.models.application_record import ApplicationRecord | ||
from app.models.candle import Candle | ||
|
||
class TestApplicationRecord(unittest.TestCase): | ||
''' This class contains tests for base methods of Candle::ApplicationRecord model class ''' | ||
|
||
def tearDown(self): | ||
''' Clean up after each test ''' | ||
ApplicationRecord.collection.cleanup() | ||
|
||
def test_zero_count(self): | ||
''' This case checks if model can get correct zero count when no record exists ''' | ||
count = Candle.count() | ||
self.assertEqual(count, 0) | ||
|
||
def test_count(self): | ||
''' This case checks if model can get correct zero count when some records are exists ''' | ||
Candle.insert_one(ds = 123456, y = 123) | ||
Candle.insert_one(ds = 123457, y = 124) | ||
self.assertEqual(Candle.count(), 2) | ||
|
||
def test_insert_one(self): | ||
''' This case checks if model correctly insert one record ''' | ||
record_search_attributes = { 'ds': 12345 } | ||
record_attributes = { 'y': 123 } | ||
expected_record_attributes = { 'model': 'Candle', **record_search_attributes, **record_attributes } | ||
Candle.insert_one(**record_search_attributes, **record_attributes) | ||
created_record = Candle.find_one(**record_search_attributes) | ||
self.assertEqual(created_record, expected_record_attributes) |
File renamed without changes.