[Groonga-commit] groonga/gcs [master] Move tests for database modifications around index field to a separate file

Back to archive index

null+****@clear***** null+****@clear*****
2012年 8月 2日 (木) 13:56:16 JST


SHIMODA Hiroshi	2012-08-02 13:56:16 +0900 (Thu, 02 Aug 2012)

  New Revision: bbcb4ea0015e740b8ddea15c51433c502d1dee27
  https://github.com/groonga/gcs/commit/bbcb4ea0015e740b8ddea15c51433c502d1dee27

  Log:
    Move tests for database modifications around index field to a separate file

  Modified files:
    lib/database/index-field.js
    test/api-configuration.test.js
    test/database-index-field.test.js

  Modified: lib/database/index-field.js (+4 -0)
===================================================================
--- lib/database/index-field.js    2012-08-02 13:35:10 +0900 (cd7d7d0)
+++ lib/database/index-field.js    2012-08-02 13:56:16 +0900 (35a018b)
@@ -237,6 +237,10 @@ IndexField.prototype = {
         source: this.columnName
       });
     }
+  },
+
+  exists: function() {
+    return !!this.column;
   }
 };
 

  Modified: test/api-configuration.test.js (+21 -49)
===================================================================
--- test/api-configuration.test.js    2012-08-02 13:35:10 +0900 (e284196)
+++ test/api-configuration.test.js    2012-08-02 13:56:16 +0900 (9f0afec)
@@ -321,29 +321,21 @@ suite('Configuration API', function() {
            'IndexField.IndexFieldType=text&' +
            'Action=DefineIndexField&Version=2011-02-01')
       .next(function(response) {
-        var dump = context.commandSync('dump', {
-              tables: 'companies'
-            });
-        var expected = 'table_create companies_00000000000000000000000000 TABLE_HASH_KEY ShortText\n' +
-                       'column_create companies_00000000000000000000000000 name COLUMN_SCALAR ShortText\n' +
-                       'table_create companies_00000000000000000000000000_BigramTerms ' +
-                         'TABLE_PAT_KEY|KEY_NORMALIZE ShortText ' +
-                         '--default_tokenizer TokenBigram\n' +
-                       'column_create companies_00000000000000000000000000_BigramTerms companies_00000000000000000000000000_name ' +
-                         'COLUMN_INDEX|WITH_POSITION companies_00000000000000000000000000 name';
-        assert.equal(dump, expected);
+        var domain = new Domain('companies', context);
+        var field = domain.getIndexField('name');
+        assert.isTrue(field.exists());
 
         response = toParsedResponse(response);
         assert.deepEqual(response.pattern,
                          { statusCode: 200,
                            body: PATTERN_DefineIndexFieldResponse_Text });
         var expectedOptions = {
-              IndexFieldName: 'name',
-              IndexFieldType: 'text',
+              IndexFieldName: field.name,
+              IndexFieldType: field.type,
               TextOptions: {
                 DefaultValue: {},
-                FacetEnabled: 'false',
-                ResultEnabled: 'true'
+                FacetEnabled: String(field.facetEnabled),
+                ResultEnabled: String(field.fresultEnabled)
               }
             };
         var options = response.body.DefineIndexFieldResponse.DefineIndexFieldResult.IndexField.Options;
@@ -365,27 +357,17 @@ suite('Configuration API', function() {
            'IndexField.IndexFieldType=uint&' +
            'Action=DefineIndexField&Version=2011-02-01')
       .next(function(response) {
-        var dump = context.commandSync('dump', {
-              tables: 'companies'
-            });
-        var expected = 'table_create companies_00000000000000000000000000 TABLE_HASH_KEY ShortText\n' +
-                       'column_create companies_00000000000000000000000000 age COLUMN_SCALAR UInt32\n' +
-                       'table_create companies_00000000000000000000000000_BigramTerms ' +
-                         'TABLE_PAT_KEY|KEY_NORMALIZE ShortText ' +
-                         '--default_tokenizer TokenBigram\n' +
-                       'table_create companies_00000000000000000000000000_age ' +
-                         'TABLE_HASH_KEY UInt32\n' +
-                       'column_create companies_00000000000000000000000000_age companies_00000000000000000000000000_age ' +
-                         'COLUMN_INDEX|WITH_POSITION companies_00000000000000000000000000 age';
-        assert.equal(dump, expected);
+        var domain = new Domain('companies', context);
+        var field = domain.getIndexField('age');
+        assert.isTrue(field.exists());
 
         response = toParsedResponse(response);
         assert.deepEqual(response.pattern,
                          { statusCode: 200,
                            body: PATTERN_DefineIndexFieldResponse_UInt });
         var expectedOptions = {
-              IndexFieldName: 'age',
-              IndexFieldType: 'uint',
+              IndexFieldName: field.name,
+              IndexFieldType: field.type,
               UIntOptions: {
                 DefaultValue: {}
               }
@@ -405,36 +387,26 @@ suite('Configuration API', function() {
       .get('/?DomainName=companies&Action=CreateDomain&Version=2011-02-01', {
         'Host': 'cloudsearch.localhost'
       })
-      .get('/?DomainName=companies&IndexField.IndexFieldName=member&' +
+      .get('/?DomainName=companies&IndexField.IndexFieldName=product&' +
            'IndexField.IndexFieldType=literal&' +
            'Action=DefineIndexField&Version=2011-02-01')
       .next(function(response) {
-        var dump = context.commandSync('dump', {
-              tables: 'companies'
-            });
-        var expected = 'table_create companies_00000000000000000000000000 TABLE_HASH_KEY ShortText\n' +
-                       'table_create companies_00000000000000000000000000_BigramTerms ' +
-                         'TABLE_PAT_KEY|KEY_NORMALIZE ShortText ' +
-                         '--default_tokenizer TokenBigram\n' +
-                       'table_create companies_00000000000000000000000000_member ' +
-                         'TABLE_HASH_KEY ShortText\n' +
-                       'column_create companies_00000000000000000000000000_member companies_00000000000000000000000000_member ' +
-                         'COLUMN_INDEX|WITH_POSITION companies_00000000000000000000000000 member\n' +
-                       'column_create companies_00000000000000000000000000 member COLUMN_SCALAR companies_00000000000000000000000000_member';
-        assert.equal(dump, expected);
+        var domain = new Domain('companies', context);
+        var field = domain.getIndexField('product');
+        assert.isTrue(field.exists());
 
         response = toParsedResponse(response);
         assert.deepEqual(response.pattern,
                          { statusCode: 200,
                            body: PATTERN_DefineIndexFieldResponse_Literal });
         var expectedOptions = {
-              IndexFieldName: 'member',
-              IndexFieldType: 'literal',
+              IndexFieldName: field.name,
+              IndexFieldType: field.type,
               LiteralOptions: {
                 DefaultValue: {},
-                FacetEnabled: 'true',
-                ResultEnabled: 'true',
-                SearchEnabled: 'true'
+                FacetEnabled: String(field.facetEnabled),
+                ResultEnabled: String(field.fresultEnabled),
+                SearchEnabled: String(field.searchEnabled)
               }
             };
         var options = response.body.DefineIndexFieldResponse.DefineIndexFieldResult.IndexField.Options;

  Modified: test/database-index-field.test.js (+156 -1)
===================================================================
--- test/database-index-field.test.js    2012-08-02 13:35:10 +0900 (ccb8da7)
+++ test/database-index-field.test.js    2012-08-02 13:56:16 +0900 (9a68bd8)
@@ -147,7 +147,6 @@ suite('database', function() {
         context = temporaryDatabase.get();
         utils.loadDumpFile(context, __dirname + '/fixture/companies/ddl.grn');
         domain = new Domain('companies', context);
-        domain.id = Domain.DEFAULT_ID;
       });
 
       teardown(function() {
@@ -156,6 +155,16 @@ suite('database', function() {
         temporaryDatabase = undefined;
       });
 
+      test('exists, for existing field', function() {
+        var field = new IndexField('name', domain);
+        assert.isTrue(field.exists());
+      });
+
+      test('exists, for non-existing field', function() {
+        var field = new IndexField('unknown', domain);
+        assert.isTrue(field.exists());
+      });
+
       test('type detection (text)', function() {
         var field = new IndexField('name', domain);
         assert.equal(field.type, 'text');
@@ -171,5 +180,151 @@ suite('database', function() {
         assert.equal(field.type, 'literal');
       });
     });
+
+    suite('database modifications', function() {
+      var temporaryDatabase;
+      var context;
+      var domain;
+
+      setup(function() {
+        temporaryDatabase = utils.createTemporaryDatabase();
+        context = temporaryDatabase.get();
+        domain = new Domain('companies', context);
+        domain.createSync();
+      });
+
+      teardown(function() {
+        temporaryDatabase.teardown();
+        temporaryDatabase = undefined;
+      });
+
+      function getNoColumnDump() {
+        return 'table_create ' + domain.tableName + ' TABLE_HASH_KEY ShortText\n' +
+               'table_create ' + domain.termsTableName + ' ' +
+                 'TABLE_PAT_KEY|KEY_NORMALIZE ShortText ' +
+                 '--default_tokenizer TokenBigram';
+      }                       
+
+      test('createSync for text field', function() {
+        var field = new IndexField('name', domain);
+        assert.isFalse(field.exists());
+
+        field.type = 'text';
+        assert.isFalse(field.exists());
+
+        field.createSync();
+        assert.isTrue(field.exists());
+
+        var dump = context.commandSync('dump', {
+              tables: domain.tableName
+            });
+        var expected = 'table_create ' + domain.tableName + ' TABLE_HASH_KEY ShortText\n' +
+                       'column_create ' + domain.tableName + ' ' + field.columnName + ' COLUMN_SCALAR ShortText\n' +
+                       'table_create ' + domain.termsTableName + ' ' +
+                         'TABLE_PAT_KEY|KEY_NORMALIZE ShortText ' +
+                         '--default_tokenizer TokenBigram\n' +
+                       'column_create ' + domain.termsTableName + ' ' + field.indexColumnName + ' ' +
+                         'COLUMN_INDEX|WITH_POSITION ' + domain.tableName + ' ' + field.columnName;
+        assert.equal(dump, expected);
+      });
+
+      test('deleteSync for text field', function() {
+        var field = new IndexField('name', domain);
+        field.type = 'text';
+        field.createSync();
+        assert.isTrue(field.exists());
+
+        field.deleteSync();
+        assert.isFalse(field.exists());
+
+        var dump = context.commandSync('dump', {
+              tables: domain.tableName
+            });
+        var expected = getNoColumnDump();
+        assert.equal(dump, expected);
+      });
+
+      test('createSync for uint field', function() {
+        var field = new IndexField('age', domain);
+        assert.isFalse(field.exists());
+
+        field.type = 'uint';
+        assert.isFalse(field.exists());
+
+        field.createSync();
+        assert.isTrue(field.exists());
+
+        var dump = context.commandSync('dump', {
+              tables: domain.tableName
+            });
+        var expected = 'table_create ' + domain.tableName + ' TABLE_HASH_KEY ShortText\n' +
+                       'column_create ' + domain.tableName + ' ' + field.columnName + ' COLUMN_SCALAR UInt32\n' +
+                       'table_create ' + domain.termsTableName + ' ' +
+                         'TABLE_PAT_KEY|KEY_NORMALIZE ShortText ' +
+                         '--default_tokenizer TokenBigram\n' +
+                       'table_create ' + field.alterTableName + ' ' +
+                         'TABLE_HASH_KEY UInt32\n' +
+                       'column_create ' + field.alterTableName + ' ' + field.indexColumnName + ' ' +
+                         'COLUMN_INDEX|WITH_POSITION ' + domain.tableName + ' ' + field.columnName;
+        assert.equal(dump, expected);
+      });
+
+      test('deleteSync for uint field', function() {
+        var field = new IndexField('age', domain);
+        field.type = 'uint';
+        field.createSync();
+        assert.isTrue(field.exists());
+
+        field.deleteSync();
+        assert.isFalse(field.exists());
+
+        var dump = context.commandSync('dump', {
+              tables: domain.tableName
+            });
+        var expected = getNoColumnDump();
+        assert.equal(dump, expected);
+      });
+
+      test('createSync for literal field', function() {
+        var field = new IndexField('product', domain);
+        assert.isFalse(field.exists());
+
+        field.type = 'literal';
+        assert.isFalse(field.exists());
+
+        field.createSync();
+        assert.isTrue(field.exists());
+
+        var dump = context.commandSync('dump', {
+              tables: 'companies'
+            });
+        var expected = 'table_create ' + domain.tableName + ' TABLE_HASH_KEY ShortText\n' +
+                       'table_create ' + domain.termsTableName + ' ' +
+                         'TABLE_PAT_KEY|KEY_NORMALIZE ShortText ' +
+                         '--default_tokenizer TokenBigram\n' +
+                       'table_create ' + field.alterTableName + ' ' +
+                         'TABLE_HASH_KEY ShortText\n' +
+                       'column_create ' + field.alterTableName + ' ' + field.indexColumnName + ' ' +
+                         'COLUMN_INDEX|WITH_POSITION ' + domain.tableName + ' ' + field.columnName + '\n' +
+                       'column_create ' + domain.tableName + ' ' + field.columnName + ' COLUMN_SCALAR ' + field.alterTableName;
+        assert.equal(dump, expected);
+      });
+
+      test('deleteSync for literal field', function() {
+        var field = new IndexField('product', domain);
+        field.type = 'literal';
+        field.createSync();
+        assert.isTrue(field.exists());
+
+        field.deleteSync();
+        assert.isFalse(field.exists());
+
+        var dump = context.commandSync('dump', {
+              tables: domain.tableName
+            });
+        var expected = getNoColumnDump();
+        assert.equal(dump, expected);
+      });
+    });
   });
 });
-------------- next part --------------
HTML$B$NE:IU%U%!%$%k$rJ]4I$7$^$7$?(B...
Download 



Groonga-commit メーリングリストの案内
Back to archive index