Print Page | Close Window

Use a non-persisted connection string with unit tests

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=2258
Printed Date: 25-Jan-2026 at 3:48am


Topic: Use a non-persisted connection string with unit tests
Posted By: sgillbee
Subject: Use a non-persisted connection string with unit tests
Date Posted: 25-Oct-2010 at 9:45am
I'm trying to test my model in a unit test suite. For obvious reasons, I don't actually care about persistence... just that the operations occur against the local cache (which is automatically discarded when I dispose the context object, right?).
 
The problem is... I can't figure out what the connection string for this use-case should be? Before constructing the container/context I've tried pre-configuring DevForce this way:
            string modelKey = "DirectoryServiceContainer";
            string modelName = "DirectoryService";
            string modelAssembly = "Halliburton.PEToolkit.DataServices.Directory.Model.dll";
            var connectionString = string.Format("metadata={0}\\{1}.csdl|{0}\\{1}.ssdl|{0}\\{1}.msl", modelAssembly, modelName);
            EdmKeyElement edmKey = new EdmKeyElement();
            edmKey.Name = modelKey;
            edmKey.Connection = connectionString;
            IdeaBladeConfig.Instance.EdmKeys.Add(edmKey);
Note that this application has *no* app.config... it's just a unit test assembly. I'm relying on the unit test framework to pre-configure the environment.
 
I've verified with a custom IDataSourceKeyResolver that the correct key is getting retrieved (by manually constructing a DefaultKeyResolver inside GetKey() and inspecting and logging the resulting ClientEdmKey). The error seems to be that the connection string is not valid. This is the actual connection string after string formatting:
container/context I've tried pre-configuring DevForce this way:            
metadata=Halliburton.PEToolkit.DataServices.Directory.Model.dll\\DirectoryService.csdl|Halliburton.PEToolkit.DataServices.Directory.Model.dll\\DirectoryService.ssdl|Halliburton.PEToolkit.DataServices.Directory.Model.dll\\DirectoryService.msl
The exception keeps saying this is an invalid connection string and that a provider must be supplied. What provider? I've got no underlying persistence layer in this case. I've tried appending "; provider=" to the end of this string with no luck.
 
Help! I'm getting very frustrated. Been at this more than a day now :(



Replies:
Posted By: smi-mark
Date Posted: 25-Oct-2010 at 9:50am
I think that this topic will help with what you are trying to achieve:

http://drc.ideablade.com/xwiki/bin/view/Documentation/AdvPersist_CompositionContext


Posted By: sgillbee
Date Posted: 25-Oct-2010 at 10:18am
Thanks for the reply. This is helpful for unit tests, but I have broader need of being able to specify programatically a connection string with no backing store. Does anyone know the correct format for this?


Posted By: sbelini
Date Posted: 25-Oct-2010 at 4:42pm
Hi sgillbee,
 
If you don't have a database to connect to, you don't really need a connection string.
 
As smi-mark suggested, the CompositionContext might be what you are looking for.
 
You "connect" to a context when the entity manager is constructed (and therefore manage your "connections" programatically). You can have multiple entity managers "connected" to different contexts (you will need to implement a ICompositionContextResolver):
 
 
  public class CompositionContextResolver : BaseCompositionContextResolver {
    public static CompositionContext Mock1 = CompositionContext.Default
     .WithGenerator(typeof(MockEntityServerQueryInterceptor))
     .WithGenerator(typeof(MockEntityServerSaveInterceptor));
     .WithName("Mock1");
    public static CompositionContext Mock2 = CompositionContext.Default
     .WithGenerator(typeof(MockEntityServerQueryInterceptor))
     .WithGenerator(typeof(MockEntityServerSaveInterceptor))
     .WithName("Mock2");
  }
 
  [PartNotDiscoverable]
  public class MockEntityServerQueryInterceptor : EntityServerQueryInterceptor {
    protected override bool ExecuteQuery() {
      throw new InvalidOperationException();
    }
  }
  [PartNotDiscoverable]
  public class MockEntityServerSaveInterceptor : EntityServerSaveInterceptor {
    protected override bool ExecuteSave() {
      throw new InvalidOperationException();
    }
  }
 
###############################################################
 
  var em1 = new NorthwindIBEntityManager(compositionContextName: CompositionContextResolver.Mock1.Name);
  var em2 = new NorthwindIBEntityManager(compositionContextName: CompositionContextResolver.Mock2.Name);
 
 
 
Could you clarify why you need the connection string, so I can better understand your issue and try to find any existing alternative solutions? (what are you trying to connect to?) 
 
Regards,
   Silvio.
 



Print Page | Close Window