public async Task GetLatestEarlierPublishedFundingVersionFromVersion()
        {
            string  fundingStreamId             = NewRandomString();
            string  fundingPeriodId             = NewRandomString();
            decimal version                     = NewRandomInteger();
            string  groupTypeIdentifier         = NewRandomString();
            string  groupTypeIdentifierValue    = NewRandomString();
            ModelsGroupingReason groupingReason = NewRandomGroupingReason();

            PublishedFundingVersion expectedLatestEarlierDocument = NewPublishedFundingVersion();

            GivenTheLatestEarlierDocument(@"SELECT
                              TOP 1 *
                        FROM publishedFundingVersion p
                        WHERE p.documentType = 'PublishedFundingVersion'
                        AND StringToNumber(CONCAT(Tostring(p.content.majorVersion), '.', Tostring(p.content.minorVersion))) < @version
                        AND p.content.fundingStreamId = @fundingStreamId
                        AND p.content.fundingPeriod.id = @fundingPeriodId
                        AND p.content.organisationGroupTypeIdentifier = @groupTypeIdentifier
                        AND p.content.organisationGroupIdentifierValue = @groupTypeIdentifierValue
                        AND p.content.groupingReason = @groupingReason
                        AND p.deleted = false
                        ORDER BY p.content.majorVersion DESC, p.content.minorVersion DESC",
                                          expectedLatestEarlierDocument,
                                          ("@fundingPeriodId", fundingPeriodId),
                                          ("@fundingStreamId", fundingStreamId),
                                          ("@version", version),
                                          ("@groupTypeIdentifier", groupTypeIdentifier),
                                          ("@groupTypeIdentifierValue", groupTypeIdentifierValue),
                                          ("@groupingReason", groupingReason.ToString()));

            PublishedFundingVersion actualLatestEarlierDocument = await _repository.GetLatestEarlierPublishedFundingVersionFromVersion(fundingStreamId,
                                                                                                                                       fundingPeriodId,
                                                                                                                                       version,
                                                                                                                                       groupTypeIdentifier,
                                                                                                                                       groupTypeIdentifierValue,
                                                                                                                                       groupingReason);

            actualLatestEarlierDocument
            .Should()
            .BeSameAs(expectedLatestEarlierDocument);
        }
        public async Task GetLatestEarlierPublishedFundingVersion()
        {
            string fundingStreamId              = NewRandomString();
            string fundingPeriodId              = NewRandomString();
            long   timeStamp                    = NewRandomTimeStamp();
            string groupTypeIdentifier          = NewRandomString();
            string groupTypeIdentifierValue     = NewRandomString();
            ModelsGroupingReason groupingReason = NewRandomGroupingReason();

            PublishedFundingVersion expectedLatestEarlierDocument = NewPublishedFundingVersion();

            GivenTheLatestEarlierDocument(@"SELECT
                              TOP 1 *
                        FROM publishedFundingVersion p
                        WHERE p.documentType = 'PublishedFundingVersion'
                        AND p._ts < @sinceTimeStamp                        
                        AND p.content.fundingStreamId = @fundingStreamId
                        AND p.content.fundingPeriod.id = @fundingPeriodId
                        AND p.content.organisationGroupTypeIdentifier = @groupTypeIdentifier
                        AND p.content.organisationGroupIdentifierValue = @groupTypeIdentifierValue
                        AND p.content.groupingReason = @groupingReason
                        AND p.deleted = false
                        ORDER BY p._ts DESC",
                                          expectedLatestEarlierDocument,
                                          ("@fundingPeriodId", fundingPeriodId),
                                          ("@fundingStreamId", fundingStreamId),
                                          ("@sinceTimeStamp", timeStamp),
                                          ("@groupTypeIdentifier", groupTypeIdentifier),
                                          ("@groupTypeIdentifierValue", groupTypeIdentifierValue),
                                          ("@groupingReason", groupingReason.ToString()));

            PublishedFundingVersion actualLatestEarlierDocument = await _repository.GetLatestEarlierPublishedFundingVersion(fundingStreamId,
                                                                                                                            fundingPeriodId,
                                                                                                                            timeStamp,
                                                                                                                            groupTypeIdentifier,
                                                                                                                            groupTypeIdentifierValue,
                                                                                                                            groupingReason);

            actualLatestEarlierDocument
            .Should()
            .BeSameAs(expectedLatestEarlierDocument);
        }
        public async Task GetLatestEarlierPublishedProviderVersionByStatus()
        {
            string fundingStreamId         = NewRandomString();
            string fundingPeriodId         = NewRandomString();
            long   timeStamp               = NewRandomTimeStamp();
            string providerId              = NewRandomString();
            PublishedProviderStatus status = new RandomEnum <PublishedProviderStatus>();

            PublishedProviderVersion expectedLatestEarlierDocument = NewPublishedProviderVersion();

            GivenTheLatestEarlierDocument(@"SELECT
                              TOP 1 *
                        FROM publishedProviderVersion p
                        WHERE p.documentType = 'PublishedProviderVersion'
                        AND p._ts < @sinceTimeStamp
                        AND p.content.fundingStreamId = @fundingStreamId
                        AND p.content.fundingPeriodId = @fundingPeriodId
                        AND p.content.providerId = @providerId
                        AND p.content.status = @status
                        AND p.deleted = false
                        ORDER BY p._ts DESC",
                                          expectedLatestEarlierDocument,
                                          ("@fundingPeriodId", fundingPeriodId),
                                          ("@fundingStreamId", fundingStreamId),
                                          ("@sinceTimeStamp", timeStamp),
                                          ("@providerId", providerId),
                                          ("@status", status));

            PublishedProviderVersion actualLatestEarlierDocument = await _repository.GetLatestEarlierPublishedProviderVersion(fundingStreamId,
                                                                                                                              fundingPeriodId,
                                                                                                                              timeStamp,
                                                                                                                              providerId,
                                                                                                                              status);

            actualLatestEarlierDocument
            .Should()
            .BeSameAs(expectedLatestEarlierDocument);
        }
        public async Task GetLatestEarlierPublishedProviderVersionByStatusFromVersion()
        {
            string  fundingStreamId        = NewRandomString();
            string  fundingPeriodId        = NewRandomString();
            decimal version                = NewRandomInteger();
            string  providerId             = NewRandomString();
            PublishedProviderStatus status = new RandomEnum <PublishedProviderStatus>();

            PublishedProviderVersion expectedLatestEarlierDocument = NewPublishedProviderVersion();

            GivenTheLatestEarlierDocument(@"SELECT
                              TOP 1 *
                        FROM publishedProviderVersion p
                        WHERE p.documentType = 'PublishedProviderVersion'
                        AND StringToNumber(CONCAT(Tostring(p.content.majorVersion), '.', Tostring(p.content.minorVersion))) < @version
                        AND p.content.fundingStreamId = @fundingStreamId
                        AND p.content.fundingPeriodId = @fundingPeriodId
                        AND p.content.providerId = @providerId
                        AND p.content.status = @status
                        AND p.deleted = false
                        ORDER BY p.content.majorVersion DESC, p.content.minorVersion DESC",
                                          expectedLatestEarlierDocument,
                                          ("@fundingPeriodId", fundingPeriodId),
                                          ("@fundingStreamId", fundingStreamId),
                                          ("@version", version),
                                          ("@providerId", providerId),
                                          ("@status", status));

            PublishedProviderVersion actualLatestEarlierDocument = await _repository.GetLatestEarlierPublishedProviderVersionFromVersion(fundingStreamId,
                                                                                                                                         fundingPeriodId,
                                                                                                                                         version,
                                                                                                                                         providerId,
                                                                                                                                         status);

            actualLatestEarlierDocument
            .Should()
            .BeSameAs(expectedLatestEarlierDocument);
        }