github.com/IBM-Cloud/terraform@v0.6.4-0.20170726051544-8872b87621df/builtin/providers/google/resource_bigquery_table_test.go (about)

     1  package google
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/acctest"
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  )
    11  
    12  func TestAccBigQueryTable_Basic(t *testing.T) {
    13  	datasetID := fmt.Sprintf("tf_test_%s", acctest.RandString(10))
    14  	tableID := fmt.Sprintf("tf_test_%s", acctest.RandString(10))
    15  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccCheckBigQueryTableDestroy,
    20  		Steps: []resource.TestStep{
    21  			{
    22  				Config: testAccBigQueryTable(datasetID, tableID),
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccBigQueryTableExists(
    25  						"google_bigquery_table.test"),
    26  				),
    27  			},
    28  
    29  			{
    30  				Config: testAccBigQueryTableUpdated(datasetID, tableID),
    31  				Check: resource.ComposeTestCheckFunc(
    32  					testAccBigQueryTableExists(
    33  						"google_bigquery_table.test"),
    34  				),
    35  			},
    36  		},
    37  	})
    38  }
    39  
    40  func testAccCheckBigQueryTableDestroy(s *terraform.State) error {
    41  	for _, rs := range s.RootModule().Resources {
    42  		if rs.Type != "google_bigquery_table" {
    43  			continue
    44  		}
    45  
    46  		config := testAccProvider.Meta().(*Config)
    47  		_, err := config.clientBigQuery.Tables.Get(config.Project, rs.Primary.Attributes["dataset_id"], rs.Primary.Attributes["name"]).Do()
    48  		if err == nil {
    49  			return fmt.Errorf("Table still present")
    50  		}
    51  	}
    52  
    53  	return nil
    54  }
    55  
    56  func testAccBigQueryTableExists(n string) resource.TestCheckFunc {
    57  	return func(s *terraform.State) error {
    58  		rs, ok := s.RootModule().Resources[n]
    59  		if !ok {
    60  			return fmt.Errorf("Not found: %s", n)
    61  		}
    62  
    63  		if rs.Primary.ID == "" {
    64  			return fmt.Errorf("No ID is set")
    65  		}
    66  		config := testAccProvider.Meta().(*Config)
    67  		_, err := config.clientBigQuery.Tables.Get(config.Project, rs.Primary.Attributes["dataset_id"], rs.Primary.Attributes["name"]).Do()
    68  		if err != nil {
    69  			return fmt.Errorf("BigQuery Table not present")
    70  		}
    71  
    72  		return nil
    73  	}
    74  }
    75  
    76  func testAccBigQueryTable(datasetID, tableID string) string {
    77  	return fmt.Sprintf(`
    78  resource "google_bigquery_dataset" "test" {
    79    dataset_id = "%s"
    80  }
    81  
    82  resource "google_bigquery_table" "test" {
    83    table_id   = "%s"
    84    dataset_id = "${google_bigquery_dataset.test.dataset_id}"
    85  
    86    time_partitioning {
    87      type = "DAY"
    88    }
    89  
    90    schema = <<EOH
    91  [
    92    {
    93      "name": "city",
    94      "type": "RECORD",
    95      "fields": [
    96        {
    97          "name": "id",
    98          "type": "INTEGER"
    99        },
   100        {
   101          "name": "coord",
   102          "type": "RECORD",
   103          "fields": [
   104            {
   105              "name": "lon",
   106              "type": "FLOAT"
   107            }
   108          ]
   109        }
   110      ]
   111    }
   112  ]
   113  EOH
   114  }`, datasetID, tableID)
   115  }
   116  
   117  func testAccBigQueryTableUpdated(datasetID, tableID string) string {
   118  	return fmt.Sprintf(`
   119  resource "google_bigquery_dataset" "test" {
   120    dataset_id = "%s"
   121  }
   122  
   123  resource "google_bigquery_table" "test" {
   124    table_id   = "%s"
   125    dataset_id = "${google_bigquery_dataset.test.dataset_id}"
   126  
   127    time_partitioning {
   128      type = "DAY"
   129    }
   130  
   131    schema = <<EOH
   132  [
   133    {
   134      "name": "city",
   135      "type": "RECORD",
   136      "fields": [
   137        {
   138          "name": "id",
   139          "type": "INTEGER"
   140        },
   141        {
   142          "name": "coord",
   143          "type": "RECORD",
   144          "fields": [
   145            {
   146              "name": "lon",
   147              "type": "FLOAT"
   148            },
   149            {
   150              "name": "lat",
   151              "type": "FLOAT"
   152            }
   153          ]
   154        }
   155      ]
   156    },
   157    {
   158      "name": "country",
   159      "type": "RECORD",
   160      "fields": [
   161        {
   162          "name": "id",
   163          "type": "INTEGER"
   164        },
   165        {
   166          "name": "name",
   167          "type": "STRING"
   168        }
   169      ]
   170    }
   171  ]
   172  EOH
   173  }`, datasetID, tableID)
   174  }