def testGetPluginState(self): db = getDbObject() db[PLUGINS].save({NAME: TEST_NAME, ENABLED: True}) obj = getPluginState(TEST_NAME) self.assertEqual(obj, True) obj = getPluginState('aaaaaaa') self.assertEqual(obj, False)
def checkGeneratedDb(self, dbName): db = getDbObject(dbName) addServiceDb(dbName) indexes = db[COLLECTION_POINTS_NAME].index_information() self.assertTrue('location_2dsphere' in indexes.keys()) self.assertTrue('date_-1' in indexes.keys()) self.assertTrue('name_1' in indexes.keys())
def testGetChannelByName(self): db = getDbObject(DB) obj_id = db[COLLECTION].save({NAME: "test_GT_1288"}) with self.assertRaises(ChannelDoesNotExist): getChannelByName("testservice", "111") obj = getChannelByName("testservice", "test_GT_1288") self.assertEqual(obj["_id"], obj_id)
def testGetPointById(self): db = getDbObject(DB) obj_id = db[COLLECTION].save({NAME: 'test_GT_1304'}) with self.assertRaises(PointDoesNotExist): getPointById('testservice', '111111111111111111111111') obj = getPointById('testservice', obj_id) self.assertEqual(obj['name'], 'test_GT_1304')
def testAddPoints(self): db = getDbObject(DB) addPoints(DB, [TEST_POINT1, TEST_POINT2]) obj1 = db[COLLECTION].find_one({'channel_id': '3'}) obj2 = db[COLLECTION].find_one({'channel_id': '5'}) self.assertEqual(obj1['location']['coordinates'][0], 10) self.assertEqual(obj2['location']['coordinates'][0], 20)
def testGetChannelById(self): db = getDbObject(DB) obj_id = db[COLLECTION].save({NAME: 'test_GT_1286'}) with self.assertRaises(ChannelDoesNotExist): getChannelById('testservice', '111111111111111111111111') obj = getChannelById('testservice', obj_id) self.assertEqual(obj['name'], 'test_GT_1286')
def testUpdatePoint(self): collection = getDbObject(DB)[COLLECTION] with self.assertRaises(PointDoesNotExist): updatePoint(DB, BAD_TEST_ID, TEST_DICT) obj = collection.find_one({ID: TEST_ID}) updatePoint(DB, TEST_ID, TEST_DICT) obj = collection.find_one({ID: TEST_ID}) self.assertEqual(obj['name'], 'test_1319')
def testDeletePointById(self): db = getDbObject(DB) obj_id = db[COLLECTION].save({NAME: 'test_GT_1306'}) with self.assertRaises(PointDoesNotExist): deletePointById('testservice', '111111111111111111111111') deletePointById('testservice', obj_id) obj = db[COLLECTION].find_one({ID: obj_id}) self.assertEqual(obj, None)
def testAddService(self): collection = getDbObject(DB)[COLLECTION] with self.assertRaises(ServiceAlreadyExistsException): obj = addService(INVALID_SERVICE_NAME, 1, ' ') obj_id = addService(VALID_SERVICE_NAME, 1, ' ') obj = collection.find_one({ID: obj_id}) self.assertNotEqual(obj, None) checkGeneratedDb(self, VALID_SERVICE_NAME)
def findUsers(number, offset, loginSubstring): collectionUsers = getDbObject(getDbName())[COLLECTION_NAME_USERS] result = list( collectionUsers.find({ LOGIN: { '$regex': ".*" + loginSubstring + ".*" } }).skip(offset).sort(LOGIN).limit(number)) return result
def testSetPluginState(self): db = getDbObject() db[PLUGINS].save({NAME: TEST_NAME, ENABLED: True}) setPluginState(TEST_NAME, False) obj = db[PLUGINS].find_one({NAME: TEST_NAME}) self.assertEqual(obj[ENABLED], False) setPluginState(TEST_NEW_NAME, True) obj = db[PLUGINS].find_one({NAME: TEST_NEW_NAME}) self.assertEqual(obj[ENABLED], True)
def testAddPoints(self): db = getDbObject(DB) addPoints(DB, [TEST_POINT1, TEST_POINT2]) obj1 = db[COLLECTION].find_one({ 'channel_id': ObjectId(TEST_POINT1['channel_id'])}) obj2 = db[COLLECTION].find_one({ 'channel_id': ObjectId(TEST_POINT2['channel_id'])}) self.assertEqual(obj1['location']['coordinates'][0], 10) self.assertEqual(obj2['location']['coordinates'][0], 20)
def testUpdateChannel(self): collection = getDbObject(DB)[COLLECTION] obj_id = collection.save({NAME: 'test_GT_1292'}) with self.assertRaises(ChannelDoesNotExist): updateChannel('testservice', '111', 'test', None, None) updateChannel('testservice', obj_id, 'test', 1, 2) obj = collection.find_one({ID: obj_id}) self.assertEqual(obj['name'], 'test') self.assertEqual(obj['json'], 1) self.assertEqual(obj['acl'], 2)
def test_getServiceIdByName_func(self): collection = getDbObject(TEST_DB)["services"] obj = collection.find_one({"_id": TEST_ID}) print('Test object: ' + unicode(obj)) try: getServiceIdByName('testservice') except ServiceNotExistException: self.assertTrue(False) with self.assertRaises(ServiceNotExistException): getServiceIdByName('OlchikovTestService')
def prepareDb(): # Connecting to db 'geomongo', collection 'log' db = getDbObject() collection = db[COLLECTION] for i in range(100): # Inserting new post with date post = {"user_id": "stub", "date": datetime((2000 + i), 1, 1).utcnow().replace(2000 + i), "message": "test", "service": "testservice"} collection.insert(post)
def addConfigurablePlugin(pluginName, existConfig): collection = getDbObject()[PLUGINS] obj = collection.find_one({"name": pluginName}) if obj is None: return False if existConfig: obj[CONFIGURABLE] = True else: obj[CONFIGURABLE] = False collection.save(obj) return True
def testGetMetadataById(self): collection = getDbObject(TEST_DB)[TEST_COLLECTION] collection.insert(TEST_DATA) obj = collection.find_one({"_id": TEST_ID}) try: result = getMetadataById(TEST_DB, TEST_ID) self.assertEqual(obj, result) except MetadataDoesNotExistException: self.assertTrue(False) with self.assertRaises(MetadataDoesNotExistException): result = getMetadataById(TEST_DB, BAD_TEST_ID)
def addUser(_id, firstName, lastName, email, login): return_id = _id try: findUserById(_id) except UserDoesNotExist: collectionUsers = getDbObject(getDbName())[COLLECTION_NAME_USERS] collectionUsers.insert( {FIND_KEY_ID: _id, FIRST_NAME: firstName, LAST_NAME: lastName, EMAIL: email, LOGIN: login} ) finally: return_id = _id return return_id
def addUser(_id, firstName, lastName, email, login): return_id = _id try: findUserById(_id) except UserDoesNotExist: collectionUsers = getDbObject(getDbName())[COLLECTION_NAME_USERS] collectionUsers.insert( {FIND_KEY_ID: _id, FIRST_NAME: firstName, LAST_NAME: lastName, EMAIL: email, LOGIN: login}) finally: return_id = _id return return_id
def prepareDb(): # Connecting to db 'geomongo', collection 'log' db = getDbObject() collection = db[COLLECTION] for i in range(100): # Inserting new post with date post = { "user_id": "stub", "date": datetime((2000 + i), 1, 1).utcnow().replace(2000 + i), "message": "test", "service": "testservice", } collection.insert(post)
def testPointResourceDelete(self): db = getDbObject(TEST_SERVICE) obj_id = db[COLLECTION].save({NAME: NAME_TEST_OBJECT}) response = requests.delete(self.getUrl(TEST_URL + unicode(obj_id))) responseText = response.text responseCode = response.status_code self.assertEquals(responseText, VALID_RESPONSE_TEXT) self.assertEquals(responseCode, VALID_RESPONSE_CODE) response = requests.delete(self.getUrl(BAD_TEST_URL)) responseText = response.text responseCode = response.status_code self.assertEquals(responseText, NOT_VALID_RESPONSE_TEXT) self.assertEquals(responseCode, NOT_VALID_RESPONSE_CODE)
def test_getServiceById_func(self): collection = getDbObject(TEST_DB)["services"] obj = collection.find_one({"_id": TEST_ID}) print('Test object: ' + unicode(obj)) testObject1 = {} try: testObject1 = getServiceById(TEST_ID) print('Test object with getServiceById: ' + unicode(obj)) print 'testObject1' + unicode(testObject1) except ServiceNotExistException: self.assertTrue(False) with self.assertRaises(ServiceNotExistException): testObject2 = getServiceById(BAD_TEST_ID) print 'testObject2' + unicode(testObject2)
def testChannelServicePostRequest(self): db = getDbObject(TEST_SERVICE) response = requests.post(self.getUrl(TEST_URL), data=DATA) responseText = response.text responseCode = response.status_code ID = json.loads(responseText) db['channels'].remove({'_id': ObjectId(ID['$oid'])}) self.assertNotEquals(responseText, []) self.assertNotEquals(responseText, {}) self.assertEquals(responseCode, 200) response = requests.post(self.getUrl(TEST_URL), data=DATA2) responseText = response.text responseCode = response.status_code self.assertEquals(responseCode, 400)
def testServiceListGetRequest(self): db = getDbObject() db[COLLECTION].save(TEST_OBJ) response = requests.get(self.getUrl(TEST_URL), params=TEST_PARAMETERS) removeService(TEST_NAME) responseText = response.text responseCode = response.status_code self.assertEquals(responseCode, VALID_RESPONSE_CODE) response = requests.get(self.getUrl(TEST_URL), params=BAD_TEST_PARAMETERS) responseText = response.text responseCode = response.status_code self.assertEquals(responseText, BAD_RESULT) self.assertEquals(responseCode, NOT_VALID_RESPONSE_CODE)
def test_GT_1509_class_open_data_to_points_loader(self): dataObj = OpenDataToPointsLoader(TEST_SERVICE, pointsList) dataObj.loadPoints() collection = getDbObject(TEST_SERVICE)[POINTS] self.assertNotEquals(None, collection.find_one( {ID: ObjectId("552833515c0dd1178d37f7ee")})) self.assertNotEquals(None, collection.find_one( {ID: ObjectId("552833515c0dd1178d37f7ff")})) collection.remove({ID: ObjectId("552833515c0dd1178d37f7ee")}) collection.remove({ID: ObjectId("552833515c0dd1178d37f7ff")}) self.assertEquals(None, collection.find_one( {ID: ObjectId("552833515c0dd1178d37f7ee")})) self.assertEquals(None, collection.find_one( {ID: ObjectId("552833515c0dd1178d37f7ff")}))
def testServiceListGetRequest(self): db = getDbObject() db[COLLECTION].save(TEST_OBJ) response = requests.get(self.getUrl(TEST_URL), params=TEST_PARAMETERS) removeService(TEST_NAME) responseText = response.text responseCode = response.status_code self.assertEquals(responseCode, VALID_RESPONSE_CODE) response = requests.get( self.getUrl(TEST_URL), params=BAD_TEST_PARAMETERS) responseText = response.text responseCode = response.status_code self.assertEquals(responseText, BAD_RESULT) self.assertEquals(responseCode, NOT_VALID_RESPONSE_CODE)
def testChannelGetRequest(self): db = getDbObject(TEST_SERVICE) obj_id = db[CHANNELS_COLLECTION].save( {'name': 'test_get_channel_by_id'}) response = requests.get(self.getUrl(TEST_URL + '/' + unicode(obj_id))) VALID_RESPONSE_TEXT = '{"_id": {"$oid": "' + \ unicode(obj_id) + '"}, "name": "test_get_channel_by_id"}' responseText = response.text responseCode = response.status_code self.assertEquals(responseText, VALID_RESPONSE_TEXT) self.assertEquals(responseCode, VALID_RESPONSE_CODE) response = requests.get(self.getUrl(BAD_TEST_URL)) responseText = response.text responseCode = response.status_code self.assertEquals(responseText, NOT_VALID_RESPONSE_TEXT) self.assertEquals(responseCode, NOT_VALID_RESPONSE_CODE)
def testUserIdLog(self): db = getDbObject() collection = db[COLLECTION_LOG_NAME] with app.test_request_context(LOGOIN_REQUEST_CONTEXT + REQUEST_PARAM): # Emulating login session collection.drop() logUserIn(request.args[USER_ID]) getUserId() db_res = collection.find_one({USER_ID: TEST_ID}) self.assertEqual(db_res[MESSAGE], AUTH_MESS) # Emulating logout session collection.drop() logUserOut() getUserId() db_res = collection.find_one({USER_ID: USER_ID}) self.assertEqual(db_res, ANONYM_MESS)
def testGetServiceListTest(self): db = getDbObject() FIRST_OBJ = list(db[SERVICES].find().sort(NAME, 1).skip(0).limit(1))[0] COUNT = db[SERVICES].find().count() self.assertNotEqual(COUNT, 0) LAST_OBJ = list(db[SERVICES].find().sort(NAME, -1).skip(0).limit(1))[0] list(db[SERVICES].find()) RESULT = getServiceList(COUNT, 0, None, None) self.assertEqual(RESULT[0], FIRST_OBJ) self.assertEqual(RESULT[COUNT - 1], LAST_OBJ) RESULT = getServiceList(None, 0, None, None) self.assertEqual(RESULT[0], FIRST_OBJ) self.assertEqual(RESULT[COUNT - 1], LAST_OBJ) RESULT = getServiceList(COUNT, 0, TEST_SUBSTRING, TEST_OWNER_ID) self.assertEqual(RESULT[0]['name'], TEST_SUBSTRING)
def testGetServiceListTest(self): db = getDbObject() FIRST_OBJ = list(db[SERVICES].find().sort(NAME, 1).skip(0).limit(1))[0] COUNT = db[SERVICES].find().count() self.assertNotEqual(COUNT, 0) LAST_OBJ = list( db[SERVICES].find().sort( NAME, -1).skip(0).limit(1))[0] list(db[SERVICES].find()) RESULT = getServiceList(COUNT, 0, None, None) self.assertEqual(RESULT[0], FIRST_OBJ) self.assertEqual(RESULT[COUNT - 1], LAST_OBJ) RESULT = getServiceList(None, 0, None, None) self.assertEqual(RESULT[0], FIRST_OBJ) self.assertEqual(RESULT[COUNT - 1], LAST_OBJ) RESULT = getServiceList(COUNT, 0, TEST_SUBSTRING, TEST_OWNER_ID) self.assertEqual(RESULT[0]['name'], TEST_SUBSTRING)
def test_GT_1509_class_open_data_to_points_loader(self): dataObj = OpenDataToPointsLoader(TEST_SERVICE, pointsList) dataObj.loadPoints() collection = getDbObject(TEST_SERVICE)[POINTS] self.assertNotEquals( None, collection.find_one({ID: ObjectId("552833515c0dd1178d37f7ee")})) self.assertNotEquals( None, collection.find_one({ID: ObjectId("552833515c0dd1178d37f7ff")})) collection.remove({ID: ObjectId("552833515c0dd1178d37f7ee")}) collection.remove({ID: ObjectId("552833515c0dd1178d37f7ff")}) self.assertEquals( None, collection.find_one({ID: ObjectId("552833515c0dd1178d37f7ee")})) self.assertEquals( None, collection.find_one({ID: ObjectId("552833515c0dd1178d37f7ff")}))
def geocoderImport(self, channelName, serviceName): # Checking for service existence getServiceIdByName(serviceName) # Getting channel id channel = getChannelByName(serviceName, channelName) channel_id = channel[ID] # Initialising requester and parser greqv = GeonamesRequestSender() gpars = GeocoderResponseParser() # Getting points collection points_coll = getDbObject(serviceName)[POINTS_COLL] # Getting first slice of <= 5000 points proc_step = 0 point_block = list(points_coll.find({ CHANNEL_ID: channel_id, JSON + '.' + ADDRESS: {"$exists": True} }).skip(proc_step).limit(PROC_BLOCK)) login = PluginConfigReader(PLUGIN_NAME).getConfigContent()[ SECTION_GEOCODING][GEONAMES_LOGIN] while point_block: current_size = len(point_block) addresses = [point[JSON][ADDRESS] for point in point_block] ids = [point[ID] for point in point_block] string_addresses = greqv.requestCoordinates( addresses, login, json_list_to_list_of_strings) json_coords = gpars.parseList(string_addresses) for i in range(0, current_size): # If no coords was found - we change nothing if json_coords[i] is None: continue points_coll.update( {ID: ids[i]}, {'$set': {'location.coordinates': json_coords[i]}} ) # Getting next slice of <= 5000 points proc_step += PROC_BLOCK point_block = list( points_coll.find().skip(proc_step).limit(PROC_BLOCK)) # Task is done self.done = True # And thread is done too after return return []
def testPointListPostRequest(self): db = getDbObject(DB) response = requests.post(self.getUrl(TEST_URL), data=json.dumps( [{ LAT: 1.1, LON: 1.1, ALT: 1.1, JSON: {'a': 'b'}, CHANNEL_ID: 'channel_id_value' }])) responseCode = response.status_code responseText = response.text obj1 = db[COLLECTION].find_one({CHANNEL_ID: CHANNEL_IDS_VALUE}) self.assertEquals([unicode(obj1['_id'])], ast.literal_eval(responseText)) self.assertEquals(responseCode, VALID_RESPONSE_CODE) response = requests.post(self.getUrl(TEST_URL), data=json.dumps( [{LAT: '1.1', LON: 1.1, ALT: 1.1, JSON: [], CHANNEL_ID:''}])) responseCode = response.status_code responseText = response.text self.assertEquals(responseCode, NOT_VALID_RESPONSE_CODE) self.assertEquals(responseText, '{}')
#!/usr/bin/env python # -*- coding: utf-8 -*- from unittest import TestCase from db_model import removeService, getServiceIdByName, getDbObject from service_not_exist_exception import ServiceNotExistException db = getDbObject() COLLECTION = 'services' NAME = 'name' TEST_OBJECT = 'test_GT_1208' class TestRemoveService(TestCase): def testRemoveService(self): db[COLLECTION].save({NAME: TEST_OBJECT}) removeService(TEST_OBJECT) with self.assertRaises(ServiceNotExistException): getServiceIdByName(TEST_OBJECT)
#!/usr/bin/env python # -*- coding: utf-8 -*- import unittest from db_model import deleteChannelById, getDbObject from channel_does_not_exist import ChannelDoesNotExist SERVICE_NAME = 'testservice' db = getDbObject(SERVICE_NAME) CHANNELS = 'channels' class TestDeleteChannelById(unittest.TestCase): def testDeleteChannelById(self): db[CHANNELS].insert({'name': 'test_GT-1289'}) BAD_ID = db[CHANNELS].insert({'name': 'test_GT-1289_2'}) db[CHANNELS].remove({'name': 'test_GT-1289_2'}) obj = db[CHANNELS].find({'name': 'test_GT-1289'}) obj = list(obj) obj_string = obj[0].get('_id') deleteChannelById(SERVICE_NAME, obj_string) with self.assertRaises(ChannelDoesNotExist): deleteChannelById(SERVICE_NAME, BAD_ID)
# VALUES TEST_USER_ID = 'test_user_id' TEST_MSG = 'test_msg' TEST_SERVICE = 'service' # FIELDS TEST_USER_ID_FIELD = 'user_id' TEST_MSG_FIELD = 'message' TEST_SERVICE_FIELD = 'service' TEST_LEVEL_FIELD = 'level' TEST_SERVICE = 'testservice' # COLLECTIONS LOG = 'log' glog = getDbObject()[LOG] tslog = getDbObject('testservice')[LOG] class TestLogLvl(unittest.TestCase): def testLogLvlIstanceLog(self): writeInstanceLog(TEST_USER_ID, TEST_MSG, LOG_LVL_INFO) obj = list(glog.find().sort('_id', DESCENDING).limit(1))[0] self.assertEqual(obj[TEST_LEVEL_FIELD], LOG_LVL_INFO) writeInstanceLog(TEST_USER_ID, TEST_MSG, LOG_LVL_WARNING) obj = list(glog.find().sort('_id', DESCENDING).limit(1))[0] self.assertEqual(obj[TEST_LEVEL_FIELD], LOG_LVL_WARNING) writeInstanceLog(TEST_USER_ID, TEST_MSG, LOG_LVL_ERROR) obj = list(glog.find().sort('_id', DESCENDING).limit(1))[0] self.assertEqual(obj[TEST_LEVEL_FIELD], LOG_LVL_ERROR)
def testSetMetadata(self): collection = getDbObject(TEST_DB)[TEST_COLLECTION] setMetadata(TEST_DB, TEST_DATA, TEST_ID) obj = collection.find_one({ID: TEST_ID}) self.assertEqual(obj, TEST_DATA)
from unittest import TestCase from geocoding_job import GeocodingJob from db_model import getDbObject from pymongo import DESCENDING from time import sleep CHANNEL_NAME = 'channelName' SERVICE_NAME = 'serviceName' TEST_DATA_FIELD = 'test_data_GT_1705' log = getDbObject('testservice')['log'] def bcgFunc(_, channelName, serviceName): log.insert({'test_data_GT_1705': [channelName, serviceName]}) gj = GeocodingJob(bcgFunc, 'channelName', None, None, 'serviceName') class TestGeocodingJob(TestCase): def testGeocodingJob(self): gj.start() self.assertIsNotNone(gj.thread) self.assertTrue(gj.thread.is_alive()) gj.stop() if gj.thread.is_alive(): while gj.thread.is_alive(): print gj.thread.is_alive()
CHANNEL_NAME = 'geocoder_plugin_test_channel' SERVICE_NAME = 'testservice' CHANNEL_ID = ObjectId("556721a52a2e7febd2744307") TEST_DATA_AFTER = [ [37.61556, 55.75222], [34.37167, 53.25209], [49.66007, 58.59665], [34.34691, 61.78491], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [30.31413, 59.93863] ] db = getDbObject(SERVICE_NAME) points = db['points'] class test: def __init__(self): self.done = False class TestGeocoderImportBackFunc(TestCase): def setUp(self): self.data_before = list(points.find({'channel_id': CHANNEL_ID})) self.addresses_before = [point['location'][ 'coordinates'] for point in self.data_before]
def findUserById(_id): collectionUsers = getDbObject(getDbName())[COLLECTION_NAME_USERS] userById = collectionUsers.find_one({FIND_KEY_ID: _id}) if userById is not None: return userById raise UserDoesNotExist
from unittest import TestCase import pymongo import os from flask import Flask from flask_restful import Api from plugin_routines import enablePlugin from db_model import getDbName, getDbObject tstDir = os.getcwd() srcDir = tstDir + '/..' app = Flask(__name__) api = Api(app) db = getDbObject(getDbName()) PLUGIN_DONE_PLUGIN = 'test_plugin' PLUGIN_FAIL_PLUGIN = 'test_plugin_for_load_fail_gt_1435' FIELD_USERID = 'user_id' COLLECTION_LOG = 'log' FIELD_MESSAGE = 'message' ID = '_id' ANONYM_USER = '******' MESSAGE_LOAD_DONE = 'Plugin ' + PLUGIN_DONE_PLUGIN + \ ' successfully loaded' MESSAGE_LOAD_FAIL = 'Error occurred while loading the plugin ' + \ PLUGIN_FAIL_PLUGIN class TestLogWritingForPluginLoad(TestCase):
def testDeleteMetadataById(self): collection = getDbObject(TEST_DB)[TEST_COLLECTION] collection.insert(TEST_DATA) deleteMetadataById(TEST_DB, TEST_ID) obj = collection.find_one({ID: TEST_ID}) self.assertEqual(obj, None)
# VALUES TEST_USER_ID = 'test_user_id' TEST_MSG = 'test_msg' TEST_SERVICE = 'service' TEST_LOG_LEVEL_INFO = LOG_LVL_INFO TEST_LOG_LEVEL_CRITICAL = LOG_LVL_CRITICAL # COLLECTIONS LOG = 'log' # FIELDS TEST_USER_ID_FIELD = 'user_id' TEST_MSG_FIELD = 'message' TEST_SERVICE_FIELD = 'service' TEST_LEVEL_FIELD = 'level' db = getDbObject(TEST_DB) db_master = getDbObject(TEST_DB_MASTER) class TestAddLogEntry(unittest.TestCase): def testAddLogEntry(self): addLogEntry(TEST_DB, TEST_USER_ID, TEST_MSG, LOG_LVL_INFO) obj = list(db[LOG].find().sort('_id', pymongo.DESCENDING).limit(1))[0] self.assertEqual(obj[TEST_MSG_FIELD], TEST_MSG) self.assertEqual(obj[TEST_USER_ID_FIELD], TEST_USER_ID) self.assertEqual(obj[TEST_LEVEL_FIELD], TEST_LOG_LEVEL_INFO) self.assertTrue(TEST_SERVICE_FIELD not in obj) addLogEntry(TEST_DB_MASTER, TEST_USER_ID, TEST_MSG, TEST_LOG_LEVEL_CRITICAL, TEST_SERVICE) obj = list(db_master[LOG].find().sort('_id', pymongo.DESCENDING).limit(1))[0]
def loadPoints(self): collection = getDbObject(self.serviceName)[POINTS] for point in self.pointsArray: collection.save(point)
from datetime import datetime from db_model import getLog, getDbObject DATE_TO = datetime(3000, 1, 1) DATE_FROM = datetime(2100, 1, 1) DATE_ISERT = datetime(2500, 1, 1) DB_GEOMONGO = "geomongo" DB_TESTSERVICE = "testservice" FAKE_DB = "blablabla" COLLECTION = "log" USER_ID = "user_id" USER_GEOMONGO_ID_VALUE = "GT-1309-geomongo" USER_TESTSERVICE_ID_VALUE = "GT-1309-testservice" collection_geomongo = getDbObject(DB_GEOMONGO)[COLLECTION] collection_testservice = getDbObject(DB_TESTSERVICE)[COLLECTION] class TestGetLogRefactoring(TestCase): def testForGeomongoDb(self): collection_geomongo.insert( {USER_ID: USER_GEOMONGO_ID_VALUE, "date": DATE_ISERT}) log_data = list(getLog(DB_GEOMONGO, 0, 0, DATE_FROM, DATE_TO)) self.assertEqual(log_data[0][USER_ID], USER_GEOMONGO_ID_VALUE) def testForTestserviceDb(self): collection_testservice.insert( {USER_ID: USER_TESTSERVICE_ID_VALUE, "date": DATE_ISERT}) log_data = list(getLog(DB_TESTSERVICE, 0, 0, DATE_FROM, DATE_TO))
def findUsers(number, offset, loginSubstring): collectionUsers = getDbObject(getDbName())[COLLECTION_NAME_USERS] result = list( collectionUsers.find({LOGIN: {"$regex": ".*" + loginSubstring + ".*"}}).skip(offset).sort(LOGIN).limit(number) ) return result