k8s.io/test-infra@v0.0.0-20240520184403-27c6b4c223d8/kettle/model_test.py (about)

     1  #!/usr/bin/env python3
     2  
     3  # Copyright 2017 The Kubernetes Authors.
     4  #
     5  # Licensed under the Apache License, Version 2.0 (the "License");
     6  # you may not use this file except in compliance with the License.
     7  # You may obtain a copy of the License at
     8  #
     9  #     http://www.apache.org/licenses/LICENSE-2.0
    10  #
    11  # Unless required by applicable law or agreed to in writing, software
    12  # distributed under the License is distributed on an "AS IS" BASIS,
    13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  # See the License for the specific language governing permissions and
    15  # limitations under the License.
    16  
    17  import unittest
    18  
    19  import model
    20  
    21  
    22  class ModelTest(unittest.TestCase):
    23      def setUp(self):
    24          self.db = model.Database(':memory:')
    25  
    26      def test_insert_build(self):
    27          self.db.insert_build('/some/dir/123', {'timestamp': 123}, {'timestamp': 140})
    28          self.assertEqual(self.db.get_existing_builds('/some/'), {('dir', '123')})
    29  
    30      def test_insert_junits(self):
    31          self.db.insert_build('/some/dir/123', {'timestamp': 123}, {'timestamp': 140})
    32          self.assertEqual(self.db.get_builds_missing_junit(), [(1, '/some/dir/123')])
    33  
    34          self.db.insert_build_junits(1, {'/some/dir/123/foo.txt': 'example'})
    35          self.assertEqual(self.db.test_results_for_build('/some/dir/123/'), ['example'])
    36  
    37      def test_incremental(self):
    38          def add_build(num):
    39              self.db.insert_build(
    40                  '/some/dir/%d' % num, {'timestamp': 123}, {'timestamp': 150})
    41  
    42          def expect(builds):
    43              rows = []
    44              have = set()
    45              for rowid, path, _started, _finished in self.db.get_builds():
    46                  rows.append(rowid)
    47                  have.add(int(path[path.rindex('/', 0, -1) + 1:]))
    48              self.assertEqual(have, builds)
    49              self.db.insert_emitted(rows)
    50              self.assertEqual(have, builds)
    51  
    52          add_build(1)
    53  
    54          expect({1})
    55          expect(set())
    56  
    57          add_build(2)
    58          add_build(3)
    59  
    60          expect({2, 3})
    61  
    62          self.db.reset_emitted()
    63  
    64          expect({1, 2, 3})
    65          expect(set())
    66  
    67  
    68  if __name__ == '__main__':
    69      unittest.main()