def test_closes_not_active(self, close_mock, open_mock, put_mock): Experiment(name = 'e1', file = 'file1.txt', since = datetime.now(), filters = {'type': '1'}).save() e2 = Experiment(name = 'e2', file = 'file2.txt', since = datetime.now(), filters = {'type': '2'}).save() RecordingPDU.FILES_PURGE_THRESHOLD = 0.1 pdu = RecordingPDU() pdu.process_message({'type': '3', 'id': '0'}) pdu.process_message({'type': '1', 'id': '1'}) pdu.process_message({'type': '2', 'id': '2'}) e2.active = False e2.save() time.sleep(0.3) pdu.process_message({'type': '1', 'id': '3'}) pdu.process_message({'type': '2', 'id': '4'}) pdu.process_message({'type': '3', 'id': '5'}) eq_(3, put_mock.call_count, "2 put expected!") put_mock.assert_any_call(Matcher("file1.txt", lambda o: o.fname), Matcher('1', lambda o: o['type'])) put_mock.assert_any_call(Matcher("file2.txt", lambda o: o.fname), Matcher('2', lambda o: o['type'])) eq_(1, close_mock.call_count, "1 close expected!") close_mock.assert_called_once_with(Matcher("file2.txt", lambda o: o.fname))
def test_inactive_matching_experiment_is_not_returned(self): Experiment.objects.all().delete() e1 = Experiment(name = 'e1', file = 'file4.txt', filters = {'a': 'b'}, active=False) e1.save() matches = Experiment.get_active_experiments_matching({'a': 'b'}) eq_(len(matches), 0)
def test_no_active(self, close_mock, open_mock, put_mock): e = Experiment(name = 'e1', file = 'file2.txt', since = datetime.now()) e.active = False e.save() pdu = RecordingPDU() pdu.process_message({'id': 1}) pdu.process_message({'id': 1}) eq_(0, close_mock.call_count, "no calls expected!") eq_(0, open_mock.call_count, "no calls expected!") eq_(0, put_mock.call_count, "no calls expected!")
def test_get_active_experiments_returns_active_experiment(self): """ Test that get_active_experiments works correctly indeed. """ e = Experiment(name = "e1", file = 'file2.txt', since = datetime.now()) e.save() active = Experiment.get_active_experiments() eq_(active.count(), 1, "There should be exactly one active experiment") e.active = False e.save() active = Experiment.get_active_experiments() eq_(active.count(), 0, "There should be no active experiments")
def test_save_and_fetch(self): """ Test that an experiment can be saved and fetched correctly. """ e = Experiment(name='e1') e.since = datetime.now() e.until = datetime.now() e.file = 'file.txt' e.filters = {} e.name = 'test1' e.save() e2 = Experiment.objects.get(id = e.id) eq_(e2.file, e.file, "File of retrieved experiment should be equal")
def test_get_active_experiments_matching(self): Experiment.objects.all().delete() e1 = Experiment(name = 'e1', file = 'file3.txt', filters = {'a': 'b'}) e1.save() e2 = Experiment(name = 'e2', file = 'file3.txt', filters = {'c': 'd'}) e2.save() matches = Experiment.get_active_experiments_matching({'a': 'b', 'c': 'd'}) ok_(set(matches), set([e1, e2])) matches = Experiment.get_active_experiments_matching({'a': 'b'}) ok_(set(matches), set([e1])) matches = Experiment.get_active_experiments_matching({'c': 'd'}) ok_(set(matches), set([e2])) matches = Experiment.get_active_experiments_matching({'e': 'f'}) eq_(len(matches), 0)
print "%20s %20s %20s" % (e.name, e.file, e.active) elif args.operation == 'start': # See if there is an existing Experiment with that name try: connect('experiments', host=settings.MONGO_SERVER) e = Experiment.objects.get(name=args.name) logger.error("There is already an experiment with the name %s!" % args.name) except DoesNotExist: e = Experiment(name=args.name, filters=json.loads(args.filters), file=args.file, since=datetime.now(), active=True) e.save() except MultipleObjectsReturned: logger.error("There is more than one experiment with name %s! " "Database is corrupted." % args.name) elif args.operation == 'stop': try: connect('experiments', host=settings.MONGO_SERVER) e = Experiment.objects.get(name=args.name) if not e.active: logger.error("The experiment %s is not active!" % args.name) else: e.active = False e.save() except DoesNotExist: logger.error("There is no experiment with name %s!" % args.name)