Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 315657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:18:42+00:00 2026-05-12T08:18:42+00:00

The following is some background info on this post. You can just skip to

  • 0

The following is some background info on this post. You can just skip to the question if you like:

In this excellent article (http://ayende.com/Blog/archive/2009/04/28/nhibernate-unit-testing.aspx) the author contends that “When using NHibernate we generally want to test only three things:
1) that properties are persisted,
2) that cascade works as expected
3) that queries return the correct result.
-) that mapping is complete & correct (implied)

My take is that he goes on to say that SQLite can and should be the unit test tool of choice to do all of the above. It should be noted that the author seems to be one of more experienced and skilled NHib developers out there, and though he doesn’t expressly say so in the article, he implies in a question later that the domain can and should be handling some of SQLite’s shortcomings.

QUESTION:

How do you use SQLite to test cascade relationships, especially given that it does not check foreign key constraints. How do you test your model to make sure foreign key constraints will not be a db issue.

Here are some units tests I came up with to test cascade behavior. The model is simply a Department that can have zero to many StaffMembers, with cascade set to NONE.

    [Test]
    public void CascadeSaveIsNone_NewDepartmentWithFetchedStaff_CanSaveDepartment()
    {
        _newDept.AddStaff(_fetchedStaff);
        Assert.That(_newDept.IsTransient(), Is.True);

        _reposDept.SaveOrUpdate(_newDept);
        _reposDept.DbContext.CommitChanges();

        Assert.That(_newDept.IsTransient(), Is.False);
    }

    [Test]
    public void CascadeSaveIsNone_NewDepartmentWithFetchedStaff_CannotSaveNewStaff()
    {
        _newDept.AddStaff(_newStaff);
        Assert.That(_newDept.IsTransient(), Is.True);
        Assert.That(_newStaff.IsTransient(), Is.True);

        _reposDept.SaveOrUpdate(_newDept);
        _reposDept.DbContext.CommitChanges();

        Assert.That(_newDept.IsTransient(), Is.False);
        Assert.That(_newStaff.IsTransient(), Is.True);
    }

    [Test]
    public void CascadeDeleteIsNone_FetchedDepartmentWithFetchedStaff_Error()
    {
        _fetchedDept.AddStaff(_fetchedStaff);
        _reposDept.SaveOrUpdate(_fetchedDept);
        _reposStaff.DbContext.CommitChanges();

        _reposDept.Delete(_fetchedDept);
        var ex = Assert.Throws<GenericADOException>(() => _reposDept.DbContext.CommitChanges());

        Console.WriteLine(ex.Message);
        Assert.That(ex.Message, Text.Contains("could not delete:"));
        Console.WriteLine(ex.InnerException.Message);
        Assert.That(ex.InnerException.Message, Text.Contains("The DELETE statement conflicted with the REFERENCE constraint"));
    }

    [Test]
    public void Nullable_NewDepartmentWithNoStaff_CanSaveDepartment()
    {
        Assert.That(_newDept.Staff.Count(), Is.EqualTo(0));

        var fetched = _reposDept.SaveOrUpdate(_newDept);
        Assert.That(fetched.IsTransient(), Is.EqualTo(false));
        Assert.That(fetched.Staff.Count(), Is.EqualTo(0));
    }

The third test, “.._FetchedDepartmentWithFetchedStaff_Error” works against Sql Server, but not SQLite since the latter does not check foreign key constraints.

Here are tests for the other side of the relationship; a StaffMember can have one Department, with cascade set to NONE.

    [Test]
    public void CascadeSaveIsNone_NewStaffWithFetchedDepartment_CanSaveStaff()
    {
        _newStaff.Department = _fetchedDept;
        _reposStaff.SaveOrUpdate(_newStaff);
        _reposStaff.DbContext.CommitChanges();

        Assert.That(_newStaff.Id, Is.GreaterThan(0));
    }

    [Test]
    public void CascadeSaveIsNone_NewStaffWithNewDepartment_Error()
    {
        _newStaff.Department = _newDept;
        Assert.That(_newStaff.IsTransient(), Is.True);

        var ex = Assert.Throws<PropertyValueException>(() => _reposStaff.SaveOrUpdate(_newStaff));
        Console.WriteLine(ex.Message);
        Assert.That(ex.Message, Text.Contains("not-null property references a null or transient value"));
    }

    [Test]
    public void CascadeDeleteIsNone_FetchedStaffWithFetchedDepartment_DeletesTheStaff_DoesNotDeleteTheDepartment()
    {
        _newStaff.Department = _fetchedDept;
        _reposStaff.SaveOrUpdate(_newStaff);
        _reposStaff.DbContext.CommitChanges();

        _reposStaff.Delete(_newStaff);
        Assert.That(_reposStaff.Get(_newStaff.Id), Is.Null);
        Assert.That(_reposDept.Get(_fetchedDept.Id), Is.EqualTo(_fetchedDept));
    }

    [Test]
    public void NotNullable_NewStaffWithUnknownDepartment_Error()
    {
        var noDept = new Department("no department");
        _newStaff.Department = noDept;

        var ex = Assert.Throws<PropertyValueException>(() => _reposStaff.SaveOrUpdate(_newStaff));
        Console.WriteLine(ex.Message);
        Assert.That(ex.Message, Text.Contains("not-null property references a null or transient"));
    }

    [Test]
    public void NotNullable_NewStaffWithNullDepartment_Error()
    {
        var noDept = new Department("no department");
        _newStaff.Department = noDept;

        var ex = Assert.Throws<PropertyValueException>(() => _reposStaff.SaveOrUpdate(_newStaff));
        Console.WriteLine(ex.Message);
        Assert.That(ex.Message, Text.Contains("not-null property references a null or transient"));
    }

These tests succed against Sql Server and SQLite. Can I trust the SQLite tests? Are these worthwhile tests?

Cheers,
Berryl

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T08:18:42+00:00Added an answer on May 12, 2026 at 8:18 am

    As i understand the article it is about testing the NHibernate Mapping. In my opinion this has nothing to do with db related issues but with testing the nhibernate attributes you set in your mapping. There is no need to assert that it is not possible to create invalid data: you only have to proof that your code creates the desired result and/or checks the things you want to check. You can test cascade, cascade-delete and delete-orphan. whatever you want the way you do it in the tests working with sqlite. But the third test tries to test the constraint, which is nothing nhibernate worries about.

    If you want to test your Db contraints you should indeed use your production db and not sqlite. You could do it with or without hibernate but this has nothing to do with your mapping.
    If you on the other hand really want a workarround for your Foreign Key tests with SQLite you could try to use this foreign_key_trigger_generator. I haven’t tried but it seems to generate before-insert-triggers that assure the existance of the referenced Pk.
    Maybe you could write a comment wheather this tool is usefull.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Are the following assumptions valid for this code? I put some background info under
I am mainly from a Sql Server background, and following some issues with getting
i have the following code which switches some fullscreen-background-images (fadeOut, fadeIn). setInterval(function() { var
Background: In some PHP code dealing with a 3rd party web-service, the following code
Some background: I setup six blogs this week, all using Wordpress 2.92, installed with
Here's some background info. I have three MySQL tables (all InnoDB). The first table
Some background first. I'm employing the following trick to prevent undesired browser caching of
First, some background info: I am making a compiler for a school project. It
Some background info; LanguageResource is the base class LanguageTranslatorResource and LanguageEditorResource inherit from LanguageResource
I know this is probably a simple question but my mind just isn't working

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.