From 3a6cf03ed27e87dafa56d08458fdc82e41609a59 Mon Sep 17 00:00:00 2001 From: Bob Florian Date: Tue, 21 Aug 2012 15:26:03 -0400 Subject: [PATCH] Added tests to execute documentaton examples. --- .../Querying for a Single Column.gdoc | 2 +- .../Querying for a slice of columns.gdoc | 2 +- .../Examples/Querying for an entire row.gdoc | 2 +- .../Querying for specific columns.gdoc | 2 +- test/data/schema.txt | 8 ++ .../cassandra/test/ExamplesTests.groovy | 126 ++++++++++++++++++ 6 files changed, 138 insertions(+), 4 deletions(-) create mode 100644 test/integration/com/reachlocal/grails/plugins/cassandra/test/ExamplesTests.groovy diff --git a/src/docs/ref/Examples/Querying for a Single Column.gdoc b/src/docs/ref/Examples/Querying for a Single Column.gdoc index 35b2cee..aa418e4 100644 --- a/src/docs/ref/Examples/Querying for a Single Column.gdoc +++ b/src/docs/ref/Examples/Querying for a Single Column.gdoc @@ -6,7 +6,7 @@ Returns [com.netflix.astyanax.model.Column|http://netflix.github.com/astyanax/ja {code} def rowKey = "xxx-1" -Column result = astyanaxService.keyspace().prepareQuery("Standard1") +def result = astyanaxService.keyspace().prepareQuery("Standard1") .getKey(rowKey) .getColumn("ColumnName1") .execute() diff --git a/src/docs/ref/Examples/Querying for a slice of columns.gdoc b/src/docs/ref/Examples/Querying for a slice of columns.gdoc index b33ebc0..704b515 100644 --- a/src/docs/ref/Examples/Querying for a slice of columns.gdoc +++ b/src/docs/ref/Examples/Querying for a slice of columns.gdoc @@ -60,7 +60,7 @@ Returns java.util.Map {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() diff --git a/src/docs/ref/Examples/Querying for an entire row.gdoc b/src/docs/ref/Examples/Querying for an entire row.gdoc index fcf2db1..532e589 100644 --- a/src/docs/ref/Examples/Querying for an entire row.gdoc +++ b/src/docs/ref/Examples/Querying for an entire row.gdoc @@ -44,7 +44,7 @@ Returns java.util.Map {code} def rowKey = "xxx-1" -def result = astyanaxService.keyspace().prepareQuery("Standard1") +def result = astyanaxService.keyspace().prepareQuery("Counter1") .getKey(rowKey) .execute() .result diff --git a/src/docs/ref/Examples/Querying for specific columns.gdoc b/src/docs/ref/Examples/Querying for specific columns.gdoc index 4c2c429..bb083c0 100644 --- a/src/docs/ref/Examples/Querying for specific columns.gdoc +++ b/src/docs/ref/Examples/Querying for specific columns.gdoc @@ -47,7 +47,7 @@ Returns java.util.Map {code} def rowKey = "xxx-1" -def result = astyanaxService.keyspace().prepareQuery("Standard1") +def result = astyanaxService.keyspace().prepareQuery("Counter1") .getKey(rowKey) .withColumnSlice("ColumnName1","ColumnName3","ColumnName5") .execute() diff --git a/test/data/schema.txt b/test/data/schema.txt index 396a72d..f75bc67 100644 --- a/test/data/schema.txt +++ b/test/data/schema.txt @@ -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; \ No newline at end of file diff --git a/test/integration/com/reachlocal/grails/plugins/cassandra/test/ExamplesTests.groovy b/test/integration/com/reachlocal/grails/plugins/cassandra/test/ExamplesTests.groovy new file mode 100644 index 0000000..deed44c --- /dev/null +++ b/test/integration/com/reachlocal/grails/plugins/cassandra/test/ExamplesTests.groovy @@ -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) + } +}