Exemple #1
0
    def test_save_inserts_new_data(self):
        """ save should create rows for new data, and new data only"""
        # run SUT
        cursor = get_cursor()
        service_id = save(
            cursor,
            'service',                 # table name
            ['service_name'],          # unique columns
            ['service_name'],          # all columns
            ('mock_service_name',),    # values to insert
        )
        cursor.close()

        # save returns an integer id for the service table
        self.assertTrue(isinstance(service_id, int))

        # save creates one row per call with the saved data
        cursor = get_cursor()
        cursor.execute(
            "SELECT service_id, service_name\n" + \
            "FROM service\n" + \
            "WHERE service_name='mock_service_name'"
        )
        result = cursor.fetchall()
        cursor.close()
        self.assertEqual(len(result), 1)
        self.assertEqual(result[0][1], 'mock_service_name')
Exemple #2
0
    def test_save_is_idempotent(self, text):
        """ when we save stuff more than once it should only get in once """
        # run SUT
        cursor = get_cursor()
        service_id = save(
            cursor,
            'service',                 # table name
            ['service_name'],          # unique columns
            ['service_name'],          # all columns
            (text,),                   # values to insert
        )
        cursor.execute(
            "SELECT service_id, service_name\n" + \
            "FROM service\n" + \
            "WHERE service_name=%s",
            (text,),
        )
        first_result = cursor.fetchall()
        cursor.close()

        cursor = get_cursor()
        service_id = save(
            cursor,
            'service',                 # table name
            ['service_name'],          # unique columns
            ['service_name'],          # all columns
            (text,),                   # values to insert
        )
        cursor.execute(
            "SELECT service_id, service_name\n" + \
            "FROM service\n" + \
            "WHERE service_name=%s",
            (text,),
        )
        second_result = cursor.fetchall()

        # confirm that the second save did not change the result
        self.assertEqual(first_result, second_result)
Exemple #3
0
    def test_save_idempotent_by_uniqueness(
            self,
            commit_hash,
            image_name,
    ):
        """
        iterations should be idempotent, even though no all of their
        columns are included in uniqueness constraints

        anything with the null character will be truncated at the null character
        because this is what postgresql does.

        """

        # set up
        cursor = get_cursor()
        # save a service so we can have a branch
        service_id = save(
            cursor,
            'service',          # table name
            ['service_name'],   # unique columns
            ['service_name'],   # all columns
            ('mock_service',),   # values to insert
        )
        # save a branch so we have a stable branch id to save
        # iterations against
        branch_id = save(
            cursor,
            'branch',                                               # table
            ['branch_name', 'merge_base_commit_hash', 'deleted_dt'],# unique
            ['branch_name', 'merge_base_commit_hash', 'service_id'],# columns
            ('mock_branch', 'mock_base_commit_hash' ,  service_id ),# values
        )

        """
        If someone tries to save a build name to a branch, commit pair that
        exists the existing build name will be used and the new build name
        being saved will be ignored.
        """
        # because this test is run many times with random data we need
        # to handle two cases. One when we have a branch, commit pair
        # that we have not saved and one when we have saved that
        # branch, commit pair

        # check to see if we have this branch, commit pair saved
        cursor.execute(
            "select image_name\n" + \
            "  from iteration\n" + \
            " where commit_hash=%s and branch_id=%s",
            (commit_hash, branch_id),
        )
        result = cursor.fetchall()
        # if we have it,
        if result:
            # we should only have one
            self.assertEqual(len(result), 1)
            # and we expect it to stay the same
            expected_image_name = result[0][0]
        else:
            # otherwise we expect the new name (up to a null character) to be
            # saved
            expected_image_name = image_name.split('\x00')[0]

        # run SUT
        # save an iteration twice
        iteration_id = save(
            cursor,
            'iteration',                                  # table name
            ['commit_hash', 'branch_id'],                 # unique columns
            ['commit_hash', 'branch_id', 'image_name'],   # all columns
            ( commit_hash ,  branch_id ,  image_name ),   # values to insert
        )
        iteration_id = save(
            cursor,
            'iteration',                                  # table name
            ['commit_hash', 'branch_id'],                 # unique columns
            ['commit_hash', 'branch_id', 'image_name'],   # all columns
            ( commit_hash ,  branch_id ,  image_name ),   # values to insert
        )
        # also with a different image name
        iteration_id = save(
            cursor,
            'iteration',                                  # table name
            ['commit_hash', 'branch_id'],                 # unique columns
            ['commit_hash', 'branch_id', 'image_name'],   # all columns
            ( commit_hash ,  branch_id , 'different?'),   # values to insert
        )
        cursor.close()
        cursor = get_cursor()

        # confirm assumptions
        # dispite three saves and one having a different image name
        cursor.execute(
            "select commit_hash, branch_id, image_name\n" + \
            "  from iteration\n" + \
            " where commit_hash=%s and branch_id=%s",
            (commit_hash, branch_id),
        )
        results = cursor.fetchall()
        cursor.close()
        # we should only have one result for this commit_hash
        self.assertEqual(len(results), 1)
        # and it should be the image name we expect (not the most recent one)
        self.assertEqual(results[0][2], expected_image_name)