Open Closed

abp 7.0: Unit Tests AbpInitializationException SqliteConnection does not support nested transactions. #4281


User avatar
0
a3x created

If you're creating a bug/problem report, please include followings:

  • ABP Framework version: v7.0.0

I updated to abp v7.0.0. I can run my application, however, when running the unit tests I receive following errors randomly for the tests.

Message:  Volo.Abp.AbpInitializationException : An error occurred during the initialize Volo.Abp.Modularity.OnApplicationInitializationModuleLifecycleContributor phase of the module TestProjectDb.TestProjectDbTestBaseModule, TestProjectDb.TestBase, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null: SqliteConnection does not support nested transactions.. See the inner exception for details. ---- System.InvalidOperationException : SqliteConnection does not support nested transactions.

Stack Trace:  ModuleManager.InitializeModules(ApplicationInitializationContext context) AbpApplicationBase.InitializeModules() AbpApplicationWithExternalServiceProvider.Initialize(IServiceProvider serviceProvider) AbpIntegratedTest1.ctor() TestProjectDbTestBase1.ctor() TestProjectDbApplicationTestBase.ctor()

I found https://support.abp.io/QA/Questions/2176/Regarding-Unit-test-issue and replaced the service as described, however, this does not fix my problem. I do understand the problem but dont know how to fix this or what changed between my last v6.0.0 and this version?


15 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Can you debug the unit test method to confirm the MyUnitOfWorkManager is working?

  • User Avatar
    0
    a3x created

    Hey,

    Yes, it does: and it does not throw any exception

    Best regards

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Can you reproduce the problem in a new template project and share it? liming.ma@volosoft.com

  • User Avatar
    0
    a3x created

    Yes, I created a new solution from template, pasted my applicationmodule staff and created a entity. Please find the source here:

    The tests also here randomly fails:

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    ok, I will check it.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Please try to update your AddressesDataSeedContributor.

    using System;
    using System.Threading.Tasks;
    using Volo.Abp.Data;
    using Volo.Abp.DependencyInjection;
    using Volo.Abp.Uow;
    using test04012022.Addresses;
    
    namespace test04012022.Addresses
    {
        public class AddressesDataSeedContributor : IDataSeedContributor, ITransientDependency
        {
            private readonly IAddressRepository _addressRepository;
            private readonly IUnitOfWorkManager _unitOfWorkManager;
    
            public AddressesDataSeedContributor(IAddressRepository addressRepository, IUnitOfWorkManager unitOfWorkManager)
            {
                _addressRepository = addressRepository;
                _unitOfWorkManager = unitOfWorkManager;
            }
    
            public async Task SeedAsync(DataSeedContext context)
            {
                if (await _addressRepository.FindAsync(Guid.Parse("a4821c19-b073-4dd4-94c0-299f3712a7ed")) == null)
                {
                    await _addressRepository.InsertAsync(new Address
                    (
                        id: Guid.Parse("a4821c19-b073-4dd4-94c0-299f3712a7ed"),
                        firstname: "e60f19e1c89147d4958618eecd2e6"
                    ));
                }
    
                if (await _addressRepository.FindAsync(Guid.Parse("87175640-2b9d-4c28-a1c2-b6aceca4969f")) == null)
                {
                    await _addressRepository.InsertAsync(new Address
                    (
                        id: Guid.Parse("87175640-2b9d-4c28-a1c2-b6aceca4969f"),
                        firstname: "b23b24db6d974e7493ef2e1bdb915ed185da359993974b27bd30cb50b0c33c2ea02b782148f845"
                    ));
                }
    
                await _unitOfWorkManager.Current.SaveChangesAsync();
            }
        }
    }
    
    
  • User Avatar
    0
    a3x created

    Hi, Thanks this works for me. However, for my real project, I have a lot of seed methods. Is there any other way than adding the if condition to every insert statement now? In addition: In my project I have tests like following which fail with the same error. What should I do here?

    [Fact]
        public async Task CreateAsync()
        {
            // Arrange
            var input = new CustomerCreateDto
            {
                Name = "bf64c99897834",
                Street = "30b28c8ff57b464d9bc2f5618f15be1c75930a933aa140cb80f614f124bb3f0461f45",
                Postcode = "aa2e0879154b4a2bbf78597283eb7e57",
                City = "5f4242b5aa61476a97c3e5af411cf498521949a2ad2c47edbbbe",
                Country = "c83c88797e63411f882bdd48759e99e87679b076e154472482c6e885b56830cf82abbd257b894cd0a8588a73ad277e3c7c6d",
                VATNo = "d6d31592918a4f0f96ee23ad51a87e0ab5b0f1b49ccb41acbf85136106af9ba445de4a676ed14b4db27c63267679c6d8def4",
                Status = default,
                State = "76d47ce8de444b898aa5565717a4f154c746171559174f87a9fff21f832690097571c7457c7544b3a3041662529c371a881e"
            };
    
            // Act
            var serviceResult = await _customersAppService.CreateAsync(input);
        
    
            // Assert
            var result = await _customerRepository.FindAsync(c => c.Id == serviceResult.Id);
    
            result.ShouldNotBe(null);
            result.Name.ShouldBe("bf64c99897834");
            result.Street.ShouldBe("30b28c8ff57b464d9bc2f5618f15be1c75930a933aa140cb80f614f124bb3f0461f45");
            result.Postcode.ShouldBe("aa2e0879154b4a2bbf78597283eb7e57");
            result.City.ShouldBe("5f4242b5aa61476a97c3e5af411cf498521949a2ad2c47edbbbe");
            result.Country.ShouldBe("c83c88797e63411f882bdd48759e99e87679b076e154472482c6e885b56830cf82abbd257b894cd0a8588a73ad277e3c7c6d");
            result.VATNo.ShouldBe("d6d31592918a4f0f96ee23ad51a87e0ab5b0f1b49ccb41acbf85136106af9ba445de4a676ed14b4db27c63267679c6d8def4");
            result.Status.ShouldBe(default);
            result.State.ShouldBe("76d47ce8de444b898aa5565717a4f154c746171559174f87a9fff21f832690097571c7457c7544b3a3041662529c371a881e");
        }
    
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    However, for my real project, I have a lot of seed methods. Is there any other way than adding the if condition to every insert statement now?

    hi

    There is no good way. we have to query first. like:

    https://github.com/abpframework/abp/blob/08034310b15e9056c019403f500a507e2b4efbdd/modules/openiddict/app/OpenIddict.Demo.Server/EntityFrameworkCore/ServerDataSeedContributor.cs#L28 https://github.com/abpframework/abp/blob/e3e1779de6df5d26f01cdc8e99ac9cbcb3d24d3c/modules/cms-kit/host/Volo.CmsKit.IdentityServer/IdentityServer/IdentityServerDataSeedContributor.cs#L68

  • User Avatar
    0
    a3x created

    Hi,

    ok, bit of a shame that I have to rewrite all the tests now.

    2 more questions:

    1. can you tell when the templates for the suite will be adjusted to match your pattern?
    2. what exactly was changed between the 6.x and the 7.0 that leads to this behavior? Is it due to .NET 7 or what change was there in the abp.io framework?
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer
    1. Is your AddressesDataSeedContributor class generated by the suite?

    2. I think it should be a change brought by EF Core 7.

  • User Avatar
    0
    christophe.baille created

    Hi there,

    I came through here as I got issues with unit tests on our solution since upgrade to ABP 7 as well, annoying as we need the tests to be validated in our CI/CD.

    To make more tests I created a new solution with ABP 7 (Blazor server), then added one entity to generate unit tests.

    I then added the FindAsync method before each Insert in the DataSeedContributor contributor class as you said.

    I still have errors that come up randomly:

    On Application.Tests project, it can be either Create, Delete, Get, GetList. When I use "Run All tests", I have often one of them getting in error with: ---- System.InvalidOperationException : SqliteConnection does not support nested transactions.

    Then, either on other projects: EntityFrameworkCore.DomainTests, EntityFrameworkCore.Tests or EntityFrameworkCore.Samples

    ---- System.NullReferenceException : Object reference not set to an instance of an object.
    

    It's very confusing as sometimes there is no error at all...

    My own solution (not the one I created for tests), have exactly the same types of errors. I was wondering if it was because my own environment, but when I run tests on Azure, I got the same errors as well.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi christophe.baille

    https://support.abp.io/QA/Questions/2176/Regarding-Unit-test-issue

  • User Avatar
    0
    christophe.baille created

    I saw this tickets yes, but as they are 1 year old I did expect that it would have been fixed on a new project with the last ABP version.

    I added this lines:

    Configure<AbpUnitOfWorkDefaultOptions>(options => { options.TransactionBehavior = UnitOfWorkTransactionBehavior.Disabled; });

    Then it works now. Thanks

  • User Avatar
    0
    ageiter created
    1. Is your AddressesDataSeedContributor class generated by the suite?

    I have the same problem as @a3x.

    The test code is generated by the suite (version 7.0.1).

    The tip from @christophe.baille has made it better, but at irregular intervals certain tests still fail. Especially when I run all tests. If I then run the failed test separately, then it works.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    The next 7.x patch version will solve this.

    https://github.com/abpframework/abp/pull/15508

Made with ❤️ on ABP v9.1.0-preview. Updated on November 01, 2024, 05:35