Skip to content

Commit

Permalink
Added tests to execute documentaton examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
bflorian committed Aug 21, 2012
1 parent 30fe7e4 commit 3a6cf03
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/docs/ref/Examples/Querying for a Single Column.gdoc
Expand Up @@ -6,7 +6,7 @@ Returns [com.netflix.astyanax.model.Column|http://netflix.github.com/astyanax/ja

{code}
def rowKey = "xxx-1"
Column<String> result = astyanaxService.keyspace().prepareQuery("Standard1")
def result = astyanaxService.keyspace().prepareQuery("Standard1")
.getKey(rowKey)
.getColumn("ColumnName1")
.execute()
Expand Down
2 changes: 1 addition & 1 deletion src/docs/ref/Examples/Querying for a slice of columns.gdoc
Expand Up @@ -60,7 +60,7 @@ Returns java.util.Map<String, Long>

{code}
def rowKey = "xxx-1"
def result = astyanaxService.keyspace().prepareQuery("Standard1")
def result = astyanaxService.keyspace().prepareQuery("Counter1")
.getKey(rowKey)
.withColumnRange("ColumnName1","ColumnNameN",false, 100)
.execute()
Expand Down
2 changes: 1 addition & 1 deletion src/docs/ref/Examples/Querying for an entire row.gdoc
Expand Up @@ -44,7 +44,7 @@ Returns java.util.Map<String, Long>

{code}
def rowKey = "xxx-1"
def result = astyanaxService.keyspace().prepareQuery("Standard1")
def result = astyanaxService.keyspace().prepareQuery("Counter1")
.getKey(rowKey)
.execute()
.result
Expand Down
2 changes: 1 addition & 1 deletion src/docs/ref/Examples/Querying for specific columns.gdoc
Expand Up @@ -47,7 +47,7 @@ Returns java.util.Map<String, Long>

{code}
def rowKey = "xxx-1"
def result = astyanaxService.keyspace().prepareQuery("Standard1")
def result = astyanaxService.keyspace().prepareQuery("Counter1")
.getKey(rowKey)
.withColumnSlice("ColumnName1","ColumnName3","ColumnName5")
.execute()
Expand Down
8 changes: 8 additions & 0 deletions test/data/schema.txt
Expand Up @@ -13,5 +13,13 @@ create column family User
];

create column family User_CTR
with comparator = UTF8Type
and default_validation_class = CounterColumnType;

create column family Standard1
with comparator = UTF8Type
and default_validation_class = UTF8Type;

create column family Counter1
with comparator = UTF8Type
and default_validation_class = CounterColumnType;
@@ -0,0 +1,126 @@
/*
* Copyright 2012 ReachLocal Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.reachlocal.grails.plugins.cassandra.test

import org.junit.Test
import static org.junit.Assert.*
import com.netflix.astyanax.connectionpool.OperationResult

/**
* @author: Bob Florian
*/
class ExamplesTests
{
def astyanaxService

@Test
void testInsertingData()
{
def results = processFile("src/docs/ref/Examples/Inserting data.gdoc")
results.each {k,v ->
assertTrue v instanceof OperationResult
}
}

@Test
void testIncrementingCounterColumns()
{
def results = processFile("src/docs/ref/Examples/Incrementing counter columns.gdoc")
}

@Test
void testQueryingForASingleColumn()
{
def results = processFile("src/docs/ref/Examples/Querying for a single column.gdoc")
assertEquals "X", results["Standard Astyanax"]
}

@Test
void testQueryingForASliceOfColumns()
{
def results = processFile("src/docs/ref/Examples/Querying for a slice of columns.gdoc")
}

@Test
void testQueryingForAnEntireRow()
{
def results = processFile("src/docs/ref/Examples/Querying for an entire row.gdoc")
}

@Test
void testQueryingForSpecificColumns()
{
def results = processFile("src/docs/ref/Examples/Querying for specific columns.gdoc")
}

@Test
void testCountingTheNumberOfColumns()
{
def results = processFile("src/docs/ref/Examples/Counting the number of columns.gdoc")
}

@Test
void testDeletingData()
{
def results = processFile("src/docs/ref/Examples/Deleting data.gdoc")
}

private processFile(filename)
{
def results = [:]
def inCode = false
def name = null
def script = new StringBuffer()
def scripts = [:]
def file = new File(filename)
file.eachLine{line ->
def trim = line.trim()
if (trim.startsWith("h2.")) {
name = line[3..-1].trim()
}
else if (trim == "{code}") {
if (inCode) {
inCode = false
scripts[name] = script.toString()
script = new StringBuffer()
}
else {
inCode = true
}
}
else if (inCode) {
script << line
script << "\n"
}
}

scripts.each {k, v ->
def result = runScript(v)
println "$k => $result"
results[name] = result
}
return results
}

private runScript(script)
{
def fullScript = "import com.netflix.astyanax.util.*\n" + script
def binding = new Binding(astyanaxService: astyanaxService)
def shell = new GroovyShell(binding)
shell.evaluate(fullScript)
}
}

0 comments on commit 3a6cf03

Please sign in to comment.