In meteor, is there any way to stub a collection for unit testing without using hwillson:stub-collections? Using this package is unfortunately not an option due to package constraints (mongo#1.0.11), and neither is using a test Mongodb instance.
Related
I am writing a CRUD app using Quarkus and Mongo, and thus am using a MongoCollection to implement this.
I am utilizing Hibernate Validators for validation to ensure my data is as it should be.
The issue I am running into is that the MongoCollection only provides updates using Bson (collection.updateOne(Bson search, Bson update)), and not for the entire object. This would be fine but keeps me from being able to properly use validators to ensure proper data integrity.
Until I hit this block, my idea for updating was to:
ingest generic json, in the form of ObjectNode and the object's id
get the object to update
Use Jackson's built-in updating features to apply the updates to the object from the given ObjectNode
Validate the resulting state
save the object to Mongo
However, this doesn't work when I can't update the whole object at once. Am I attacking this from the right angle? I've found a lot on how to do updates, but not a lot related to validation. I also see that I can specify on the Mongo side validation rules, but as I am fairly 'hands off' when using Mongo in this way, so needing to apply special Bson validation isn't ideal.
Is it possible for me to just re-insert the updated object to Mongo using `collection.insertOne(object)`? this assumes that the object would have the same `_id` as the original. Would this update the object as intended, or are there side effects?
Edit:: no, it is not. Mongo throws an error for duplicate keys.
Found it, what I wanted was collection.findOneAndReplace()
I'm currently developing a Meteor application which will be based on different packages (maybe created by different developers).
My question is if there's a meteor'ish way to namespace collections so that they won't collide with other available collections.
For example, I have a package which uses the collection with the name 'todos' and another one which may also include a collection with the name 'todos'. Is there a way to namespace these collections (e.g. with their package-Name prepended)?
I've found out that there's an option for namespacing in MongoDB with a '.':
http://docs.mongodb.org/manual/faq/developers/#what-is-a-namespace-in-mongodb
Is this also the best way to namespace collections in Meteor?
Coming to Meteor with a Rails background, I can remember that you can create namespaced Engines in rails (which also creates namespaced database-Tables). Is there something similar in Meteor?
The closest I came was with this Issue I've found:
https://github.com/CollectionFS/Meteor-cfs-gridfs/issues/6
Thanks in advance!
Using a period in the collection name is a perfectly fine namespacing scheme for Meteor collections just as it is if you were using MongoDB alone.
var TodoCollection = Meteor.Collection('packageName.todos');
Packages such as CollectionFS use this technique to avoid collection name collisions.
I'm working on my first Scala application, where we use an ActiveRecord style to retrieve data from MongoDB.
I have models like User and Category, which all have a companion object that uses the trait:
class MongoModel[T <: IdentifiableModel with CaseClass] extends ModelCompanion[T, ObjectId]
ModelCompanion is a Salat class which provide common MongoDB crud operations.
This permits to retrieve data like this:
User.profile(userId)
I never had any experience with this ActiveRecord query style. But I know Rails people are using it. And I think I saw it on Play documentation (version 1.2?) to deal with JPA.
For now it works fine, but I want to be able to run integration tests on my MongoDB.
I can run an "embedded" MongoDB with a library. The big deal is that my host/port configuration are actually kind of hardcoded on the MongoModel class which is extended by all the model companions.
I want to be able to specify a different host/port when I run integration tests (or any other "profile" I could create in the future).
I understand well dependency injection, using Spring for many years in Java, and the drawbacks of all this static stuff in my application. I saw that there is now a scala-friendly way to configure a Spring application, but I'm not sure using Spring is appropriate in Scala.
I have read some stuff about the Cake pattern and it seems to do what I want, being some kind of typesafe, compile-time-checked spring context.
Should I definitely go to the Cake pattern, or is there any other elegant alternative in Scala?
Can I keep using an ActiveRecord style or is it a total anti-pattern for testability?
Thanks
No any static references - using Cake pattern you got 2 different classes for 2 namespaces/environments, each overriding "host/port" resource on its own. Create a trait containing your resources, inherit it 2 times (by providing actual information about host/port, depending on environment) and add to appropriate companion objects (for prod and for test). Inside MongoModel add self type that is your new trait, and refactor all host/port references in MongoModel, to use that self type (your new trait).
I'd definitely go with the Cake Pattern.
You can read the following article with show an example of how to use the Cake Pattern in a Play2 application:
http://julien.richard-foy.fr/blog/2011/11/26/dependency-injection-in-scala-with-play-2-it-s-free/
What is the best practice to work efficiently with MongoDB and PHPUnit? What should (or could) I use to mock objects that access MongoDB? PHPUnit Mocker, Mockery, Phrophecy, Phactory?
If you look at mocking data for SQL databases, there are lots of opinions here.
Some people suggest using an in-memory SQL database.
Some people just mock the ORM calls and assume that the ORM to DB portion is tested.
Some people just use a "local" DB for unit testing and just ignore the whole "mocking" concept.
Given the lack of consensus on SQL, it's even less likely that you will find consensus on the new DBs like MongoDB.
I think there are some important details to consider here.
Are you using some form of ORM / ODM? Just the driver directly?
Are you trying to mock all communications with the DB? Are you trying to mock the ODM?
If you are just trying to mock communications to DB, then the ideal solution is a "fake" implementation of the MongoDB driver. This is probably a lot of work as the driver was never written with "mockability" in mind.
If you have an ODM, then you can simply mock the ODM calls and assume the ODM is doing its job. Ideally the ODM should provide some mockable interface, but this is not always the case.
Again, this answer comes back down to what you're really planning to test and what you consider as a good unit test. Unfortunately, most of these products are still very new so there is very little guidance in this space.
Phactory provides direct support for mocking MongoDB.
Edit: Phactory is no longer maintained. However, I've found a new project called php-mongomock that seems to solve this problem:
<?php
use Helmich\MongoMock\MockCollection;
$collection = new MockCollection();
$collection->createIndex(['foo' => 1]);
$documentId = $collection->insertOne(['foo' => 'bar'])->insertedId();
$collection->updateOne(['_id' => $documentId], ['$set' => ['foo' => 'baz']]);
I have a Spring-based system that uses Hibernate Search 3.4 (on top of Hibernate 3.5.4). Integration tests are managed by Spring, with #Transactional annotation. At the moment test data (entities that are to be indexed) is loaded by Liquibase script, we use it's Spring integration. It's very inconvenient to manage.
My new solution is to have test data defined as Spring beans and wire them as Resources, by name. This part works.
I tried to have these beans persisted and indexed in setUp method of my test cases (and in test methods themselves) but I failed. They get into DB fine but I can't get them indexed. I tried calling index() on FullTextEntityManager (with flushToIndexes), I tried createIndexer().startAndWait().
What else can I do?
Or may be there is some better option of testing HS?
Thank You in advance
My new solution is to have test data defined as Spring beans and wire
them as Resources, by name. This part works.
sounds like a strange setup for a unit test. To be honest I am not quote sure how you do this.
In Search itself an in memory database (H2) is used together with a Lucene RAM directory. The benefits of such a setup is that it is fast and easy to avoid dependencies between tests.
I tried to have these beans persisted and indexed in setUp method of
my test cases (and in test methods themselves) but I failed. They get
into DB fine but I can't get them indexed.
If automatic indexing is enabled and the persisting of the test data is occurring within an transaction, it should work. A common mistake in combination with Spring is to use the wrong transaction manager. The Hibernate Search forum has a lot of threads around this, for example this one - https://forum.hibernate.org/viewtopic.php?f=9&t=998155. Since you are not giving any concrete configuration and code examples it is hard to give more specific advice.
I tried createIndexer().startAndWait()
that is also a good approach. I would recommend this approach if you want to insert not such a couple of test entities, but a whole set of data. In this case it can make sense to use a framework like dbunit to insert the testdata and then manually index the data. createIndexer().startAndWait() is the right tool for that. Extracting all this loading/persisting/indexing functionality into a common test base class is the way to go. The base class can also be responsible to do all the Spring bootstrapping.
Again, to give more specific feedback you have to refine your question.
I have a complete different approach, when I write any queries, i want to write a complete test suite, but data creation has always been pain(special mention to when test customer gets corrupt and all your test suite breaks.
To solve this I created Random-JPA. It's simple and easy to integrate. The whole idea is you create fresh data and test.
You Can find the full documentation here