New Posts New Posts RSS Feed: Use a non-persisted connection string with unit tests
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Use a non-persisted connection string with unit tests

 Post Reply Post Reply
Author
sgillbee View Drop Down
Newbie
Newbie
Avatar

Joined: 15-Oct-2010
Posts: 4
Post Options Post Options   Quote sgillbee Quote  Post ReplyReply Direct Link To This Post Topic: Use a non-persisted connection string with unit tests
    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 :(
Back to Top
smi-mark View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 24-Feb-2009
Location: Dallas, Texas
Posts: 343
Post Options Post Options   Quote smi-mark Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
sgillbee View Drop Down
Newbie
Newbie
Avatar

Joined: 15-Oct-2010
Posts: 4
Post Options Post Options   Quote sgillbee Quote  Post ReplyReply Direct Link To This Post 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?
Back to Top
sbelini View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 13-Aug-2010
Location: Oakland
Posts: 786
Post Options Post Options   Quote sbelini Quote  Post ReplyReply Direct Link To This Post 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.
 
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down