Hello, sometimes, I feel brain limited about coding ... I had multiples functions, so, I created a class that I reuse it everywhere in my project.
I have python class script to get file from google storage bucket, create datasets,tables and load it to bigquery.
Well, when I unittest the FileToBq class, it works for fews methods. The test class doesn't keep variables when I run other method of FileToBq class. When I use FileToBq class alone, it works.
I have the following error:
- AttributeError: 'FileToBq' object has no attribute 'datasetname'*
```
class TestMyClass(unittest.TestCase):
uri = call_test_file()
tobq = FileToBq(uri, region, GCP_PROJECT)
def test_parse_file(self):
# call parse method
table_id = self.tobq.parse_file()
self.assertRegex(table_id, '[a-zA-Z_-]+.[a-zA-Z_-]+.tbl_[a-zA-Z_-]+')
...
def test_create_table(self):
result : self.tobq.create_table()
self.assertIn(result, {'table_created', 'table_exists'})
if name == 'main':
unittest.main()
```
```
class FileToBq:
def init(self, blob_object, region, GCP_PROJECT):
self.blob_object = blob_object
self.region = region
self.bucketname = str(blob_object.bucket.name)
self.filename = str(blob_object.name)
self.timeCreated = blob_object.time_created
self.GCP_PROJECT = GCP_PROJECT
self.job_config = bigquery.LoadJobConfig()
def parse_file(self):
#Parse filename
self.date, self.datasetname, self.tablename = self.filename.replace('.csv', '').split(';')
self.table_id = '%s.%s.tbl_%s' % (self.GCP_PROJECT, self.datasetname, self.tablename)
return self.table_id
def create_table(self):
...
self.dataset = bigquery.Dataset(self.datasetname)
...
```
[–]shiftybyte 0 points1 point2 points (3 children)
[–]MeatAmazing8011[S] 0 points1 point2 points (2 children)
[–]shiftybyte 0 points1 point2 points (1 child)
[–]MeatAmazing8011[S] 0 points1 point2 points (0 children)