def test_set_state(self):
        desired_state = Scenario.State.FAILED
        initial_state = self.client.scenarios.get_state(
            _(self.scenario_1.name, self.scenario_1.user))
        self.assertNotEqual(initial_state, desired_state)

        self.client.scenarios.set_state(
            _(self.scenario_1.name, self.scenario_1.user), desired_state)

        final_state = self.client.scenarios.get_state(
            _(self.scenario_1.name, self.scenario_1.user))
        self.assertEqual(final_state, desired_state)
    def test_delete_scenario(self):
        self.client.scenarios.get(_(self.scenario_1.name,
                                    self.scenario_1.user))
        self.client.scenarios.delete(
            _(self.scenario_1.name, self.scenario_1.user))

        with self.assertRaises(DMSClientException) as error:
            self.client.scenarios.get(
                _(self.scenario_1.name, self.scenario_1.user))
        exception = error.exception

        self.assertEqual(404, exception.status_code)
    def test_update_fields(self):
        self.client.scenarios.set_fields(
            _(self.scenario_1.name, self.scenario_1.user),
            cpu_time=8399456,
            query='{"query": ""}',
            started_at=datetime(2017, 8, 23, 7, 30, 0),
            ended_at=datetime(2017, 8, 23, 7, 30, 10))
        scenario = self.client.scenarios.get(
            _(self.scenario_1.name, self.scenario_1.user))

        self.assertEqual(scenario.cpu_time, 8399456)
        self.assertEqual(scenario.query, '{"query": ""}')
        self.assertEqual(scenario.started_at, datetime(2017, 8, 23, 7, 30, 0))
        self.assertEqual(scenario.ended_at, datetime(2017, 8, 23, 7, 30, 10))
 def tearDown(self):
     super(ScenarioControllerTestCase, self).tearDown()
     self.client.segments.delete_by_drive_id('TEST-DRIVE-123')
     for scenario in [self.scenario_1, self.scenario_2]:
         try:
             self.client.scenarios.delete(_(scenario.name, scenario.user))
             pass
         except DMSClientException:
             pass
 def test_update_fields_protected(self):
     with self.assertRaises(ValueError) as error:
         self.client.scenarios.set_fields(_(self.scenario_1.name,
                                            self.scenario_1.user),
                                          created_at=datetime(
                                              2017, 8, 23, 7, 30, 0))
     exception = error.exception
     self.assertIn("'created_at' is a protected field of Scenario.",
                   str(exception))
    def test_create_scenario_from_segments(self):
        for i in range(20):
            segment = factories.SegmentFactory(tags=['abcdef'],
                                               drive_id='TEST-DRIVE-123')
            self.client.segments.create(segment)

        query = {'query': {'term': {'tags': 'abcdef'}}}

        segments = self.client.segments.find_by_query(query)
        scenario = Scenario.from_segments(name=self.scenario_2.name,
                                          user=self.scenario_2.user,
                                          query=query,
                                          sensor_versions=[{
                                              'sensor': 'CAM',
                                              'version': 'v1.2'
                                          }],
                                          segments=segments)
        self.client.scenarios.create(scenario)

        scenario_returned = self.client.scenarios.get(
            _(scenario.name, scenario.user))

        self.assertEqual(scenario, scenario_returned)
        self.assertEqual(len(scenario.scenario_segments), 20)
 def test_get_scenario_by_name_user(self):
     s = self.client.scenarios.get(
         _(self.scenario_1.name, self.scenario_1.user))
     self.assertEqual(s, self.scenario_1)
 def test_create_and_get_scenario(self):
     self.client.scenarios.create(self.scenario_2)
     s = self.client.scenarios.get(
         _(self.scenario_2.name, self.scenario_2.user))
     self.assertEqual(s, self.scenario_2)
 def test_get_state_nonexistent(self):
     f = self.client.scenarios.get_state
     self.assertRaises(DMSDocumentNotFoundError, f,
                       _('FAKE_NAME_123', 'FAKE_USER_123'))
 def test_get_state(self):
     state = self.client.scenarios.get_state(
         _(self.scenario_1.name, self.scenario_1.user))
     self.assertEqual(state, Scenario.State.COMPLETED)
 def test_set_state_nonexistent_segment(self):
     f = self.client.scenarios.set_state
     self.assertRaises(DMSDocumentNotFoundError, f,
                       _('FAKE_NAME_123', 'FAKE_USER_123'),
                       Scenario.State.CREATED)
 def test_set_invalid_state(self):
     f = self.client.scenarios.set_state
     self.assertRaises(ValueError, f,
                       _(self.scenario_1.name, self.scenario_1.user),
                       'fake-state')