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);
        }