def run_get(test_id, run_type): status = StatusCore(test_id=test_id) status.add_unknown_if_none_exist() run = status.should_i_run(run_type) if run is None: abort(404) return run
def test_add_unknown_if_none_exists(self): from core.Status import Status test_id = str(uuid.uuid4()) status = Status(test_id) assert status.get() == None status.add_unknown_if_none_exist() status_saved = Status(test_id).get() assert status_saved._id != None assert status_saved._last == True assert status_saved._status == 'UNKNOWN'
def test_purge(self): from core.Status import Status from core.Base import Base test_id = str(uuid.uuid4()) test_status1 = 'FAILURE' details = {'browser': 'Firefox'} test_type = str(uuid.uuid4()) status1 = Status(test_id, test_type, test_status1, details=details) status1.save_and_update() rv = self.app_test.get('/test/tests/{0}/purge'.format(test_id)) assert rv.status_code == 200 res = json.loads(rv.data.decode('utf-8')) assert res['result'] == 'Success' assert res['purge'] == {'nb_removed': 0} status_count = Status.list({Status._test_id: test_id}) assert len(status_count) == 1 Base().upsert_by_id(Status.collection, ObjectId(status1._id), {Status._on: datetime.datetime.now() - datetime.timedelta(days=8)}) status2 = Status(test_id, test_type, test_status1, details=details) status2.save_and_update() status_count = Status.list({Status._test_id: test_id}) assert len(status_count) == 2 rv = self.app_test.get('/test/tests/{0}/purge'.format(test_id)) assert rv.status_code == 200 res = json.loads(rv.data.decode('utf-8')) assert res['result'] == 'Success' assert res['purge'] == {'nb_removed': 1} status_count = Status.list({Status._test_id: test_id}) assert len(status_count) == 1
def test_filter_details(self): from core.Filter import Filter from core.Status import Status my_filter = Filter.from_dict({'operator': 'EQUAL', 'field': 'Status._details[\'BROWSER\']', 'value': 'Firefox'}) assert my_filter != False s1 = Status(str(uuid.uuid4()), str(uuid.uuid4()), 'FAILURE', details={'BROWSER': 'Firefox'}) s1._on = datetime.datetime.now() - datetime.timedelta(seconds=5) s2 = Status(str(uuid.uuid4()), str(uuid.uuid4()), 'SUCCESS', details={'BROWSER': 'Chrome'}) s2._on = datetime.datetime.now() + datetime.timedelta(seconds=5) assert my_filter.check_status(s1) == True assert my_filter.check_status(s2) == False status_filter = my_filter.check_statuses([s1, s2]) assert len(status_filter) == 1 assert status_filter[0]._test_id == s1._test_id
def test_filter_greater_than(self): from core.Filter import Filter from core.Status import Status my_filter = Filter.from_dict({'operator': 'GREATER THAN', 'field': 'Status._on', 'value': 'datetime.datetime.now() + datetime.timedelta(days=1)'}) assert my_filter != False s1 = Status(str(uuid.uuid4()), str(uuid.uuid4()), 'FAILURE', details={}) s1._on = datetime.datetime.now() - datetime.timedelta(seconds=5) s2 = Status(str(uuid.uuid4()), str(uuid.uuid4()), 'SUCCESS', details={}) s2._on = datetime.datetime.now() + datetime.timedelta(days=1, seconds=5) assert my_filter.check_status(s1) == False assert my_filter.check_status(s2) == True status_filter = my_filter.check_statuses([s1, s2]) assert len(status_filter) == 1 assert status_filter[0]._test_id == s2._test_id
def test_expand_failed(self): from core import Base from core.Status import Status test_id = str(uuid.uuid4()) test_status = 'SUCCESS' test_type = str(uuid.uuid4()) status = Status(test_id, test_type, test_status) status_id = str(Base.Base().insert(Status.collection, status.to_dict())) rv = self.app.get('/api/v1/statuses/{0}'.format(status_id)) assert rv.status_code == 200 res = json.loads(rv.data.decode('utf-8')) assert type(res['status']['test']) in [type('String'), type(u'Unicode')] rv = self.app.get('/api/v1/statuses/{0}?expand=test'.format(status_id)) assert rv.status_code == 200 res = json.loads(rv.data.decode('utf-8')) assert type(res['status']['test']) in [type('String'), type(u'Unicode')]
def test_get_run(self): from core.Status import Status from core.TestType import TestType from core.Base import Base test_id = str(uuid.uuid4()) test_status = 'SUCCESS' test_type = str(uuid.uuid4()) field1 = 'browser' field2 = 'environment' tt = TestType(test_type, doc_fields_to_index=[field1, field2]) tt.add_run_type('new', 'ANY', {'operator': 'EQUAL', 'field': 1, 'value': 1}) tt.save() details1 = {field1: 'Firefox', field2: 'master'} status1 = Status(test_id, test_type, test_status, details=details1) status1.save() rv = self.app_test_type.get('/test/test_types/{0}/runs/{1}'.format(str(uuid.uuid4()), 'default')) assert rv.status_code == 404 rv = self.app_test_type.get('/test/test_types/{0}/runs/{1}'.format(test_type, str(uuid.uuid4()))) assert rv.status_code == 404 rv = self.app_test_type.get('/test/test_types/{0}/runs/{1}'.format(test_type, 'default')) assert rv.status_code == 200 res = json.loads(rv.data.decode('utf-8')) assert res['result'] == 'Success' assert res['run']['modifier'] == TestType._default_run['modifier'] assert res['run']['condition'] == TestType._default_run['condition'].to_dict() rv = self.app_test_type.get('/test/test_types/{0}/runs/{1}'.format(test_type, 'new')) assert rv.status_code == 200 res = json.loads(rv.data.decode('utf-8')) assert res['result'] == 'Success' assert res['run']['modifier'] == 'ANY' assert res['run']['condition'] == {'operator': 'EQUAL', 'field': 1, 'value': 1} rv = self.app_test_type.get('/test/test_types/{0}/runs'.format(str(uuid.uuid4()))) assert rv.status_code == 404 rv = self.app_test_type.get('/test/test_types/{0}/runs'.format(test_type)) assert rv.status_code == 200 res = json.loads(rv.data.decode('utf-8')) assert res['result'] == 'Success' assert res['count'] == 2 rv = self.app_test_type.get('/test/test_types/{0}'.format(test_type)) assert rv.status_code == 200 res = json.loads(rv.data.decode('utf-8')) assert res['result'] == 'Success' assert 'run' not in res['test_type']
def test_should_i_run_custom(self): from core.Status import Status from core.TestType import TestType test_type = str(uuid.uuid4()) tt = TestType(test_type) tt.add_run_type('ALLOK', 'ALL', {'field': 'Status._status', 'operator': 'EQUAL', 'value': 'SUCCESS'}) tt.save() test_id = str(uuid.uuid4()) details = {'browser': 'Firefox'} test_type = str(uuid.uuid4()) test_status1 = 'SUCCESS' status1 = Status(test_id, test_type, test_status1, details=details) status1.save_and_update() rv = self.app_test.get('/test/tests/{0}/run/ALLOK'.format(test_id)) assert rv.status_code == 200 res = json.loads(rv.data.decode('utf-8')) assert res['result'] == 'Success' assert res['run'] == True test_status2 = 'FAILURE' status2 = Status(test_id, test_type, test_status2, details=details) status2.save_and_update() rv = self.app_test.get('/test/tests/{0}/run/ALLOK'.format(test_id)) assert rv.status_code == 200 res = json.loads(rv.data.decode('utf-8')) assert res['result'] == 'Success' assert res['run'] == False rv = self.app_test.get('/test/tests/{0}/run'.format(test_id)) assert rv.status_code == 200 res = json.loads(rv.data.decode('utf-8')) assert res['result'] == 'Success' assert res['run'] == False test_status2 = 'SUCCESS' status2 = Status(test_id, test_type, test_status2, details=details) status2.save_and_update() rv = self.app_test.get('/test/tests/{0}/run/ALLOK'.format(test_id)) assert rv.status_code == 200 res = json.loads(rv.data.decode('utf-8')) assert res['result'] == 'Success' assert res['run'] == False rv = self.app_test.get('/test/tests/{0}/run'.format(test_id)) assert rv.status_code == 200 res = json.loads(rv.data.decode('utf-8')) assert res['result'] == 'Success' assert res['run'] == True rv = self.app_test.get('/test/tests/{0}/run/{1}'.format(test_id, str(uuid.uuid4()))) assert rv.status_code == 404
def post(self): purge_result = None parser = reqparse.RequestParser() parser.add_argument('purge', help='Do we purge ?', required=False, location='args') args = parser.parse_args() purge = True if args['purge'] is not None: if args['purge'].lower() == 'false': purge = False elif args['purge'].lower() == 'true': purge = True else: abort(400) data = request.get_json() status = StatusCore(test_id=data['test_id'], test_type=data['type'], status=data['status'], details=data['details']) if purge: purge_result = status.purge() status.save_and_update() status = prep_status(status) return jsonify(result='Success', status=status, purge=purge_result)
def test_get_last(self): from core.Status import Status from core.Test import Test from core.Base import Base test_id = str(uuid.uuid4()) test_status1 = 'FAILURE' details = {'browser': random.choice(['Firefox', 'Chrome'])} test_type = str(uuid.uuid4()) status1 = Status(test_id, test_type, test_status1, details=details) status1.save_and_update() at = Base().get_all(Test.collection, {}) assert at.count() == 1 assert at[0]['test_id'] == test_id test_status2 = 'SUCCESS' status2 = Status(test_id, test_type, test_status2, details=details) status2.save_and_update() at = Base().get_all(Test.collection, {}) assert at.count() == 1 Base().upsert_by_id(Status.collection, bson.ObjectId(status1._id), {Status._on: datetime.datetime.now() - datetime.timedelta(seconds=3)}) Base().upsert_by_id(Status.collection, bson.ObjectId(status2._id), {Status._on: datetime.datetime.now() - datetime.timedelta(seconds=3)}) sl = Status(test_id).get_last() assert sl._status == 'SUCCESS' assert sl._test_id == test_id at = Base().get_all(Test.collection, {}) assert at.count() == 1 assert at[0]['last_seen'] > Status(base_id=status2._id).get()._on + datetime.timedelta(seconds=1)
def test_index(self): from core.Index import Index from core.Status import Status from core.TestType import TestType from core.Base import Base test_id = str(uuid.uuid4()) test_status = 'SUCCESS' test_type = str(uuid.uuid4()) field1 = 'browser' field2 = 'environment' TestType(test_type, doc_fields_to_index=[field1, field2]).save() details1 = {field1: 'Firefox', field2: 'master'} status1 = Status(test_id, test_type, test_status, details=details1) status1.save() details2 = {field1: 'Chrome', field2: 'master'} status2 = Status(test_id, test_type, test_status, details=details2) status2.save() rv = self.app_test_type.get('/test/test_types/{0}/indexes/{1}'.format(str(uuid.uuid4()), field1)) assert rv.status_code == 404 rv = self.app_test_type.get('/test/test_types/{0}/indexes/{1}'.format(test_type, str(uuid.uuid4()))) assert rv.status_code == 404 rv = self.app_test_type.get('/test/test_types/{0}/indexes/{1}'.format(test_type, field1)) assert rv.status_code == 200 res = json.loads(rv.data.decode('utf-8')) assert res['index']['field'] == field1 assert res['index']['type'] == test_type assert sorted(res['index']['values']) == sorted(['Firefox', 'Chrome'])
def test_get(test_id): test = TestCore.get_one({TestCore._test_id:test_id}) if test is None: abort(404) statuses = {} lastStatuses = StatusCore.list(query_filter={StatusCore._test_id: test._test_id, StatusCore._last: True}, sort=[(StatusCore._on, Base.DESC)]) if len(lastStatuses) != 0: statuses['last_status'] = lastStatuses[0] lastSuccess = StatusCore.list(query_filter={StatusCore._test_id: test._test_id, StatusCore._status: 'SUCCESS'}, sort=[(StatusCore._on, Base.DESC)]) if len(lastSuccess) != 0: statuses['last_status_success'] = lastSuccess[0] lastFailure = StatusCore.list(query_filter={StatusCore._test_id: test._test_id, StatusCore._status: 'FAILURE'}, sort=[(StatusCore._on, Base.DESC)]) if len(lastFailure) != 0: statuses['last_status_failure'] = lastFailure[0] test = prep_test(test, statuses) return test
def test_get_purge(self): from core.Status import Status from core.TestType import TestType from core.Base import Base test_id = str(uuid.uuid4()) test_status = 'SUCCESS' test_type = str(uuid.uuid4()) field1 = 'browser' field2 = 'environment' tt = TestType(test_type, doc_fields_to_index=[field1, field2]) tt.save() details1 = {field1: 'Firefox', field2: 'master'} status1 = Status(test_id, test_type, test_status, details=details1) status1.save() rv = self.app_test_type.get('/test/test_types/{0}/purge'.format(str(uuid.uuid4()))) assert rv.status_code == 404 rv = self.app_test_type.get('/test/test_types/{0}/purge'.format(test_type)) assert rv.status_code == 200 res = json.loads(rv.data.decode('utf-8')) assert res['result'] == 'Success' assert res['purge']['action'] == TestType._default_purge['action'] assert res['purge']['condition'] == TestType._default_purge['condition'].to_dict()
def test_list_page(self): from core.Status import Status from core import Base test_id1 = str(uuid.uuid4()) test_status = 'FAILURE' details = {'browser': random.choice(['Firefox', 'Chrome'])} test_type = str(uuid.uuid4()) nb = 10 statuses = {} for i in range(nb): status = Status(test_id1, test_type, test_status, details=details) status.save() Base.Base().upsert_by_id(Status.collection, bson.ObjectId(status._id), {Status._on: datetime.datetime.now() - datetime.timedelta(seconds=nb - i + 1)}) statuses[i] = status ast = Status.list(sort=[('on', Base.ASC)], page=1, nb_item=2) assert len(ast) == 2 assert ast[0].to_dict() == Status(base_id=statuses[2]._id).get().to_dict() assert ast[1].to_dict() == Status(base_id=statuses[3]._id).get().to_dict() ast = Status.list(sort=[('on', Base.DESC)], page=2, nb_item=3) assert len(ast) == 3 assert ast[0].to_dict() == Status(base_id=statuses[3]._id).get().to_dict() assert ast[1].to_dict() == Status(base_id=statuses[2]._id).get().to_dict() assert ast[2].to_dict() == Status(base_id=statuses[1]._id).get().to_dict()
def test_remove(self): from core.Status import Status from core.Base import Base test_id1 = str(uuid.uuid4()) test_status1 = random.choice(['SUCCESS', 'FAILURE']) test_type = str(uuid.uuid4()) details = {'browser': random.choice(['Firefox', 'Chrome'])} status1 = Status(test_id1, test_type, test_status1, details=details, last=True) status1.save() test_id2 = str(uuid.uuid4()) test_status2 = random.choice(['SUCCESS', 'FAILURE']) status2 = Status(test_id2, test_type, test_status2, details=details, last=True) status2.save() status2.remove() ast = Base().get_all(Status.collection, {}) assert ast.count() == 1 assert ast[0]['test_id'] == test_id1
def test_purge(self): from core.Status import Status from core.Base import Base test_id = str(uuid.uuid4()) test_status1 = 'FAILURE' details = {'browser': random.choice(['Firefox', 'Chrome'])} test_type = str(uuid.uuid4()) status1 = Status(test_id, test_type, test_status1, details=details) status1.save_and_update() ast = Base().get_all(Status.collection, {}) Base().upsert_by_id(Status.collection, bson.ObjectId(status1._id), {Status._on: datetime.datetime.now() - datetime.timedelta(days=8)}) ast = Base().get_all(Status.collection, {}) test_id2 = str(uuid.uuid4()) test_status2 = 'SUCCESS' status2 = Status(test_id2, test_type, test_status2, details=details) status2.save_and_update() test_status3 = 'SUCCESS' status3 = Status(test_id, test_type, test_status3, details=details) status3.save_and_update() res = status3.purge() assert res['nb_removed'] == 1 ast = Base().get_all(Status.collection, {}) assert ast.count() == 2 assert sorted([str(st['_id']) for st in ast]) == sorted([status2._id, status3._id])
def test_should_i_run_default(self): from core.Status import Status test_id = str(uuid.uuid4()) test_status1 = 'FAILURE' details = {'browser': 'Firefox'} test_type = str(uuid.uuid4()) status1 = Status(test_id, test_type, test_status1, details=details) status1.save_and_update() rv = self.app_test.get('/test/tests/{0}/run'.format(test_id)) assert rv.status_code == 200 res = json.loads(rv.data.decode('utf-8')) assert res['result'] == 'Success' assert res['run'] == False test_id2 = str(uuid.uuid4()) test_status2 = 'SUCCESS' status2 = Status(test_id2, test_type, test_status2, details=details) status2.save_and_update() rv = self.app_test.get('/test/tests/{0}/run'.format(test_id)) assert rv.status_code == 200 res = json.loads(rv.data.decode('utf-8')) assert res['result'] == 'Success' assert res['run'] == False test_status3 = 'SUCCESS' test_type = str(uuid.uuid4()) status3 = Status(test_id, test_type, test_status3, details=details) status3.save_and_update() rv = self.app_test.get('/test/tests/{0}/run'.format(test_id)) assert rv.status_code == 200 res = json.loads(rv.data.decode('utf-8')) assert res['result'] == 'Success' assert res['run'] == True rv = self.app_test.get('/test/tests/{0}/run'.format(str(uuid.uuid4()))) assert rv.status_code == 200 res = json.loads(rv.data.decode('utf-8')) assert res['result'] == 'Success' assert res['run'] == True rv = self.app_test.get('/test/tests/{0}/run/default'.format(test_id)) assert rv.status_code == 200 res = json.loads(rv.data.decode('utf-8')) assert res['result'] == 'Success' assert res['run'] == True
def test_should_i_run_default(self): from core.Status import Status test_id = str(uuid.uuid4()) test_status1 = 'FAILURE' details = {'browser': random.choice(['Firefox', 'Chrome'])} test_type = str(uuid.uuid4()) status1 = Status(test_id, test_type, test_status1, details=details) status1.save_and_update() res = Status(test_id).should_i_run() assert res == False test_id2 = str(uuid.uuid4()) test_status2 = 'SUCCESS' status2 = Status(test_id2, test_type, test_status2, details=details) status2.save_and_update() res = Status(test_id).should_i_run() assert res == False test_status3 = 'SUCCESS' status3 = Status(test_id, test_type, test_status3, details=details) status3.save_and_update() res = Status(test_id).should_i_run() assert res == True
def test_get(self): from core.Status import Status test_id = str(uuid.uuid4()) test_status = 'FAILURE' details = {'browser': random.choice(['Firefox', 'Chrome'])} test_type = str(uuid.uuid4()) status = Status(test_id, test_type, test_status, details=details) status.save() status_jid = Status(base_id=status._id) status_get = status_jid.get() assert status_get.to_dict() == status.to_dict()
def test_index(self): from core.Index import Index from core.Status import Status from core.TestType import TestType from core.Base import Base test_id = str(uuid.uuid4()) test_status = random.choice(['SUCCESS', 'FAILURE']) test_type = str(uuid.uuid4()) field1 = 'browser' field2 = 'environment' details1 = {field1: 'Firefox'} TestType(test_type, doc_fields_to_index=[field1, field2]).save() status1 = Status(test_id, test_type, test_status, details=details1) details2 = {field1: 'Chrome'} status2 = Status(test_id, test_type, test_status, details=details2) details3 = {field2: 'master'} status3 = Status(test_id, test_type, test_status, details=details3) #Index.index(status1) status1.save() ast = Base().get_all(Index.collection, {}) assert ast.count() == 1 assert ast[0]['type'] == test_type assert ast[0]['field'] == field1 assert ast[0]['values'] == ['Firefox'] #Index.index(status2) status2.save() ast = Base().get_all(Index.collection, {}) assert ast.count() == 1 assert sorted(ast[0]['values']) == sorted(['Chrome', 'Firefox']) #Index.index(status3) status3.save() ast = Base().get_all(Index.collection, {}) assert ast.count() == 2 ast = Base().get_all(Index.collection, {'field': 'browser'}) assert ast.count() == 1 assert sorted(ast[0]['values']) == sorted(['Chrome', 'Firefox']) ast = Base().get_all(Index.collection, {'field': 'environment'}) assert ast.count() == 1 assert ast[0]['values'] == ['master']
def test_update_last(self): from core.Status import Status from core.Base import Base test_id1 = str(uuid.uuid4()) test_status1 = random.choice(['SUCCESS', 'FAILURE']) test_type = str(uuid.uuid4()) details = {'browser': random.choice(['Firefox', 'Chrome'])} status1 = Status(test_id1, test_type, test_status1, details=details, last=True) status1.save() test_id2 = str(uuid.uuid4()) test_status2 = random.choice(['SUCCESS', 'FAILURE']) status2 = Status(test_id2, test_type, test_status2, details=details, last=True) status2.save() st = Base().get_one(Status.collection, {}) assert st['last'] == True status2.update_last() ast = Base().get_all(Status.collection, {'last': True}) assert ast.count() == 1 st = Base().get_one(Status.collection, {'test_id': test_id2}) assert st['last'] == False
def test_list(self): from core.Status import Status from core import Base test_id1 = str(uuid.uuid4()) test_status = 'FAILURE' details = {'browser': random.choice(['Firefox', 'Chrome'])} test_type = str(uuid.uuid4()) status1 = Status(test_id1, test_type, test_status, details=details) status1.save() Base.Base().upsert_by_id(Status.collection, bson.ObjectId(status1._id), {Status._on: datetime.datetime.now() - datetime.timedelta(seconds=1)}) test_id2 = str(uuid.uuid4()) status2 = Status(test_id2, test_type, test_status, details=details) status2.save() ast = Status.list(sort=[('on', Base.DESC)]) assert len(ast) == 2 assert ast[0].to_dict() == Status(base_id=status2._id).get().to_dict() assert ast[1].to_dict() == Status(base_id=status1._id).get().to_dict() ast = Status.list(sort=[('on', Base.ASC)]) assert len(ast) == 2 assert ast[0].to_dict() == Status(base_id=status1._id).get().to_dict() assert ast[1].to_dict() == Status(base_id=status2._id).get().to_dict()
def test_save(self): from core.Status import Status from core.Test import Test from core.Index import Index from core.TestType import TestType from core.Base import Base test_id = str(uuid.uuid4()) test_status = random.choice(['SUCCESS', 'FAILURE']) test_type = str(uuid.uuid4()) details = {'browser': random.choice(['Firefox', 'Chrome'])} status = Status(test_id, test_type, test_status, details=details) TestType(test_type, doc_fields_to_index=['browser']).save() now = datetime.datetime.now() status.save() ast = Base().get_all(Status.collection, {}) assert ast.count() == 1 assert ast[0]['test_id'] == test_id assert ast[0]['status'] == test_status assert ast[0]['details'] == details assert ast[0]['type'] == test_type assert ast[0]['on'] < now + datetime.timedelta(seconds=1) at = Base().get_all(Test.collection, {}) assert at.count() == 1 assert at[0]['test_id'] == test_id assert at[0]['type'] == test_type assert at[0]['last_seen'] < ast[0]['on'] + datetime.timedelta(seconds=1) st = Base().get_one(Index.collection, {}) assert st['type'] == test_type assert st['field'] == 'browser' assert st['values'] == [details['browser']] st = Base().get_one(TestType.collection, {}) assert st['type'] == test_type assert st['doc_fields'] == ['browser'] test_id = str(uuid.uuid4()) test_status = 'TO_RERUN' test_type = str(uuid.uuid4()) status = Status(test_id, test_type, test_status) now = datetime.datetime.now() status.save() st = Base().get_one(Status.collection, {'test_id': test_id}) assert st['status'] == 'CUSTOM' assert st['details']['original_status'] == test_status
def test_save_and_update(self): from core.Status import Status from core.Base import Base test_id = str(uuid.uuid4()) test_status1 = 'FAILURE' test_type = str(uuid.uuid4()) details = {'browser': random.choice(['Firefox', 'Chrome'])} status1 = Status(test_id, test_type, test_status1, details=details) status1.save_and_update() st = Base().get_one(Status.collection, {}) assert st['last'] == True test_status2 = 'SUCCESS' status2 = Status(test_id, test_type, test_status2, details=details) status2.save_and_update() ast = Base().get_all(Status.collection, {}) assert ast.count() == 2 ast = Base().get_all(Status.collection, {'last': False}) assert ast.count() == 1 assert ast[0]['status'] == 'FAILURE' ast = Base().get_all(Status.collection, {'last': True}) assert ast.count() == 1 assert ast[0]['status'] == 'SUCCESS'
def test_should_i_run_custom_all(self): from core.Status import Status from core.TestType import TestType test_type = str(uuid.uuid4()) tt = TestType(test_type) tt.add_run_type('default', 'ALL', {'field': 'Status._status', 'operator': 'EQUAL', 'value': 'SUCCESS'}) tt.save() test_id = str(uuid.uuid4()) test_status1 = 'FAILURE' details = {'browser': random.choice(['Firefox', 'Chrome'])} status1 = Status(test_id, test_type, test_status1, details=details) status1.save_and_update() res = Status(test_id).should_i_run() assert res == False test_id2 = str(uuid.uuid4()) test_status2 = 'SUCCESS' status2 = Status(test_id2, test_type, test_status2, details=details) status2.save_and_update() res = Status(test_id).should_i_run() assert res == False test_status3 = 'SUCCESS' status3 = Status(test_id, test_type, test_status3, details=details) status3.save_and_update() res = Status(test_id).should_i_run() assert res == False res = Status(test_id2).should_i_run() assert res == True status2 = Status(test_id2, test_type, test_status2, details=details) status2.save_and_update() res = Status(test_id2).should_i_run() assert res == True
def get(self): parser = reqparse.RequestParser() parser.add_argument('test_id', type=str, help='test ID', required=False, location='args') parser.add_argument('status', type=str, help='status', required=False, location='args') parser.add_argument('type', type=str, help='test type', required=False, location='args') parser.add_argument('field', type=str, help='details field name', required=False, location='args') parser.add_argument('value', type=str, help='details field value', required=False, location='args') for i_field in range(10): parser.add_argument('field{0}'.format(i_field), type=str, help='details field name', required=False, location='args') parser.add_argument('value{0}'.format(i_field), type=str, help='details field value', required=False, location='args') parser.add_argument('nb_status', type=int, help='number of status to return, by default 100', required=False, location='args', default=100) parser.add_argument('page', type=int, help='page to return', required=False, location='args', default=0) args = parser.parse_args() query_filter = {} query = {'page': args['page'], 'nb_status': args['nb_status']} if args['test_id'] is not None: if args['test_id'][0] == '!': query_filter[StatusCore._test_id] = {'$ne': args['test_id'][1:]} else: query_filter[StatusCore._test_id] = args['test_id'] query['test_id'] = args['test_id'] if args['status'] is not None: if args['status'][0] == '!': query_filter[StatusCore._status] = {'$ne': args['status'][1:]} else: query_filter[StatusCore._status] = args['status'] query['status'] = args['status'] if args['type'] is not None: if args['type'][0] == '!': query_filter[StatusCore._type] = {'$ne': args['type'][1:]} else: query_filter[StatusCore._type] = args['type'] query['type'] = args['type'] if args['field'] is not None and args['value'] is not None: if args['value'][0] == '!': query_filter[StatusCore._details + '.' + args['field']] = {'$ne': args['value'][1:]} else: query_filter[StatusCore._details + '.' + args['field']] = args['value'] query['field'] = args['field'] query['value'] = args['value'] i_field = 1 next_field = True while next_field: if args['field{0}'.format(i_field)] is not None and args['value{0}'.format(i_field)] is not None: if args['value{0}'.format(i_field)][0] == '!': query_filter[StatusCore._details + '.' + args['field{0}'.format(i_field)]] = {'$ne': args['value{0}'.format(i_field)][1:]} else: query_filter[StatusCore._details + '.' + args['field{0}'.format(i_field)]] = args['value{0}'.format(i_field)] query['field{0}'.format(i_field)] = args['field{0}'.format(i_field)] query['value{0}'.format(i_field)] = args['value{0}'.format(i_field)] i_field += 1 else: next_field = False statuses = StatusCore.list(query_filter=query_filter, sort=[(StatusCore._on, Base.DESC)], page=args['page'], nb_item=args['nb_status']) statuses_preped = [prep_status(status) for status in statuses] pagination = {'_links': {}} if statuses.count() > len(statuses): add_link_or_expand(pagination, 'self', 'statuses', **query) if args['page'] < statuses.count() // args['nb_status']: query['page'] = args['page'] + 1 add_link_or_expand(pagination, 'next', 'statuses', **query) if args['page'] > 0: query['page'] = args['page'] - 1 add_link_or_expand(pagination, 'prev', 'statuses', **query) query['page'] = 0 add_link_or_expand(pagination, 'first', 'statuses', **query) query['page'] = statuses.count() // args['nb_status'] add_link_or_expand(pagination, 'last', 'statuses', **query) return jsonify(result='Success', statuses=statuses_preped, count=len(statuses), total=statuses.count(), pagination=pagination['_links'])
def test_repr_getter_setter(self): from core.Status import Status test_id = str(uuid.uuid4()) test_status = random.choice(['SUCCESS', 'FAILURE']) test_type = str(uuid.uuid4()) status = Status(test_id, test_type, test_status) assert '{0}'.format(status) == '<Status {0} ({1}) : {2} on the {3}>'.format(test_id, test_type, test_status, None) assert status.to_dict() == {'test_id': test_id, 'status': test_status, 'type': test_type} status._on = datetime.datetime.now() status._last = True status._details = {'browser': random.choice(['Firefox', 'Chrome'])} assert status.to_dict()['on'] == status._on.replace(microsecond=0) assert status.to_dict()['last'] == status._last assert status.to_dict()['details'] == status._details status2 = status.from_dict(status.to_dict()) assert status2.to_dict() == status.to_dict()
def get(self, test_id): status = StatusCore(test_id=test_id) status.add_unknown_if_none_exist() result = status.purge() return jsonify(result='Success', purge=result)