How to test values which retrieve from db in jhipster - mysql

I am new to jhipster. I am trying to implement a test method to test values which are retrieved from mysql db. When I am trying to execute "gradlew test" command it will fail the relevant test case by saying "java.lang.AssertionError: No value at JSON path "$.[0].id". And I have added H2 Console db table values manually. My test method as follows.
#Test
#Transactional
public void getAllPlayersNS() throws Exception {
// Get all the playersNList
restPlayersNMockMvc.perform(get("/api/players-ns?sort=id,asce"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
.andExpect(jsonPath("$.[0].id").value(1));
}
Where I went wrong?

As far as I know the integration tests in JHipster use H2 in memory so the changes you made using H2 console are probably not used by these tests and so our table is empty (this is what your failed assertion means) because they were stored in H2 on disk in target/h2db folder (if you chose this option at project generation).
So either, your test should create players using PlayerNSRepository or you should add a Liquibase migration that loads them from CSV (look at users.csv) and restrict it to H2 db and maybe using test Liquibase context.

Related

For django testing, how do I use keepdb with mariadb

I have a database with a lot of nonmanaged tables which I'm using for a django app. For testing I'm wanting to use the --keepdb option so that I don't have to repopulate these tables every time. I'm using MariaDB for my database. If I don't use the keepdb option everything works fine, the test database gets created and destroyed properly.
But when I try to run the test keeping the database:
$ python manage.py test --keepdb
I get the following error:
Using existing test database for alias 'default'...
Got an error creating the test database: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'CREATE DATABASE IF NOT EXISTS test_livedb ;\n SET sql_note' at line 2")
I assume that this is an issue with a different syntax between MariaDB and MySQL. Is there anyway to get the keepdb option to work with MariaDB?
thanks very much!
For what it's worth: This bug was introduced in Django 2.0.0 and fixed in Django 2.1.3 (https://code.djangoproject.com/ticket/29827)
Two things - check out Factory Boy (for creating test data) and I would suggest checking out Pytest as well. With non-managed tables, the issue I think you'll run into is that (at least in my experience) django won't create them in the test environment and you end up running into issues because there is no migration file to create those tables (since they're unmanaged). Django runs the migration files when creating the test environment.
With Pytest you can run with a --nomigrations flag which builds your test database directly off the models (thus creating the tables you need for your unmanaged models).
If you combine Pytest and Factory Boy you should be able to come up with the ability to setup your test data so it works as expected, is repeatable and testable without issue.
I actually approach it like this (slightly hacky, but it works with our complex setup):
On my model:
class Meta(object):
db_table = 'my_custom_table'
managed = getattr(settings, 'UNDER_TEST', False)
I create the UNDER_TEST variable in settings.py like this:
# Create global variable that will tell if our application is under test
UNDER_TEST = (len(sys.argv) > 1 and sys.argv[1] == 'test')
That way - when the application is UNDER_TEST the model is marked as managed (and Pytest will create the appropriate DB table). Then FactoryBoy handles putting all my test data into that table (either in setUp of the test or elsewhere) so I can test against it.
That's my suggestion - others might have something a little more clear or cleaner.

Unable to connect with multiple mysql schema in spring batch admin

I am new to spring batch and batch admin. I stuck in a scenario where i want to use multiple datasource. i.e. One for batch meta-data and business schema(application tables).
I am using below code in my batch-mysql.properties file.
For batch matadata tables
batch.jdbc.driver=com.mysql.jdbc.Driver
batch.jdbc.url=jdbc:mysql://localhost:3306/batch
batch.jdbc.user=root
batch.jdbc.password=root
batch.jdbc.testWhileIdle=true
batch.jdbc.validationQuery=SELECT 1
batch.drop.script=classpath:/org/springframework/batch/core/schema-drop-mysql.sql
batch.schema.script=classpath:/org/springframework/batch/core/schema-mysql.sql
batch.business.schema.script=classpath*:business-schema-mysql.sql
For application schema
db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/applicationschema
db.user=root
db.password=root
if i remove below line of code
batch.business.schema.script=classpath*:business-schema-mysql.sql
then i am getting an exception that above property couldn't found.
if keep as it is then it is creating application table in batch matadata schema.
Just don't supply any value to the property batch.business.schema.script. Try to leave it like shown below :
batch.business.schema.script=
Also, if you don't want to drop and create batch meta tables every time your application is started, you should set batch.data.source.init=false in your batch properties file.
EDIT : This is how my batch-oracle.properties file looks like :
batch.database.incrementer.class=org.springframework.jdbc.support.incrementer.OracleSequenceMaxValueIncrementer
batch.isolationlevel=READ_COMMITTED
batch.business.schema.script=
batch.data.source.init=false
batch.job.service.reaper.interval=6000
batch.schema.script=classpath:/org/springframework/batch/core/schema-drop-oracle10g.sql
batch.jdbc.url=jdbc:oracle:thin:#localhost:1521:mydb
batch.table.prefix=BATCH_
batch.lob.handler.class=org.springframework.jdbc.support.lob.DefaultLobHandler
batch.verify.cursor.position=true
batch.jdbc.validationQuery=SELECT 1 FROM dual
batch.jdbc.password=mypassword
batch.jdbc.testWhileIdle=false
batch.jdbc.user=user
batch.jdbc.pool.size=5
batch.drop.script=classpath:/org/springframework/batch/core/schema-drop-oracle10g.sql

INSERT throw errors

I'm developing an API with Nodejs and I have a create project endpoint and a create patient endpoint. Both of them work if I create the database tables with mySql.
But I need to run first create project which creates some tables to be used by create user endpoint. The problem comes when I call user endpoint and throws this:
** Connection to project psapp_demo successfully.
*** Error: ER_NO_SUCH_TABLE: Table 'psapp_demo_proposa58_pr.displayer_patient' doesn't exist
But database psapp_demo_proposa58_pr and table displayer_patient exist and are accessible throw PHPmyadmin.
Code for creating a new patient is based on a promise which is catching the error:
Services.user.create(connection,req,userId,req.body,clinicId)
.then((userId) => { req.userId = userId; next() })
.fail((err) => next(err))
Anyway, database created with a project endpoint also does not work with phpmyadmin so problem seems to realy on the database, not the code. This is the error where you can see I can even navigate with phpmyadmin throw that databases but are not located when I try to insert something:
A SELECT operation throws empty which is right. With a SELECT, there is not error.
Do you know why could it be happening? Thanks

Creating classes in pimcore

I'm new to the whole pimcore thing. I am trying to play around and create classes. The issue is, I am not able to create more than 1 class, and in the database it is nameless, so when I try to create another class, it also tries to store it in the database with no name, which ends up showing an SQL error saying that there is a duplicate entry. Any ideas what the reason behind this could be?
I installed pimcore on an nginx server, I am trying to create classes by choosing Settings->Objects->Classes and then "Add Class", creating the first class was ok, I entered a name for the class and it was successfully added, however the name field in the corresponding database entry is empty, as in empty string ' '. So, when I try to add another class and pimcore attempts to store it in the table "classes", it returns an error saying that it would be a duplicate entry since they both are nameless, i.e. the name entered isn't added. The following error is what I managed to find using developer tools, could be helpful.
[WARN] Unable to parse the JSON returned by the server
minified_javascript_core_f5757da….js?_dc=3708:5684 Error: You're trying to decode an invalid JSON String:
Fatal error: Call to a member function hasChilds() on null in /var/www/html/pimproject/pimcore/modules/admin/controllers/DocumentController.php on line 59
at new Ext.Error (http://192.10.0.0/pimcore/static6/js/lib/ext/ext-all.js?_dc=3708:22:27054)
at Function.Ext.apply.raise (http://192.10.0.10/pimcore/static6/js/lib/ext/ext-all.js?_dc=3708:22:27447)
at Object.Ext.raise (http://192.10.0.10/pimcore/static6/js/lib/ext/ext-all.js?_dc=3708:22:27594)
at Object.Ext.JSON.me.decode (http://192.10.0.10/pimcore/static6/js/lib/ext/ext-all.js?_dc=3708:22:385102)
at Ext.define.onProxyLoad (http://192.10.0.10/website/var/tmp/minified_javascript_core_f5757da9fa29d5bf13e6aa5058eff9f7.js?_dc=3708:5641:28)
at Ext.cmd.derive.triggerCallbacks (http://192.10.0.10/pimcore/static6/js/lib/ext/ext-all.js?_dc=3708:22:594533)
at Ext.cmd.derive.setCompleted (http://192.10.0.10/pimcore/static6/js/lib/ext/ext-all.js?_dc=3708:22:594231)
at Ext.cmd.derive.setException (http://192.10.0.10/pimcore/static6/js/lib/ext/ext-all.js?_dc=3708:22:594444)
at Ext.cmd.derive.process (http://192.10.0.10/pimcore/static6/js/lib/ext/ext-all.js?_dc=3708:22:593638)
at Ext.cmd.derive.processResponse (http://192.10.0.10/pimcore/static6/js/lib/ext/ext-all.js?_dc=3708:22:648303)
Just reinstall Pimcore.
It can be some composer or submodules error.
I strongly recommend for the first installation to run Demo project https://github.com/pimcore/demo not Skeleton, especially if you are using Docker. Later, when you will get the feeling of Pimcore, feel free to install Skeleton or any other project.
Pimcore is stable working for years. If you had some problems before -- nowadays, it is stable.

Raw SQL within Pylons app that uses SQLAlchemy?

I've inherited a Pylons app that uses SQLAlchemy. I know nothing about SQLAlchemy and very little about Pylons :)
I need to run some raw SQL from within the app. The SQLAlchemy currently seems to be working in the following way (example code):
import myapp.model as model
model.Session.query(model.KeyValue) # existing code
.join(model.Key)
.filter(model.Key.name == name)
).count() == 0, name
How do I get it to run raw SQL? I see that I need an execute() statement, but how exactly do I run it? The following both fail:
model.Session.execute('create table hello_world;')
model.Connection.execute("""
create table hello_world;
""")
What's the magic invocation? There's no reference to a Connection object in the existing code, and I'm not sure how to create one.
You can obtain connection that is used by Session by using its connection method:
connection = model.Session.connection()
Then you can issue your query:
connection.execute('create table hello_world;')
Note that in Pylons model.Session is not a sqlalchemy.orm.session.Session class. It's an instance of sqlalchemy.orm.scoping.ScopedSession. That's how it's created in model.meta module:
Session = scoped_session(sessionmaker())
My first impulse is to recommend trying the execute() method of an instance of Connection, instead of the execute() method of the class itself as your example code suggests that you're doing.
Are you working off of the Pylons Book examples ?