示例#1
0
 def test_database(self):
     with tempfile.NamedTemporaryFile() as tokens_tempfile:
         tokens_path = Path(tokens_tempfile.name)
         tokens.Tokens(definitions={}, assignments={}).dump(tokens_path)
         db = tokens.TokensDatabase(tokens_path)
         self.assertEqual(
             db.get(),
             tokens.Tokens(definitions={}, assignments={}),
         )
         d = tokens.Tokens.Definition(kind='values', args=['c'])
         pod_id = '00000000-0000-0000-0000-000000000001'
         with db.writing() as t:
             t.add_definition('x', d)
             t.assign('x', pod_id, 'x1')
         self.assertEqual(
             db.get(),
             tokens.Tokens(
                 definitions={'x': d},
                 assignments={
                     'x': [
                         tokens.Tokens.Assignment(pod_id=pod_id,
                                                  name='x1',
                                                  value='c'),
                     ],
                 },
             ),
         )
示例#2
0
 def test_check_invariants(self):
     with self.assertRaises(AssertionError):
         tokens.Tokens(
             definitions={},
             assignments={'invalid-name': []},
         )
     with self.assertRaises(AssertionError):
         tokens.Tokens(
             definitions=self.DEFINITIONS,
             assignments={'no_such_token': []},
         )
     with self.assertRaises(AssertionError):
         tokens.Tokens(
             definitions=self.DEFINITIONS,
             assignments={
                 'x':
                 self.make_assignments(self.POD_ID_1, '1', self.POD_ID_2,
                                       '1'),
             },
         )
     with self.assertRaises(AssertionError):
         tokens.Tokens(
             definitions=self.DEFINITIONS,
             assignments={
                 'y':
                 self.make_assignments(self.POD_ID_1, 'p', self.POD_ID_2,
                                       'p'),
             },
         )
示例#3
0
 def test_dump_and_load(self):
     t = tokens.Tokens(
         definitions=self.DEFINITIONS,
         assignments={
             'x': self.make_assignments(self.POD_ID_1, '0'),
             'y': self.make_assignments(self.POD_ID_1, 'p', self.POD_ID_2,
                                        'q'),
         },
     )
     with tempfile.NamedTemporaryFile() as tokens_tempfile:
         tokens_path = Path(tokens_tempfile.name)
         t.dump(tokens_path)
         self.assertEqual(tokens.Tokens.load(tokens_path), t)
示例#4
0
 def test_abort(self):
     with tempfile.NamedTemporaryFile() as tokens_tempfile:
         tokens_path = Path(tokens_tempfile.name)
         tokens.Tokens(definitions={}, assignments={}).dump(tokens_path)
         db = tokens.TokensDatabase(tokens_path)
         self.assertEqual(
             db.get(),
             tokens.Tokens(definitions={}, assignments={}),
         )
         d = tokens.Tokens.Definition(kind='values', args=['c'])
         pod_id = '00000000-0000-0000-0000-000000000001'
         try:
             with db.writing() as t:
                 t.add_definition('x', d)
                 t.assign('x', pod_id, 'x1')
                 raise RuntimeError
         except RuntimeError:
             pass
         self.assertEqual(
             db.get(),
             tokens.Tokens(definitions={}, assignments={}),
         )
示例#5
0
 def test_unassign(self):
     t = tokens.Tokens(
         definitions=self.DEFINITIONS,
         assignments={
             'x': self.make_assignments(self.POD_ID_1, '0'),
             'y': self.make_assignments(self.POD_ID_1, 'p'),
         },
     )
     t.unassign('x', self.POD_ID_1, 'foo')
     t.check_invariants()
     self.assertEqual(
         t.assignments,
         {'y': self.make_assignments(self.POD_ID_1, 'p')},
     )
示例#6
0
 def test_cleanup(self):
     t = tokens.Tokens(
         definitions=self.DEFINITIONS,
         assignments={
             'x': self.make_assignments(self.POD_ID_1, '0'),
             'y': self.make_assignments(self.POD_ID_1, 'p', self.POD_ID_2,
                                        'q'),
         },
     )
     t.cleanup({self.POD_ID_1})
     t.check_invariants()
     self.assertEqual(
         t.assignments,
         {
             'x': self.make_assignments(self.POD_ID_1, '0'),
             'y': self.make_assignments(self.POD_ID_1, 'p'),
         },
     )
示例#7
0
    def test_assign(self):
        t = tokens.Tokens(definitions=self.DEFINITIONS, assignments={})
        self.assertEqual(t.assignments, {})

        with self.assertRaises(AssertionError):
            t.assign('no_such_token', self.POD_ID_1, 'x1')
        self.assertEqual(t.assignments, {})

        self.assertEqual(t.assign('x', self.POD_ID_1, 'foo'), '0')
        t.check_invariants()
        self.assertEqual(t.assignments, {
            'x': self.make_assignments(self.POD_ID_1, '0'),
        })

        with self.assertRaises(AssertionError):
            t.assign('x', self.POD_ID_2, 'foo')
        self.assertEqual(t.assignments, {
            'x': self.make_assignments(self.POD_ID_1, '0'),
        })