Grails Finance 0.6 Testing

This entry is part of 15 in the series Grails Finance

Testing, testing, 123 …

Test a little, code a little, go home, eat a little, sleep a little. This is the lifestyle recommended by the League of Agile Methodology Experts. That and wearing t-shirts with agile slogans printed on them and matching pants. I would like to do that today, the testing part I mean.

Integration tests

Grails make it easy to create integration tests. Let’s do that real quick

1
grails create-integration-test SimulationIntegration

I modified the test to create and delete a Simulation object on the fly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import grails.test.*
 
class SimulationIntegrationTests extends GrailsUnitTestCase {
    protected void setUp() {
        super.setUp()
    }
 
    protected void tearDown() {
        super.tearDown()
    }
 
    void testSaveAndDelete() {
		def simulation = new Simulation(name : "sim1", description : " first test")
		assertNotNull simulation.save()
 
		def foundSimulation = Simulation.get(simulation.id)
		foundSimulation.delete()
 
		assertFalse Simulation.exists(foundSimulation.id)
    }
}

You can run the test with

1
grails test-app -integration

Another integraion test

The problem with having many domain classes is that you need many tests. LAME practicioners use the word little, because otherwise testing gets boring. If you have the code already as I do, then it becomes test a little, then test a little more, which is not recommended at all. Sigh, here is another test

1
grails create-integration-test SessionIntegration

The test adds a few sessions to the simulation. Note the use of the dynamic addTo method.

1
2
3
4
5
6
7
8
9
10
11
12
    void testFirstSimulation() {
		def simulation = new Simulation(name : "sim1", description : " first test").save()
		def session1 = new Session(description : "First session")
		simulation.addToSessions( session1 )
		def session2 = new Session(description : "Second session")
		simulation.addToSessions( session2 )
		assertEquals 2, Simulation.get(simulation.id).sessions.size()
 
		def foundSimulation = Simulation.get(simulation.id)	
		def descriptions = foundSimulation.sessions.collect { it.description }
		assertEquals( ['First session', 'Second session'], descriptions.sort())
    }

Intermission

Let’s get something to drink. I have been researching trading concepts and approaches lately. So this is my list of popular ideas:

  • Trend following – determine when a market is trending and follow it until the trend vanishes.
  • Hedging – lower investment risk with short selling and/or derivatives.
  • Fundamental analysis
  • Value investing – invest based on intrinsic value determination.
  • Band trading – trade within a band.
  • Spreads – buying and selling different legs of an option or future.
  • Arbitrage – taking advantage of price differences between different markets.

OK, back to the main program.

Yet another integration test

Yes, more tests, but being LAME is not easy.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
    void testSessionRunCreation() {
		def simulation = new Simulation(name : "sim1", description : " first test").save()
		def session = new Session(description : "First session")
 
		def run1 = new SessionRun()
		def param1 = new StrategyParameter( name : "param1", 
			description : "first parameter", val : "1.0")
		run1.addToParameters( param1 )
		session.addToRuns( run1 )
		assertEquals 1, session.runs.size()
 
		def run2 = new SessionRun()
		def param2 = new StrategyParameter( name : "param2", 
			description : "second parameter", val : "2.0")
		run2.addToParameters( param2 )
		session.addToRuns( run2 )
		assertEquals 2, session.runs.size()
 
		simulation.addToSessions( session )
 
		assertEquals ([ 'param1' , 'param2' ],	
			simulation.sessions.runs.parameters*.name.flatten().sort())
 
		def foundSimulation = Simulation.get(simulation.id)
		assertEquals ([ 'param1' , 'param2' ],	
			foundSimulation.sessions.runs.parameters*.name.flatten().sort())
    }

Random links of interest

I would like to apologize for the boring stuff above. Please accept these links of interest as compensation.

This entry was posted in programming and tagged , , . Bookmark the permalink.